diff --git a/lib/Model/PaymentIntentStatusResponse.php b/lib/Model/PaymentIntentStatusResponse.php index 9fed2c3..899bf2c 100644 --- a/lib/Model/PaymentIntentStatusResponse.php +++ b/lib/Model/PaymentIntentStatusResponse.php @@ -427,7 +427,7 @@ public function setStatus($status) if (is_null($status)) { throw new \InvalidArgumentException('non-nullable status cannot be null'); } - if ($this->getSelectedPaymentMethod() == PaymentMethod::CARD_PAY) { + if ($this->getSelectedPaymentMethod() == PaymentMethod::CARD_PAY || $this->getSelectedPaymentMethod() == PaymentMethod::DIRECT_API) { $type = '\Tatrapayplus\TatrapayplusApiClient\Model\CardPayStatusStructure'; $value = ObjectSerializer::deserialize($status, $type, null); } elseif (is_string($status)) { diff --git a/lib/TatraPayPlusService.php b/lib/TatraPayPlusService.php index 1982890..8681482 100644 --- a/lib/TatraPayPlusService.php +++ b/lib/TatraPayPlusService.php @@ -220,10 +220,14 @@ public static function remove_card_holder_diacritics($initiate_payment_request) { if ($initiate_payment_request instanceof InitiateDirectTransactionRequest) { $card_detail = $initiate_payment_request->getTdsData(); - $card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder())); + if ($card_detail) { + $card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder())); + } } else { $card_detail = $initiate_payment_request->getCardDetail(); - $card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder())); + if ($card_detail) { + $card_detail->setCardHolder(self::remove_diacritics($card_detail->getCardHolder())); + } } return $initiate_payment_request; diff --git a/tests/tests.php b/tests/tests.php index 29b2625..cd11762 100644 --- a/tests/tests.php +++ b/tests/tests.php @@ -51,6 +51,29 @@ public function __construct() $this->client_secret = getenv("TATRAPAY_CLIENT_SECRET"); } + private function getDirectTransactionPayloadRequiredFieldsOnly( + float $total, + string $currency, + ): InitiateDirectTransactionRequest + { + $request_data = new InitiateDirectTransactionRequest([ + "amount" => new Amount([ + "amount_value" => $total, + "currency" => $currency, + ]), + "is_pre_authorization" => true, + "end_to_end" => new E2e([ + "variable_symbol" => "123", + ]), + "tds_data" => new DirectTransactionTDSData([ + "card_holder" => "--", + "email" => "janko.hrasko@example.com", + ]), + "token" => "ABC12345", + ]); + return $request_data; + } + private function getDirectTransactionPayload( float $total, string $currency, @@ -85,13 +108,41 @@ private function getDirectTransactionPayload( "location" => "Test 123", "country" => "SK", ]), - "token" => new Token([ - "google_pay_token" => "ABC12345" - ]), + "token" => "ABC12345", ]); return $request_data; } + private function getPaymentPayloadRequiredFieldsOnly( + float $total, + string $currency, + bool $save_card = false + ): InitiatePaymentRequest { + $order_id = uniqid(); + + $basePayment = new BasePayment([ + "instructed_amount" => new Amount([ + "amount_value" => $total, + "currency" => $currency, + ]), + "end_to_end" => new E2e([ + "variable_symbol" => "123", + ]), + ]); + $userData = new UserData([ + "first_name" => "|Jan\ko|", + "last_name" => "\\`", + "email" => "janko.hrasko@example.com", + ]); + $bankTransfer = new BankTransfer(); + + return new InitiatePaymentRequest([ + "base_payment" => $basePayment, + "bank_transfer" => $bankTransfer, + "user_data" => $userData, + ]); + } + private function getPaymentPayload( float $total, string $currency, @@ -308,6 +359,44 @@ public function testInitiatePaymentCheckPaymentStatus(): void ); } + public function testInitiatePaymentCheckPaymentStatusRequiredFieldsOnly(): void + { + $accept_language = "sk"; + $preferred_method = null; + $initiate_payment_request = $this->getPaymentPayloadRequiredFieldsOnly(10, "EUR"); + + $api_instance = new TatraPayPlusAPIApi( + $this->client_id, + $this->client_secret + ); + + $response = $api_instance->initiatePayment( + "http://localhost", + $initiate_payment_request, + $preferred_method, + $accept_language + ); + + $this->assertFalse(is_null($response["object"])); + $this->assertFalse(is_null($response["response"])); + + $payment_id = $response["object"]->getPaymentId(); + $this->assertFalse(is_null($payment_id)); + + [$simple_status, $response] = $api_instance->getPaymentIntentStatus($payment_id); + + $this->assertFalse(is_null($response["object"])); + $this->assertSame($response["response"]->getStatusCode(), 200); + $this->assertSame( + $response["object"]->getAuthorizationStatus(), + PaymentIntentStatusResponse::AUTHORIZATION_STATUS__NEW + ); + $this->assertSame( + $simple_status, + TatraPayPlusService::SIMPLE_STATUS_PENDING + ); + } + public function testCancelPaymentIntent(): void { $api_instance = new TatraPayPlusAPIApi( @@ -524,41 +613,68 @@ public function testLogger() $this->assertSame(16, count($logger->lines)); } - public function testInitiateDirectTransactionMocked(): void + public function testInitiateDirectTransaction(): void { - $mock_response_body = json_encode( - array( - "paymentId" => "123456789", - "redirectFormHtml" => "custom HTML" - ) + $api_instance = new TatraPayPlusAPIApi( + $this->client_id, + $this->client_secret ); - $mock_response = new HttpResponse($mock_response_body, [], 201); - $mock_client = $this->getMockBuilder(CurlClient::class) - ->onlyMethods(["send"]) - ->getMock(); - $mock_client->method("send")->will($this->returnValue($mock_response)); + $request_data = $this->getDirectTransactionPayload(1.01, "EUR"); - $api_instance = $this->getMockBuilder(TatraPayPlusAPIApi::class) - ->onlyMethods(["addAuthHeader"]) - ->setConstructorArgs([$this->client_id, $this->client_secret]) - ->getMock(); - $api_instance - ->method("addAuthHeader") - ->will($this->returnCallback("mock_addAuthHeader")); - $api_instance->setClient($mock_client); + $response = $api_instance->initiateDirectTransaction( + "http://localhost", + $request_data, + ); + $payment_id = $response["object"]->getPaymentId(); - $request_data = $this->getDirectTransactionPayload(10, "EUR"); + $this->assertFalse(is_null($response["object"])); + $this->assertFalse(is_null($response["response"])); + $this->assertFalse(is_null($payment_id)); + + [$simple_status, $response] = $api_instance->getPaymentIntentStatus($payment_id); + + $this->assertFalse(is_null($response["object"])); + $this->assertSame($response["response"]->getStatusCode(), 200); + $this->assertSame( + $response["object"]->getAuthorizationStatus(), + PaymentIntentStatusResponse::AUTHORIZATION_STATUS_AUTH_DONE + ); + $this->assertSame( + $simple_status, + TatraPayPlusService::SIMPLE_STATUS_PENDING + ); + } + + public function testInitiateDirectTransactionRequiredFieldsOnly(): void + { + $api_instance = new TatraPayPlusAPIApi( + $this->client_id, + $this->client_secret + ); + $request_data = $this->getDirectTransactionPayloadRequiredFieldsOnly(1.01, "EUR"); $response = $api_instance->initiateDirectTransaction( "http://localhost", $request_data, ); + $payment_id = $response["object"]->getPaymentId(); $this->assertFalse(is_null($response["object"])); $this->assertFalse(is_null($response["response"])); + $this->assertFalse(is_null($payment_id)); + + [$simple_status, $response] = $api_instance->getPaymentIntentStatus($payment_id); - $this->assertSame("123456789", $response["object"]->getPaymentId()); - $this->assertSame("custom HTML", $response["object"]->getRedirectFormHtml()); + $this->assertFalse(is_null($response["object"])); + $this->assertSame($response["response"]->getStatusCode(), 200); + $this->assertSame( + $response["object"]->getAuthorizationStatus(), + PaymentIntentStatusResponse::AUTHORIZATION_STATUS_AUTH_DONE + ); + $this->assertSame( + $simple_status, + TatraPayPlusService::SIMPLE_STATUS_PENDING + ); } public function testMapSimpleStatus()