From c888e3c67330d3bcbcec351243a3e99e678a76e5 Mon Sep 17 00:00:00 2001 From: Nuryagdy Mustapayev Date: Wed, 22 May 2024 16:53:28 +0200 Subject: [PATCH] issue #171 EstPos added support for excessive amount for post auth tx --- docs/PRE-AUTH-POST-EXAMPLE.md | 31 ++++--------------- examples/_common-codes/regular/post-auth.php | 25 ++++----------- .../EstPosRequestDataMapper.php | 14 +++++++-- tests/Functional/EstV3PosTest.php | 21 +++++++------ tests/Functional/PaymentTestTrait.php | 13 ++++---- .../EstPosRequestDataMapperTest.php | 26 +++++++++++++--- 6 files changed, 63 insertions(+), 67 deletions(-) diff --git a/docs/PRE-AUTH-POST-EXAMPLE.md b/docs/PRE-AUTH-POST-EXAMPLE.md index 2e84b65a..6750159d 100644 --- a/docs/PRE-AUTH-POST-EXAMPLE.md +++ b/docs/PRE-AUTH-POST-EXAMPLE.md @@ -199,10 +199,11 @@ $lastResponse = $session->get('last_response'); function createPostPayOrder(string $gatewayClass, array $lastResponse, string $ip, ?float $postAuthAmount = null): array { $postAuth = [ - 'id' => $lastResponse['order_id'], - 'amount' => $postAuthAmount ?? $lastResponse['amount'], - 'currency' => $lastResponse['currency'], - 'ip' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $ip : '127.0.0.1', + 'id' => $lastResponse['order_id'], + 'amount' => $postAuthAmount ?? $lastResponse['amount'], + 'pre_auth_amount' => $lastResponse['amount'], // amount > pre_auth_amount durumlar icin kullanilir + 'currency' => $lastResponse['currency'], + 'ip' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $ip : '127.0.0.1', ]; if (\Mews\Pos\Gateways\GarantiPos::class === $gatewayClass) { @@ -219,7 +220,7 @@ function createPostPayOrder(string $gatewayClass, array $lastResponse, string $i $lastResponse = $session->get('last_response'); $preAuthAmount = $lastResponse['amount']; -// otorizasyon kapama amount'u ön otorizasyon amount'tan daha fazla olabilir. +// Bazi gatewaylerde otorizasyon kapama amount'u ön otorizasyon amount'tan daha fazla olabilir. $postAuthAmount = $lastResponse['amount'] + 0.02; $gatewayClass = get_class($pos); @@ -230,26 +231,6 @@ $order = createPostPayOrder( $postAuthAmount ); -// ============================================================================================ -// OZEL DURUMLAR ICIN KODLAR START -// ============================================================================================ -/** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ -$eventDispatcher->addListener( - \Mews\Pos\Event\RequestDataPreparedEvent::class, - function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($preAuthAmount, $postAuthAmount) { - if (\Mews\Pos\Gateways\EstPos::class === $event->getGatewayClass() || \Mews\Pos\Gateways\EstV3Pos::class === $event->getGatewayClass()) { - // ($preAuthAmount < $postAuthAmount) durumda API isteğe ekstra değerler eklenmesi gerekiyor. - if ($preAuthAmount < $postAuthAmount) { - $requestData = $event->getRequestData(); - $requestData['Extra']['PREAMT'] = $preAuthAmount; - $event->setRequestData($requestData); - } - } - }); -// ============================================================================================ -// OZEL DURUMLAR ICIN KODLAR END -// ============================================================================================ - try { $pos->payment($paymentModel, $order, $transaction); var_dump($response); diff --git a/examples/_common-codes/regular/post-auth.php b/examples/_common-codes/regular/post-auth.php index eca671a6..11bd2eb2 100644 --- a/examples/_common-codes/regular/post-auth.php +++ b/examples/_common-codes/regular/post-auth.php @@ -11,10 +11,11 @@ function createPostPayOrder(string $gatewayClass, array $lastResponse, string $ip, ?float $postAuthAmount = null): array { $postAuth = [ - 'id' => $lastResponse['order_id'], - 'amount' => $postAuthAmount ?? $lastResponse['amount'], - 'currency' => $lastResponse['currency'], - 'ip' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $ip : '127.0.0.1', + 'id' => $lastResponse['order_id'], + 'amount' => $postAuthAmount ?? $lastResponse['amount'], + 'pre_auth_amount' => $lastResponse['amount'], // amount > pre_auth_amount durumlar icin kullanilir + 'currency' => $lastResponse['currency'], + 'ip' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $ip : '127.0.0.1', ]; if (\Mews\Pos\Gateways\GarantiPos::class === $gatewayClass) { @@ -32,7 +33,7 @@ function createPostPayOrder(string $gatewayClass, array $lastResponse, string $i $preAuthAmount = $lastResponse['amount']; // otorizasyon kapama amount'u ön otorizasyon amount'tan daha fazla olabilir. -$postAuthAmount = $lastResponse['amount'] + 0.02; +$postAuthAmount = $lastResponse['amount'] + 0.20; $gatewayClass = get_class($pos); $order = createPostPayOrder( @@ -42,20 +43,6 @@ function createPostPayOrder(string $gatewayClass, array $lastResponse, string $i $postAuthAmount ); -// ($preAuthAmount < $postAuthAmount) durumda API isteğe ekstra değerler eklenmesi gerekiyor. -/** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ -$eventDispatcher->addListener( - \Mews\Pos\Event\RequestDataPreparedEvent::class, - function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($preAuthAmount, $postAuthAmount) { - if (\Mews\Pos\Gateways\EstPos::class === $event->getGatewayClass() || \Mews\Pos\Gateways\EstV3Pos::class === $event->getGatewayClass()) { - if ($preAuthAmount < $postAuthAmount) { - $requestData = $event->getRequestData(); - $requestData['Extra']['PREAMT'] = $preAuthAmount; - $event->setRequestData($requestData); - } - } - }); - $transaction = PosInterface::TX_TYPE_PAY_POST_AUTH; require '../../_templates/_finish_non_secure_post_auth_payment.php'; diff --git a/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php index 04806f59..cdd53a23 100644 --- a/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php @@ -128,11 +128,18 @@ public function createNonSecurePostAuthPaymentRequestData(AbstractPosAccount $po { $order = $this->preparePostPaymentOrder($order); - return $this->getRequestAccountData($posAccount) + [ + $requestData = $this->getRequestAccountData($posAccount) + [ 'Type' => $this->mapTxType(PosInterface::TX_TYPE_PAY_POST_AUTH), 'OrderId' => (string) $order['id'], 'Total' => isset($order['amount']) ? (float) $this->formatAmount($order['amount']) : null, ]; + + if (isset($order['amount']) && isset($order['pre_auth_amount']) && $order['pre_auth_amount'] < $order['amount']) { + // when amount < pre_auth_amount then we need to send PREAMT value + $requestData['Extra']['PREAMT'] = $order['pre_auth_amount']; + } + + return $requestData; } /** @@ -330,8 +337,9 @@ protected function preparePaymentOrder(array $order): array protected function preparePostPaymentOrder(array $order): array { return [ - 'id' => $order['id'], - 'amount' => $order['amount'] ?? null, + 'id' => $order['id'], + 'amount' => $order['amount'] ?? null, + 'pre_auth_amount' => $order['pre_auth_amount'] ?? null, ]; } diff --git a/tests/Functional/EstV3PosTest.php b/tests/Functional/EstV3PosTest.php index 589540e3..0be4dc8b 100644 --- a/tests/Functional/EstV3PosTest.php +++ b/tests/Functional/EstV3PosTest.php @@ -185,20 +185,20 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent) use (&$eventIsThro */ public function testNonSecurePostPaymentSuccessWithMoreAmount(array $lastResponse): array { - $order = $this->createPostPayOrder(\get_class($this->pos), $lastResponse); - $preAuthAmount = $order['amount']; - // deduct more money than in pre auth request - $order['amount'] += 0.05; + $order = $this->createPostPayOrder( + \get_class($this->pos), + $lastResponse, + // deduct more money than in pre auth request + $lastResponse['amount'] + 0.20, + ); + $eventIsThrown = false; $this->eventDispatcher->addListener( RequestDataPreparedEvent::class, - function (RequestDataPreparedEvent $requestDataPreparedEvent) use (&$eventIsThrown, $preAuthAmount): void { + function (RequestDataPreparedEvent $requestDataPreparedEvent) use (&$eventIsThrown): void { $eventIsThrown = true; $this->assertSame(PosInterface::TX_TYPE_PAY_POST_AUTH, $requestDataPreparedEvent->getTxType()); - $this->assertCount(6, $requestDataPreparedEvent->getRequestData()); - $requestData = $requestDataPreparedEvent->getRequestData(); - $requestData['Extra']['PREAMT'] = $preAuthAmount; - $requestDataPreparedEvent->setRequestData($requestData); + $this->assertCount(7, $requestDataPreparedEvent->getRequestData()); }); $this->pos->payment( @@ -207,8 +207,9 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent) use (&$eventIsThro PosInterface::TX_TYPE_PAY_POST_AUTH ); - $this->assertTrue($this->pos->isSuccess()); $response = $this->pos->getResponse(); + $this->assertTrue($this->pos->isSuccess(), $response['error_message'] ?? null); + $this->assertIsArray($response); $this->assertNotEmpty($response); $this->assertTrue($eventIsThrown); diff --git a/tests/Functional/PaymentTestTrait.php b/tests/Functional/PaymentTestTrait.php index b4c15be5..a58e3714 100644 --- a/tests/Functional/PaymentTestTrait.php +++ b/tests/Functional/PaymentTestTrait.php @@ -18,7 +18,7 @@ trait PaymentTestTrait private function createPaymentOrder( string $paymentModel, string $currency = PosInterface::CURRENCY_TRY, - float $amount = 1.01, + float $amount = 10.01, int $installment = 0, bool $tekrarlanan = false ): array @@ -69,13 +69,14 @@ private function createPaymentOrder( return $order; } - private function createPostPayOrder(string $gatewayClass, array $lastResponse): array + private function createPostPayOrder(string $gatewayClass, array $lastResponse, ?float $postAuthAmount = null): array { $postAuth = [ - 'id' => $lastResponse['order_id'], - 'amount' => $lastResponse['amount'], - 'currency' => $lastResponse['currency'], - 'ip' => '127.0.0.1', + 'id' => $lastResponse['order_id'], + 'amount' => $postAuthAmount ?? $lastResponse['amount'], + 'pre_auth_amount' => $lastResponse['amount'], + 'currency' => $lastResponse['currency'], + 'ip' => '127.0.0.1', ]; if (\Mews\Pos\Gateways\GarantiPos::class === $gatewayClass) { diff --git a/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php index 2ac444ac..da26e053 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php @@ -52,7 +52,7 @@ protected function setUp(): void 'TRPS0200' ); - $this->order = [ + $this->order = [ 'id' => 'order222', 'ip' => '127.0.0.1', 'amount' => '100.25', @@ -63,7 +63,7 @@ protected function setUp(): void 'lang' => PosInterface::LANG_TR, ]; $this->dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->crypt = $this->createMock(CryptInterface::class); + $this->crypt = $this->createMock(CryptInterface::class); $this->requestDataMapper = new EstPosRequestDataMapper($this->dispatcher, $this->crypt); $this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); @@ -480,7 +480,7 @@ public static function threeDFormDataProvider(): array public static function postAuthRequestDataProvider(): array { return [ - 'without_amount' => [ + 'without_amount' => [ 'order' => [ 'id' => '2020110828BC', ], @@ -493,7 +493,7 @@ public static function postAuthRequestDataProvider(): array 'Total' => null, ], ], - 'with_amount' => [ + 'with_amount' => [ 'order' => [ 'id' => '2020110828BC', 'amount' => 1.0, @@ -507,6 +507,24 @@ public static function postAuthRequestDataProvider(): array 'Total' => 1.0, ], ], + 'with_pre_auth_amount' => [ + 'order' => [ + 'id' => '2020110828BC', + 'amount' => 1.1, + 'pre_auth_amount' => 1.0, + ], + 'expected' => [ + 'Name' => 'ISBANKAPI', + 'Password' => 'ISBANK07', + 'ClientId' => '700655000200', + 'Type' => 'PostAuth', + 'OrderId' => '2020110828BC', + 'Total' => 1.1, + 'Extra' => [ + 'PREAMT' => 1.0, + ], + ], + ], ]; }