Skip to content

Commit

Permalink
Support credit orders
Browse files Browse the repository at this point in the history
  • Loading branch information
voronkovich authored Aug 27, 2021
1 parent 5685f4c commit 03d29e1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down Expand Up @@ -73,6 +74,13 @@ class Client
*/
private $prefixDefault;

/**
* Credit API endpoints prefix.
*
* @var string
*/
private $prefixCredit;

/**
* Apple Pay endpoint prefix.
*
Expand Down Expand Up @@ -124,6 +132,7 @@ public function __construct(array $options = [])
'token',
'userName',
'prefixDefault',
'prefixCredit',
'prefixApple',
'prefixGoogle',
'prefixSamsung',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
52 changes: 52 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 03d29e1

Please sign in to comment.