diff --git a/CHANGELOG.md b/CHANGELOG.md index 73c500b..c531244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 1.0.1 + +1. request method changed to POST +2. test_mode configuration variable renamed to testMode +3. added sslVerify configuration variable. +>This allows you to disable ssl validation (don't do this!) or use a custom SSL certificate from file system + ## 0.1.3 Optimize registerOrder input patam processing diff --git a/README.md b/README.md index 54347cf..141515f 100644 --- a/README.md +++ b/README.md @@ -15,17 +15,16 @@ composer require vhar/sberbank ```php 'ваш-api-логин', 'shopPassword' => 'ваш-api-пароль', - 'test_mode' => 0, // 0 - production, 1 - test + 'testMode' => 0, // 0 - production, 1 - test + 'sslVerify' => 1 // 0 - игнорировать ошибки SSL сертификата (не делайте так!), 1 - проверять SSL сертификат (по умолчанию), '/path/to/cert.pem' - использовать пользовательский сертификат для проверки ]; -$http_client = new Client(); -$sber = new SBClient($config, $http_client); +$sber = new SBClient($config); /** * В примере показаны только обязательные поля. @@ -57,17 +56,16 @@ if (isset($response->errorCode) && $response->errorMessage) { ```php 'ваш-api-логин', 'shopPassword' => 'ваш-api-пароль', - 'test_mode' => 0, // 0 - production, 1 - test + 'testMode' => 0, // 0 - production, 1 - test + 'sslVerify' => 1 // 0 - игнорировать ошибки SSL сертификата (не делайте так!), 1 - проверять SSL сертификат (по умолчанию), '/path/to/cert.pem' - использовать пользовательский сертификат для проверки ]; -$http_client = new Client(); -$sber = new SBClient($config, $http_client); +$sber = new SBClient($config); /** * В примере показано только обязательное поле. @@ -97,17 +95,16 @@ if (isset($response->errorCode) && $response->errorCode) { ```php 'ваш-api-логин', 'shopPassword' => 'ваш-api-пароль', - 'test_mode' => 0, // 0 - production, 1 - test + 'testMode' => 0, // 0 - production, 1 - test + 'sslVerify' => 1 // 0 - игнорировать ошибки SSL сертификата (не делайте так!), 1 - проверять SSL сертификат (по умолчанию), '/path/to/cert.pem' - использовать пользовательский сертификат для проверки ]; -$http_client = new Client(); -$sber = new SBClient($config, $http_client); +$sber = new SBClient($config); /** * см. https://securepayments.sberbank.ru/wiki/doku.php/integration:api:rest:requests:refund diff --git a/composer.json b/composer.json index a3f16d8..ecfba87 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "vhar/sberbank", "description": "Simple PHP client for Sperbank acquiring REST API", - "version": "0.1.3", + "version": "1.0.1", "type": "library", "keywords": ["sberbank", "sberbank online", "credit card", "sberbank acquiring"], "require": { diff --git a/src/SBClient.php b/src/SBClient.php index d57769a..91e4432 100644 --- a/src/SBClient.php +++ b/src/SBClient.php @@ -4,14 +4,13 @@ * * @package vhar\sberbank * @author Vladimir Kharinenkov - * @version 0.1.3 + * @version 1.0.1 * */ namespace VHar\Sberbank; use ErrorException; -use GuzzleHttp\Client; /** * Class SBClient for working with Sberbank REST API. @@ -31,18 +30,36 @@ class SBClient */ const API_TEST_URL = 'https://3dsec.sberbank.ru'; - /** - * Basic request options - * @var array - */ - private $config; - /** * Http Client * @var Client */ private $client; + /** + * Shop login for REST API access + * @var string + */ + private $shopLogin; + + /** + * Shop password for REST API access + * @var string + */ + private $shopPassword; + + /** + * use test or production payment gateway + * @var int + */ + private $testMode; + + /** + * SSL certificate validation + * @var mixed + */ + private $sslVerify; + /** * Clients accept an array of constructor parameters. * @@ -53,28 +70,45 @@ class SBClient * - shopPassword: (string) Shop password for REST API access * * Additional option: - * - test_mode: (int) + * - sslVerify: mixed + * + * + * + * - testMode: (int) * * * default is 0 - * - * @param Client $client GuzzleHttp\Client used to send the requests. */ - - public function __construct(array $config = [], Client $client) + public function __construct(array $config = []) { - $this->client = $client ?? new GuzzleHttp\Client(); - $this->shopLogin = $config['shopLogin'] ?? null; + if (!$this->shopLogin) { throw new ErrorException('Please provide shopLogin'); } + $this->shopPassword = $config['shopPassword'] ?? null; + if (!$this->shopPassword) { throw new ErrorException('Please provide shopPassword'); } - $this->test_mode = $config['test_mode'] ?? 0; + $this->testMode = $config['testMode'] ?? 0; + + $this->sslVerify = true; + + if (isset($config['sslVerify'])) { + if ($config['sslVerify'] === 0) { + $this->sslVerify = false; + } elseif (is_file($config['sslVerify'])) { + $this->sslVerify = $config['sslVerify']; + } + } + + $this->client = new \GuzzleHttp\Client([ + 'base_uri' => $this->getApiUrl(), + 'verify' => $this->sslVerify, + ]); } /** @@ -109,7 +143,7 @@ public function __construct(array $config = [], Client $client) */ public function registerOrder($order) { - $url = $this->getApiUrl() . '/payment/rest/register.do?%s'; + $url = '/payment/rest/register.do?%s'; if (!$order['orderNumber']) { throw new ErrorException('Please provide orderNumber'); @@ -143,7 +177,7 @@ public function registerOrder($order) } $request = sprintf($url, http_build_query($params)); - $response = $this->client->request('GET', $request); + $response = $this->client->request('POST', $request); return json_decode($response->getBody()); } @@ -181,7 +215,7 @@ public function registerOrder($order) */ public function getOrderStatusExtended($order) { - $url = $this->getApiUrl() . '/payment/rest/getOrderStatusExtended.do?%s'; + $url = '/payment/rest/getOrderStatusExtended.do?%s'; if (!isset($order['orderNumber']) && !isset($order['orderId'])) { throw new ErrorException('Please provide orderId OR orderNumber'); @@ -201,7 +235,7 @@ public function getOrderStatusExtended($order) if (isset($order['language'])) $params['language'] = $order['language']; $request = sprintf($url, http_build_query($params)); - $response = $this->client->request('GET', $request); + $response = $this->client->request('POST', $request); return json_decode($response->getBody()); } @@ -240,7 +274,7 @@ public function getOrderStatusExtended($order) */ public function reverseOrder($order) { - $url = $this->getApiUrl() . '/payment/rest/reverse.do?%s'; + $url = '/payment/rest/reverse.do?%s'; if (!$order['amount']) { throw new ErrorException('Please provide amount'); @@ -267,7 +301,7 @@ public function reverseOrder($order) } $request = sprintf($url, http_build_query($params)); - $response = $this->client->request('GET', $request); + $response = $this->client->request('POST', $request); return json_decode($response->getBody()); } @@ -302,7 +336,7 @@ public function reverseOrder($order) */ public function refundOrder($order) { - $url = $this->getApiUrl() . '/payment/rest/refund.do?%s'; + $url = '/payment/rest/refund.do?%s'; if (!$order['amount']) { throw new ErrorException('Please provide amount'); @@ -328,19 +362,19 @@ public function refundOrder($order) } $request = sprintf($url, http_build_query($params)); - $response = $this->client->request('GET', $request); + $response = $this->client->request('POST', $request); return json_decode($response->getBody()); } /** - * Get API URL based on test_mode $config option + * Get API URL based on testMode $config option * * @return string Production or Test REST API URL */ private function getApiUrl() { - if ($this->test_mode) { + if ($this->testMode) { return self::API_TEST_URL; } else { return self::API_PROD_URL;