Skip to content

Commit df14dd5

Browse files
authored
Merge pull request #5 from SmartBase-SK/dev_direct
fix(direct_payment): fix direct api status response, add non-mock tests for direct api payments
2 parents 298cb62 + 875e49c commit df14dd5

File tree

3 files changed

+148
-28
lines changed

3 files changed

+148
-28
lines changed

lib/Model/PaymentIntentStatusResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public function setStatus($status)
427427
if (is_null($status)) {
428428
throw new \InvalidArgumentException('non-nullable status cannot be null');
429429
}
430-
if ($this->getSelectedPaymentMethod() == PaymentMethod::CARD_PAY) {
430+
if ($this->getSelectedPaymentMethod() == PaymentMethod::CARD_PAY || $this->getSelectedPaymentMethod() == PaymentMethod::DIRECT_API) {
431431
$type = '\Tatrapayplus\TatrapayplusApiClient\Model\CardPayStatusStructure';
432432
$value = ObjectSerializer::deserialize($status, $type, null);
433433
} elseif (is_string($status)) {

lib/TatraPayPlusService.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,14 @@ public static function remove_card_holder_diacritics($initiate_payment_request)
220220
{
221221
if ($initiate_payment_request instanceof InitiateDirectTransactionRequest) {
222222
$card_detail = $initiate_payment_request->getTdsData();
223-
$card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder()));
223+
if ($card_detail) {
224+
$card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder()));
225+
}
224226
} else {
225227
$card_detail = $initiate_payment_request->getCardDetail();
226-
$card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder()));
228+
if ($card_detail) {
229+
$card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder()));
230+
}
227231
}
228232

229233
return $initiate_payment_request;

tests/tests.php

Lines changed: 141 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ public function __construct()
5151
$this->client_secret = getenv("TATRAPAY_CLIENT_SECRET");
5252
}
5353

54+
private function getDirectTransactionPayloadRequiredFieldsOnly(
55+
float $total,
56+
string $currency,
57+
): InitiateDirectTransactionRequest
58+
{
59+
$request_data = new InitiateDirectTransactionRequest([
60+
"amount" => new Amount([
61+
"amount_value" => $total,
62+
"currency" => $currency,
63+
]),
64+
"is_pre_authorization" => true,
65+
"end_to_end" => new E2e([
66+
"variable_symbol" => "123",
67+
]),
68+
"tds_data" => new DirectTransactionTDSData([
69+
"card_holder" => "--",
70+
"email" => "[email protected]",
71+
]),
72+
"token" => "ABC12345",
73+
]);
74+
return $request_data;
75+
}
76+
5477
private function getDirectTransactionPayload(
5578
float $total,
5679
string $currency,
@@ -85,13 +108,41 @@ private function getDirectTransactionPayload(
85108
"location" => "Test 123",
86109
"country" => "SK",
87110
]),
88-
"token" => new Token([
89-
"google_pay_token" => "ABC12345"
90-
]),
111+
"token" => "ABC12345",
91112
]);
92113
return $request_data;
93114
}
94115

116+
private function getPaymentPayloadRequiredFieldsOnly(
117+
float $total,
118+
string $currency,
119+
bool $save_card = false
120+
): InitiatePaymentRequest {
121+
$order_id = uniqid();
122+
123+
$basePayment = new BasePayment([
124+
"instructed_amount" => new Amount([
125+
"amount_value" => $total,
126+
"currency" => $currency,
127+
]),
128+
"end_to_end" => new E2e([
129+
"variable_symbol" => "123",
130+
]),
131+
]);
132+
$userData = new UserData([
133+
"first_name" => "|Jan\ko|",
134+
"last_name" => "<Hraško>\\`",
135+
"email" => "[email protected]",
136+
]);
137+
$bankTransfer = new BankTransfer();
138+
139+
return new InitiatePaymentRequest([
140+
"base_payment" => $basePayment,
141+
"bank_transfer" => $bankTransfer,
142+
"user_data" => $userData,
143+
]);
144+
}
145+
95146
private function getPaymentPayload(
96147
float $total,
97148
string $currency,
@@ -308,6 +359,44 @@ public function testInitiatePaymentCheckPaymentStatus(): void
308359
);
309360
}
310361

362+
public function testInitiatePaymentCheckPaymentStatusRequiredFieldsOnly(): void
363+
{
364+
$accept_language = "sk";
365+
$preferred_method = null;
366+
$initiate_payment_request = $this->getPaymentPayloadRequiredFieldsOnly(10, "EUR");
367+
368+
$api_instance = new TatraPayPlusAPIApi(
369+
$this->client_id,
370+
$this->client_secret
371+
);
372+
373+
$response = $api_instance->initiatePayment(
374+
"http://localhost",
375+
$initiate_payment_request,
376+
$preferred_method,
377+
$accept_language
378+
);
379+
380+
$this->assertFalse(is_null($response["object"]));
381+
$this->assertFalse(is_null($response["response"]));
382+
383+
$payment_id = $response["object"]->getPaymentId();
384+
$this->assertFalse(is_null($payment_id));
385+
386+
[$simple_status, $response] = $api_instance->getPaymentIntentStatus($payment_id);
387+
388+
$this->assertFalse(is_null($response["object"]));
389+
$this->assertSame($response["response"]->getStatusCode(), 200);
390+
$this->assertSame(
391+
$response["object"]->getAuthorizationStatus(),
392+
PaymentIntentStatusResponse::AUTHORIZATION_STATUS__NEW
393+
);
394+
$this->assertSame(
395+
$simple_status,
396+
TatraPayPlusService::SIMPLE_STATUS_PENDING
397+
);
398+
}
399+
311400
public function testCancelPaymentIntent(): void
312401
{
313402
$api_instance = new TatraPayPlusAPIApi(
@@ -524,41 +613,68 @@ public function testLogger()
524613
$this->assertSame(16, count($logger->lines));
525614
}
526615

527-
public function testInitiateDirectTransactionMocked(): void
616+
public function testInitiateDirectTransaction(): void
528617
{
529-
$mock_response_body = json_encode(
530-
array(
531-
"paymentId" => "123456789",
532-
"redirectFormHtml" => "custom HTML"
533-
)
618+
$api_instance = new TatraPayPlusAPIApi(
619+
$this->client_id,
620+
$this->client_secret
534621
);
535-
$mock_response = new HttpResponse($mock_response_body, [], 201);
536-
$mock_client = $this->getMockBuilder(CurlClient::class)
537-
->onlyMethods(["send"])
538-
->getMock();
539-
$mock_client->method("send")->will($this->returnValue($mock_response));
622+
$request_data = $this->getDirectTransactionPayload(1.01, "EUR");
540623

541-
$api_instance = $this->getMockBuilder(TatraPayPlusAPIApi::class)
542-
->onlyMethods(["addAuthHeader"])
543-
->setConstructorArgs([$this->client_id, $this->client_secret])
544-
->getMock();
545-
$api_instance
546-
->method("addAuthHeader")
547-
->will($this->returnCallback("mock_addAuthHeader"));
548-
$api_instance->setClient($mock_client);
624+
$response = $api_instance->initiateDirectTransaction(
625+
"http://localhost",
626+
$request_data,
627+
);
628+
$payment_id = $response["object"]->getPaymentId();
549629

550-
$request_data = $this->getDirectTransactionPayload(10, "EUR");
630+
$this->assertFalse(is_null($response["object"]));
631+
$this->assertFalse(is_null($response["response"]));
632+
$this->assertFalse(is_null($payment_id));
633+
634+
[$simple_status, $response] = $api_instance->getPaymentIntentStatus($payment_id);
635+
636+
$this->assertFalse(is_null($response["object"]));
637+
$this->assertSame($response["response"]->getStatusCode(), 200);
638+
$this->assertSame(
639+
$response["object"]->getAuthorizationStatus(),
640+
PaymentIntentStatusResponse::AUTHORIZATION_STATUS_AUTH_DONE
641+
);
642+
$this->assertSame(
643+
$simple_status,
644+
TatraPayPlusService::SIMPLE_STATUS_PENDING
645+
);
646+
}
647+
648+
public function testInitiateDirectTransactionRequiredFieldsOnly(): void
649+
{
650+
$api_instance = new TatraPayPlusAPIApi(
651+
$this->client_id,
652+
$this->client_secret
653+
);
654+
$request_data = $this->getDirectTransactionPayloadRequiredFieldsOnly(1.01, "EUR");
551655

552656
$response = $api_instance->initiateDirectTransaction(
553657
"http://localhost",
554658
$request_data,
555659
);
660+
$payment_id = $response["object"]->getPaymentId();
556661

557662
$this->assertFalse(is_null($response["object"]));
558663
$this->assertFalse(is_null($response["response"]));
664+
$this->assertFalse(is_null($payment_id));
665+
666+
[$simple_status, $response] = $api_instance->getPaymentIntentStatus($payment_id);
559667

560-
$this->assertSame("123456789", $response["object"]->getPaymentId());
561-
$this->assertSame("custom HTML", $response["object"]->getRedirectFormHtml());
668+
$this->assertFalse(is_null($response["object"]));
669+
$this->assertSame($response["response"]->getStatusCode(), 200);
670+
$this->assertSame(
671+
$response["object"]->getAuthorizationStatus(),
672+
PaymentIntentStatusResponse::AUTHORIZATION_STATUS_AUTH_DONE
673+
);
674+
$this->assertSame(
675+
$simple_status,
676+
TatraPayPlusService::SIMPLE_STATUS_PENDING
677+
);
562678
}
563679

564680
public function testMapSimpleStatus()

0 commit comments

Comments
 (0)