Skip to content

Commit

Permalink
Merge pull request #215 from mewebstudio/171-estpos-support-for-exces…
Browse files Browse the repository at this point in the history
…sive-amount-on-post-auth-tx

issue #171 EstPos added support for excessive amount for post auth tx
  • Loading branch information
nuryagdym authored May 22, 2024
2 parents 255f328 + c888e3c commit 5545501
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 67 deletions.
31 changes: 6 additions & 25 deletions docs/PRE-AUTH-POST-EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand All @@ -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);
Expand Down
25 changes: 6 additions & 19 deletions examples/_common-codes/regular/post-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand All @@ -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';
14 changes: 11 additions & 3 deletions src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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,
];
}

Expand Down
21 changes: 11 additions & 10 deletions tests/Functional/EstV3PosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions tests/Functional/PaymentTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function setUp(): void
'TRPS0200'
);

$this->order = [
$this->order = [
'id' => 'order222',
'ip' => '127.0.0.1',
'amount' => '100.25',
Expand All @@ -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);
Expand Down Expand Up @@ -480,7 +480,7 @@ public static function threeDFormDataProvider(): array
public static function postAuthRequestDataProvider(): array
{
return [
'without_amount' => [
'without_amount' => [
'order' => [
'id' => '2020110828BC',
],
Expand All @@ -493,7 +493,7 @@ public static function postAuthRequestDataProvider(): array
'Total' => null,
],
],
'with_amount' => [
'with_amount' => [
'order' => [
'id' => '2020110828BC',
'amount' => 1.0,
Expand All @@ -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,
],
],
],
];
}

Expand Down

0 comments on commit 5545501

Please sign in to comment.