diff --git a/src/Client.php b/src/Client.php index e93be12..3fd26e6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -24,6 +24,7 @@ class Client const API_URI = 'https://securepayments.sberbank.ru'; const API_URI_TEST = 'https://3dsec.sberbank.ru'; const API_PREFIX_DEFAULT = '/payment/rest/'; + const API_PREFIX_CREDIT = '/sbercredit/'; const API_PREFIX_APPLE = '/payment/applepay/'; const API_PREFIX_GOOGLE = '/payment/google/'; const API_PREFIX_SAMSUNG = '/payment/samsung/'; @@ -73,6 +74,13 @@ class Client */ private $prefixDefault; + /** + * Credit API endpoints prefix. + * + * @var string + */ + private $prefixCredit; + /** * Apple Pay endpoint prefix. * @@ -124,6 +132,7 @@ public function __construct(array $options = []) 'token', 'userName', 'prefixDefault', + 'prefixCredit', 'prefixApple', 'prefixGoogle', 'prefixSamsung', @@ -158,6 +167,7 @@ public function __construct(array $options = []) $this->currency = $options['currency'] ?? null; $this->apiUri = $options['apiUri'] ?? self::API_URI; $this->prefixDefault = $options['prefixDefault'] ?? self::API_PREFIX_DEFAULT; + $this->prefixCredit = $options['prefixCredit'] ?? self::API_PREFIX_CREDIT; $this->prefixApple = $options['prefixApple'] ?? self::API_PREFIX_APPLE; $this->prefixGoogle = $options['prefixGoogle'] ?? self::API_PREFIX_GOOGLE; $this->prefixSamsung = $options['prefixSamsung'] ?? self::API_PREFIX_SAMSUNG; @@ -220,6 +230,38 @@ public function registerOrderPreAuth($orderId, int $amount, string $returnUrl, a return $this->doRegisterOrder($orderId, $amount, $returnUrl, $data, $this->prefixDefault . 'registerPreAuth.do'); } + /** + * Register a new credit order. + * + * @see https://securepayments.sberbank.ru/wiki/doku.php/integration:api:rest:requests:register_cart_credit + * + * @param int|string $orderId An order identifier + * @param int $amount An order amount + * @param string $returnUrl An url for redirecting a user after successfull order handling + * @param array $data Additional data + * + * @return array A server's response + */ + public function registerCreditOrder($orderId, int $amount, string $returnUrl, array $data = []): array + { + return $this->doRegisterOrder($orderId, $amount, $returnUrl, $data, $this->prefixCredit . 'register.do'); + } + + /** + * Register a new credit order using a 2-step payment process. + * + * @param int|string $orderId An order identifier + * @param int $amount An order amount + * @param string $returnUrl An url for redirecting a user after successfull order handling + * @param array $data Additional data + * + * @return array A server's response + */ + public function registerCreditOrderPreAuth($orderId, int $amount, string $returnUrl, array $data = []): array + { + return $this->doRegisterOrder($orderId, $amount, $returnUrl, $data, $this->prefixCredit . 'registerPreAuth.do'); + } + private function doRegisterOrder($orderId, int $amount, string $returnUrl, array $data = [], $method = 'register.do'): array { $data['orderNumber'] = $orderId; @@ -613,7 +655,7 @@ public function execute(string $action, array $data = []): array $action = $this->prefixDefault . $action; } - $rest = 0 === \strpos($action, $this->prefixDefault); + $rest = (0 === \strpos($action, $this->prefixDefault)) || (0 === \strpos($action, $this->prefixCredit)); $uri = $this->apiUri . $action; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index b1248dd..9da3b72 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -282,6 +282,58 @@ public function testRegisterANewPreAuthorizedOrder() $client->registerOrderPreAuth('eee-eee-eee', 1200, 'https://github.com/voronkovich/sberbank-acquiring-client', ['currency' => 330]); } + public function testRegistersANewCreditOrder() + { + $httpClient = $this->getHttpClientToTestSendingData( + '/sbercredit/register.do', + 'currency=643&orderNumber=aaa-aaa-aaa&amount=50000&returnUrl=https%3A%2F%2Fgithub.com%2Fvoronkovich%2Fsberbank-acquiring-client&token=secret' + ); + + $client = new Client([ + 'token' => 'secret', + 'httpClient' => $httpClient, + ]); + + $client->registerCreditOrder('aaa-aaa-aaa', 50000, 'https://github.com/voronkovich/sberbank-acquiring-client', [ + 'currency' => 643 + ]); + } + + public function testRegistersANewCreditOrderWithCustomPrefix() + { + $httpClient = $this->getHttpClientToTestSendingData( + '/custom/credit/register.do', + 'currency=643&orderNumber=aaa-aaa-aaa&amount=50000&returnUrl=https%3A%2F%2Fgithub.com%2Fvoronkovich%2Fsberbank-acquiring-client&token=secret' + ); + + $client = new Client([ + 'token' => 'secret', + 'httpClient' => $httpClient, + 'prefixCredit'=> '/custom/credit/', + ]); + + $client->registerCreditOrder('aaa-aaa-aaa', 50000, 'https://github.com/voronkovich/sberbank-acquiring-client', [ + 'currency' => 643 + ]); + } + + public function testRegistersANewPreAuthorizedCreditOrder() + { + $httpClient = $this->getHttpClientToTestSendingData( + '/sbercredit/registerPreAuth.do', + 'currency=643&orderNumber=aaa-aaa-aaa&amount=50000&returnUrl=https%3A%2F%2Fgithub.com%2Fvoronkovich%2Fsberbank-acquiring-client&token=secret' + ); + + $client = new Client([ + 'token' => 'secret', + 'httpClient' => $httpClient, + ]); + + $client->registerCreditOrderPreAuth('aaa-aaa-aaa', 50000, 'https://github.com/voronkovich/sberbank-acquiring-client', [ + 'currency' => 643 + ]); + } + /** * @testdox Throws an exception if a "jsonParams" is not an array. */