Skip to content

Commit

Permalink
issue #204 kuveytpos add support for partial refund
Browse files Browse the repository at this point in the history
  • Loading branch information
nuryagdym committed May 21, 2024
1 parent 0945dc9 commit 7a58cd9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
31 changes: 26 additions & 5 deletions docs/REFUND-EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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());
```
33 changes: 29 additions & 4 deletions examples/_common-codes/regular/refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions src/Gateways/KuveytPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
*
* @return array<string, mixed>
*
* @throws UnsupportedTransactionTypeException
* @throws SoapFault
*/
protected function send($contents, string $txType, string $paymentModel, string $url): array
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/KuveytPosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ public static function createCancelRequestDataProvider(): iterable
'RRN' => '318923298433',
'Stan' => '298433',
'ProvisionNumber' => '241839',
'TransactionType' => 0,
'VPosMessage' => [
'APIVersion' => KuveytPosRequestDataMapper::API_VERSION,
'InstallmentMaturityCommisionFlag' => 0,
Expand Down Expand Up @@ -400,7 +399,6 @@ public static function createRefundRequestDataProvider(): Generator
'RRN' => '318923298433',
'Stan' => '298433',
'ProvisionNumber' => '241839',
'TransactionType' => 0,
'VPosMessage' => [
'APIVersion' => KuveytPosRequestDataMapper::API_VERSION,
'InstallmentMaturityCommisionFlag' => 0,
Expand Down Expand Up @@ -451,7 +449,6 @@ public static function createStatusRequestDataProvider(): iterable
'Amount' => 0,
'MerchantId' => '80',
'OrderId' => 0,
'TransactionType' => 0,
'VPosMessage' => [
'APIVersion' => KuveytPosRequestDataMapper::API_VERSION,
'InstallmentMaturityCommisionFlag' => 0,
Expand Down

0 comments on commit 7a58cd9

Please sign in to comment.