Skip to content

Commit

Permalink
Merge pull request #241 from mewebstudio/response-mapping-refactorings
Browse files Browse the repository at this point in the history
Response mapping refactorings
  • Loading branch information
nuryagdym authored Oct 12, 2024
2 parents 239338c + 1055b66 commit 6cd8a98
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ private function map3DCommonResponseData(array $raw3DAuthResponseData, ?array $r
'currency' => $this->mapCurrency($raw3DAuthResponseData['Currency']),
'transaction_time' => isset($raw3DAuthResponseData['TRXDATE']) ? new \DateTimeImmutable($raw3DAuthResponseData['TRXDATE']) : null,
'eci' => $raw3DAuthResponseData['Eci'],
/**
* TxnStat 3D doğrulama sonucunu belirtir :
* Y : Başarılı
* N : Başarısız
* A : Half Secure)
* U : Teknik Hata
* E : Hata
*/
'tx_status' => $raw3DAuthResponseData['TxnStat'],
'cavv' => null,
'md_error_message' => $raw3DAuthResponseData['ErrorMessage'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class KuveytPosResponseDataMapper extends AbstractResponseDataMapper
/**
* Order Status Codes
*
* @var array<int, string>
* @var array<int, PosInterface::PAYMENT_STATUS_*>
*/
protected array $orderStatusMappings = [
1 => PosInterface::PAYMENT_STATUS_PAYMENT_COMPLETED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ class VakifKatilimPosResponseDataMapper extends AbstractResponseDataMapper
'51' => 'reject',
];

/**
* Order Status Codes
*
* @var array<int, PosInterface::PAYMENT_STATUS_*>
*/
protected array $orderStatusMappings = [
1 => PosInterface::PAYMENT_STATUS_PAYMENT_COMPLETED,
4 => PosInterface::PAYMENT_STATUS_FULLY_REFUNDED,
5 => PosInterface::PAYMENT_STATUS_PARTIALLY_REFUNDED,
6 => PosInterface::PAYMENT_STATUS_CANCELED,
];

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -491,20 +503,16 @@ private function mapSingleOrderHistoryTransaction(array $rawTx): array
$defaultResponse['installment_count'] = $this->mapInstallment($rawTx['InstallmentCount']);
$defaultResponse['masked_number'] = $rawTx['CardNumber'];
$defaultResponse['first_amount'] = (float) $rawTx['FirstAmount'];
$defaultResponse['order_status'] = $rawTx['LastOrderStatusDescription'];

/**
* OrderStatus
* 1 => Satis
* 6 => Iptal
*/
if ('1' === $rawTx['OrderStatus']) {
$defaultResponse['order_status'] = $this->orderStatusMappings[$rawTx['LastOrderStatus']] ?? $rawTx['LastOrderStatusDescription'];
$initialOrderStatus = $this->orderStatusMappings[$rawTx['OrderStatus']] ?? null;

if (PosInterface::PAYMENT_STATUS_PAYMENT_COMPLETED === $initialOrderStatus) {
$defaultResponse['capture_amount'] = isset($rawTx['TranAmount']) ? (float) $rawTx['TranAmount'] : 0;
$defaultResponse['capture'] = $defaultResponse['first_amount'] === $defaultResponse['capture_amount'];
if ($defaultResponse['capture']) {
$defaultResponse['capture_time'] = $defaultResponse['transaction_time'];
}
} elseif ('6' === $rawTx['OrderStatus']) {
} elseif (PosInterface::PAYMENT_STATUS_CANCELED === $initialOrderStatus) {
$defaultResponse['cancel_time'] = $defaultResponse['transaction_time'];
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Factory/CryptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ class CryptFactory
public static function createGatewayCrypt(string $gatewayClass, LoggerInterface $logger): CryptInterface
{
$classMappings = [
ToslaPos::class => ToslaPosCrypt::class,
AkbankPos::class => AkbankPosCrypt::class,
EstV3Pos::class => EstV3PosCrypt::class,
EstPos::class => EstPosCrypt::class,
EstV3Pos::class => EstV3PosCrypt::class,
GarantiPos::class => GarantiPosCrypt::class,
InterPos::class => InterPosCrypt::class,
KuveytPos::class => KuveytPosCrypt::class,
VakifKatilimPos::class => KuveytPosCrypt::class,
PayFlexCPV4Pos::class => PayFlexCPV4Crypt::class,
PayForPos::class => PayForPosCrypt::class,
PosNet::class => PosNetCrypt::class,
PosNetV1Pos::class => PosNetV1PosCrypt::class,
PayFlexCPV4Pos::class => PayFlexCPV4Crypt::class,
ToslaPos::class => ToslaPosCrypt::class,
VakifKatilimPos::class => KuveytPosCrypt::class,
];

if (isset($classMappings[$gatewayClass])) {
Expand Down
10 changes: 5 additions & 5 deletions src/Factory/RequestDataMapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ class RequestDataMapperFactory
public static function createGatewayRequestMapper(string $gatewayClass, EventDispatcherInterface $eventDispatcher, CryptInterface $crypt, array $currencies = []): RequestDataMapperInterface
{
$classMappings = [
ToslaPos::class => ToslaPosRequestDataMapper::class,
AkbankPos::class => AkbankPosRequestDataMapper::class,
EstPos::class => EstPosRequestDataMapper::class,
EstV3Pos::class => EstV3PosRequestDataMapper::class,
GarantiPos::class => GarantiPosRequestDataMapper::class,
InterPos::class => InterPosRequestDataMapper::class,
KuveytPos::class => KuveytPosRequestDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosRequestDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosRequestDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosRequestDataMapper::class,
PayForPos::class => PayForPosRequestDataMapper::class,
PosNet::class => PosNetRequestDataMapper::class,
PosNetV1Pos::class => PosNetV1PosRequestDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosRequestDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosRequestDataMapper::class,
ToslaPos::class => ToslaPosRequestDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosRequestDataMapper::class,
];
if (isset($classMappings[$gatewayClass])) {
return new $classMappings[$gatewayClass]($eventDispatcher, $crypt, $currencies);
}

throw new DomainException('unsupported gateway');
throw new DomainException(\sprintf('Request data mapper not found for the gateway %s', $gatewayClass));
}
}
12 changes: 6 additions & 6 deletions src/Factory/ResponseDataMapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ class ResponseDataMapperFactory
public static function createGatewayResponseMapper(string $gatewayClass, RequestDataMapperInterface $requestDataMapper, LoggerInterface $logger): ResponseDataMapperInterface
{
$classMappings = [
ToslaPos::class => ToslaPosResponseDataMapper::class,
AkbankPos::class => AkbankPosResponseDataMapper::class,
EstV3Pos::class => EstPosResponseDataMapper::class,
EstPos::class => EstPosResponseDataMapper::class,
EstV3Pos::class => EstPosResponseDataMapper::class,
GarantiPos::class => GarantiPosResponseDataMapper::class,
InterPos::class => InterPosResponseDataMapper::class,
KuveytPos::class => KuveytPosResponseDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosResponseDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosResponseDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosResponseDataMapper::class,
PayForPos::class => PayForPosResponseDataMapper::class,
PosNet::class => PosNetResponseDataMapper::class,
PosNetV1Pos::class => PosNetV1PosResponseDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosResponseDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosResponseDataMapper::class,
ToslaPos::class => ToslaPosResponseDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosResponseDataMapper::class,
];

if (isset($classMappings[$gatewayClass])) {
Expand All @@ -74,6 +74,6 @@ public static function createGatewayResponseMapper(string $gatewayClass, Request
);
}

throw new DomainException('unsupported gateway');
throw new DomainException(\sprintf('Response data mapper not found for the gateway %s', $gatewayClass));
}
}
6 changes: 3 additions & 3 deletions src/Factory/SerializerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public static function createGatewaySerializer(string $gatewayClass): Serializer
{
/** @var SerializerInterface[] $serializers */
$serializers = [
ToslaPosSerializer::class,
AkbankPosSerializer::class,
EstPosSerializer::class,
GarantiPosSerializer::class,
InterPosSerializer::class,
KuveytPosSerializer::class,
VakifKatilimPosSerializer::class,
PayFlexV4PosSerializer::class,
PayFlexCPV4PosSerializer::class,
PayFlexV4PosSerializer::class,
PayForPosSerializer::class,
PosNetSerializer::class,
PosNetV1PosSerializer::class,
ToslaPosSerializer::class,
VakifKatilimPosSerializer::class,
];

foreach ($serializers as $serializer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ public static function statusTestDataProvider(): iterable
'installment_count' => 0,
'masked_number' => '5353********7017',
'order_id' => '1995434716',
'order_status' => null,
'order_status' => PosInterface::PAYMENT_STATUS_PAYMENT_COMPLETED,
'payment_model' => null,
'proc_return_code' => '00',
'ref_ret_num' => '035909014127',
Expand Down Expand Up @@ -797,7 +797,7 @@ public static function statusTestDataProvider(): iterable
'installment_count' => 0,
'masked_number' => '5188********2666',
'order_id' => '20240701CF44',
'order_status' => 'Iptal',
'order_status' => PosInterface::PAYMENT_STATUS_CANCELED,
'payment_model' => '3d',
'proc_return_code' => '00',
'ref_ret_num' => '418315149569',
Expand Down Expand Up @@ -1283,7 +1283,7 @@ public static function historyTestDataProvider(): array
'capture_time' => null,
'error_message' => null,
'ref_ret_num' => '036008014143',
'order_status' => 'Satis',
'order_status' => PosInterface::PAYMENT_STATUS_PAYMENT_COMPLETED,
'transaction_type' => null,
'first_amount' => 2.7,
'capture_amount' => 0,
Expand Down Expand Up @@ -1494,7 +1494,7 @@ public static function orderHistoryTestDataProvider(): array
'first_amount' => 10.01,
'installment_count' => 2,
'masked_number' => '5351********9885',
'order_status' => 'Iptal',
'order_status' => PosInterface::PAYMENT_STATUS_CANCELED,
'payment_model' => '3d',
'proc_return_code' => '00',
'ref_ret_num' => '418315158962',
Expand All @@ -1515,7 +1515,7 @@ public static function orderHistoryTestDataProvider(): array
'first_amount' => 10.01,
'installment_count' => 2,
'masked_number' => '5351********9885',
'order_status' => 'Iptal',
'order_status' => PosInterface::PAYMENT_STATUS_CANCELED,
'payment_model' => '3d',
'proc_return_code' => '00',
'ref_ret_num' => '418315158962',
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Factory/AccountFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @license MIT
*/

namespace Factory;
namespace Mews\Pos\Tests\Unit\Factory;

use Mews\Pos\Factory\AccountFactory;
use Mews\Pos\PosInterface;
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Factory/CryptFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @license MIT
*/

namespace Mews\Pos\Tests\Unit\Factory;

use Mews\Pos\Factory\CryptFactory;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* @covers \Mews\Pos\Factory\CryptFactory
*/
class CryptFactoryTest extends TestCase
{

/**
* @dataProvider createGatewayCryptDataProvider
*/
public function testCreateGatewayCrypt(string $gatewayClass, string $serializerClass): void
{
$logger = $this->createMock(LoggerInterface::class);
$crypt = CryptFactory::createGatewayCrypt($gatewayClass, $logger);
$this->assertInstanceOf($serializerClass, $crypt);
}

public static function createGatewayCryptDataProvider(): array
{
return [
[\Mews\Pos\Gateways\AkbankPos::class, \Mews\Pos\Crypt\AkbankPosCrypt::class],
[\Mews\Pos\Gateways\EstPos::class, \Mews\Pos\Crypt\EstPosCrypt::class],
[\Mews\Pos\Gateways\EstV3Pos::class, \Mews\Pos\Crypt\EstV3PosCrypt::class],
[\Mews\Pos\Gateways\GarantiPos::class, \Mews\Pos\Crypt\GarantiPosCrypt::class],
[\Mews\Pos\Gateways\InterPos::class, \Mews\Pos\Crypt\InterPosCrypt::class],
[\Mews\Pos\Gateways\KuveytPos::class, \Mews\Pos\Crypt\KuveytPosCrypt::class],
[\Mews\Pos\Gateways\PayFlexV4Pos::class, \Mews\Pos\Crypt\NullCrypt::class],
[\Mews\Pos\Gateways\PayFlexCPV4Pos::class, \Mews\Pos\Crypt\PayFlexCPV4Crypt::class],
[\Mews\Pos\Gateways\PayForPos::class, \Mews\Pos\Crypt\PayForPosCrypt::class],
[\Mews\Pos\Gateways\PosNet::class, \Mews\Pos\Crypt\PosNetCrypt::class],
[\Mews\Pos\Gateways\PosNetV1Pos::class, \Mews\Pos\Crypt\PosNetV1PosCrypt::class],
[\Mews\Pos\Gateways\ToslaPos::class, \Mews\Pos\Crypt\ToslaPosCrypt::class],
[\Mews\Pos\Gateways\VakifKatilimPos::class, \Mews\Pos\Crypt\KuveytPosCrypt::class],
[\stdClass::class, \Mews\Pos\Crypt\NullCrypt::class],
];
}
}
Loading

0 comments on commit 6cd8a98

Please sign in to comment.