From 7a58cd93e13e845ea8dea91043d336935be15134 Mon Sep 17 00:00:00 2001 From: Nuryagdy Mustapayev Date: Tue, 21 May 2024 10:24:38 +0200 Subject: [PATCH] issue #204 kuveytpos add support for partial refund --- docs/REFUND-EXAMPLE.md | 31 ++++++++++++++--- examples/_common-codes/regular/refund.php | 33 ++++++++++++++++--- .../KuveytPosRequestDataMapper.php | 3 -- src/Gateways/KuveytPos.php | 4 +-- tests/Functional/KuveytPosTest.php | 2 +- .../KuveytPosRequestDataMapperTest.php | 3 -- 6 files changed, 57 insertions(+), 19 deletions(-) diff --git a/docs/REFUND-EXAMPLE.md b/docs/REFUND-EXAMPLE.md index b7b529b5..6663ae12 100644 --- a/docs/REFUND-EXAMPLE.md +++ b/docs/REFUND-EXAMPLE.md @@ -44,11 +44,11 @@ try { require 'config.php'; -function createRefundOrder(string $gatewayClass, array $lastResponse, string $ip): array +function createRefundOrder(string $gatewayClass, array $lastResponse, string $ip, ?float $refundAmount = null): array { $refundOrder = [ 'id' => $lastResponse['order_id'], // MerchantOrderId - 'amount' => $lastResponse['amount'], + 'amount' => $refundAmount ?? $lastResponse['amount'], 'currency' => $lastResponse['currency'], 'ref_ret_num' => $lastResponse['ref_ret_num'], 'ip' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $ip : '127.0.0.1', @@ -90,9 +90,30 @@ function createRefundOrder(string $gatewayClass, array $lastResponse, string $ip // odemeden aldiginiz cevap: $pos->getResponse(); $lastResponse = $session->get('last_response'); $ip = '127.0.0.1'; -$order = createRefundOrder(get_class($pos), $lastResponse, $ip); + +$originalAmount = $lastResponse['amount']; +// partial refund: +// $refundAmount = $originalAmount - 1; +// full refund: +$refundAmount = $originalAmount; + +$order = createRefundOrder(get_class($pos), $lastResponse, $ip, $refundAmount); + +/** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ +$eventDispatcher->addListener( + \Mews\Pos\Event\RequestDataPreparedEvent::class, + function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($pos, $originalAmount, $refundAmount) { + if ($originalAmount > $refundAmount) { + // partial refund icin calisacak kodlar + if (get_class($pos) === \Mews\Pos\Gateways\KuveytPos::class) { + $requestData = $event->getRequestData(); + // KuveytPos'da partial refund icin farkli transactionType istenmekte + $requestData['VPosMessage']['TransactionType'] = 'PartialDrawback'; + $event->setRequestData($requestData); + } + } + }); $pos->refund($order); -$response = $pos->getResponse(); -var_dump($response); +var_dump($pos->getResponse()); ``` diff --git a/examples/_common-codes/regular/refund.php b/examples/_common-codes/regular/refund.php index 9d076c56..44656ec9 100644 --- a/examples/_common-codes/regular/refund.php +++ b/examples/_common-codes/regular/refund.php @@ -10,11 +10,11 @@ require '../../_templates/_header.php'; -function createRefundOrder(string $gatewayClass, array $lastResponse, string $ip): array +function createRefundOrder(string $gatewayClass, array $lastResponse, string $ip, ?float $refundAmount = null): array { $refundOrder = [ 'id' => $lastResponse['order_id'], // MerchantOrderId - 'amount' => $lastResponse['amount'], + 'amount' => $refundAmount ?? $lastResponse['amount'], 'currency' => $lastResponse['currency'], 'ref_ret_num' => $lastResponse['ref_ret_num'], 'ip' => filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $ip : '127.0.0.1', @@ -53,12 +53,37 @@ function createRefundOrder(string $gatewayClass, array $lastResponse, string $ip return $refundOrder; } +$lastResponse = $session->get('last_response'); +$originalAmount = $lastResponse['amount']; +// partial refund: +// $refundAmount = $originalAmount - 1; +// full refund: +$refundAmount = $originalAmount; -$order = createRefundOrder(get_class($pos), $session->get('last_response'), $ip); -dump($order); +$order = createRefundOrder( + get_class($pos), + $lastResponse, + $ip, + $refundAmount +); $transaction = PosInterface::TX_TYPE_REFUND; +/** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ +$eventDispatcher->addListener( + \Mews\Pos\Event\RequestDataPreparedEvent::class, + function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($pos, $originalAmount, $refundAmount) { + if ($originalAmount > $refundAmount) { + // partial refund icin calisacak kodlar + if (get_class($pos) === \Mews\Pos\Gateways\KuveytPos::class) { + $requestData = $event->getRequestData(); + // KuveytPos'da partial refund icin farkli transactionType istenmekte + $requestData['VPosMessage']['TransactionType'] = 'PartialDrawback'; + $event->setRequestData($requestData); + } + } + }); + try { $pos->refund($order); } catch (Exception $e) { diff --git a/src/DataMapper/RequestDataMapper/KuveytPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/KuveytPosRequestDataMapper.php index 1f5dcc32..970dcaf1 100644 --- a/src/DataMapper/RequestDataMapper/KuveytPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/KuveytPosRequestDataMapper.php @@ -226,7 +226,6 @@ public function createStatusRequestData(AbstractPosAccount $posAccount, array $o */ 'StartDate' => $order['start_date']->format('Y-m-d\TH:i:s'), 'EndDate' => $order['end_date']->format('Y-m-d\TH:i:s'), - 'TransactionType' => 0, 'VPosMessage' => $this->getRequestAccountData($posAccount) + [ 'APIVersion' => self::API_VERSION, 'InstallmentMaturityCommisionFlag' => 0, @@ -277,7 +276,6 @@ public function createCancelRequestData(AbstractPosAccount $posAccount, array $o 'RRN' => $order['ref_ret_num'], 'Stan' => $order['transaction_id'], 'ProvisionNumber' => $order['auth_code'], - 'TransactionType' => 0, 'VPosMessage' => $this->getRequestAccountData($posAccount) + [ 'APIVersion' => self::API_VERSION, 'InstallmentMaturityCommisionFlag' => 0, @@ -328,7 +326,6 @@ public function createRefundRequestData(AbstractPosAccount $posAccount, array $o 'RRN' => $order['ref_ret_num'], 'Stan' => $order['transaction_id'], 'ProvisionNumber' => $order['auth_code'], - 'TransactionType' => 0, 'VPosMessage' => $this->getRequestAccountData($posAccount) + [ 'APIVersion' => self::API_VERSION, 'InstallmentMaturityCommisionFlag' => 0, diff --git a/src/Gateways/KuveytPos.php b/src/Gateways/KuveytPos.php index 6e7cfec0..e85cae6a 100644 --- a/src/Gateways/KuveytPos.php +++ b/src/Gateways/KuveytPos.php @@ -200,7 +200,6 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr * * @return array * - * @throws UnsupportedTransactionTypeException * @throws SoapFault */ protected function send($contents, string $txType, string $paymentModel, string $url): array @@ -237,7 +236,6 @@ protected function send($contents, string $txType, string $paymentModel, string * * @throws SoapFault * @throws RuntimeException - * @throws UnsupportedTransactionTypeException */ private function sendSoapRequest(array $contents, string $txType, string $url): array { @@ -269,7 +267,7 @@ private function sendSoapRequest(array $contents, string $txType, string $url): $client = new SoapClient($url, $options); try { - $result = $client->__soapCall($this->requestDataMapper->mapTxType($txType), ['parameters' => ['request' => $contents]]); + $result = $client->__soapCall($contents['VPosMessage']['TransactionType'], ['parameters' => ['request' => $contents]]);; } catch (SoapFault $throwable) { $this->logger->error('soap error response', [ 'message' => $throwable->getMessage(), diff --git a/tests/Functional/KuveytPosTest.php b/tests/Functional/KuveytPosTest.php index 73f53561..76758c53 100644 --- a/tests/Functional/KuveytPosTest.php +++ b/tests/Functional/KuveytPosTest.php @@ -161,7 +161,7 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent) use (&$eventIsThro $response = $this->pos->getResponse(); - $this->assertTrue($this->pos->isSuccess()); + $this->assertTrue($this->pos->isSuccess(), $response['error_message'] ?? null); $this->assertIsArray($response); $this->assertNotEmpty($response); diff --git a/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php index 717995b3..633116c3 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php @@ -345,7 +345,6 @@ public static function createCancelRequestDataProvider(): iterable 'RRN' => '318923298433', 'Stan' => '298433', 'ProvisionNumber' => '241839', - 'TransactionType' => 0, 'VPosMessage' => [ 'APIVersion' => KuveytPosRequestDataMapper::API_VERSION, 'InstallmentMaturityCommisionFlag' => 0, @@ -400,7 +399,6 @@ public static function createRefundRequestDataProvider(): Generator 'RRN' => '318923298433', 'Stan' => '298433', 'ProvisionNumber' => '241839', - 'TransactionType' => 0, 'VPosMessage' => [ 'APIVersion' => KuveytPosRequestDataMapper::API_VERSION, 'InstallmentMaturityCommisionFlag' => 0, @@ -451,7 +449,6 @@ public static function createStatusRequestDataProvider(): iterable 'Amount' => 0, 'MerchantId' => '80', 'OrderId' => 0, - 'TransactionType' => 0, 'VPosMessage' => [ 'APIVersion' => KuveytPosRequestDataMapper::API_VERSION, 'InstallmentMaturityCommisionFlag' => 0,