Skip to content

Commit

Permalink
Add merchantData to oneclick/init request
Browse files Browse the repository at this point in the history
  • Loading branch information
JanBukva authored and janlanger committed May 3, 2021
1 parent 404dd3d commit 57ac5fc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/Call/OneClick/InitOneClickPaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
use SlevomatCsobGateway\Price;
use SlevomatCsobGateway\Validator;
use function base64_encode;

class InitOneClickPaymentRequest
{
Expand All @@ -32,27 +33,35 @@ class InitOneClickPaymentRequest
/** @var string */
private $clientIp;

/** @var string|null */
private $merchantData;

public function __construct(
string $merchantId,
string $origPayId,
string $orderId,
string $clientIp,
?Price $price = null,
?string $description = null
?string $description = null,
?string $merchantData = null
)
{
Validator::checkPayId($origPayId);
Validator::checkOrderId($orderId);
if ($description !== null) {
Validator::checkDescription($description);
}
if ($merchantData !== null) {
Validator::checkMerchantData($merchantData);
}

$this->merchantId = $merchantId;
$this->origPayId = $origPayId;
$this->orderId = $orderId;
$this->clientIp = $clientIp;
$this->price = $price;
$this->description = $description;
$this->merchantData = $merchantData;
}

public function send(ApiClient $apiClient): PaymentResponse
Expand All @@ -73,6 +82,10 @@ public function send(ApiClient $apiClient): PaymentResponse
$requestData['description'] = $this->description;
}

if ($this->merchantData !== null) {
$requestData['merchantData'] = base64_encode($this->merchantData);
}

$response = $apiClient->post(
'oneclick/init',
$requestData,
Expand All @@ -85,6 +98,7 @@ public function send(ApiClient $apiClient): PaymentResponse
'totalAmount' => null,
'currency' => null,
'description' => null,
'merchantData' => null,
]),
new SignatureDataFormatter([
'payId' => null,
Expand Down
6 changes: 4 additions & 2 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ public function createOneclickInitPayment(
string $orderId,
string $clientIp,
?Price $price = null,
?string $description = null
?string $description = null,
?string $merchantData = null
): InitOneClickPaymentRequest
{
return new InitOneClickPaymentRequest(
Expand All @@ -159,7 +160,8 @@ public function createOneclickInitPayment(
$orderId,
$clientIp,
$price,
$description
$description,
$merchantData
);
}

Expand Down
5 changes: 4 additions & 1 deletion tests/unit/Call/OneClick/InitOneClickPaymentRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SlevomatCsobGateway\Call\ResultCode;
use SlevomatCsobGateway\Currency;
use SlevomatCsobGateway\Price;
use function base64_encode;

class InitOneClickPaymentRequestTest extends TestCase
{
Expand All @@ -32,6 +33,7 @@ public function testSend(): void
'totalAmount' => 1789600,
'currency' => 'CZK',
'description' => 'Nákup na vasobchod.cz (Lenovo ThinkPad Edge E540, Doprava PPL)',
'merchantData' => base64_encode('some-base64-encoded-merchant-data'),
])
->willReturn(
new Response(ResponseCode::get(ResponseCode::S200_OK), [
Expand All @@ -49,7 +51,8 @@ public function testSend(): void
'5547',
'127.0.0.1',
new Price(1789600, Currency::get(Currency::CZK)),
'Nákup na vasobchod.cz (Lenovo ThinkPad Edge E540, Doprava PPL)'
'Nákup na vasobchod.cz (Lenovo ThinkPad Edge E540, Doprava PPL)',
'some-base64-encoded-merchant-data'
);

$paymentResponse = $initPaymentRequest->send($apiClient);
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function testCreateOneclickInitPayment(): void
'5547123',
'127.0.0.1',
new Price(1789600, Currency::get(Currency::CZK)),
'Nákup na vasobchod.cz (Lenovo ThinkPad Edge E540, Doprava PPL)'
'Nákup na vasobchod.cz (Lenovo ThinkPad Edge E540, Doprava PPL)',
'some-base64-encoded-merchant-data'
);

self::assertTrue(true);
Expand Down

0 comments on commit 57ac5fc

Please sign in to comment.