Skip to content

Commit

Permalink
Finish update-to-version3.5
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/services/BaseServiceResponse.php
#	src/services/ComplexRequest.php
  • Loading branch information
lashnev committed Sep 19, 2018
2 parents dc0e250 + eafbc15 commit 4dc7a03
Show file tree
Hide file tree
Showing 24 changed files with 701 additions and 460 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ composer.phar
# Netbeans project files
/nbproject/

# PhpStorm
.idea/

# Merchant settings for integration tests
/tests/integration/MerchantSettings.php
/tests/integration/merchant_data/*
Expand Down
20 changes: 1 addition & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,4 @@ vendor/bin/phpunit tests/integration

## Примеры использования

### 1. Создание чека

```php
$client = new PostClient($this->starrysApiUrl, $this->secretKeyPath, $this->certPath);
$line = new Line('Test product', 1, 10.00, Line::TAX_VAT18);
$line->addPayAttribute(Line::PAY_ATTRIBUTE_TYPE_FULL_PAID_WITH_GET_PRODUCT);

$complexServise = new ComplexRequest(time());
$complexServise->addDocumentType(ComplexRequest::DOCUMENT_TYPE_BUY)
->addEmail('[email protected]')
->addGroup($this->group)
->addPhone('79050000000')
->addPlace('www.test.ru')
->addTaxMode($this->taxMode)
->addLine($line)
->addNonCash(10.00);

$response = new ComplexResponse($client->sendRequest($complexServise));
```
Примеры использования можно найти в интеграционных тестах tests/integration
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "payprocessing/starrys",
"description": "Starrys",
"require": {
"php": ">=5.3",
"psr/log": "1.0.2"
"php": ">=5.5",
"psr/log": "1.0.2",
"myclabs/php-enum": "^1.6"
},
"require-dev": {
"php": ">=5.6",
Expand Down
150 changes: 78 additions & 72 deletions src/clients/PostClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,99 @@

namespace Platron\Starrys\clients;

use Platron\Starrys\clients\iClient;
use Platron\Starrys\SdkException;
use Platron\Starrys\services\BaseServiceRequest;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class PostClient implements iClient {
class PostClient implements iClient
{

/** @var string */
protected $url;
/** @var string путь до приватного ключа */
protected $secretKeyPath;
/** @var string путь до сертификата */
protected $certPath;

/** @var LoggerInterface */
protected $logger;
/** @var int */
protected $connectionTimeout = 30;
/**
* Секретный ключ для подписи запросов
/** @var string путь до приватного ключа */
protected $secretKeyPath;
/** @var string путь до сертификата */
protected $certPath;

/** @var LoggerInterface */
protected $logger;
/** @var int */
protected $connectionTimeout = 30;

/**
* Секретный ключ для подписи запросов
* @param string $url Путь для запросов https://<адрес, указанный в личном кабинете>:<порт, указанный в личном кабинете>
* @param string $secretKeyPath
* @param string $secretKeyPath
* @param string $certPath
*/
public function __construct($url, $secretKeyPath, $certPath){
*/
public function __construct($url, $secretKeyPath, $certPath)
{
$this->url = $url;
$this->secretKeyPath = $secretKeyPath;
$this->certPath = $certPath;
}

/**
* Установить логер
* @param LoggerInterface $logger
* @return self
*/
public function setLogger(LoggerInterface $logger){
$this->logger = $logger;
return $this;
}

/**
* Установка максимального времени ожидания
* @param int $connectionTimeout
* @return self
*/
public function setConnectionTimeout($connectionTimeout){
$this->connectionTimeout = $connectionTimeout;
return $this;
}

/**
* @inheritdoc
*/
public function sendRequest(BaseServiceRequest $service) {
$requestParameters = $service->getParameters();
$requestUrl = $this->url.$service->getUrlPath();

$curl = curl_init($requestUrl);
if(!empty($requestParameters)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($requestParameters));
}
$this->secretKeyPath = $secretKeyPath;
$this->certPath = $certPath;
}

/**
* Установить логер
* @param LoggerInterface $logger
* @return self
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
return $this;
}

/**
* Установка максимального времени ожидания
* @param int $connectionTimeout
* @return self
*/
public function setConnectionTimeout($connectionTimeout)
{
$this->connectionTimeout = $connectionTimeout;
return $this;
}

/**
* @param BaseServiceRequest $service
* @return \stdClass
* @throws SdkException
*/
public function sendRequest(BaseServiceRequest $service)
{
$requestParameters = $service->getParameters();
$requestUrl = $this->url . $service->getUrlPath();

$curl = curl_init($requestUrl);
if (!empty($requestParameters)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($requestParameters));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSLKEY, $this->secretKeyPath);
curl_setopt($curl, CURLOPT_SSLCERT, $this->certPath);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);
$response = curl_exec($curl);
if($this->logger){
$this->logger->log(LogLevel::INFO, 'Requested url '.$requestUrl.' params '. json_encode($requestParameters));
$this->logger->log(LogLevel::INFO, 'Response '.$response);
}
if(curl_errno($curl)){
curl_setopt($curl, CURLOPT_SSLKEY, $this->secretKeyPath);
curl_setopt($curl, CURLOPT_SSLCERT, $this->certPath);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);

$response = curl_exec($curl);

if ($this->logger) {
$this->logger->log(LogLevel::INFO, 'Requested url ' . $requestUrl . ' params ' . json_encode($requestParameters));
$this->logger->log(LogLevel::INFO, 'Response ' . $response);
}

if (curl_errno($curl)) {
throw new SdkException(curl_error($curl), curl_errno($curl));
}
if(empty(json_decode($response))){
throw new SdkException('Not json response '.$response);
}

if (empty(json_decode($response))) {
throw new SdkException('Not json response ' . $response);
}

return json_decode($response);
}
}

}
15 changes: 8 additions & 7 deletions src/clients/iClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use Platron\Starrys\services\BaseServiceRequest;

interface iClient {

/**
* Послать запрос
* @param \Platron\Starrys\BaseService $service
*/
public function sendRequest(BaseServiceRequest $service);
interface iClient
{

/**
* Послать запрос
* @param BaseServiceRequest $service
*/
public function sendRequest(BaseServiceRequest $service);
}
22 changes: 22 additions & 0 deletions src/data_objects/AgentData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Platron\Starrys\data_objects;

class AgentData extends BaseDataObject
{
/** @var string */
protected $Operation;
/** @var string */
protected $Phone;

/**
* AgentData constructor.
* @param string $operation
* @param int $phone
*/
public function __construct($operation, $phone)
{
$this->Operation = (string)$operation;
$this->Phone = (string)$phone;
}
}
19 changes: 12 additions & 7 deletions src/data_objects/BaseDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

namespace Platron\Starrys\data_objects;

abstract class BaseDataObject {
/**
abstract class BaseDataObject
{
/**
* Получить параметры, сгенерированные командой
* @return array
*/
public function getParameters() {
$filledvars = array();
public function getParameters()
{
$fieldVars = array();
foreach (get_object_vars($this) as $name => $value) {
if ($value) {
$filledvars[$name] = $value;
if ($value instanceof BaseDataObject) {
$fieldVars[$name] = $value->getParameters();
} else {
$fieldVars[$name] = $value;
}
}
}

return $filledvars;
return $fieldVars;
}
}
18 changes: 18 additions & 0 deletions src/data_objects/GetPaymentOperatorData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Platron\Starrys\data_objects;

class GetPaymentOperatorData extends BaseDataObject
{
/** @var string */
protected $Phone;

/**
* GetPaymentOperatorData constructor.
* @param $phone
*/
public function __construct($phone)
{
$this->Phone = (string)$phone;
}
}
Loading

0 comments on commit 4dc7a03

Please sign in to comment.