Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Model/PaymentIntentStatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 6 additions & 2 deletions lib/TatraPayPlusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
166 changes: 141 additions & 25 deletions tests/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" => "[email protected]",
]),
"token" => "ABC12345",
]);
return $request_data;
}

private function getDirectTransactionPayload(
float $total,
string $currency,
Expand Down Expand Up @@ -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" => "<Hraško>\\`",
"email" => "[email protected]",
]);
$bankTransfer = new BankTransfer();

return new InitiatePaymentRequest([
"base_payment" => $basePayment,
"bank_transfer" => $bankTransfer,
"user_data" => $userData,
]);
}

private function getPaymentPayload(
float $total,
string $currency,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down