From 16d79e9b7513a4e9a758487f7add0f7ee6e728bb Mon Sep 17 00:00:00 2001 From: nuryagdy Date: Sun, 22 Nov 2020 18:05:48 +0300 Subject: [PATCH 1/5] code refactoring and improvement --- README.md | 46 +- config/pos.php | 41 +- examples/_main_config.php | 3 + examples/akbank/3d-pay/_config.php | 14 +- examples/akbank/3d-pay/form.php | 9 +- examples/akbank/3d-pay/response.php | 2 +- examples/akbank/3d/_config.php | 17 +- examples/akbank/3d/form.php | 7 +- examples/akbank/3d/index.php | 10 +- examples/akbank/3d/response.php | 2 +- examples/akbank/regular/_config.php | 18 +- examples/akbank/regular/cancel.php | 11 +- examples/akbank/regular/direct.php | 3 +- examples/akbank/regular/history.php | 6 +- examples/akbank/regular/post.php | 10 +- examples/akbank/regular/refund.php | 11 +- examples/akbank/regular/response.php | 7 +- examples/akbank/regular/status.php | 14 +- .../finansbank-payfor/3d-host/_config.php | 14 +- examples/finansbank-payfor/3d-host/index.php | 5 +- .../finansbank-payfor/3d-host/response.php | 10 +- examples/finansbank-payfor/3d-pay/_config.php | 14 +- .../finansbank-payfor/3d-pay/payment-form.php | 5 +- .../finansbank-payfor/3d-pay/response.php | 12 +- examples/finansbank-payfor/3d/_config.php | 14 +- .../finansbank-payfor/3d/payment-form.php | 8 +- examples/finansbank-payfor/3d/response.php | 9 +- .../finansbank-payfor/regular/_config.php | 13 +- examples/finansbank-payfor/regular/cancel.php | 11 +- examples/finansbank-payfor/regular/direct.php | 7 +- .../finansbank-payfor/regular/history.php | 9 +- .../finansbank-payfor/regular/post-auth.php | 20 +- examples/finansbank-payfor/regular/refund.php | 6 +- .../finansbank-payfor/regular/response.php | 19 +- examples/finansbank-payfor/regular/status.php | 10 +- examples/garanti/3d-pay/_config.php | 15 +- examples/garanti/3d-pay/form.php | 10 +- examples/garanti/3d-pay/response.php | 10 +- examples/garanti/3d/_config.php | 17 +- examples/garanti/3d/form.php | 10 +- examples/garanti/3d/response.php | 10 +- examples/garanti/regular/_config.php | 20 +- examples/garanti/regular/cancel.php | 17 +- examples/garanti/regular/direct.php | 7 +- examples/garanti/regular/history.php | 8 +- examples/garanti/regular/post.php | 6 +- examples/garanti/regular/refund.php | 16 +- examples/garanti/regular/response.php | 11 +- examples/garanti/regular/status.php | 15 +- examples/ykb/3d/_config.php | 21 +- examples/ykb/3d/form.php | 5 +- examples/ykb/3d/index.php | 3 +- examples/ykb/3d/response.php | 6 +- examples/ykb/regular/_config.php | 17 +- examples/ykb/regular/cancel.php | 20 +- examples/ykb/regular/direct.php | 7 +- examples/ykb/regular/history.php | 4 +- examples/ykb/regular/post.php | 10 +- examples/ykb/regular/refund.php | 11 +- examples/ykb/regular/response.php | 7 +- examples/ykb/regular/status.php | 11 +- src/Entity/Account/AbstractPosAccount.php | 118 ++ src/Entity/Account/EstPosAccount.php | 10 + src/Entity/Account/GarantiPosAccount.php | 65 + src/Entity/Account/PayForAccount.php | 10 + src/Entity/Account/PosNetAccount.php | 75 ++ src/EstPos.php | 893 ------------- .../MissingAccountInfoException.php | 24 + src/Exceptions/NotImplementedException.php | 18 + src/Factory/AccountFactory.php | 101 ++ src/Factory/PosFactory.php | 51 + src/GarantiPos.php | 1076 ---------------- src/Gateways/AbstractGateway.php | 597 +++++++++ src/Gateways/EstPos.php | 765 ++++++++++++ src/Gateways/GarantiPos.php | 994 +++++++++++++++ src/{ => Gateways}/PayForPos.php | 765 +++++------- src/Gateways/PosNet.php | 1054 ++++++++++++++++ src/{ => Gateways}/PosNetCrypt.php | 73 +- src/Pos.php | 230 ---- src/PosHelpersTrait.php | 105 -- src/PosInterface.php | 77 +- src/PosNet.php | 1100 ----------------- tests/EstPostTest.php | 129 -- tests/GarantiPosTest.php | 95 -- tests/Gateways/EstPostTest.php | 407 ++++++ tests/Gateways/GarantiPosTest.php | 519 ++++++++ tests/Gateways/PayForTest.php | 396 ++++++ tests/{ => Gateways}/PosNetCryptTest.php | 12 +- tests/Gateways/PosNetTest.php | 421 +++++++ tests/PayForTest.php | 131 -- tests/PosNetTest.php | 106 -- tests/PosTest.php | 89 -- 92 files changed, 6307 insertions(+), 4910 deletions(-) create mode 100644 src/Entity/Account/AbstractPosAccount.php create mode 100644 src/Entity/Account/EstPosAccount.php create mode 100644 src/Entity/Account/GarantiPosAccount.php create mode 100644 src/Entity/Account/PayForAccount.php create mode 100644 src/Entity/Account/PosNetAccount.php delete mode 100644 src/EstPos.php create mode 100644 src/Exceptions/MissingAccountInfoException.php create mode 100644 src/Exceptions/NotImplementedException.php create mode 100644 src/Factory/AccountFactory.php create mode 100644 src/Factory/PosFactory.php delete mode 100644 src/GarantiPos.php create mode 100644 src/Gateways/AbstractGateway.php create mode 100644 src/Gateways/EstPos.php create mode 100644 src/Gateways/GarantiPos.php rename src/{ => Gateways}/PayForPos.php (62%) create mode 100644 src/Gateways/PosNet.php rename src/{ => Gateways}/PosNetCrypt.php (55%) delete mode 100644 src/Pos.php delete mode 100644 src/PosHelpersTrait.php delete mode 100644 src/PosNet.php delete mode 100644 tests/EstPostTest.php delete mode 100644 tests/GarantiPosTest.php create mode 100644 tests/Gateways/EstPostTest.php create mode 100644 tests/Gateways/GarantiPosTest.php create mode 100644 tests/Gateways/PayForTest.php rename tests/{ => Gateways}/PosNetCryptTest.php (67%) create mode 100644 tests/Gateways/PosNetTest.php delete mode 100644 tests/PayForTest.php delete mode 100644 tests/PosNetTest.php delete mode 100644 tests/PosTest.php diff --git a/README.md b/README.md index 6864cf4e..1428dce2 100644 --- a/README.md +++ b/README.md @@ -47,18 +47,13 @@ $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); // API kullanıcı bilgileri -$account = [ - 'bank' => 'akbank', - 'model' => 'regular', - 'client_id' => 'XXXXXXXX', - 'username' => 'XXXXXXXX', - 'password' => 'XXXXXXXX', - 'env' => 'test', // test veya production. test ise; API Test Url, production ise; API Production URL kullanılır. -]; +$account = \Mews\Pos\Factory\AccountFactory::createEstPosAccount('akbank', 'XXXXXXX', 'XXXXXXX', 'XXXXXXX', '3d', 'XXXXXXX', \Mews\Pos\Gateways\EstPos::LANG_TR); // API kullanıcı hesabı ile paket bir değişkene aktarılıyor try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + // test veya production. test ise; API Test Url, production ise; API Production URL kullanılır. + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); exit(); @@ -84,30 +79,29 @@ $order = [ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth (Direkt satış için pay, ön provizyon için pre) ]; // Kredi kartı bilgieri $card = new \Mews\Pos\Entity\Card\CreditCardEstPos('1111222233334444', '20', '01', '000'); // API kullanıcısı ile oluşturulan $pos değişkenine prepare metoduyla sipariş bilgileri gönderiliyor -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); // Ödeme tamamlanıyor -$payment = $pos->payment($card); +$pos->payment($card); // Ödeme başarılı mı? -$payment->isSuccess(); +$pos->isSuccess(); //veya $pos->isSuccess(); // Ödeme başarısız mı? -$payment->isError(); +$pos->isError(); //veya $pos->isError(); // Sonuç çıktısı -dump($payment->getResponse()); +dump($pos->getResponse()); ```` @@ -124,21 +118,18 @@ Projenizde bir ayar dosyası oluşturup (pos_ayarlar.php gibi), paket içerisind [ - 'TRY' => 949, - 'USD' => 840, - 'EUR' => 978, - 'GBP' => 826, - 'JPY' => 392, - 'RUB' => 643, +// 'TRY' => 949, +// 'USD' => 840, ], // Banka sanal pos tanımlamaları 'banks' => [ 'akbank' => [ 'name' => 'AKBANK T.A.S.', - 'class' => \Mews\Pos\EstPos::class, + 'class' => \Mews\Pos\Gateways\EstPos::class, 'urls' => [ 'production' => 'https://www.sanalakpos.com/fim/api', 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', @@ -152,7 +143,7 @@ return [ // Yeni eklenen banka 'isbank' => [ 'name' => 'İŞ BANKASI .A.S.', - 'class' => \Mews\Pos\EstPos::class, // Altyapı sınıfı + 'class' => \Mews\Pos\Gateways\EstPos::class, // Altyapı sınıfı 'urls' => [ 'production' => 'xxxx', // API Url 'test' => 'xxxx', // API Test Url @@ -169,8 +160,9 @@ return [ Bundan sonra nesnemizi, yeni ayarlarımıza göre oluşturup kullanmamız gerekir. Örnek: ```php +//yeni ayar yolu ya da degeri $yeni_ayarlar = require './pos_ayarlar.php'; -$pos = new \Mews\Pos\Pos($account, $yeni_ayarlar); +$pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account, $yeni_ayarlar); ``` ### Örnek Kodlar @@ -187,7 +179,9 @@ http://localhost/ URL projenin `examples` klasörünün içine bakar. - Dökümantasyon hazırlanacak - UnitTest yazılacak -> Bu hiçbir zaman olmayabilir, birisi el atarsa sevinirim :) -> Değerli yorum, öneri ve katkılarınızı bekliyorum. +> Değerli yorum, öneri ve katkılarınızı +> +> Sorun bulursanız veya eklenmesi gereken POS sistemi varsa lütfen issue oluşturun. License ---- diff --git a/config/pos.php b/config/pos.php index 8d78183b..fe77d4df 100644 --- a/config/pos.php +++ b/config/pos.php @@ -1,22 +1,16 @@ [ - 'TRY' => 949, - 'USD' => 840, - 'EUR' => 978, - 'GBP' => 826, - 'JPY' => 392, - 'RUB' => 643, +// 'TRY' => 949, +// 'USD' => 840, ], - // Banks 'banks' => [ 'akbank' => [ 'name' => 'AKBANK T.A.S.', - 'class' => Mews\Pos\EstPos::class, + 'class' => Mews\Pos\Gateways\EstPos::class, 'urls' => [ 'production' => 'https://www.sanalakpos.com/fim/api', 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', @@ -28,7 +22,7 @@ ], 'ziraat' => [ 'name' => 'Ziraat Bankası', - 'class' => Mews\Pos\EstPos::class, + 'class' => Mews\Pos\Gateways\EstPos::class, 'urls' => [ 'production' => 'https://sanalpos2.ziraatbank.com.tr/fim/api', 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', @@ -40,7 +34,7 @@ ], 'finansbank' => [ 'name' => 'QNB Finansbank', - 'class' => Mews\Pos\EstPos::class, + 'class' => Mews\Pos\Gateways\EstPos::class, 'urls' => [ 'production' => 'https://www.fbwebpos.com/fim/api', 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', @@ -52,7 +46,7 @@ ], 'halkbank' => [ 'name' => 'Halkbank', - 'class' => Mews\Pos\EstPos::class, + 'class' => Mews\Pos\Gateways\EstPos::class, 'urls' => [ 'production' => 'https://sanalpos.halkbank.com.tr/fim/api', 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', @@ -64,7 +58,7 @@ ], 'teb' => [ 'name' => 'TEB', - 'class' => Mews\Pos\EstPos::class, + 'class' => Mews\Pos\Gateways\EstPos::class, 'urls' => [ 'production' => 'https://sanalpos.teb.com.tr/fim/api', 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', @@ -76,7 +70,7 @@ ], 'isbank' => [ 'name' => 'İşbank', - 'class' => Mews\Pos\EstPos::class, + 'class' => Mews\Pos\Gateways\EstPos::class, 'urls' => [ 'production' => 'https://sanalpos.isbank.com.tr/fim/api', 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', @@ -88,7 +82,8 @@ ], 'isbank-payflex' => [ 'name' => 'İşbank - PayFlex', - 'class' => Mews\Pos\PayFlex::class, + //TODO implement PayFlex + 'class' => Mews\Pos\Gateways\PayFlex::class, 'urls' => [ 'production' => 'https://trx.payflex.com.tr/VposWeb/v3/Vposreq.aspx', 'test' => 'https://sanalpos.innova.com.tr/ISBANK_v4/VposWeb/v3/Vposreq.aspx', @@ -100,7 +95,7 @@ ], 'yapikredi' => [ 'name' => 'Yapıkredi', - 'class' => Mews\Pos\PosNet::class, + 'class' => Mews\Pos\Gateways\PosNet::class, 'urls' => [ 'production' => 'https://www.posnet.ykb.com/PosnetWebService/XML', 'test' => 'https://setmpos.ykb.com/PosnetWebService/XML', @@ -109,17 +104,10 @@ 'test' => 'https://setmpos.ykb.com/3DSWebService/YKBPaymentService', ], ], - 'order' => [ - 'id_total_length' => 24, - 'id_length' => 20, - 'id_3d_prefix' => 'TDSC', - 'id_3d_pay_prefix' => '', //? - 'id_regular_prefix' => '' //? - ] ], 'garanti' => [ 'name' => 'Garanti', - 'class' => Mews\Pos\GarantiPos::class, + 'class' => Mews\Pos\Gateways\GarantiPos::class, 'urls' => [ 'production' => 'https://sanalposprov.garanti.com.tr/VPServlet', 'test' => 'https://sanalposprovtest.garanti.com.tr/VPServlet', @@ -131,7 +119,7 @@ ], 'qnbfinansbank-payfor' => [ 'name' => 'QNBFinansbank-PayFor', - 'class' => Mews\Pos\PayForPos::class, + 'class' => Mews\Pos\Gateways\PayForPos::class, 'urls' => [ 'production' => 'https://vpos.qnbfinansbank.com/Gateway/XMLGate.aspx', 'test' => 'https://vpostest.qnbfinansbank.com/Gateway/XmlGate.aspx', @@ -146,5 +134,4 @@ ] ] ], - ]; diff --git a/examples/_main_config.php b/examples/_main_config.php index 1d87ec48..514b1093 100644 --- a/examples/_main_config.php +++ b/examples/_main_config.php @@ -1,4 +1,7 @@ getClientIp(); -$account = [ - 'bank' => 'akbank', - 'model' => '3d_pay', - 'client_id' => 'XXXXXXX', - 'store_key' => 'XXXXXXX', - 'env' => 'test', - 'lang' => \Mews\Pos\EstPos::LANG_TR -]; +$account = \Mews\Pos\Factory\AccountFactory::createEstPosAccount('akbank', 'XXXXXXX', 'XXXXXXX', '', '3d_pay', 'XXXXXXX'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/akbank/3d-pay/form.php b/examples/akbank/3d-pay/form.php index a3dd80f4..ffb027d7 100644 --- a/examples/akbank/3d-pay/form.php +++ b/examples/akbank/3d-pay/form.php @@ -11,7 +11,7 @@ $orderId = date('Ymd') . strtoupper(substr(uniqid(sha1(time())),0,4)); -$amount = (double) 320; +$amount = (double) 32.0; $instalment = '0'; $success_url = $baseUrl . 'response.php'; @@ -29,8 +29,7 @@ 'ip' => $ip, 'success_url' => $success_url, 'fail_url' => $fail_url, - 'transaction' => 'pay', // pay => Auth, pre PreAuth - 'lang' => \Mews\Pos\EstPos::LANG_TR, + 'lang' => \Mews\Pos\Gateways\EstPos::LANG_TR, 'rand' => $rand, ]; @@ -45,9 +44,9 @@ $request->get('type') ); -$pos->prepare($order, $card); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY, $card); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); ?>
diff --git a/examples/akbank/3d-pay/response.php b/examples/akbank/3d-pay/response.php index fa0e6968..2b2f1d5a 100644 --- a/examples/akbank/3d-pay/response.php +++ b/examples/akbank/3d-pay/response.php @@ -11,7 +11,7 @@ $order = (array) json_decode($redis->lPop('order')); -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); $payment = $pos->payment(); $response = $payment->getResponse(); ?> diff --git a/examples/akbank/3d/_config.php b/examples/akbank/3d/_config.php index 25ead372..636d45ac 100644 --- a/examples/akbank/3d/_config.php +++ b/examples/akbank/3d/_config.php @@ -3,24 +3,15 @@ require '../../_main_config.php'; $path = '/akbank/3d/'; -$baseUrl = $hostUrl . $path; +$baseUrl = $hostUrl.$path; $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); - -$account = [ - 'bank' => 'akbank', - 'model' => '3d', - 'client_id' => 'XXXXXXX', - 'username' => 'XXXXXXX', - 'password' => 'XXXXXXX', - 'store_key' => 'XXXXXXX', - 'env' => 'test', - 'lang' => \Mews\Pos\EstPos::LANG_TR -]; +$account = \Mews\Pos\Factory\AccountFactory::createEstPosAccount('akbank', 'XXXXXXX', 'XXXXXXX', 'XXXXXXX', '3d', 'XXXXXXX', \Mews\Pos\Gateways\EstPos::LANG_TR); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/akbank/3d/form.php b/examples/akbank/3d/form.php index 9f35cfa1..7a0b8793 100644 --- a/examples/akbank/3d/form.php +++ b/examples/akbank/3d/form.php @@ -29,8 +29,7 @@ 'ip' => $ip, 'success_url' => $success_url, 'fail_url' => $fail_url, - 'transaction' => 'pay', // pay => Auth, pre PreAuth, - 'lang' => \Mews\Pos\EstPos::LANG_TR, + 'lang' => \Mews\Pos\Gateways\EstPos::LANG_TR, 'rand' => $rand, ]; @@ -45,9 +44,9 @@ $request->get('type') ); -$pos->prepare($order, $card); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY, $card); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); ?> diff --git a/examples/akbank/3d/index.php b/examples/akbank/3d/index.php index 93002431..c3ebd1d4 100644 --- a/examples/akbank/3d/index.php +++ b/examples/akbank/3d/index.php @@ -4,26 +4,26 @@ require '../../template/_header.php'; -$url = $baseUrl . 'form.php'; +$url = $baseUrl.'form.php'; ?>
- +
- +
@@ -45,7 +45,7 @@
- +

diff --git a/examples/akbank/3d/response.php b/examples/akbank/3d/response.php index f0f15906..3f77b6fc 100644 --- a/examples/akbank/3d/response.php +++ b/examples/akbank/3d/response.php @@ -11,7 +11,7 @@ $order = (array) json_decode($redis->lPop('order')); -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); $payment = $pos->payment(); $response = $payment->getResponse(); ?> diff --git a/examples/akbank/regular/_config.php b/examples/akbank/regular/_config.php index 06151dd4..a8ec53aa 100644 --- a/examples/akbank/regular/_config.php +++ b/examples/akbank/regular/_config.php @@ -3,29 +3,21 @@ require '../../_main_config.php'; $path = '/akbank/regular/'; -$baseUrl = $hostUrl . $path; +$baseUrl = $hostUrl.$path; $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); - -$account = [ - 'bank' => 'akbank', - 'model' => 'regular', - 'client_id' => 'XXXXXXX', - 'username' => 'XXXXXXX', - 'password' => 'XXXXXXX', - 'env' => 'test', - 'lang' => \Mews\Pos\EstPos::LANG_TR -]; +$account = \Mews\Pos\Factory\AccountFactory::createEstPosAccount('akbank', 'XXXXXXX', 'XXXXXXX', '', 'regular'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { dump($e->getCode(), $e->getMessage()); } -$gateway = $baseUrl . 'response.php'; +$gateway = $baseUrl.'response.php'; $templateTitle = 'Regular Payment'; diff --git a/examples/akbank/regular/cancel.php b/examples/akbank/regular/cancel.php index 5b682666..b52bfef1 100644 --- a/examples/akbank/regular/cancel.php +++ b/examples/akbank/regular/cancel.php @@ -7,11 +7,14 @@ require '../../template/_header.php'; // Cancel Order -$cancel = $pos->bank->cancel([ - 'order_id' => '20181029A3C1', -]); +$order = [ + 'id' => '20181029A3C1', +]; +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_CANCEL); -$response = $cancel->getResponse(); +$pos->cancel(); + +$response = $pos->getResponse(); ?>
diff --git a/examples/akbank/regular/direct.php b/examples/akbank/regular/direct.php index 21b34777..e4032e57 100644 --- a/examples/akbank/regular/direct.php +++ b/examples/akbank/regular/direct.php @@ -14,13 +14,12 @@ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth ]; $card = new \Mews\Pos\Entity\Card\CreditCardEstPos('4355084355084358', '20', '12', '000'); try { - $pos->prepare($order); + $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); } catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { dump($e->getCode(), $e->getMessage()); } diff --git a/examples/akbank/regular/history.php b/examples/akbank/regular/history.php index 31acedf2..5071507f 100644 --- a/examples/akbank/regular/history.php +++ b/examples/akbank/regular/history.php @@ -7,7 +7,7 @@ require '../../template/_header.php'; // History Order -$query= $pos->bank->history([ +$query= $pos->history([ 'order_id' => '201810297189', ]); @@ -15,8 +15,8 @@ ?>
-

- proc_return_code == '00' ? 'History Order is successful!' : 'History Order is not successful!'; ?> +

+ proc_return_code === '00' ? 'History Order is successful!' : 'History Order is not successful!'; ?>

All Data Dump:
diff --git a/examples/akbank/regular/post.php b/examples/akbank/regular/post.php index b1f2e90f..08529a6a 100644 --- a/examples/akbank/regular/post.php +++ b/examples/akbank/regular/post.php @@ -22,7 +22,8 @@ require '../../template/_header.php'; try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { @@ -31,11 +32,10 @@ $order = [ 'id' => '201810297189', - 'transaction' => 'post', ]; try { - $pos->prepare($order); + $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_POST_PAY); } catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { dump($e->getCode(), $e->getMessage()); } @@ -46,8 +46,8 @@ ?>
-

- isSuccess() == '00' ? 'Post Auth Order is successful!' : 'Post Auth Order is not successful!'; ?> +

+ isSuccess() === '00' ? 'Post Auth Order is successful!' : 'Post Auth Order is not successful!'; ?>

All Data Dump:
diff --git a/examples/akbank/regular/refund.php b/examples/akbank/regular/refund.php index 10dac0d2..e2971cc4 100644 --- a/examples/akbank/regular/refund.php +++ b/examples/akbank/regular/refund.php @@ -6,13 +6,14 @@ require '../../template/_header.php'; -// Refund Order -$refund = $pos->bank->refund([ - 'order_id' => '201810297E8B', +$pos->prepare([ + 'id' => '201810297E8B', 'amount' => '100', -]); +], \Mews\Pos\Gateways\AbstractGateway::TX_REFUND); +// Refund Order +$pos->refund(); -$response = $refund->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/akbank/regular/response.php b/examples/akbank/regular/response.php index a5583ffc..ef64507c 100644 --- a/examples/akbank/regular/response.php +++ b/examples/akbank/regular/response.php @@ -21,10 +21,9 @@ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pre', // pay => Auth, pre PreAuth ]; -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PRE_PAY); $card = new \Mews\Pos\Entity\Card\CreditCardEstPos( $request->get('number'), @@ -33,8 +32,8 @@ $request->get('cvv') ); -$payment = $pos->payment($card); -$response = $payment->getResponse(); +$pos->payment($card); +$response = $pos->getResponse(); ?>
diff --git a/examples/akbank/regular/status.php b/examples/akbank/regular/status.php index e5167182..68903e71 100644 --- a/examples/akbank/regular/status.php +++ b/examples/akbank/regular/status.php @@ -6,17 +6,19 @@ require '../../template/_header.php'; +$order = [ + 'id' => '201810297189', +]; +$pos->prepare($order,\Mews\Pos\Gateways\AbstractGateway::TX_STATUS); // Query Order -$query= $pos->bank->status([ - 'order_id' => '201810297189' -]); +$pos->status(); -$response = $query->getResponse(); +$response = $pos->getResponse(); ?>
-

- proc_return_code == '00' ? 'Query Order is successful!' : 'Query Order is not successful!'; ?> +

+ proc_return_code === '00' ? 'Query Order is successful!' : 'Query Order is not successful!'; ?>

All Data Dump:
diff --git a/examples/finansbank-payfor/3d-host/_config.php b/examples/finansbank-payfor/3d-host/_config.php index cfaaadca..acf1eb89 100644 --- a/examples/finansbank-payfor/3d-host/_config.php +++ b/examples/finansbank-payfor/3d-host/_config.php @@ -8,19 +8,11 @@ $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); -$account = [ - 'bank' => 'qnbfinansbank-payfor', - 'model' => '3d_host', - 'client_id' => '085300000009704', - 'username' => 'QNB_API_KULLANICI_3DPAY', - 'password' => 'UcBN0', - 'store_key' => '12345678', //MerchantPass only needed for 3D payment - 'env' => 'test', - 'lang' => \Mews\Pos\PayForPos::LANG_EN, -]; +$account = \Mews\Pos\Factory\AccountFactory::createPayForAccount('qnbfinansbank-payfor', '085300000009704', 'QNB_API_KULLANICI_3DPAY', 'UcBN0', '3d_host', '12345678'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/finansbank-payfor/3d-host/index.php b/examples/finansbank-payfor/3d-host/index.php index b30a7fba..99928b66 100644 --- a/examples/finansbank-payfor/3d-host/index.php +++ b/examples/finansbank-payfor/3d-host/index.php @@ -23,14 +23,13 @@ 'ip' => $ip, 'success_url' => $successUrl, 'fail_url' => $failUrl, - 'transaction' => 'pay', // pay => Auth, pre PreAuth, 'rand' => $rand, ]; $redis->lPush('order', json_encode($order)); -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); ?> diff --git a/examples/finansbank-payfor/3d-host/response.php b/examples/finansbank-payfor/3d-host/response.php index da93af15..72d46f9a 100644 --- a/examples/finansbank-payfor/3d-host/response.php +++ b/examples/finansbank-payfor/3d-host/response.php @@ -12,9 +12,9 @@ dump($account); dump($order); -$pos->prepare($order); -$payment = $pos->payment(); -$response = $payment->getResponse(); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); +$pos->payment(); +$response = $pos->getResponse(); if ($pos->isSuccess()) { $redis->lPush('order', json_encode($order)); @@ -100,9 +100,7 @@
isSuccess()) : ?> - - Cancel payment - + Cancel payment Order Status diff --git a/examples/finansbank-payfor/3d-pay/_config.php b/examples/finansbank-payfor/3d-pay/_config.php index 4c2bddde..1377afe0 100644 --- a/examples/finansbank-payfor/3d-pay/_config.php +++ b/examples/finansbank-payfor/3d-pay/_config.php @@ -8,19 +8,11 @@ $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); -$account = [ - 'bank' => 'qnbfinansbank-payfor', - 'model' => '3d_pay', - 'client_id' => '085300000009704', - 'username' => 'QNB_API_KULLANICI_3DPAY', - 'password' => 'UcBN0', - 'store_key' => '12345678', //MerchantPass only needed for 3D payment - 'env' => 'test', - 'lang' => \Mews\Pos\PayForPos::LANG_EN, -]; +$account = \Mews\Pos\Factory\AccountFactory::createPayForAccount('qnbfinansbank-payfor', '085300000009704', 'QNB_API_KULLANICI_3DPAY', 'UcBN0', '3d_pay', '12345678'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/finansbank-payfor/3d-pay/payment-form.php b/examples/finansbank-payfor/3d-pay/payment-form.php index 1ae8d126..97d4f611 100644 --- a/examples/finansbank-payfor/3d-pay/payment-form.php +++ b/examples/finansbank-payfor/3d-pay/payment-form.php @@ -23,14 +23,13 @@ 'ip' => $ip, 'success_url' => $successUrl, 'fail_url' => $failUrl, - 'transaction' => 'pay', // pay => Auth, pre PreAuth, 'rand' => $rand, ]; $redis->lPush('order', json_encode($order)); -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); ?> diff --git a/examples/finansbank-payfor/3d-pay/response.php b/examples/finansbank-payfor/3d-pay/response.php index 78983b57..757212ea 100644 --- a/examples/finansbank-payfor/3d-pay/response.php +++ b/examples/finansbank-payfor/3d-pay/response.php @@ -12,9 +12,9 @@ $order = (array) json_decode($redis->lPop('order')); dump($order); -$pos->prepare($order); -$payment = $pos->payment(); -$response = $payment->getResponse(); +$pos->prepare($order,\Mews\Pos\Gateways\AbstractGateway::TX_PAY); +$pos->payment(); +$response = $pos->getResponse(); if ($pos->isSuccess()) { $redis->lPush('order', json_encode($order)); @@ -106,12 +106,10 @@
diff --git a/examples/finansbank-payfor/3d/_config.php b/examples/finansbank-payfor/3d/_config.php index 87107e9a..a6c14e5b 100644 --- a/examples/finansbank-payfor/3d/_config.php +++ b/examples/finansbank-payfor/3d/_config.php @@ -8,19 +8,11 @@ $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); -$account = [ - 'bank' => 'qnbfinansbank-payfor', - 'model' => '3d', - 'client_id' => '085300000009704', - 'username' => 'QNB_API_KULLANICI_3DPAY', - 'password' => 'UcBN0', - 'store_key' => '12345678', //MerchantPass only needed for 3D payment - 'env' => 'test', - 'lang' => \Mews\Pos\PayForPos::LANG_EN, -]; +$account = \Mews\Pos\Factory\AccountFactory::createPayForAccount('qnbfinansbank-payfor', '085300000009704', 'QNB_API_KULLANICI_3DPAY', 'UcBN0', '3d', '12345678'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/finansbank-payfor/3d/payment-form.php b/examples/finansbank-payfor/3d/payment-form.php index 17e773e8..7c878e45 100644 --- a/examples/finansbank-payfor/3d/payment-form.php +++ b/examples/finansbank-payfor/3d/payment-form.php @@ -23,15 +23,15 @@ 'ip' => $ip, 'success_url' => $successUrl, 'fail_url' => $failUrl, - 'transaction' => 'pay', // pay => Auth, pre PreAuth, 'rand' => $rand, ]; -$pos->prepare($order); -$order['hash'] = $pos->get3DHash(); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); +//$order['hash'] = $pos->create3D(); $redis->lPush('order', json_encode($order)); +$redis->lPush('transaction', \Mews\Pos\Gateways\AbstractGateway::TX_PAY); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); ?> diff --git a/examples/finansbank-payfor/3d/response.php b/examples/finansbank-payfor/3d/response.php index a14f28cb..f4882e5a 100644 --- a/examples/finansbank-payfor/3d/response.php +++ b/examples/finansbank-payfor/3d/response.php @@ -9,12 +9,13 @@ exit(); } $order = (array) json_decode($redis->lPop('order')); +$txType = $redis->lPop('transaction'); dump($account); dump($order); -$pos->prepare($order); -$payment = $pos->payment(); -$response = $payment->getResponse(); +$pos->prepare($order, $txType); +$pos->payment(); +$response = $pos->getResponse(); if ($pos->isSuccess()) { $redis->lPush('order', json_encode($order)); @@ -100,7 +101,7 @@
isSuccess()) : ?> - + Cancel payment Order Status diff --git a/examples/finansbank-payfor/regular/_config.php b/examples/finansbank-payfor/regular/_config.php index 526dfdbc..ac84c6d1 100644 --- a/examples/finansbank-payfor/regular/_config.php +++ b/examples/finansbank-payfor/regular/_config.php @@ -8,18 +8,11 @@ $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); -$account = [ - 'bank' => 'qnbfinansbank-payfor', - 'model' => 'regular', - 'client_id' => '085300000009704', - 'username' => 'QNB_API_KULLANICI_3DPAY', - 'password' => 'UcBN0', - 'env' => 'test', - 'lang' => \Mews\Pos\PayForPos::LANG_EN, -]; +$account = \Mews\Pos\Factory\AccountFactory::createPayForAccount('qnbfinansbank-payfor', '085300000009704', 'QNB_API_KULLANICI_3DPAY', 'UcBN0'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/finansbank-payfor/regular/cancel.php b/examples/finansbank-payfor/regular/cancel.php index 98a0059a..2387f1e3 100644 --- a/examples/finansbank-payfor/regular/cancel.php +++ b/examples/finansbank-payfor/regular/cancel.php @@ -13,17 +13,12 @@ 'currency' => $ord['currency'], ]; -/*$order = [ - 'id' => '202011012784', - 'currency' => 'TRY', -];*/ - -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_CANCEL); // Cancel Order -$cancel = $pos->bank->cancel([]); +$pos->cancel(); -$response = $cancel->getResponse(); +$response = $pos->getResponse(); ?> diff --git a/examples/finansbank-payfor/regular/direct.php b/examples/finansbank-payfor/regular/direct.php index e066bbbc..e0ceee78 100644 --- a/examples/finansbank-payfor/regular/direct.php +++ b/examples/finansbank-payfor/regular/direct.php @@ -17,17 +17,16 @@ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth ]; -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); $card = new \Mews\Pos\Entity\Card\CreditCardPayFor('4155650100416111', '25', '01', '123', 'John Doe'); -$payment = $pos->payment($card); +$pos->payment($card); -$response = $payment->getResponse(); +$response = $pos->getResponse(); ?> diff --git a/examples/finansbank-payfor/regular/history.php b/examples/finansbank-payfor/regular/history.php index 475be423..c06f0435 100644 --- a/examples/finansbank-payfor/regular/history.php +++ b/examples/finansbank-payfor/regular/history.php @@ -6,15 +6,16 @@ require '../../template/_header.php'; + // History Order -$query= $pos->bank->history([ +$pos->history([ //siparis tarihi - 'reqDate' => '20201031', + //'reqDate' => '20201031', //veya siparis ID - 'orderId' => '20201031C06E', + 'id' => '20201031C06E', ]); -$response = $query->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/finansbank-payfor/regular/post-auth.php b/examples/finansbank-payfor/regular/post-auth.php index 1b1885c9..b3e17533 100644 --- a/examples/finansbank-payfor/regular/post-auth.php +++ b/examples/finansbank-payfor/regular/post-auth.php @@ -7,7 +7,8 @@ require '../../template/_header.php'; try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { @@ -15,18 +16,16 @@ } $order = (array) json_decode($redis->lPop('order')); -$order['transaction'] = 'post'; try { - $pos->prepare($order); + $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_POST_PAY); } catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { dump($e->getCode(), $e->getMessage()); } -$card = new \Mews\Pos\Entity\Card\CreditCardPayFor('4155650100416111', '25', '01', '123', 'John Doe'); -$payment = $pos->payment($card); +$pos->payment(null); -$response = $payment->getResponse(); +$response = $pos->getResponse(); if ($pos->isSuccess()) { $redis->lPush('order', json_encode($order)); @@ -44,13 +43,10 @@

- isSuccess()) : ?> - - -
+ isSuccess()) : ?> + < Cancel payment + < Click to payment form
diff --git a/examples/finansbank-payfor/regular/refund.php b/examples/finansbank-payfor/regular/refund.php index a5dcdacd..b954806d 100644 --- a/examples/finansbank-payfor/regular/refund.php +++ b/examples/finansbank-payfor/regular/refund.php @@ -13,11 +13,9 @@ 'currency' => 'TRY', ]; -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_REFUND); -$refund = $pos->bank->refund([]); - -$response = $refund->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/finansbank-payfor/regular/response.php b/examples/finansbank-payfor/regular/response.php index 9842a377..7c80aaae 100644 --- a/examples/finansbank-payfor/regular/response.php +++ b/examples/finansbank-payfor/regular/response.php @@ -21,12 +21,11 @@ 'installment' => '4', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth //'lang' => \Mews\Pos\PayForPos::LANG_TR ]; - -$pos->prepare($order); +$transaction = \Mews\Pos\Gateways\AbstractGateway::TX_PRE_PAY; +$pos->prepare($order, $transaction); $card = new \Mews\Pos\Entity\Card\CreditCardPayFor( $request->get('number'), @@ -36,9 +35,9 @@ $request->get('name') ); -$payment = $pos->payment($card); +$pos->payment($card); -$response = $payment->getResponse(); +$response = $pos->getResponse(); if ($pos->isSuccess()) { $redis->lPush('order', json_encode($order)); @@ -49,9 +48,9 @@

- + isSuccess() ? 'Payment is successful!' : 'Payment is not successful!'; ?> - + isSuccess() ? 'Pre Authorization is successful!' : 'Pre Authorization is not successful!'; ?>

@@ -116,11 +115,11 @@
isSuccess()) : ?> - - Finish provisioning + + Finish provisioning > - + Cancel payment Order Status diff --git a/examples/finansbank-payfor/regular/status.php b/examples/finansbank-payfor/regular/status.php index 2a3ef056..7337fdd4 100644 --- a/examples/finansbank-payfor/regular/status.php +++ b/examples/finansbank-payfor/regular/status.php @@ -12,17 +12,17 @@ 'id' => $ord['id'], ]; -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_STATUS); -$query= $pos->bank->status([]); +$pos->status(); -$response = $query->getResponse(); +$response = $pos->getResponse(); ?>
-

- proc_return_code == '00' ? 'Query Order is successful!' : 'Query Order is not successful!'; ?> +

+ proc_return_code === '00' ? 'Query Order is successful!' : 'Query Order is not successful!'; ?>

All Data Dump:
diff --git a/examples/garanti/3d-pay/_config.php b/examples/garanti/3d-pay/_config.php index 5a02aec7..f055b31a 100644 --- a/examples/garanti/3d-pay/_config.php +++ b/examples/garanti/3d-pay/_config.php @@ -7,20 +7,11 @@ $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); - -$account = [ - 'bank' => 'garanti', - 'model' => '3d_pay', - 'client_id' => '7000679', - 'terminal_id' => '30691298', - 'username' => 'PROVAUT', - 'password' => '123qweASD/', - 'store_key' => '12345678', - 'env' => 'test', -]; +$account = \Mews\Pos\Factory\AccountFactory::createGarantiPosAccount('garanti', '7000679', 'PROVAUT', '123qweASD/', '30691298', '3d_pay', '12345678'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/garanti/3d-pay/form.php b/examples/garanti/3d-pay/form.php index a4f81e0c..29187be5 100644 --- a/examples/garanti/3d-pay/form.php +++ b/examples/garanti/3d-pay/form.php @@ -17,9 +17,6 @@ $success_url = $baseUrl . 'response.php'; $fail_url = $baseUrl . 'response.php'; -$transaction = 'pay'; // pay => Auth, pre PreAuth -$transaction_type = $pos->bank->types[$transaction]; - $rand = microtime(); $order = [ @@ -32,8 +29,7 @@ 'ip' => $ip, 'success_url' => $success_url, 'fail_url' => $fail_url, - 'transaction' => $transaction, - 'lang' => \Mews\Pos\GarantiPos::LANG_TR, + 'lang' => \Mews\Pos\Gateways\GarantiPos::LANG_TR, 'rand' => $rand, ]; @@ -48,9 +44,9 @@ $request->get('type') ); -$pos->prepare($order, $card); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY, $card); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); ?> diff --git a/examples/garanti/3d-pay/response.php b/examples/garanti/3d-pay/response.php index 7fddef6f..9d7ef0e7 100644 --- a/examples/garanti/3d-pay/response.php +++ b/examples/garanti/3d-pay/response.php @@ -11,16 +11,16 @@ $order = (array) json_decode($redis->lPop('order')); -$pos->prepare($order); -$payment = $pos->payment(); -$response = $payment->getResponse(); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); +$pos->payment(); +$response = $pos->getResponse(); ?>
-

- isSuccess() ? 'Payment is successful!' : 'Payment is not successful'; ?> +

+ isSuccess() ? 'Payment is successful!' : 'Payment is not successful'; ?>


diff --git a/examples/garanti/3d/_config.php b/examples/garanti/3d/_config.php index 6024fbfc..59b2cd5e 100644 --- a/examples/garanti/3d/_config.php +++ b/examples/garanti/3d/_config.php @@ -3,24 +3,15 @@ require '../../_main_config.php'; $path = '/garanti/3d/'; -$baseUrl = $hostUrl . $path; +$baseUrl = $hostUrl.$path; $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); - -$account = [ - 'bank' => 'garanti', - 'model' => '3d', - 'client_id' => '7000679', - 'terminal_id' => '30691298', - 'username' => 'PROVAUT', - 'password' => '123qweASD/', - 'store_key' => '12345678', - 'env' => 'test', -]; +$account = \Mews\Pos\Factory\AccountFactory::createGarantiPosAccount('garanti', '7000679', 'PROVAUT', '123qweASD/', '30691298', '3d', '12345678'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/garanti/3d/form.php b/examples/garanti/3d/form.php index c6276ebc..e138b791 100644 --- a/examples/garanti/3d/form.php +++ b/examples/garanti/3d/form.php @@ -11,7 +11,7 @@ $orderId = date('Ymd') . strtoupper(substr(uniqid(sha1(time())),0,4)); -$amount = (double) 1; +$amount = (float) 1; $instalment = '0'; $success_url = $baseUrl . 'response.php'; @@ -29,8 +29,7 @@ 'ip' => $ip, 'success_url' => $success_url, 'fail_url' => $fail_url, - 'transaction' => 'pay', // pay => Auth, pre PreAuth, - 'lang' => \Mews\Pos\GarantiPos::LANG_TR, + 'lang' => \Mews\Pos\Gateways\GarantiPos::LANG_TR, 'rand' => $rand, ]; @@ -45,9 +44,10 @@ $request->get('type') ); -$pos->prepare($order, $card); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY, $card); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); +dump($form_data); ?> diff --git a/examples/garanti/3d/response.php b/examples/garanti/3d/response.php index e4699ed7..ca04f60d 100644 --- a/examples/garanti/3d/response.php +++ b/examples/garanti/3d/response.php @@ -11,14 +11,14 @@ $order = (array) json_decode($redis->lPop('order')); -$pos->prepare($order); -$payment = $pos->payment(); -$response = $payment->getResponse(); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); +$pos->payment(); +$response = $pos->getResponse(); ?>
-

- isSuccess() ? 'Payment is successful!' : 'Payment is not successful'; ?> +

+ isSuccess() ? 'Payment is successful!' : 'Payment is not successful'; ?>


diff --git a/examples/garanti/regular/_config.php b/examples/garanti/regular/_config.php index c1acbbfe..19eaee3f 100644 --- a/examples/garanti/regular/_config.php +++ b/examples/garanti/regular/_config.php @@ -3,31 +3,21 @@ require '../../_main_config.php'; $path = '/garanti/regular/'; -$baseUrl = $hostUrl . $path; +$baseUrl = $hostUrl.$path; $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); - -$account = [ - 'bank' => 'garanti', - 'model' => 'regular', - 'client_id' => '7000679', - 'terminal_id' => '30691298', - 'username' => 'PROVAUT', - 'password' => '123qweASD/', - 'refund_username' => 'PROVRFN', - 'refund_password' => '123qweASD/', - 'env' => 'test', -]; +$account = \Mews\Pos\Factory\AccountFactory::createGarantiPosAccount('garanti', '7000679', 'PROVAUT', '123qweASD/', '30691298', 'regular', '', 'PROVRFN', '123qweASD/'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { dump($e->getCode(), $e->getMessage()); } -$gateway = $baseUrl . 'response.php'; +$gateway = $baseUrl.'response.php'; $templateTitle = 'Regular Payment'; diff --git a/examples/garanti/regular/cancel.php b/examples/garanti/regular/cancel.php index 5d902ba0..80c58374 100644 --- a/examples/garanti/regular/cancel.php +++ b/examples/garanti/regular/cancel.php @@ -6,22 +6,25 @@ require '../../template/_header.php'; -// Cancel Order -$cancel = $pos->bank->cancel([ - 'order_id' => '20181114DF2C', +$order = [ + 'id' => '20181114DF2C', 'ip' => $ip, 'email' => 'mail@customer.com', 'ref_ret_num' => '831803579226', 'amount' => 1, 'currency' => 'TRY', -]); +]; + +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_CANCEL); +// Cancel Order +$pos->cancel(); -$response = $cancel->getResponse(); +$response = $pos->getResponse(); ?>
-

- proc_return_code == '00' ? 'Cancel Order is successful!' : 'Cancel Order is not successful!'; ?> +

+ proc_return_code === '00' ? 'Cancel Order is successful!' : 'Cancel Order is not successful!'; ?>

All Data Dump:
diff --git a/examples/garanti/regular/direct.php b/examples/garanti/regular/direct.php index 5911aa93..b3ea8978 100644 --- a/examples/garanti/regular/direct.php +++ b/examples/garanti/regular/direct.php @@ -14,16 +14,15 @@ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth ]; try { - $pos->prepare($order); + $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); } catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { dump($e->getCode(), $e->getMessage()); } $card = new \Mews\Pos\Entity\Card\CreditCardGarantiPos('4282209027132016', '20', '05', '165'); -$payment = $pos->payment($card); +$pos->payment($card); -dump($payment->getResponse()); +dump($pos->getResponse()); diff --git a/examples/garanti/regular/history.php b/examples/garanti/regular/history.php index 5585875e..1b5ef553 100644 --- a/examples/garanti/regular/history.php +++ b/examples/garanti/regular/history.php @@ -7,18 +7,18 @@ require '../../template/_header.php'; // History Order -$query= $pos->bank->history([ +$pos->history([ 'order_id' => '2018111377EF', 'currency' => 'TRY', 'ip' => $ip, ]); -$response = $query->getResponse(); +$response = $pos->getResponse(); ?>
-

- proc_return_code == '00' ? 'History Order is successful!' : 'History Order is not successful!'; ?> +

+ proc_return_code === '00' ? 'History Order is successful!' : 'History Order is not successful!'; ?>

All Data Dump:
diff --git a/examples/garanti/regular/post.php b/examples/garanti/regular/post.php index 99ee1d65..cf9997f2 100644 --- a/examples/garanti/regular/post.php +++ b/examples/garanti/regular/post.php @@ -23,7 +23,8 @@ require '../../template/_header.php'; try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { @@ -32,14 +33,13 @@ $order = [ 'id' => '201810231553', - 'transaction' => 'post', 'amount' => '1', 'ref_ret_num' => '829603332856', 'ip' => $ip, ]; try { - $pos->prepare($order); + $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_POST_PAY); } catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { dump($e->getCode(), $e->getMessage()); } diff --git a/examples/garanti/regular/refund.php b/examples/garanti/regular/refund.php index 5bb4eed6..a0a42cfa 100644 --- a/examples/garanti/regular/refund.php +++ b/examples/garanti/regular/refund.php @@ -6,22 +6,24 @@ require '../../template/_header.php'; -// Refund Order -$refund = $pos->bank->refund([ - 'order_id' => '201811142A0A', +$order = [ + 'id' => '201811142A0A', 'ip' => $ip, 'email' => 'mail@customer.com', 'ref_ret_num' => '831803586333', 'amount' => 1, 'currency' => 'TRY', -]); +]; +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_REFUND); +// Refund Order +$pos->refund(); -$response = $refund->getResponse(); +$response = $pos->getResponse(); ?>
-

- proc_return_code == '00' ? 'Refund Order is successful!' : 'Refund Order is not successful!'; ?> +

+ proc_return_code === '00' ? 'Refund Order is successful!' : 'Refund Order is not successful!'; ?>

All Data Dump:
diff --git a/examples/garanti/regular/response.php b/examples/garanti/regular/response.php index ad5b85f9..f7c9c1a8 100644 --- a/examples/garanti/regular/response.php +++ b/examples/garanti/regular/response.php @@ -21,10 +21,9 @@ 'installment' => '1', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => S, pre => preauth ]; -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); $card = new \Mews\Pos\Entity\Card\CreditCardGarantiPos( $request->get('number'), @@ -33,14 +32,14 @@ $request->get('cvv') ); -$payment = $pos->payment($card); +$pos->payment($card); -$response = $payment->getResponse(); +$response = $pos->getResponse(); ?>
-

- isSuccess() ? 'Payment is successful!' : 'Payment is not successful!'; ?> +

+ isSuccess() ? 'Payment is successful!' : 'Payment is not successful!'; ?>


diff --git a/examples/garanti/regular/status.php b/examples/garanti/regular/status.php index c11629cb..5da4b986 100644 --- a/examples/garanti/regular/status.php +++ b/examples/garanti/regular/status.php @@ -6,14 +6,17 @@ require '../../template/_header.php'; -// Query Order -$query= $pos->bank->status([ - 'order_id' => '201812195CF2', +$order = [ + 'id' => '201812195CF2', 'currency' => 'TRY', - 'ip' => $ip -]); + 'ip' => $ip, +]; + +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_STATUS); +// Query Order +$pos->status(); -$response = $query->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/ykb/3d/_config.php b/examples/ykb/3d/_config.php index bd998769..fcd86571 100644 --- a/examples/ykb/3d/_config.php +++ b/examples/ykb/3d/_config.php @@ -5,27 +5,16 @@ $path = '/ykb/3d/'; $baseUrl = $hostUrl . $path; -$success_url = $baseUrl . 'response.php'; -$fail_url = $baseUrl . 'response.php'; +$success_url = $fail_url = $baseUrl . 'response.php'; + +$account = \Mews\Pos\Factory\AccountFactory::createPosNetAccount('yapikredi', 'XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX', '3d', '10,10,10,10,10,10,10,10'); $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); -$account = [ - 'bank' => 'yapikredi', - 'model' => '3d', - 'client_id' => 'XXXXXX', - 'terminal_id' => 'XXXXXX', - 'posnet_id' => 'XXXXXX', - 'username' => 'XXXXXX', - 'password' => 'XXXXXX', - 'store_key' => '10,10,10,10,10,10,10,10', - 'promotion_code' => '', - 'env' => 'test', -]; - try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { diff --git a/examples/ykb/3d/form.php b/examples/ykb/3d/form.php index 0eb6fe72..c8eeba21 100644 --- a/examples/ykb/3d/form.php +++ b/examples/ykb/3d/form.php @@ -14,7 +14,6 @@ 'name' => $_POST['name'], 'amount' => $_POST['amount'], 'currency' => $_POST['currency'], - 'transaction' => $_POST['transaction'], 'success_url' => $_POST['success_url'], 'fail_url' => $_POST['fail_url'], 'lang' => $_POST['lang'], @@ -31,9 +30,9 @@ $request->get('type') ); -$pos->prepare($order, $card); +$pos->prepare($order, $_POST['transaction'], $card); -$form_data = $pos->get3dFormData(); +$form_data = $pos->get3DFormData(); ?> Redirecting... diff --git a/examples/ykb/3d/index.php b/examples/ykb/3d/index.php index b0d5bec8..a7033fd8 100644 --- a/examples/ykb/3d/index.php +++ b/examples/ykb/3d/index.php @@ -15,7 +15,6 @@ 'currency' => 'TRY', 'success_url' => $success_url, 'fail_url' => $fail_url, - 'transaction' => 'pay', // pay => Sale, pre Auth 'lang' => \Mews\Pos\PosNet::LANG_TR, ]; ?> @@ -64,7 +63,7 @@ - + diff --git a/examples/ykb/3d/response.php b/examples/ykb/3d/response.php index ab6a7d32..b8139882 100644 --- a/examples/ykb/3d/response.php +++ b/examples/ykb/3d/response.php @@ -11,9 +11,9 @@ $order = (array) json_decode($redis->lPop('order')); -$pos->prepare($order); -$payment = $pos->payment(); -$response = $payment->getResponse(); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); +$pos->payment(); +$response = $pos->getResponse(); ?>
diff --git a/examples/ykb/regular/_config.php b/examples/ykb/regular/_config.php index 9b1660db..3f697496 100644 --- a/examples/ykb/regular/_config.php +++ b/examples/ykb/regular/_config.php @@ -3,28 +3,21 @@ require '../../_main_config.php'; $path = '/ykb/regular/'; -$baseUrl = $hostUrl . $path; +$baseUrl = $hostUrl.$path; $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $ip = $request->getClientIp(); - -$account = [ - 'bank' => 'yapikredi', - 'model' => 'regular', - 'client_id' => '6706598320', - 'terminal_id' => '67322946', - 'posnet_id' => '27426', - 'env' => 'test', -]; +$account = \Mews\Pos\Factory\AccountFactory::createPosNetAccount('yapikredi', '6706598320', 'XXXXXX', 'XXXXXX', '67322946', '27426', 'regular', '10,10,10,10,10,10,10,10'); try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { dump($e->getCode(), $e->getMessage()); } -$gateway = $baseUrl . 'response.php'; +$gateway = $baseUrl.'response.php'; $templateTitle = 'Regular Payment'; diff --git a/examples/ykb/regular/cancel.php b/examples/ykb/regular/cancel.php index 3e7c1fd7..7f2c61ab 100644 --- a/examples/ykb/regular/cancel.php +++ b/examples/ykb/regular/cancel.php @@ -6,21 +6,27 @@ require '../../template/_header.php'; +$order = [ + 'id' => '201811133F3F', +]; +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_CANCEL); + // Cancel Order -$cancel = $pos->bank->cancel([ - 'order_id' => '201811133F3F', -]); +$pos->cancel(); /* // faster params... -$cancel = $pos->bank->cancel([ - 'order_id' => '201810295863', +$order = [ + 'id' => '201810295863', 'host_ref_num' => '018711539490000181', 'auth_code' => '115394', -]); +]; +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_CANCEL); + +$pos->cancel(); */ -$response = $cancel->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/ykb/regular/direct.php b/examples/ykb/regular/direct.php index 6e944654..50c16878 100644 --- a/examples/ykb/regular/direct.php +++ b/examples/ykb/regular/direct.php @@ -14,17 +14,16 @@ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth ]; $card = new \Mews\Pos\Entity\Card\CreditCardPosNet('4355084355084358', '18', '12', '000'); try { - $pos->prepare($order); + $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); } catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { dump($e->getCode(), $e->getMessage()); } -$payment = $pos->payment($card); +$pos->payment($card); -dump($payment->getResponse()); +dump($pos->getResponse()); diff --git a/examples/ykb/regular/history.php b/examples/ykb/regular/history.php index 4b2cd530..09369544 100644 --- a/examples/ykb/regular/history.php +++ b/examples/ykb/regular/history.php @@ -7,11 +7,11 @@ require '../../template/_header.php'; // History Order -$query = $pos->bank->history([ +$pos->history([ 'order_id' => '201811133F3F', ]); -$response = $query->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/ykb/regular/post.php b/examples/ykb/regular/post.php index 98d5b1d1..6e97a0ec 100644 --- a/examples/ykb/regular/post.php +++ b/examples/ykb/regular/post.php @@ -22,7 +22,8 @@ require '../../template/_header.php'; try { - $pos = new \Mews\Pos\Pos($account); + $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); + $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); } catch (\Mews\Pos\Exceptions\BankClassNullException $e) { @@ -35,18 +36,17 @@ 'amount' => '100', 'currency' => 'TRY', 'installment' => '2', - 'transaction' => 'post', ]; try { - $pos->prepare($order); + $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_POST_PAY); } catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { dump($e->getCode(), $e->getMessage()); } -$payment = $pos->payment(); +$pos->payment(); -$response = $payment->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/ykb/regular/refund.php b/examples/ykb/regular/refund.php index 287f260b..33266e05 100644 --- a/examples/ykb/regular/refund.php +++ b/examples/ykb/regular/refund.php @@ -6,14 +6,15 @@ require '../../template/_header.php'; -// Refund Order -$refund = $pos->bank->refund([ - 'order_id' => '20181113DCDB', +$pos->prepare([ + 'id' => '20181113DCDB', 'amount' => '1', 'currency' => 'TRY', -]); +], \Mews\Pos\Gateways\AbstractGateway::TX_REFUND); +// Refund Order +$pos->refund(); -$response = $refund->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/ykb/regular/response.php b/examples/ykb/regular/response.php index 45d2a611..8edf92b4 100644 --- a/examples/ykb/regular/response.php +++ b/examples/ykb/regular/response.php @@ -20,10 +20,9 @@ 'amount' => $amount, 'installment' => 1, 'currency' => 'TRY', - 'transaction' => 'pay', // pay => Sale, pre Auth ]; -$pos->prepare($order); +$pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); $card = [ 'number' => $request->get('number'), @@ -32,9 +31,9 @@ 'cvv' => $request->get('cvv'), ]; -$payment = $pos->payment($card); +$pos->payment($card); -$response = $payment->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/examples/ykb/regular/status.php b/examples/ykb/regular/status.php index be3b6ec4..8a34b30d 100644 --- a/examples/ykb/regular/status.php +++ b/examples/ykb/regular/status.php @@ -6,12 +6,15 @@ require '../../template/_header.php'; +$order = [ + 'id' => '201811133F3F', +]; +$pos->prepare($order,\Mews\Pos\Gateways\AbstractGateway::TX_STATUS); + // Query Order -$query= $pos->bank->status([ - 'order_id' => '201811133F3F', -]); +$pos->status(); -$response = $query->getResponse(); +$response = $pos->getResponse(); ?>
diff --git a/src/Entity/Account/AbstractPosAccount.php b/src/Entity/Account/AbstractPosAccount.php new file mode 100644 index 00000000..546fd8b3 --- /dev/null +++ b/src/Entity/Account/AbstractPosAccount.php @@ -0,0 +1,118 @@ +model = $model; + $this->clientId = $clientId; + $this->username = $username; + $this->password = $password; + $this->storeKey = $storeKey; + $this->lang = $lang; + $this->bank = $bank; + } + + /** + * @return string + */ + public function getClientId(): string + { + return $this->clientId; + } + + /** + * @return string + */ + public function getModel(): string + { + return $this->model; + } + + /** + * @return string + */ + public function getUsername(): string + { + return $this->username; + } + + /** + * @return string + */ + public function getPassword(): string + { + return $this->password; + } + + /** + * @return string|null + */ + public function getStoreKey(): ?string + { + return $this->storeKey; + } + + /** + * @return string + */ + public function getLang(): string + { + return $this->lang; + } + + /** + * @return string + */ + public function getBank(): string + { + return $this->bank; + } +} \ No newline at end of file diff --git a/src/Entity/Account/EstPosAccount.php b/src/Entity/Account/EstPosAccount.php new file mode 100644 index 00000000..f752a914 --- /dev/null +++ b/src/Entity/Account/EstPosAccount.php @@ -0,0 +1,10 @@ +model = $model; + $this->terminalId = $terminalId; + $this->refundUsername = $refundUsername; + $this->refundPassword = $refundPassword; + } + + /** + * @return string + */ + public function getRefundPassword(): string + { + return $this->refundPassword; + } + + /** + * @return string + */ + public function getRefundUsername(): string + { + return $this->refundUsername; + } + + /** + * @return string + */ + public function getTerminalId(): string + { + return $this->terminalId; + } + +} \ No newline at end of file diff --git a/src/Entity/Account/PayForAccount.php b/src/Entity/Account/PayForAccount.php new file mode 100644 index 00000000..77dd853e --- /dev/null +++ b/src/Entity/Account/PayForAccount.php @@ -0,0 +1,10 @@ +model = $model; + $this->terminalId = $terminalId; + $this->posNetId = $posNetId; + } + + /** + * @return string + */ + public function getRefundPassword(): string + { + return $this->refundPassword; + } + + /** + * @return string + */ + public function getRefundUsername(): string + { + return $this->refundUsername; + } + + /** + * @return string + */ + public function getTerminalId(): string + { + return $this->terminalId; + } + + /** + * @return string + */ + public function getPosNetId(): string + { + return $this->posNetId; + } + +} \ No newline at end of file diff --git a/src/EstPos.php b/src/EstPos.php deleted file mode 100644 index 0b60f36a..00000000 --- a/src/EstPos.php +++ /dev/null @@ -1,893 +0,0 @@ - 'approved', - '01' => 'bank_call', - '02' => 'bank_call', - '05' => 'reject', - '09' => 'try_again', - '12' => 'invalid_transaction', - '28' => 'reject', - '51' => 'insufficient_balance', - '54' => 'expired_card', - '57' => 'does_not_allow_card_holder', - '62' => 'restricted_card', - '77' => 'request_rejected', - '99' => 'general_error', - ]; - - /** - * Transaction Types - * - * @var array - */ - public $types = [ - 'pay' => 'Auth', - 'pre' => 'PreAuth', - 'post' => 'PostAuth', - ]; - - /** - * Currencies - * - * @var array - */ - public $currencies = []; - - /** - * Transaction Type - * - * @var string - */ - public $type; - - /** - * API Account - * - * @var array - */ - protected $account = []; - - /** - * Order Details - * - * @var array - */ - protected $order = []; - - /** - * @var CreditCardEstPos|null - */ - protected $card; - - /** - * Request - * - * @var Request - */ - protected $request; - - /** - * Response Raw Data - * - * @var object - */ - protected $data; - - /** - * Processed Response Data - * - * @var mixed - */ - protected $response; - - /** - * Configuration - * - * @var array - */ - protected $config = []; - - /** - * EstPos constructor. - * - * @param array $config - * @param mixed $account - * @param array $currencies - */ - public function __construct($config, $account, array $currencies) - { - $this->config = $config; - $this->account = $account; - $this->currencies = $currencies; - - $this->url = isset($this->config['urls'][$this->account->env]) ? - $this->config['urls'][$this->account->env] : - $this->config['urls']['production']; - - $this->gateway = isset($this->config['urls']['gateway'][$this->account->env]) ? - $this->config['urls']['gateway'][$this->account->env] : - $this->config['urls']['gateway']['production']; - - return $this; - } - - /** - * @inheritDoc - */ - public function createXML(array $data, $encoding = 'ISO-8859-9'): string - { - return $this->traitCreateXML(['CC5Request' => $data], $encoding); - } - - /** - * Create Regular Payment XML - * - * @return string - */ - protected function createRegularPaymentXML() - { - $requestData = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->type, - 'IPAddress' => $this->order->ip, - 'Email' => $this->order->email, - 'OrderId' => $this->order->id, - 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, - 'Total' => $this->order->amount, - 'Currency' => $this->order->currency, - 'Taksit' => $this->order->installment, - 'CardType' => $this->card->getType(), - 'Number' => $this->card->getNumber(), - 'Expires' => $this->card->getExpirationDate(), - 'Cvv2Val' => $this->card->getCvv(), - 'Mode' => 'P', - 'GroupId' => '', - 'TransId' => '', - 'BillTo' => [ - 'Name' => $this->order->name ? $this->order->name : null, - ], - ]; - - return $this->createXML($requestData); - } - - /** - * Create Regular Payment Post XML - * - * @return string - */ - protected function createRegularPostXML() - { - $requestData = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->types[$this->order->transaction], - 'OrderId' => $this->order->id, - ]; - - return $this->createXML($requestData); - } - - /** - * Create 3D Payment XML - * @return string - */ - protected function create3DPaymentXML() - { - $requestData = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->type, - 'IPAddress' => $this->order->ip, - 'Email' => $this->order->email, - 'OrderId' => $this->order->id, - 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, - 'Total' => $this->order->amount, - 'Currency' => $this->order->currency, - 'Taksit' => $this->order->installment, - 'Number' => $this->request->get('md'), - 'Expires' => '', - 'Cvv2Val' => '', - 'PayerTxnId' => $this->request->get('xid'), - 'PayerSecurityLevel' => $this->request->get('eci'), - 'PayerAuthenticationCode' => $this->request->get('cavv'), - 'CardholderPresentCode' => '13', - 'Mode' => 'P', - 'GroupId' => '', - 'TransId' => '', - ]; - - if ($this->order->name) { - $requestData['BillTo'] = [ - 'Name' => $this->order->name, - ]; - } - - return $this->createXML($requestData); - } - - /** - * Get ProcReturnCode - * - * @return string|null - */ - protected function getProcReturnCode() - { - return isset($this->data->ProcReturnCode) ? (string)$this->data->ProcReturnCode : null; - } - - /** - * Get Status Detail Text - * - * @return string|null - */ - protected function getStatusDetail() - { - $proc_return_code = $this->getProcReturnCode(); - - return $proc_return_code ? (isset($this->codes[$proc_return_code]) ? (string)$this->codes[$proc_return_code] : null) : null; - } - - /** - * @return mixed - */ - public function getResponse() - { - return $this->response; - } - - /** - * Create 3D Hash - * - * @return string - */ - public function create3DHash() - { - $hash_str = ''; - - if ($this->account->model == '3d') { - $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->order->rand . $this->account->store_key; - } elseif ($this->account->model == '3d_pay') { - $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->type . $this->order->installment . $this->order->rand . $this->account->store_key; - } - - return base64_encode(sha1($hash_str, true)); - } - - /** - * Check 3D Hash - * - * @param array $data - * @return bool - */ - public function check3DHash($data) - { - $hash_params = $data['HASHPARAMS']; - $hash_params_val = $data['HASHPARAMSVAL']; - $hash_param = $data['HASH']; - $params_val = ''; - - $hashparams_arr = explode(':', $hash_params); - foreach ($hashparams_arr as $value) { - if(!empty($value) && isset($data[$value])){ - $params_val = $params_val . $data[$value]; - } - } - - $hash_val = $params_val . $this->account->store_key; - $hash = base64_encode(sha1($hash_val, true)); - - $return = false; - if ($hash_params && !($params_val != $hash_params_val || $hash_param != $hash)) { - $return = true; - } - - return $return; - } - - /** - * Regular Payment - * - * @return $this - * @throws GuzzleException - */ - public function makeRegularPayment() - { - $contents = ''; - if (in_array($this->order->transaction, ['pay', 'pre'])) { - $contents = $this->createRegularPaymentXML(); - } elseif ($this->order->transaction == 'post') { - $contents = $this->createRegularPostXML(); - } - - $this->send($contents); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $this->response = (object)[ - 'id' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'group_id' => isset($this->data->GroupId) ? $this->printData($this->data->GroupId) : null, - 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'auth_code' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->printData($this->data->HostRefNum) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, - 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, - 'campaign_url' => null, - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, - 'all' => $this->data, - ]; - - return $this; - } - - /** - * Make 3D Payment - * - * @return $this - * @throws GuzzleException - */ - public function make3DPayment() - { - $this->request = Request::createFromGlobals(); - - $status = 'declined'; - if ($this->check3DHash($this->request->request->all())) { - $contents = $this->create3DPaymentXML(); - $this->send($contents); - } - - $transaction_security = 'MPI fallback'; - if ($this->getProcReturnCode() == '00') { - if ($this->request->get('mdStatus') == '1') { - $transaction_security = 'Full 3D Secure'; - } elseif (in_array($this->request->get('mdStatus'), [2, 3, 4])) { - $transaction_security = 'Half 3D Secure'; - } - - $status = 'approved'; - } - - $this->response = (object)[ - 'id' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'group_id' => isset($this->data->GroupId) ? $this->printData($this->data->GroupId) : null, - 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'transaction_security' => $transaction_security, - 'auth_code' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->printData($this->data->HostRefNum) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, - 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, - 'md_status' => $this->request->get('mdStatus'), - 'hash' => (string)$this->request->get('HASH'), - 'rand' => (string)$this->request->get('rnd'), - 'hash_params' => (string)$this->request->get('HASHPARAMS'), - 'hash_params_val' => (string)$this->request->get('HASHPARAMSVAL'), - 'masked_number' => (string)$this->request->get('maskedCreditCard'), - 'month' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Month'), - 'year' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Year'), - 'amount' => (string)$this->request->get('amount'), - 'currency' => (string)$this->request->get('currency'), - 'tx_status' => (string)$this->request->get('txstatus'), - 'eci' => (string)$this->request->get('eci'), - 'cavv' => (string)$this->request->get('cavv'), - 'xid' => (string)$this->request->get('xid'), - 'md_error_message' => (string)$this->request->get('mdErrorMsg'), - 'name' => (string)$this->request->get('firmaadi'), - 'campaign_url' => null, - 'email' => (string)$this->request->get('Email'), - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, - 'all' => $this->data, - '3d_all' => $this->request->request->all(), - ]; - - return $this; - } - - /** - * Make 3D Pay Payment - * - * @return $this - */ - public function make3DPayPayment() - { - $this->request = Request::createFromGlobals(); - - $status = 'declined'; - - if ($this->check3DHash($this->request->request->all()) && (string)$this->request->get('ProcReturnCode') == '00') { - if (in_array($this->request->get('mdStatus'), [1, 2, 3, 4])) { - $status = 'approved'; - } - } - - $transaction_security = 'MPI fallback'; - if ($status == 'approved') { - if ($this->request->get('mdStatus') == '1') { - $transaction_security = 'Full 3D Secure'; - } elseif (in_array($this->request->get('mdStatus'), [2, 3, 4])) { - $transaction_security = 'Half 3D Secure'; - } - } - - $this->response = (object)[ - 'id' => (string)$this->request->get('AuthCode'), - 'trans_id' => (string)$this->request->get('TransId'), - 'auth_code' => (string)$this->request->get('AuthCode'), - 'host_ref_num' => (string)$this->request->get('HostRefNum'), - 'response' => (string)$this->request->get('Response'), - 'order_id' => (string)$this->request->get('oid'), - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'transaction_security' => $transaction_security, - 'code' => (string)$this->request->get('ProcReturnCode'), - 'md_status' => $this->request->get('mdStatus'), - 'status' => $status, - 'status_detail' => isset($this->codes[$this->request->get('ProcReturnCode')]) ? (string)$this->request->get('ProcReturnCode') : null, - 'hash' => (string)$this->request->get('HASH'), - 'rand' => (string)$this->request->get('rnd'), - 'hash_params' => (string)$this->request->get('HASHPARAMS'), - 'hash_params_val' => (string)$this->request->get('HASHPARAMSVAL'), - 'masked_number' => (string)$this->request->get('maskedCreditCard'), - 'month' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Month'), - 'year' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Year'), - 'amount' => (string)$this->request->get('amount'), - 'currency' => (string)$this->request->get('currency'), - 'tx_status' => (string)$this->request->get('txstatus'), - 'eci' => (string)$this->request->get('eci'), - 'cavv' => (string)$this->request->get('cavv'), - 'xid' => (string)$this->request->get('xid'), - 'error_code' => (string)$this->request->get('ErrCode'), - 'error_message' => (string)$this->request->get('ErrMsg'), - 'md_error_message' => (string)$this->request->get('mdErrorMsg'), - 'name' => (string)$this->request->get('firmaadi'), - 'email' => (string)$this->request->get('Email'), - 'campaign_url' => null, - 'extra' => $this->request->get('Extra'), - 'all' => $this->request->request->all(), - ]; - - return $this; - } - - /** - * Get 3d Form Data - * - * @return array - */ - public function get3DFormData() - { - $data = []; - - if ($this->order) { - $this->order->hash = $this->create3DHash(); - - $inputs = [ - 'clientid' => $this->account->client_id, - 'storetype' => $this->account->model, - 'hash' => $this->order->hash, - 'cardType' => $this->card->getCardCode(), - 'pan' => $this->card->getNumber(), - 'Ecom_Payment_Card_ExpDate_Month' => $this->card->getExpireMonth(), - 'Ecom_Payment_Card_ExpDate_Year' => $this->card->getExpireYear(), - 'cv2' => $this->card->getCvv(), - 'firmaadi' => $this->order->name, - 'Email' => $this->order->email, - 'amount' => $this->order->amount, - 'oid' => $this->order->id, - 'okUrl' => $this->order->success_url, - 'failUrl' => $this->order->fail_url, - 'rnd' => $this->order->rand, - 'lang' => $this->getLang(), - 'currency' => $this->order->currency, - ]; - - if ($this->account->model == '3d_pay') { - $inputs = array_merge($inputs, [ - 'islemtipi' => $this->type, - 'taksit' => $this->order->installment, - ]); - } - - $data = [ - 'gateway' => $this->gateway, - 'success_url' => $this->order->success_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand, - 'hash' => $this->order->hash, - 'inputs' => $inputs, - ]; - } - - return $data; - } - - /** - * Send contents to WebService - * - * @param $contents - * @return $this - * @throws GuzzleException - */ - public function send($contents) - { - $client = new Client(); - - $response = $client->request('POST', $this->url, [ - 'body' => $contents - ]); - - $this->data = $this->XMLStringToObject($response->getBody()->getContents()); - - return $this; - } - - /** - * Prepare Order - * - * @param object $order - * @param CreditCardEstPos|null $card - * - * @return mixed - * - * @throws UnsupportedTransactionTypeException - */ - public function prepare($order, $card = null) - { - $this->type = $this->types['pay']; - if (isset($order->transaction)) { - if (array_key_exists($order->transaction, $this->types)) { - $this->type = $this->types[$order->transaction]; - } else { - throw new UnsupportedTransactionTypeException('Unsupported transaction type!'); - } - } - - $this->order = $order; - $this->card = $card; - } - - /** - * Make Payment - * - * @param CreditCardEstPos $card - * - * @return mixed - * - * @throws UnsupportedPaymentModelException - * @throws GuzzleException - */ - public function payment($card) - { - $this->card = $card; - - $model = 'regular'; - if (isset($this->account->model) && $this->account->model) { - $model = $this->account->model; - } - - if ($model == 'regular') { - $this->makeRegularPayment(); - } elseif ($model == '3d') { - $this->make3DPayment(); - } elseif ($model == '3d_pay') { - $this->make3DPayPayment(); - } else { - throw new UnsupportedPaymentModelException(); - } - - return $this; - } - - /** - * Refund Order - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function refund(array $meta) - { - $requestData = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Type' => 'Credit', - ]; - - if ($meta['amount']) { - $requestData['Total'] = $meta['amount']; - } - - $xml = $this->createXML($requestData); - $this->send($xml); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $this->response = (object)[ - 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, - 'group_id' => isset($this->data->GroupId) ? $this->data->GroupId : null, - 'response' => isset($this->data->Response) ? $this->data->Response : null, - 'auth_code' => isset($this->data->AuthCode) ? $this->data->AuthCode : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->data->HostRefNum : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, - 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->data->Extra->ERRORCODE : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, - ]; - - return $this; - } - - /** - * Cancel Order - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function cancel(array $meta) - { - $requestData = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Type' => 'Void', - ]; - $xml = $this->createXML($requestData); - - $this->send($xml); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $this->response = (object)[ - 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, - 'group_id' => isset($this->data->GroupId) ? $this->data->GroupId : null, - 'response' => isset($this->data->Response) ? $this->data->Response : null, - 'auth_code' => isset($this->data->AuthCode) ? $this->data->AuthCode : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->data->HostRefNum : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, - 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->data->Extra->ERRORCODE : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, - ]; - - return $this; - } - - /** - * Order Status - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function status(array $meta) - { - $requestData = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Extra' => [ - 'ORDERSTATUS' => 'QUERY', - ], - ]; - $xml = $this->createXML($requestData); - - $this->send($xml); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $first_amount = isset($this->data->Extra->ORIG_TRANS_AMT) ? $this->printData($this->data->Extra->ORIG_TRANS_AMT) : null; - $capture_amount = isset($this->data->Extra->CAPTURE_AMT) ? $this->printData($this->data->Extra->CAPTURE_AMT) : null; - $capture = $first_amount == $capture_amount ? true : false; - - $this->response = (object)[ - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->printData($this->data->ErrMsg) : null, - 'host_ref_num' => isset($this->data->Extra->HOST_REF_NUM) ? $this->printData($this->data->Extra->HOST_REF_NUM) : null, - 'order_status' => isset($this->data->Extra->ORDERSTATUS) ? $this->printData($this->data->Extra->ORDERSTATUS) : null, - 'process_type' => isset($this->data->Extra->CHARGE_TYPE_CD) ? $this->printData($this->data->Extra->CHARGE_TYPE_CD) : null, - 'pan' => isset($this->data->Extra->PAN) ? $this->printData($this->data->Extra->PAN) : null, - 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->printData($this->data->Extra->NUMCODE) : null, - 'first_amount' => $first_amount, - 'capture_amount' => $capture_amount, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'capture' => $capture, - 'all' => $this->data, - 'xml' => $xml, - ]; - - return $this; - } - - /** - * Order History - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function history(array $meta) - { - $requestData = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Extra' => [ - 'ORDERHISTORY' => 'QUERY', - ], - ]; - $xml = $this->createXML($requestData); - - $this->send($xml); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $this->response = (object)[ - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->printData($this->data->ErrMsg) : null, - 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->printData($this->data->Extra->NUMCODE) : null, - 'trans_count' => isset($this->data->Extra->TRXCOUNT) ? $this->printData($this->data->Extra->TRXCOUNT) : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, - 'xml' => $xml, - ]; - - return $this; - } - - /** - * @return array - */ - public function getConfig() - { - return $this->config; - } - - /** - * @return mixed - */ - public function getAccount() - { - return $this->account; - } - - /** - * @return array - */ - public function getCurrencies() - { - return $this->currencies; - } - - /** - * @return mixed - */ - public function getOrder() - { - return $this->order; - } - - /** - * @return CreditCardEstPos|null - */ - public function getCard() - { - return $this->card; - } - - /** - * bank returns error messages for specified language value - * usually accepted values are tr,en - * @return string - */ - private function getLang() - { - if ($this->order && isset($this->order->lang)) { - return $this->order->lang; - } - if (isset($this->account->lang)) { - return $this->account->lang; - } - - return self::LANG_TR; - } -} diff --git a/src/Exceptions/MissingAccountInfoException.php b/src/Exceptions/MissingAccountInfoException.php new file mode 100644 index 00000000..618ec8d2 --- /dev/null +++ b/src/Exceptions/MissingAccountInfoException.php @@ -0,0 +1,24 @@ +getBank(), $config['banks'])) { + throw new BankNotFoundException(); + } + + // Gateway Class + $class = $config['banks'][$posAccount->getBank()]['class']; + + if (!$class) { + throw new BankClassNullException(); + } + + $currencies = []; + if (isset($config['currencies'])) { + $currencies = $config['currencies']; + } + + // Create Bank Class Object + return new $class($config['banks'][$posAccount->getBank()], $posAccount, $currencies); + } +} \ No newline at end of file diff --git a/src/GarantiPos.php b/src/GarantiPos.php deleted file mode 100644 index 7e5c02a5..00000000 --- a/src/GarantiPos.php +++ /dev/null @@ -1,1076 +0,0 @@ - 'approved', - '01' => 'bank_call', - '02' => 'bank_call', - '05' => 'reject', - '09' => 'try_again', - '12' => 'invalid_transaction', - '28' => 'reject', - '51' => 'insufficient_balance', - '54' => 'expired_card', - '57' => 'does_not_allow_card_holder', - '62' => 'restricted_card', - '77' => 'request_rejected', - '99' => 'general_error', - ]; - - /** - * Transaction Types - * - * @var array - */ - public $types = [ - 'pay' => 'sales', - 'pre' => 'preauth', - 'post' => 'postauth', - ]; - - /** - * Currencies - * - * @var array - */ - public $currencies = []; - - /** - * Transaction Type - * - * @var string - */ - public $type; - - /** - * API Account - * - * @var array - */ - protected $account = []; - - /** - * Order Details - * - * @var array - */ - protected $order = []; - - /** - * @var CreditCardGarantiPos - */ - protected $card; - - /** - * Request - * - * @var Request - */ - protected $request; - - /** - * Response Raw Data - * - * @var object - */ - protected $data; - - /** - * Processed Response Data - * - * @var mixed - */ - protected $response; - - /** - * Configuration - * - * @var array - */ - protected $config = []; - - /** - * Mode - * - * @var string - */ - protected $mode = 'PROD'; - - /** - * API version - * @var string - */ - protected $version = 'v0.01'; - - /** - * GarantiPost constructor. - * - * @param array $config - * @param array $account - * @param array $currencies - */ - public function __construct($config, $account, array $currencies) - { - $request = Request::createFromGlobals(); - $this->request = $request->request; - - $this->config = $config; - $this->account = $account; - $this->currencies = $currencies; - - $this->url = isset($this->config['urls'][$this->account->env]) ? - $this->config['urls'][$this->account->env] : - $this->config['urls']['production']; - - $this->gateway = isset($this->config['urls']['gateway'][$this->account->env]) ? - $this->config['urls']['gateway'][$this->account->env] : - $this->config['urls']['gateway']['production']; - - if ($this->account->env == 'test') { - $this->mode = 'TEST'; - } - - return $this; - } - - /** - * @inheritDoc - */ - public function createXML(array $data, $encoding = 'UTF-8'): string - { - return $this->traitCreateXML(['GVPSRequest' => $data], $encoding); - } - - /** - * Make Security Data - * - * @param bool $refund - * @return string - */ - protected function makeSecurityData($refund = false) - { - $map = [ - $this->account->{($refund ? 'refund_' : null) . 'password'}, - str_pad((int) $this->account->terminal_id, 9, 0, STR_PAD_LEFT), - ]; - - return strtoupper(sha1(implode('', $map))); - } - - /** - * Make Hash Data - * - * @param $security_data - * @return string - */ - protected function makeHashData($security_data) - { - $map = [ - $this->order->id, - $this->account->terminal_id, - isset($this->card) ? $this->card->getNumber() : null, - $this->amountFormat($this->order->amount), - $security_data, - ]; - - return strtoupper(sha1(implode('', $map))); - } - - /** - * Make 3d Hash Data - * - * @param $security_data - * @return string - */ - protected function make3dHashData($security_data) - { - $map = [ - $this->account->terminal_id, - $this->order->id, - $this->amountFormat($this->order->amount), - $this->order->success_url, - $this->order->fail_url, - $this->type, - $this->order->installment ? $this->order->installment : '', - $this->account->store_key, - $security_data, - ]; - - return strtoupper(sha1(implode('', $map))); - } - - /** - * Make 3d Hash Data - * - * @param $security_data - * @return string - */ - protected function make3dRequestHashData($security_data) - { - $map = [ - $this->order->id, - $this->account->terminal_id, - $this->amountFormat($this->order->amount), - $security_data, - ]; - - return strtoupper(sha1(implode('', $map))); - } - - /** - * @return mixed - */ - public function getResponse() - { - return $this->response; - } - - /** - * Amount Formatter - * - * @param double $amount - * @return int - */ - public function amountFormat($amount) - { - return (int) str_replace('.', '', number_format($amount, 2, '.', '')); - } - - /** - * Create Regular Payment XML - * - * @return string - */ - protected function createRegularPaymentXML() - { - $security_data = $this->makeSecurityData(); - $hash_data = $this->makeHashData($security_data); - - $requestData = [ - 'Mode' => $this->mode, - 'Version' => 'v0.01', - 'Terminal' => [ - 'ProvUserID' => $this->account->username, - 'UserID' => $this->account->username, - 'HashData' => $hash_data, - 'ID' => $this->account->terminal_id, - 'MerchantID' => $this->account->client_id, - ], - 'Customer' => [ - 'IPAddress' => $this->order->ip, - 'EmailAddress' => $this->order->email, - ], - 'Card' => [ - 'Number' => $this->card->getNumber(), - 'ExpireDate' => $this->card->getExpirationDate(), - 'CVV2' => $this->card->getCvv(), - ], - 'Order' => [ - 'OrderID' => $this->order->id, - 'GroupID' => '', - 'AddressList' => [ - 'Address' => [ - 'Type' => 'S', - 'Name' => $this->order->name, - 'LastName' => '', - 'Company' => '', - 'Text' => '', - 'District' => '', - 'City' => '', - 'PostalCode' => '', - 'Country' => '', - 'PhoneNumber' => '', - ], - ], - ], - 'Transaction' => [ - 'Type' => $this->type, - 'InstallmentCnt' => $this->order->installment > 1 ? $this->order->installment : '', - 'Amount' => $this->amountFormat($this->order->amount), - 'CurrencyCode' => $this->order->currency, - 'CardholderPresentCode' => '0', - 'MotoInd' => 'N', - 'Description' => '', - 'OriginalRetrefNum' => '', - ], - ]; - - return $this->createXML($requestData); - } - - /** - * Create Regular Payment Post XML - * - * @return string - */ - protected function createRegularPostXML() - { - $security_data = $this->makeSecurityData(); - $hash_data = $this->makeHashData($security_data); - - $requestData = [ - 'Mode' => $this->mode, - 'Version' => 'v0.1', - 'Terminal' => [ - 'ProvUserID' => $this->account->username, - 'UserID' => $this->account->username, - 'HashData' => $hash_data, - 'ID' => $this->account->terminal_id, - 'MerchantID' => $this->account->client_id, - ], - 'Customer' => [ - 'IPAddress' => $this->order->ip, - 'EmailAddress' => isset($this->order->email) ? $this->order->email : null, - ], - 'Order' => [ - 'OrderID' => $this->order->id, - ], - 'Transaction' => [ - 'Type' => $this->types[$this->order->transaction], - 'Amount' => $this->amountFormat($this->order->amount), - 'CurrencyCode' => $this->order->currency, - 'OriginalRetrefNum' => $this->order->ref_ret_num, - ], - ]; - - return $this->createXML($requestData); - } - - /** - * Create 3D Payment XML - * @return string - */ - protected function create3DPaymentXML() - { - $security_data = $this->makeSecurityData(); - $hash_data = $this->makeHashData($security_data); - - $requestData = [ - 'Mode' => $this->mode, - 'Version' => $this->version, - 'ChannelCode' => '', - 'Terminal' => [ - 'ProvUserID' => $this->account->username, - 'UserID' => $this->account->username, - 'HashData' => $hash_data, - 'ID' => $this->account->terminal_id, - 'MerchantID' => $this->account->client_id, - ], - 'Customer' => [ - 'IPAddress' => $this->request->get('customeripaddress'), - 'EmailAddress' => $this->request->get('customeremailaddress'), - ], - 'Card' => [ - 'Number' => '', - 'ExpireDate' => '', - 'CVV2' => '', - ], - 'Order' => [ - 'OrderID' => $this->request->get('orderid'), - 'GroupID' => '', - 'AddressList' => [ - 'Address' => [ - 'Type' => 'B', - 'Name' => $this->order->name, - 'LastName' => '', - 'Company' => '', - 'Text' => '', - 'District' => '', - 'City' => '', - 'PostalCode' => '', - 'Country' => '', - 'PhoneNumber' => '', - ], - ], - ], - 'Transaction' => [ - 'Type' => $this->request->get('txntype'), - 'InstallmentCnt' => $this->order->installment ? $this->order->installment : '', - 'Amount' => $this->request->get('txnamount'), - 'CurrencyCode' => $this->request->get('txncurrencycode'), - 'CardholderPresentCode' => '13', - 'MotoInd' => 'N', - 'Secure3D' => [ - 'AuthenticationCode' => $this->request->get('cavv'), - 'SecurityLevel' => $this->request->get('eci'), - 'TxnID' => $this->request->get('xid'), - 'Md' => $this->request->get('md'), - ], - ] - ]; - - return $this->createXML($requestData); - } - - /** - * Get ProcReturnCode - * - * @return string|null - */ - protected function getProcReturnCode() - { - return isset($this->data->Transaction->Response->Code) ? (string) $this->data->Transaction->Response->Code : null; - } - - /** - * Get Status Detail Text - * - * @return string|null - */ - protected function getStatusDetail() - { - $proc_return_code = $this->getProcReturnCode(); - - return $proc_return_code ? (isset($this->codes[$proc_return_code]) ? (string) $this->codes[$proc_return_code] : null) : null; - } - - /** - * Create 3D Hash - * - * @return string - */ - public function create3DHash() - { - $hash_str = ''; - - if ($this->account->model == '3d') { - $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->order->rand . $this->account->store_key; - } elseif ($this->account->model == '3d_pay') { - $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->order->transaction_type . $this->order->installment . $this->order->rand . $this->account->store_key; - } - - return base64_encode(sha1($hash_str, true)); - } - - /** - * Regular Payment - * - * @return $this - * @throws GuzzleException - */ - public function makeRegularPayment() - { - $contents = ''; - if (in_array($this->order->transaction, ['pay', 'pre'])) { - $contents = $this->createRegularPaymentXML(); - } elseif ($this->order->transaction == 'post') { - $contents = $this->createRegularPostXML(); - } - - $this->send($contents); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $this->response = (object) [ - 'id' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'order_id' => isset($this->data->Order->OrderID) ? $this->printData($this->data->Order->OrderID) : null, - 'group_id' => isset($this->data->Order->GroupID) ? $this->printData($this->data->Order->GroupID) : null, - 'trans_id' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'response' => isset($this->data->Transaction->Response->Message) ? $this->printData($this->data->Transaction->Response->Message) : null, - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'auth_code' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'host_ref_num' => isset($this->data->Transaction->RetrefNum) ? $this->printData($this->data->Transaction->RetrefNum) : null, - 'ret_ref_num' => isset($this->data->Transaction->RetrefNum) ? $this->printData($this->data->Transaction->RetrefNum) : null, - 'hash_data' => isset($this->data->Transaction->HashData) ? $this->printData($this->data->Transaction->HashData) : null, - 'proc_return_code' => $this->getProcReturnCode(), - 'code' => $this->getProcReturnCode(), - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Transaction->Response->Code) ? $this->printData($this->data->Transaction->Response->Code) : null, - 'error_message' => isset($this->data->Transaction->Response->ErrorMsg) ? $this->printData($this->data->Transaction->Response->ErrorMsg) : null, - 'campaign_url' => isset($this->data->Transaction->CampaignChooseLink) ? $this->printData($this->data->Transaction->CampaignChooseLink) : null, - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, - 'all' => $this->data, - ]; - - return $this; - } - - /** - * Make 3D Payment - * - * @return $this - * @throws GuzzleException - */ - public function make3DPayment() - { - $status = 'declined'; - $response = 'Declined'; - $proc_return_code = '99'; - $transaction_security = 'MPI fallback'; - if (in_array($this->request->get('mdstatus'), [1, 2, 3, 4])) { - if ($this->request->get('mdstatus') == '1') { - $transaction_security = 'Full 3D Secure'; - } elseif (in_array($this->request->get('mdstatus'), [2, 3, 4])) { - $transaction_security = 'Half 3D Secure'; - } - - $contents = $this->create3DPaymentXML(); - $this->send($contents); - - if ($this->data->Transaction->Response->ReasonCode == '00') { - $response = 'Approved'; - $proc_return_code = $this->data->Transaction->Response->ReasonCode; - $status = 'approved'; - } - } - - $this->response = (object) [ - 'id' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'order_id' => $this->request->get('oid'), - 'group_id' => isset($this->data->Transaction->SequenceNum) ? $this->printData($this->data->Transaction->SequenceNum) : null, - 'trans_id' => $this->request->get('transid'), - 'response' => $response, - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'transaction_security' => $transaction_security, - 'auth_code' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'host_ref_num' => isset($this->data->Transaction->RetrefNum) ? $this->printData($this->data->Transaction->RetrefNum) : null, - 'proc_return_code' => $proc_return_code, - 'ret_ref_num' => isset($this->data->Transaction->RetrefNum) ? $this->printData($this->data->Transaction->RetrefNum) : null, - 'batch_num' => isset($this->data->Transaction->BatchNum) ? $this->printData($this->data->Transaction->BatchNum) : null, - 'code' => $proc_return_code, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Transaction->Response->ErrorCode) ? $this->printData($this->data->Transaction->Response->ErrorCode) : null, - 'error_message' => isset($this->data->Transaction->Response->ErrorMsg) ? $this->printData($this->data->Transaction->Response->ErrorMsg) : null, - 'reason_code' => isset($this->data->Transaction->Response->ReasonCode) ? $this->printData($this->data->Transaction->Response->ReasonCode) : null, - 'campaign_url' => isset($this->data->Transaction->CampaignChooseLink) ? $this->printData($this->data->Transaction->CampaignChooseLink) : null, - 'md_status' => $this->request->get('mdstatus'), - 'rand' => (string) $this->request->get('rnd'), - 'hash' => (string) $this->request->get('secure3dhash'), - 'hash_params' => (string) $this->request->get('hashparams'), - 'hash_params_val' => (string) $this->request->get('hashparamsval'), - 'secure_3d_hash' => (string) $this->request->get('secure3dhash'), - 'secure_3d_level' => (string) $this->request->get('secure3dsecuritylevel'), - 'masked_number' => (string) $this->request->get('MaskedPan'), - 'amount' => (string) $this->request->get('amount'), - 'currency' => (string) $this->request->get('currency'), - 'tx_status' => (string) $this->request->get('txstatus'), - 'eci' => (string) $this->request->get('eci'), - 'cavv' => (string) $this->request->get('cavv'), - 'xid' => (string) $this->request->get('xid'), - 'md_error_message' => (string) $this->request->get('mderrormessage'), - 'name' => (string) $this->request->get('firmaadi'), - 'email' => (string) $this->request->get('Email'), - 'extra' => null, - 'all' => $this->data, - '3d_all' => $this->request->all(), - ]; - - return $this; - } - - /** - * Make 3D Pay Payment - * - * @return $this - */ - public function make3DPayPayment() - { - $status = 'declined'; - $response = 'Declined'; - $proc_return_code = $this->request->get('procreturncode'); - - $transaction_security = 'MPI fallback'; - if (in_array($this->request->get('mdstatus'), [1, 2, 3, 4])) { - if ($this->request->get('mdstatus') == '1') { - $transaction_security = 'Full 3D Secure'; - } elseif (in_array($this->request->get('mdstatus'), [2, 3, 4])) { - $transaction_security = 'Half 3D Secure'; - } - - $status = 'approved'; - $response = 'Approved'; - } - - $this->response = (object) [ - 'id' => (string) $this->request->get('authcode'), - 'order_id' => (string) $this->request->get('oid'), - 'trans_id' => (string) $this->request->get('transid'), - 'auth_code' => (string) $this->request->get('authcode'), - 'host_ref_num' => (string) $this->request->get('hostrefnum'), - 'response' => $response, - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'transaction_security' => $transaction_security, - 'proc_return_code' => $proc_return_code, - 'code' => $proc_return_code, - 'md_status' => $this->request->get('mdStatus'), - 'status' => $status, - 'status_detail' => isset($this->codes[$this->request->get('ProcReturnCode')]) ? (string) $this->request->get('ProcReturnCode') : null, - 'hash' => (string) $this->request->get('secure3dhash'), - 'rand' => (string) $this->request->get('rnd'), - 'hash_params' => (string) $this->request->get('hashparams'), - 'hash_params_val' => (string) $this->request->get('hashparamsval'), - 'masked_number' => (string) $this->request->get('MaskedPan'), - 'amount' => (string) $this->request->get('amount'), - 'currency' => (string) $this->request->get('currency'), - 'tx_status' => (string) $this->request->get('txstatus'), - 'eci' => (string) $this->request->get('eci'), - 'cavv' => (string) $this->request->get('cavv'), - 'xid' => (string) $this->request->get('xid'), - 'error_code' => (string) $this->request->get('errcode'), - 'error_message' => (string) $this->request->get('errmsg'), - 'md_error_message' => (string) $this->request->get('mderrormessage'), - 'campaign_url' => null, - 'name' => (string) $this->request->get('firmaadi'), - 'email' => (string) $this->request->get('Email'), - 'extra' => $this->request->get('Extra'), - 'all' => $this->request->all(), - ]; - - return $this; - } - - /** - * Get 3d Form Data - * - * @return array - */ - public function get3DFormData() - { - $security_data = $this->makeSecurityData(); - $hash_data = $this->make3dHashData($security_data); - - $inputs = [ - 'secure3dsecuritylevel' => $this->account->model == '3d_pay' ? '3D_PAY' : '3D', - 'mode' => $this->mode, - 'apiversion' => $this->version, - 'terminalprovuserid' => $this->account->username, - 'terminaluserid' => $this->account->username, - 'terminalmerchantid' => $this->account->client_id, - 'txntype' => $this->type, - 'txnamount' => $this->amountFormat($this->order->amount), - 'txncurrencycode' => $this->order->currency, - 'txninstallmentcount' => $this->order->installment > 1 ? $this->order->installment : '', - 'orderid' => $this->order->id, - 'terminalid' => $this->account->terminal_id, - 'successurl' => $this->order->success_url, - 'errorurl' => $this->order->fail_url, - 'customeremailaddress' => isset($this->order->email) ? $this->order->email : null, - 'customeripaddress' => $this->order->ip, - 'cardnumber' => $this->card->getNumber(), - 'cardexpiredatemonth' => $this->card->getExpireMonth(), - 'cardexpiredateyear' => $this->card->getExpireYear(), - 'cardcvv2' => $this->card->getCvv(), - 'secure3dhash' => $hash_data, - ]; - - return [ - 'gateway' => $this->gateway, - 'success_url' => $this->order->success_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand, - 'hash' => $hash_data, - 'inputs' => $inputs, - ]; - } - - /** - * Send contents to WebService - * - * @param $contents - * @return $this - * @throws GuzzleException - */ - public function send($contents) - { - $client = new Client(); - - $response = $client->request('POST', $this->url, [ - 'body' => $contents - ]); - - $this->data = $this->XMLStringToObject($response->getBody()->getContents()); - - return $this; - } - - /** - * Prepare Order - * - * @param object $order - * @param CreditCardGarantiPos|null $card - * - * @return mixed - * - * @throws UnsupportedTransactionTypeException - */ - public function prepare($order, $card = null) - { - $this->type = $this->types['pay']; - if (isset($order->transaction)) { - if (array_key_exists($order->transaction, $this->types)) { - $this->type = $this->types[$order->transaction]; - } else { - throw new UnsupportedTransactionTypeException('Unsupported transaction type!'); - } - } - - $this->order = $order; - $this->card = $card; - } - - /** - * Make Payment - * - * @param CreditCardGarantiPos $card - * - * @return mixed - * - * @throws UnsupportedPaymentModelException - * @throws GuzzleException - */ - public function payment($card) - { - $this->card = $card; - - $model = 'regular'; - if (isset($this->account->model) && $this->account->model) { - $model = $this->account->model; - } - - if ($model == 'regular') { - $this->makeRegularPayment(); - } elseif ($model == '3d') { - $this->make3DPayment(); - } elseif ($model == '3d_pay') { - $this->make3DPayPayment(); - } else { - throw new UnsupportedPaymentModelException(); - } - - return $this; - } - - /** - * Refund or Cancel Order - * - * @param array $meta - * @param $type - * @return $this - * @throws GuzzleException - */ - protected function refundOrCancel(array $meta, $type) - { - $this->order = (object) [ - 'id' => $meta['order_id'], - 'amount' => isset($meta['amount']) ? $meta['amount'] : null, - ]; - - $security_data = $this->makeSecurityData(true); - $hash_data = $this->makeHashData($security_data); - - $currency = (int) $this->currencies[$meta['currency']]; - - $requestData = [ - 'Mode' => $this->mode, - 'Version' => $this->version, - 'ChannelCode' => '', - 'Terminal' => [ - 'ProvUserID' => $this->account->refund_username, - 'UserID' => $this->account->refund_username, - 'HashData' => $hash_data, - 'ID' => $this->account->terminal_id, - 'MerchantID' => $this->account->client_id, - ], - 'Customer' => [ - 'IPAddress' => isset($meta['ip']) ? $meta['ip'] : null, - 'EmailAddress' => isset($meta['email']) ? $meta['email'] : null, - ], - 'Order' => [ - 'OrderID' => $this->order->id, - 'GroupID' => '', - ], - 'Transaction' => [ - 'Type' => $type, - 'InstallmentCnt' => '', - 'Amount' => $this->amountFormat($this->order->amount), - 'CurrencyCode' => $currency, - 'CardholderPresentCode' => '0', - 'MotoInd' => 'N', - 'OriginalRetrefNum' => $meta['ref_ret_num'], - ], - ]; - - $xml = $this->createXML($requestData); - $this->send($xml); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $this->response = (object) [ - 'id' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'order_id' => isset($this->data->Order->OrderID) ? $this->printData($this->data->Order->OrderID) : null, - 'group_id' => isset($this->data->Order->GroupID) ? $this->printData($this->data->Order->GroupID) : null, - 'trans_id' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'response' => isset($this->data->Transaction->Response->Message) ? $this->printData($this->data->Transaction->Response->Message) : null, - 'auth_code' => isset($this->data->Transaction->AuthCode) ? $this->data->Transaction->AuthCode : null, - 'host_ref_num' => isset($this->data->Transaction->RetrefNum) ? $this->printData($this->data->Transaction->RetrefNum) : null, - 'ret_ref_num' => isset($this->data->Transaction->RetrefNum) ? $this->printData($this->data->Transaction->RetrefNum) : null, - 'hash_data' => isset($this->data->Transaction->HashData) ? $this->printData($this->data->Transaction->HashData) : null, - 'proc_return_code' => $this->getProcReturnCode(), - 'code' => $this->getProcReturnCode(), - 'error_code' => isset($this->data->Transaction->Response->Code) ? $this->printData($this->data->Transaction->Response->Code) : null, - 'error_message' => isset($this->data->Transaction->Response->ErrorMsg) ? $this->printData($this->data->Transaction->Response->ErrorMsg) : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, - ]; - - return $this; - } - - /** - * Refund Order - * - * @param $meta - * @return $this - * @throws GuzzleException - */ - public function refund(array $meta) - { - return $this->refundOrCancel($meta, 'refund'); - } - - /** - * Cancel Order - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function cancel(array $meta) - { - return $this->refundOrCancel($meta, 'void'); - } - - /** - * Order Status or History - * - * @param array $meta - * @param $type - * @return $this - * @throws GuzzleException - */ - protected function statusOrHistory(array $meta, $type) - { - $obj_item = 'OrderInqResult'; - if ($type == 'orderhistoryinq') { - $obj_item = 'OrderHistInqResult'; - } - - $this->order = (object) [ - 'id' => isset($meta['order_id']) ? $meta['order_id'] : null, - 'currency' => isset($this->currencies[$meta['currency']]) ? $this->currencies[$meta['currency']] : null, - 'amount' => '1', - ]; - - $security_data = $this->makeSecurityData(); - $hash_data = $this->makeHashData($security_data); - - $requestData = [ - 'Mode' => $this->mode, - 'Version' => 'v0.01', - 'ChannelCode' => '', - 'Terminal' => [ - 'ProvUserID' => $this->account->username, - 'UserID' => $this->account->username, - 'HashData' => $hash_data, - 'ID' => $this->account->terminal_id, - 'MerchantID' => $this->account->client_id, - ], - 'Customer' => [ - 'IPAddress' => isset($meta['ip']) ? $meta['ip'] : null, - 'EmailAddress' => isset($meta['email']) ? $meta['email'] : null, - ], - 'Order' => [ - 'OrderID' => $this->order->id, - 'GroupID' => '', - ], - 'Card' => [ - 'Number' => '', - 'ExpireDate' => '', - 'CVV2' => '', - ], - 'Transaction' => [ - 'Type' => $type, - 'InstallmentCnt' => '', - 'Amount' => $this->order->amount ? $this->amountFormat($this->order->amount) : null, - 'CurrencyCode' => $this->order->currency, - 'CardholderPresentCode' => '0', - 'MotoInd' => 'N', - ], - ]; - $xml = $this->createXML($requestData); - - $this->send($xml); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $data = [ - 'id' => isset($this->data->Order->{$obj_item}->AuthCode) ? $this->printData($this->data->Order->{$obj_item}->AuthCode) : null, - 'order_id' => isset($this->data->Order->OrderID) ? $this->printData($this->data->Order->OrderID) : null, - 'group_id' => isset($this->data->Order->GroupID) ? $this->printData($this->data->Order->GroupID) : null, - 'trans_id' => isset($this->data->Transaction->AuthCode) ? $this->printData($this->data->Transaction->AuthCode) : null, - 'response' => isset($this->data->Transaction->Response->Message) ? $this->printData($this->data->Transaction->Response->Message) : null, - 'auth_code' => isset($this->data->Order->{$obj_item}->AuthCode) ? $this->printData($this->data->Order->{$obj_item}->AuthCode) : null, - 'host_ref_num' => isset($this->data->Order->{$obj_item}->RetrefNum) ? $this->printData($this->data->Order->{$obj_item}->RetrefNum) : null, - 'ret_ref_num' => isset($this->data->Order->{$obj_item}->RetrefNum) ? $this->printData($this->data->Order->{$obj_item}->RetrefNum) : null, - 'hash_data' => isset($this->data->Transaction->HashData) ? $this->printData($this->data->Transaction->HashData) : null, - 'proc_return_code' => $this->getProcReturnCode(), - 'code' => $this->getProcReturnCode(), - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Transaction->Response->Code) ? $this->printData($this->data->Transaction->Response->Code) : null, - 'error_message' => isset($this->data->Transaction->Response->ErrorMsg) ? $this->printData($this->data->Transaction->Response->ErrorMsg) : null, - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, - 'all' => $this->data, - ]; - - if ($type == 'orderhistoryinq') { - $data = array_merge($data, [ - 'order_txn' => isset($this->data->Order->OrderHistInqResult->OrderTxnList->OrderTxn) ? $this->data->Order->OrderHistInqResult->OrderTxnList->OrderTxn : [] - ]); - } - - $this->response = (object) $data; - - return $this; - } - - /** - * Order Status - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function status(array $meta) - { - return $this->statusOrHistory($meta, 'orderinq'); - } - - /** - * Order History - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function history(array $meta) - { - return $this->statusOrHistory($meta, 'orderhistoryinq'); - } - - /** - * @return array - */ - public function getConfig() - { - return $this->config; - } - - /** - * @return mixed - */ - public function getAccount() - { - return $this->account; - } - - /** - * @return array - */ - public function getCurrencies() - { - return $this->currencies; - } - - /** - * @return mixed - */ - public function getOrder() - { - return $this->order; - } - - /** - * @return CreditCardGarantiPos|null - */ - public function getCard() - { - return $this->card; - } - - /** - * bank returns error messages for specified language value - * usually accepted values are tr,en - * @return string - */ - private function getLang() - { - if ($this->order && isset($this->order->lang)) { - return $this->order->lang; - } - if (isset($this->account->lang)) { - return $this->account->lang; - } - - return self::LANG_TR; - } -} diff --git a/src/Gateways/AbstractGateway.php b/src/Gateways/AbstractGateway.php new file mode 100644 index 00000000..aff0552f --- /dev/null +++ b/src/Gateways/AbstractGateway.php @@ -0,0 +1,597 @@ +config = $config; + $this->account = $account; + + if (count($currencies) > 0) { + $this->currencies = $currencies; + } + } + + /** + * @inheritDoc + */ + public function prepare(array $order, string $txType, $card = null) + { + $this->setTxType($txType); + + switch ($txType) { + case self::TX_PAY: + case self::TX_PRE_PAY: + $this->order = $this->preparePaymentOrder($order); + break; + case self::TX_POST_PAY: + $this->order = $this->preparePostPaymentOrder($order); + break; + case self::TX_CANCEL: + $this->order = $this->prepareCancelOrder($order); + break; + case self::TX_REFUND: + $this->order = $this->prepareRefundOrder($order); + break; + case self::TX_STATUS: + $this->order = $this->prepareStatusOrder($order); + break; + case self::TX_HISTORY: + $this->order = $this->prepareHistoryOrder($order); + break; + } + + $this->card = $card; + } + + /** + * @return mixed + */ + public function getResponse() + { + return $this->response; + } + + /** + * @return array + */ + public function getCurrencies() + { + return $this->currencies; + } + + /** + * @return array + */ + public function getConfig() + { + return $this->config; + } + + /** + * @return AbstractPosAccount + */ + abstract public function getAccount(); + + /** + * @return AbstractCreditCard + */ + abstract public function getCard(); + + /** + * @return mixed + */ + public function getOrder() + { + return $this->order; + } + + /** + * Create XML DOM Document + * + * @param array $nodes + * @param string $encoding + * + * @return string the XML, or false if an error occurred. + */ + public function createXML(array $nodes, $encoding = 'UTF-8') + { + $rootNodeName = array_keys($nodes)[0]; + $encoder = new XmlEncoder(); + + return $encoder->encode($nodes[$rootNodeName], 'xml', [ + XmlEncoder::ROOT_NODE_NAME => $rootNodeName, + XmlEncoder::ENCODING => $encoding, + ]); + } + + /** + * Print Data + * + * @param $data + * + * @return null|string + */ + public function printData($data) + { + if ((is_object($data) || is_array($data)) && !count((array) $data)) { + $data = null; + } + + return (string) $data; + } + + /** + * Is success + * + * @return bool + */ + public function isSuccess() + { + if (isset($this->response) && 'approved' === $this->response->status) { + return true; + } + + return false; + } + + /** + * Is error + * + * @return bool + */ + public function isError() + { + return !$this->isSuccess(); + } + + /** + * Converts XML string to object + * + * @param string $data + * + * @return object + */ + public function XMLStringToObject($data) + { + $encoder = new XmlEncoder(); + $xml = $encoder->decode($data, 'xml'); + + return (object) json_decode(json_encode($xml)); + } + + /** + * @return string + */ + public function getApiURL() + { + return $this->config['urls'][$this->getModeInWord()]; + } + + /** + * @return string + */ + public function get3DGatewayURL() + { + return $this->config['urls']['gateway'][$this->getModeInWord()]; + } + + /** + * @return string + */ + public function get3DHostGatewayURL() + { + return isset($this->config['urls']['gateway_3d_host'][$this->getModeInWord()]) ? $this->config['urls']['gateway_3d_host'][$this->getModeInWord()] : null; + } + + /** + * @return bool + */ + public function isTestMode(): bool + { + return $this->testMode; + } + + /** + * @param string $txType + * + * @throws UnsupportedTransactionTypeException + */ + public function setTxType(string $txType) + { + if (array_key_exists($txType, $this->types)) { + $this->type = $this->types[$txType]; + } else { + throw new UnsupportedTransactionTypeException(); + } + } + + /** + * @param AbstractCreditCard|null $card + * + * @return $this + * + * @throws GuzzleException + * @throws UnsupportedPaymentModelException + */ + public function payment($card = null) + { + $this->card = $card; + + $model = $this->account->getModel(); + + if ('regular' === $model) { + $this->makeRegularPayment(); + } elseif ('3d' === $model) { + $this->make3DPayment(); + } elseif ('3d_pay' === $model) { + $this->make3DPayPayment(); + } elseif ('3d_host' === $model) { + $this->make3DHostPayment(); + } else { + throw new UnsupportedPaymentModelException(); + } + + return $this; + } + + /** + * @inheritDoc + */ + public function makeRegularPayment() + { + $contents = ''; + if (in_array($this->type, [$this->types[self::TX_PAY], $this->types[self::TX_PRE_PAY]])) { + $contents = $this->createRegularPaymentXML(); + } elseif ($this->types[self::TX_POST_PAY] === $this->type) { + $contents = $this->createRegularPostXML(); + } + + $this->send($contents); + + $this->response = (object) $this->mapPaymentResponse($this->data); + + return $this; + } + + /** + * @inheritDoc + */ + public function refund() + { + $xml = $this->createRefundXML(); + $this->send($xml); + + $this->response = $this->mapRefundResponse($this->data); + + return $this; + } + + /** + * @inheritDoc + */ + public function cancel() + { + $xml = $this->createCancelXML(); + $this->send($xml); + + $this->response = $this->mapCancelResponse($this->data); + + return $this; + } + + /** + * @inheritDoc + */ + public function status() + { + $xml = $this->createStatusXML(); + + $this->send($xml); + + $this->response = $this->mapStatusResponse($this->data); + + return $this; + } + + /** + * @inheritDoc + */ + public function history(array $meta) + { + $xml = $this->createHistoryXML($meta); + + $this->send($xml); + + $this->response = $this->mapHistoryResponse($this->data); + + return $this; + } + + /** + * @param bool $testMode + * + * @return $this + */ + public function setTestMode(bool $testMode): self + { + $this->testMode = $testMode; + + return $this; + } + + /** + * @param string $currency TRY, USD + * + * @return string + */ + public function mapCurrency(string $currency): string + { + return isset($this->currencies[$currency]) ? $this->currencies[$currency] : $currency; + } + + /** + * Create Regular Payment XML + * + * @return string + */ + abstract public function createRegularPaymentXML(); + + /** + * Create Regular Payment Post XML + * + * @return string + */ + abstract public function createRegularPostXML(); + + /** + * Creates XML string for history inquiry + * + * @param array $customQueryData + * + * @return string + */ + abstract public function createHistoryXML($customQueryData); + + /** + * Creates XML string for order status inquiry + * @return mixed + */ + abstract public function createStatusXML(); + + /** + * Creates XML string for order cancel operation + * @return string + */ + abstract public function createCancelXML(); + + /** + * Creates XML string for order refund operation + * @return mixed + */ + abstract public function createRefundXML(); + + /** + * Creates 3D Payment XML + * @param $responseData + * + * @return string + */ + abstract public function create3DPaymentXML($responseData); + + /** + * returns form data, key values, necessary for 3D payment + * + * @return array + */ + abstract public function get3DFormData(); + + /** + * prepares order for payment request + * @param array $order + * + * @return object + */ + abstract protected function preparePaymentOrder(array $order); + + /** + * prepares order for TX_POST_PAY type request + * @param array $order + * + * @return object + */ + abstract protected function preparePostPaymentOrder(array $order); + + /** + * prepares order for order status request + * @param array $order + * + * @return object + */ + abstract protected function prepareStatusOrder(array $order); + + /** + * prepares order for history request + * @param array $order + * + * @return object + */ + abstract protected function prepareHistoryOrder(array $order); + + /** + * prepares order for cancel request + * @param array $order + * + * @return object + */ + abstract protected function prepareCancelOrder(array $order); + + /** + * prepares order for refund request + * @param array $order + * + * @return object + */ + abstract protected function prepareRefundOrder(array $order); + + /** + * @param array $raw3DAuthResponseData response from 3D authentication + * @param object $rawPaymentResponseData + * + * @return object + */ + abstract protected function map3DPaymentData($raw3DAuthResponseData, $rawPaymentResponseData); + + /** + * @param array $raw3DAuthResponseData response from 3D authentication + * + * @return object + */ + abstract protected function map3DPayResponseData($raw3DAuthResponseData); + + /** + * Processes regular payment response data + * + * @param object $responseData + * + * @return array + */ + abstract protected function mapPaymentResponse($responseData); + + /** + * @param $rawResponseData + * + * @return object + */ + abstract protected function mapRefundResponse($rawResponseData); + + /** + * @param $rawResponseData + * + * @return object + */ + abstract protected function mapCancelResponse($rawResponseData); + + /** + * @param object $rawResponseData + * + * @return object + */ + abstract protected function mapStatusResponse($rawResponseData); + + /** + * @param object $rawResponseData + * + * @return mixed + */ + abstract protected function mapHistoryResponse($rawResponseData); + + + /** + * bank returns error messages for specified language value + * usually accepted values are tr,en + * @return string + */ + protected function getLang() + { + if ($this->order && isset($this->order->lang)) { + return $this->order->lang; + } + + return $this->account->getLang(); + } + + /** + * return values are used as a key in config file + * @return string + */ + private function getModeInWord() + { + return !$this->isTestMode() ? 'production' : 'test'; + } +} \ No newline at end of file diff --git a/src/Gateways/EstPos.php b/src/Gateways/EstPos.php new file mode 100644 index 00000000..86523d5e --- /dev/null +++ b/src/Gateways/EstPos.php @@ -0,0 +1,765 @@ + 'approved', + '01' => 'bank_call', + '02' => 'bank_call', + '05' => 'reject', + '09' => 'try_again', + '12' => 'invalid_transaction', + '28' => 'reject', + '51' => 'insufficient_balance', + '54' => 'expired_card', + '57' => 'does_not_allow_card_holder', + '62' => 'restricted_card', + '77' => 'request_rejected', + '99' => 'general_error', + ]; + + /** + * Transaction Types + * + * @var array + */ + protected $types = [ + self::TX_PAY => 'Auth', + self::TX_PRE_PAY => 'PreAuth', + self::TX_POST_PAY => 'PostAuth', + self::TX_CANCEL => 'Void', + self::TX_REFUND => 'Credit', + self::TX_STATUS => 'ORDERSTATUS', + self::TX_HISTORY => 'ORDERHISTORY', + ]; + + + /** + * Currency mapping + * + * @var array + */ + protected $currencies = [ + 'TRY' => 949, + 'USD' => 840, + 'EUR' => 978, + 'GBP' => 826, + 'JPY' => 392, + 'RUB' => 643, + ]; + + /** + * @var EstPosAccount + */ + protected $account; + + /** + * @var CreditCardEstPos|null + */ + protected $card; + + /** + * EstPos constructor. + * + * @param array $config + * @param EstPosAccount $account + * @param array $currencies + */ + public function __construct($config, $account, array $currencies = []) + { + parent::__construct($config, $account, $currencies); + } + + /** + * @inheritDoc + */ + public function createXML(array $data, $encoding = 'ISO-8859-9'): string + { + return parent::createXML(['CC5Request' => $data], $encoding); + } + + /** + * Create 3D Hash + * + * @return string + */ + public function create3DHash() + { + $hashStr = ''; + + if ($this->account->getModel() === '3d') { + $hashStr = $this->account->getClientId() . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->order->rand . $this->account->getStoreKey(); + } elseif ($this->account->getModel() === '3d_pay') { + $hashStr = $this->account->getClientId() . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->type . $this->order->installment . $this->order->rand . $this->account->getStoreKey(); + } + + return base64_encode(sha1($hashStr, true)); + } + + /** + * Check 3D Hash + * @param array $data + * + * @return bool + */ + public function check3DHash($data) + { + $hashParams = $data['HASHPARAMS']; + $hashParamsVal = $data['HASHPARAMSVAL']; + $hashParam = $data['HASH']; + $paramsVal = ''; + + $hashParamsArr = explode(':', $hashParams); + foreach ($hashParamsArr as $value) { + if (!empty($value) && isset($data[$value])) { + $paramsVal = $paramsVal.$data[$value]; + } + } + + $hashVal = $paramsVal.$this->account->getStoreKey(); + $hash = base64_encode(sha1($hashVal, true)); + + $return = false; + if ($hashParams && !($paramsVal !== $hashParamsVal || $hashParam !== $hash)) { + $return = true; + } + + return $return; + } + + /** + * @inheritDoc + */ + public function make3DPayment() + { + $request = Request::createFromGlobals(); + + + if ($this->check3DHash($request->request->all())) { + $contents = $this->create3DPaymentXML($request->request->all()); + $this->send($contents); + } + + $this->response = $this->map3DPaymentData($request->request->all(), $this->data); + + return $this; + } + + /** + * @inheritDoc + */ + public function make3DPayPayment() + { + $request = Request::createFromGlobals(); + + $this->response = $this->map3DPayResponseData($request->request->all()); + + return $this; + } + + /** + * @inheritDoc + */ + public function make3DHostPayment() + { + throw new NotImplementedException(); + } + + /** + * @inheritDoc + */ + public function get3DFormData() + { + $data = []; + + if ($this->order) { + $this->order->hash = $this->create3DHash(); + + $inputs = [ + 'clientid' => $this->account->getClientId(), + 'storetype' => $this->account->getModel(), + 'hash' => $this->order->hash, + 'cardType' => $this->card->getCardCode(), + 'pan' => $this->card->getNumber(), + 'Ecom_Payment_Card_ExpDate_Month' => $this->card->getExpireMonth(), + 'Ecom_Payment_Card_ExpDate_Year' => $this->card->getExpireYear(), + 'cv2' => $this->card->getCvv(), + 'firmaadi' => $this->order->name, + 'Email' => $this->order->email, + 'amount' => $this->order->amount, + 'oid' => $this->order->id, + 'okUrl' => $this->order->success_url, + 'failUrl' => $this->order->fail_url, + 'rnd' => $this->order->rand, + 'lang' => $this->getLang(), + 'currency' => $this->order->currency, + ]; + + if ($this->account->getModel() === '3d_pay') { + $inputs = array_merge($inputs, [ + 'islemtipi' => $this->type, + 'taksit' => $this->order->installment, + ]); + } + + $data = [ + 'gateway' => $this->get3DGatewayURL(), + 'inputs' => $inputs, + ]; + } + + return $data; + } + + /** + * @inheritDoc + */ + public function send($contents) + { + $client = new Client(); + + $response = $client->request('POST', $this->getApiURL(), [ + 'body' => $contents, + ]); + + $this->data = $this->XMLStringToObject($response->getBody()->getContents()); + + return $this; + } + + /** + * @inheritDoc + */ + public function history(array $meta) + { + $xml = $this->createHistoryXML($meta); + + $this->send($xml); + + $this->response = $this->mapHistoryResponse($this->data); + + return $this; + } + + /** + * @return mixed + */ + public function getAccount() + { + return $this->account; + } + + /** + * @return CreditCardEstPos|null + */ + public function getCard() + { + return $this->card; + } + + /** + * @inheritDoc + */ + public function createRegularPaymentXML() + { + $requestData = [ + 'Name' => $this->account->getUsername(), + 'Password' => $this->account->getPassword(), + 'ClientId' => $this->account->getClientId(), + 'Type' => $this->type, + 'IPAddress' => $this->order->ip, + 'Email' => $this->order->email, + 'OrderId' => $this->order->id, + 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, + 'Total' => $this->order->amount, + 'Currency' => $this->order->currency, + 'Taksit' => $this->order->installment, + 'CardType' => $this->card->getType(), + 'Number' => $this->card->getNumber(), + 'Expires' => $this->card->getExpirationDate(), + 'Cvv2Val' => $this->card->getCvv(), + 'Mode' => 'P', //TODO what is this constant for? + 'GroupId' => '', + 'TransId' => '', + 'BillTo' => [ + 'Name' => $this->order->name ? $this->order->name : null, + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRegularPostXML() + { + $requestData = [ + 'Name' => $this->account->getUsername(), + 'Password' => $this->account->getPassword(), + 'ClientId' => $this->account->getClientId(), + 'Type' => $this->types[self::TX_POST_PAY], + 'OrderId' => $this->order->id, + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function create3DPaymentXML($responseData) + { + $requestData = [ + 'Name' => $this->account->getUsername(), + 'Password' => $this->account->getPassword(), + 'ClientId' => $this->account->getClientId(), + 'Type' => $this->type, + 'IPAddress' => $this->order->ip, + 'Email' => $this->order->email, + 'OrderId' => $this->order->id, + 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, + 'Total' => $this->order->amount, + 'Currency' => $this->order->currency, + 'Taksit' => $this->order->installment, + 'Number' => $responseData['md'], + 'Expires' => '', + 'Cvv2Val' => '', + 'PayerTxnId' => $responseData['xid'], + 'PayerSecurityLevel' => $responseData['eci'], + 'PayerAuthenticationCode' => $responseData['cavv'], + 'CardholderPresentCode' => '13', + 'Mode' => 'P', + 'GroupId' => '', + 'TransId' => '', + ]; + + if ($this->order->name) { + $requestData['BillTo'] = [ + 'Name' => $this->order->name, + ]; + } + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createStatusXML() + { + $requestData = [ + 'Name' => $this->account->getUsername(), + 'Password' => $this->account->getPassword(), + 'ClientId' => $this->account->getClientId(), + 'OrderId' => $this->order->id, + 'Extra' => [ + $this->types[self::TX_STATUS] => 'QUERY', + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createHistoryXML($customQueryData) + { + $requestData = [ + 'Name' => $this->account->getUsername(), + 'Password' => $this->account->getPassword(), + 'ClientId' => $this->account->getClientId(), + 'OrderId' => $this->order->id, + 'Extra' => [ + $this->types[self::TX_HISTORY] => 'QUERY', + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createCancelXML() + { + $requestData = [ + 'Name' => $this->account->getUsername(), + 'Password' => $this->account->getPassword(), + 'ClientId' => $this->account->getClientId(), + 'OrderId' => $this->order->id, + 'Type' => $this->types[self::TX_CANCEL], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRefundXML() + { + $requestData = [ + 'Name' => $this->account->getUsername(), + 'Password' => $this->account->getPassword(), + 'ClientId' => $this->account->getClientId(), + 'OrderId' => $this->order->id, + 'Type' => $this->types[self::TX_REFUND], + ]; + + if (isset($this->order->amount)) { + $requestData['Total'] = $this->order->amount; + } + + return $this->createXML($requestData); + } + + /** + * Get ProcReturnCode + * + * @return string|null + */ + protected function getProcReturnCode() + { + return isset($this->data->ProcReturnCode) ? (string) $this->data->ProcReturnCode : null; + } + + /** + * Get Status Detail Text + * + * @return string|null + */ + protected function getStatusDetail() + { + $procReturnCode = $this->getProcReturnCode(); + + return $procReturnCode ? (isset($this->codes[$procReturnCode]) ? (string) $this->codes[$procReturnCode] : null) : null; + } + + /** + * @inheritDoc + */ + protected function map3DPaymentData($raw3DAuthResponseData, $rawPaymentResponseData) + { + $transactionSecurity = 'MPI fallback'; + if ($this->getProcReturnCode() === '00') { + if ($raw3DAuthResponseData['mdStatus'] == '1') { + $transactionSecurity = 'Full 3D Secure'; + } elseif (in_array($raw3DAuthResponseData['mdStatus'], [2, 3, 4])) { + $transactionSecurity = 'Half 3D Secure'; + } + } + + $paymentResponseData = $this->mapPaymentResponse($rawPaymentResponseData); + + $threeDResponse = [ + 'transaction_security' => $transactionSecurity, + 'md_status' => $raw3DAuthResponseData['mdStatus'], + 'hash' => (string) $raw3DAuthResponseData['HASH'], + 'rand' => (string) $raw3DAuthResponseData['rnd'], + 'hash_params' => (string) $raw3DAuthResponseData['HASHPARAMS'], + 'hash_params_val' => (string) $raw3DAuthResponseData['HASHPARAMSVAL'], + 'masked_number' => (string) $raw3DAuthResponseData['maskedCreditCard'], + 'month' => (string) $raw3DAuthResponseData['Ecom_Payment_Card_ExpDate_Month'], + 'year' => (string) $raw3DAuthResponseData['Ecom_Payment_Card_ExpDate_Year'], + 'amount' => (string) $raw3DAuthResponseData['amount'], + 'currency' => (string) $raw3DAuthResponseData['currency'], + 'tx_status' => (string) $raw3DAuthResponseData['txstatus'], + 'eci' => (string) $raw3DAuthResponseData['eci'], + 'cavv' => (string) $raw3DAuthResponseData['cavv'], + 'xid' => (string) $raw3DAuthResponseData['xid'], + 'md_error_message' => (string) $raw3DAuthResponseData['mdErrorMsg'], + 'name' => (string) $raw3DAuthResponseData['firmaadi'], + 'email' => (string) $raw3DAuthResponseData['Email'], + '3d_all' => $raw3DAuthResponseData, + ]; + + return (object) array_merge($threeDResponse, $paymentResponseData); + } + + /** + * @inheritDoc + */ + protected function map3DPayResponseData($raw3DAuthResponseData) + { + $status = 'declined'; + + if ($this->check3DHash($raw3DAuthResponseData) && $raw3DAuthResponseData['ProcReturnCode'] === '00') { + if (in_array($raw3DAuthResponseData['mdStatus'], [1, 2, 3, 4])) { + $status = 'approved'; + } + } + + $transactionSecurity = 'MPI fallback'; + if ('approved' === $status) { + if ($raw3DAuthResponseData['mdStatus'] == '1') { + $transactionSecurity = 'Full 3D Secure'; + } elseif (in_array($raw3DAuthResponseData['mdStatus'], [2, 3, 4])) { + $transactionSecurity = 'Half 3D Secure'; + } + } + + return (object) [ + 'id' => $raw3DAuthResponseData['AuthCode'], + 'trans_id' => $raw3DAuthResponseData['TransId'], + 'auth_code' => $raw3DAuthResponseData['AuthCode'], + 'host_ref_num' => $raw3DAuthResponseData['HostRefNum'], + 'response' => $raw3DAuthResponseData['Response'], + 'order_id' => $raw3DAuthResponseData['oid'], + 'transaction_type' => $this->type, + 'transaction' => $this->type, + 'transaction_security' => $transactionSecurity, + 'code' => $raw3DAuthResponseData['ProcReturnCode'], + 'md_status' => $raw3DAuthResponseData['mdStatus'], + 'status' => $status, + 'status_detail' => isset($this->codes[$raw3DAuthResponseData['ProcReturnCode']]) ? $raw3DAuthResponseData['ProcReturnCode'] : null, + 'hash' => $raw3DAuthResponseData['HASH'], + 'rand' => $raw3DAuthResponseData['rnd'], + 'hash_params' => $raw3DAuthResponseData['HASHPARAMS'], + 'hash_params_val' => $raw3DAuthResponseData['HASHPARAMSVAL'], + 'masked_number' => $raw3DAuthResponseData['maskedCreditCard'], + 'month' => $raw3DAuthResponseData['Ecom_Payment_Card_ExpDate_Month'], + 'year' => $raw3DAuthResponseData['Ecom_Payment_Card_ExpDate_Year'], + 'amount' => $raw3DAuthResponseData['amount'], + 'currency' => $raw3DAuthResponseData['currency'], + 'tx_status' => $raw3DAuthResponseData['txstatus'], + 'eci' => $raw3DAuthResponseData['eci'], + 'cavv' => $raw3DAuthResponseData['cavv'], + 'xid' => $raw3DAuthResponseData['xid'], + 'error_code' => $raw3DAuthResponseData['ErrCode'], + 'error_message' => $raw3DAuthResponseData['ErrMsg'], + 'md_error_message' => $raw3DAuthResponseData['mdErrorMsg'], + 'name' => $raw3DAuthResponseData['firmaadi'], + 'email' => $raw3DAuthResponseData['Email'], + 'campaign_url' => null, + 'extra' => $raw3DAuthResponseData['Extra'], + 'all' => $raw3DAuthResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapRefundResponse($rawResponseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + return (object) [ + 'order_id' => isset($rawResponseData->OrderId) ? $rawResponseData->OrderId : null, + 'group_id' => isset($rawResponseData->GroupId) ? $rawResponseData->GroupId : null, + 'response' => isset($rawResponseData->Response) ? $rawResponseData->Response : null, + 'auth_code' => isset($rawResponseData->AuthCode) ? $rawResponseData->AuthCode : null, + 'host_ref_num' => isset($rawResponseData->HostRefNum) ? $rawResponseData->HostRefNum : null, + 'proc_return_code' => isset($rawResponseData->ProcReturnCode) ? $rawResponseData->ProcReturnCode : null, + 'trans_id' => isset($rawResponseData->TransId) ? $rawResponseData->TransId : null, + 'error_code' => isset($rawResponseData->Extra->ERRORCODE) ? $rawResponseData->Extra->ERRORCODE : null, + 'error_message' => isset($rawResponseData->ErrMsg) ? $rawResponseData->ErrMsg : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapCancelResponse($rawResponseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + $this->response = (object) [ + 'order_id' => isset($rawResponseData->OrderId) ? $rawResponseData->OrderId : null, + 'group_id' => isset($rawResponseData->GroupId) ? $rawResponseData->GroupId : null, + 'response' => isset($rawResponseData->Response) ? $rawResponseData->Response : null, + 'auth_code' => isset($rawResponseData->AuthCode) ? $rawResponseData->AuthCode : null, + 'host_ref_num' => isset($rawResponseData->HostRefNum) ? $rawResponseData->HostRefNum : null, + 'proc_return_code' => isset($rawResponseData->ProcReturnCode) ? $rawResponseData->ProcReturnCode : null, + 'trans_id' => isset($rawResponseData->TransId) ? $rawResponseData->TransId : null, + 'error_code' => isset($rawResponseData->Extra->ERRORCODE) ? $rawResponseData->Extra->ERRORCODE : null, + 'error_message' => isset($rawResponseData->ErrMsg) ? $rawResponseData->ErrMsg : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapStatusResponse($rawResponseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + $firstAmount = isset($rawResponseData->Extra->ORIG_TRANS_AMT) ? $this->printData($rawResponseData->Extra->ORIG_TRANS_AMT) : null; + $captureAmount = isset($rawResponseData->Extra->CAPTURE_AMT) ? $this->printData($rawResponseData->Extra->CAPTURE_AMT) : null; + $capture = $firstAmount === $captureAmount ? true : false; + + return (object) [ + 'order_id' => isset($rawResponseData->OrderId) ? $this->printData($rawResponseData->OrderId) : null, + 'response' => isset($rawResponseData->Response) ? $this->printData($rawResponseData->Response) : null, + 'proc_return_code' => isset($rawResponseData->ProcReturnCode) ? $this->printData($rawResponseData->ProcReturnCode) : null, + 'trans_id' => isset($rawResponseData->TransId) ? $this->printData($rawResponseData->TransId) : null, + 'error_message' => isset($rawResponseData->ErrMsg) ? $this->printData($rawResponseData->ErrMsg) : null, + 'host_ref_num' => isset($rawResponseData->Extra->HOST_REF_NUM) ? $this->printData($rawResponseData->Extra->HOST_REF_NUM) : null, + 'order_status' => isset($rawResponseData->Extra->ORDERSTATUS) ? $this->printData($rawResponseData->Extra->ORDERSTATUS) : null, + 'process_type' => isset($rawResponseData->Extra->CHARGE_TYPE_CD) ? $this->printData($rawResponseData->Extra->CHARGE_TYPE_CD) : null, + 'pan' => isset($rawResponseData->Extra->PAN) ? $this->printData($rawResponseData->Extra->PAN) : null, + 'num_code' => isset($rawResponseData->Extra->NUMCODE) ? $this->printData($rawResponseData->Extra->NUMCODE) : null, + 'first_amount' => $firstAmount, + 'capture_amount' => $captureAmount, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'capture' => $capture, + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapPaymentResponse($responseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + return [ + 'id' => isset($responseData->AuthCode) ? $this->printData($responseData->AuthCode) : null, + 'order_id' => isset($responseData->OrderId) ? $this->printData($responseData->OrderId) : null, + 'group_id' => isset($responseData->GroupId) ? $this->printData($responseData->GroupId) : null, + 'trans_id' => isset($responseData->TransId) ? $this->printData($responseData->TransId) : null, + 'response' => isset($responseData->Response) ? $this->printData($responseData->Response) : null, + 'transaction_type' => $this->type, + 'transaction' => $this->type, + 'auth_code' => isset($responseData->AuthCode) ? $this->printData($responseData->AuthCode) : null, + 'host_ref_num' => isset($responseData->HostRefNum) ? $this->printData($responseData->HostRefNum) : null, + 'proc_return_code' => isset($responseData->ProcReturnCode) ? $this->printData($responseData->ProcReturnCode) : null, + 'code' => isset($responseData->ProcReturnCode) ? $this->printData($responseData->ProcReturnCode) : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => isset($responseData->Extra->ERRORCODE) ? $this->printData($responseData->Extra->ERRORCODE) : null, + 'error_message' => isset($responseData->Extra->ERRORCODE) ? $this->printData($responseData->ErrMsg) : null, + 'campaign_url' => null, + 'extra' => isset($responseData->Extra) ? $responseData->Extra : null, + 'all' => $responseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapHistoryResponse($rawResponseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + return (object) [ + 'order_id' => isset($rawResponseData->OrderId) ? $this->printData($rawResponseData->OrderId) : null, + 'response' => isset($rawResponseData->Response) ? $this->printData($rawResponseData->Response) : null, + 'proc_return_code' => isset($rawResponseData->ProcReturnCode) ? $this->printData($rawResponseData->ProcReturnCode) : null, + 'error_message' => isset($rawResponseData->ErrMsg) ? $this->printData($rawResponseData->ErrMsg) : null, + 'num_code' => isset($rawResponseData->Extra->NUMCODE) ? $this->printData($rawResponseData->Extra->NUMCODE) : null, + 'trans_count' => isset($rawResponseData->Extra->TRXCOUNT) ? $this->printData($rawResponseData->Extra->TRXCOUNT) : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function preparePaymentOrder(array $order) + { + // Installment + $installment = 0; + if (isset($order['installment']) && $order['installment'] > 1) { + $installment = (int) $order['installment']; + } + + // Order + return (object) array_merge($order, [ + 'installment' => $installment, + 'currency' => $this->mapCurrency($order['currency']), + ]); + } + + /** + * @inheritDoc + */ + protected function preparePostPaymentOrder(array $order) + { + return (object) [ + 'id' => $order['id'], + ]; + } + + /** + * @inheritDoc + */ + protected function prepareStatusOrder(array $order) + { + return (object) $order; + } + + /** + * @inheritDoc + */ + protected function prepareHistoryOrder(array $order) + { + return (object) $order; + } + + /** + * @inheritDoc + */ + protected function prepareCancelOrder(array $order) + { + return (object) $order; + } + + /** + * @inheritDoc + */ + protected function prepareRefundOrder(array $order) + { + return (object) $order; + } +} diff --git a/src/Gateways/GarantiPos.php b/src/Gateways/GarantiPos.php new file mode 100644 index 00000000..39dc31eb --- /dev/null +++ b/src/Gateways/GarantiPos.php @@ -0,0 +1,994 @@ + 'approved', + '01' => 'bank_call', + '02' => 'bank_call', + '05' => 'reject', + '09' => 'try_again', + '12' => 'invalid_transaction', + '28' => 'reject', + '51' => 'insufficient_balance', + '54' => 'expired_card', + '57' => 'does_not_allow_card_holder', + '62' => 'restricted_card', + '77' => 'request_rejected', + '99' => 'general_error', + ]; + + /** + * Transaction Types + * + * @var array + */ + protected $types = [ + self::TX_PAY => 'sales', + self::TX_PRE_PAY => 'preauth', + self::TX_POST_PAY => 'postauth', + self::TX_CANCEL => 'void', + self::TX_REFUND => 'refund', + self::TX_HISTORY => 'orderhistoryinq', + self::TX_STATUS => 'orderinq', + ]; + + + /** + * currency mapping + * + * @var array + */ + protected $currencies = [ + 'TRY' => 949, + 'USD' => 840, + 'EUR' => 978, + 'GBP' => 826, + 'JPY' => 392, + 'RUB' => 643, + ]; + + /** + * @var GarantiPosAccount + */ + protected $account; + + /** + * @var CreditCardGarantiPos + */ + protected $card; + + /** + * GarantiPost constructor. + * + * @param array $config + * @param GarantiPosAccount $account + * @param array $currencies + */ + public function __construct($config, $account, array $currencies = []) + { + parent::__construct($config, $account, $currencies); + } + + /** + * @return GarantiPosAccount + */ + public function getAccount() + { + return $this->account; + } + + + /** + * @return CreditCardGarantiPos|null + */ + public function getCard() + { + return $this->card; + } + + /** + * @inheritDoc + */ + public function createXML(array $data, $encoding = 'UTF-8'): string + { + return parent::createXML(['GVPSRequest' => $data], $encoding); + } + + /** + * @inheritDoc + */ + public function make3DPayment() + { + $request = Request::createFromGlobals(); + //TODO hash check + if (in_array($request->get('mdstatus'), [1, 2, 3, 4])) { + $contents = $this->create3DPaymentXML($request->request->all()); + $this->send($contents); + } + + $this->response = $this->map3DPaymentData($request->request->all(), $this->data); + + return $this; + } + + /** + * @inheritDoc + */ + public function make3DPayPayment() + { + $request = Request::createFromGlobals(); + + $this->response = $this->map3DPayResponseData($request->request->all()); + + return $this; + } + + + /** + * @inheritDoc + */ + public function history(array $meta) + { + $xml = $this->createHistoryXML($meta); + + $this->send($xml); + + $this->response = $this->mapHistoryResponse($this->data); + + return $this; + } + + /** + * @inheritDoc + */ + public function send($contents) + { + $client = new Client(); + + $response = $client->request('POST', $this->getApiURL(), [ + 'body' => $contents, + ]); + + $this->data = $this->XMLStringToObject($response->getBody()->getContents()); + + return $this; + } + + /** + * @inheritDoc + */ + public function get3DFormData() + { + $hashData = $this->create3DHash(); + + $inputs = [ + 'secure3dsecuritylevel' => $this->account->getModel() === '3d_pay' ? '3D_PAY' : '3D', + 'mode' => $this->getMode(), + 'apiversion' => self::API_VERSION, + 'terminalprovuserid' => $this->account->getUsername(), + 'terminaluserid' => $this->account->getUsername(), + 'terminalmerchantid' => $this->account->getClientId(), + 'txntype' => $this->type, + 'txnamount' => $this->order->amount, + 'txncurrencycode' => $this->order->currency, + 'txninstallmentcount' => $this->order->installment, + 'orderid' => $this->order->id, + 'terminalid' => $this->account->getTerminalId(), + 'successurl' => $this->order->success_url, + 'errorurl' => $this->order->fail_url, + 'customeremailaddress' => isset($this->order->email) ? $this->order->email : null, + 'customeripaddress' => $this->order->ip, + 'cardnumber' => $this->card->getNumber(), + 'cardexpiredatemonth' => $this->card->getExpireMonth(), + 'cardexpiredateyear' => $this->card->getExpireYear(), + 'cardcvv2' => $this->card->getCvv(), + 'secure3dhash' => $hashData, + ]; + + return [ + 'gateway' => $this->get3DGatewayURL(), + 'inputs' => $inputs, + ]; + } + + /** + * TODO + * @inheritDoc + */ + public function make3DHostPayment() + { + throw new NotImplementedException(); + } + + /** + * @inheritDoc + */ + public function createRegularPaymentXML() + { + $requestData = [ + 'Mode' => $this->getMode(), + 'Version' => self::API_VERSION, + 'Terminal' => [ + 'ProvUserID' => $this->account->getUsername(), + 'UserID' => $this->account->getUsername(), + 'HashData' => $this->createHashData(), + 'ID' => $this->account->getTerminalId(), + 'MerchantID' => $this->account->getTerminalId(), + ], + 'Customer' => [ + 'IPAddress' => $this->order->ip, + 'EmailAddress' => $this->order->email, + ], + 'Card' => [ + 'Number' => $this->card->getNumber(), + 'ExpireDate' => $this->card->getExpirationDate(), + 'CVV2' => $this->card->getCvv(), + ], + 'Order' => [ + 'OrderID' => $this->order->id, + 'GroupID' => '', + 'AddressList' => [ + 'Address' => [ + 'Type' => 'S', + 'Name' => $this->order->name, + 'LastName' => '', + 'Company' => '', + 'Text' => '', + 'District' => '', + 'City' => '', + 'PostalCode' => '', + 'Country' => '', + 'PhoneNumber' => '', + ], + ], + ], + 'Transaction' => [ + 'Type' => $this->type, + 'InstallmentCnt' => $this->order->installment, + 'Amount' => $this->order->amount, + 'CurrencyCode' => $this->order->currency, + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + 'Description' => '', + 'OriginalRetrefNum' => '', + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRegularPostXML() + { + $requestData = [ + 'Mode' => $this->getMode(), + 'Version' => self::API_VERSION, + 'Terminal' => [ + 'ProvUserID' => $this->account->getUsername(), + 'UserID' => $this->account->getUsername(), + 'HashData' => $this->createHashData(), + 'ID' => $this->account->getTerminalId(), + 'MerchantID' => $this->account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $this->order->ip, + 'EmailAddress' => $this->order->email, + ], + 'Order' => [ + 'OrderID' => $this->order->id, + ], + 'Transaction' => [ + 'Type' => $this->types[self::TX_POST_PAY], + 'Amount' => $this->order->amount, + 'CurrencyCode' => $this->order->currency, + 'OriginalRetrefNum' => $this->order->ref_ret_num, + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function create3DPaymentXML($responseData) + { + $requestData = [ + 'Mode' => $this->getMode(), + 'Version' => self::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $this->account->getUsername(), + 'UserID' => $this->account->getUsername(), + 'HashData' => $this->createHashData(), + 'ID' => $this->account->getTerminalId(), + 'MerchantID' => $this->account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $responseData['customeripaddress'], + 'EmailAddress' => $responseData['customeremailaddress'], + ], + 'Card' => [ + 'Number' => '', + 'ExpireDate' => '', + 'CVV2' => '', + ], + 'Order' => [ + 'OrderID' => $responseData['orderid'], + 'GroupID' => '', + 'AddressList' => [ + 'Address' => [ + 'Type' => 'B', + 'Name' => $this->order->name, + 'LastName' => '', + 'Company' => '', + 'Text' => '', + 'District' => '', + 'City' => '', + 'PostalCode' => '', + 'Country' => '', + 'PhoneNumber' => '', + ], + ], + ], + 'Transaction' => [ + 'Type' => $responseData['txntype'], + 'InstallmentCnt' => $this->order->installment, + 'Amount' => $responseData['txnamount'], + 'CurrencyCode' => $responseData['txncurrencycode'], + 'CardholderPresentCode' => '13', + 'MotoInd' => 'N', + 'Secure3D' => [ + 'AuthenticationCode' => $responseData['cavv'], + 'SecurityLevel' => $responseData['eci'], + 'TxnID' => $responseData['xid'], + 'Md' => $responseData['md'], + ], + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createCancelXML() + { + $requestData = [ + 'Mode' => $this->getMode(), + 'Version' => self::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $this->account->getRefundUsername(), + 'UserID' => $this->account->getRefundUsername(), + 'HashData' => $this->createHashData(), + 'ID' => $this->account->getTerminalId(), + 'MerchantID' => $this->account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $this->order->ip, + 'EmailAddress' => $this->order->email, + ], + 'Order' => [ + 'OrderID' => $this->order->id, + 'GroupID' => '', + ], + 'Transaction' => [ + 'Type' => $this->types[self::TX_CANCEL], + 'InstallmentCnt' => $this->order->installment, + 'Amount' => $this->order->amount, //TODO we need this field here? + 'CurrencyCode' => $this->order->currency, + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + 'OriginalRetrefNum' => $this->order->ref_ret_num, + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRefundXML() + { + $requestData = [ + 'Mode' => $this->getMode(), + 'Version' => self::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $this->account->getRefundUsername(), + 'UserID' => $this->account->getRefundUsername(), + 'HashData' => $this->createHashData(), + 'ID' => $this->account->getTerminalId(), + 'MerchantID' => $this->account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $this->order->ip, + 'EmailAddress' => $this->order->email, + ], + 'Order' => [ + 'OrderID' => $this->order->id, + 'GroupID' => '', + ], + 'Transaction' => [ + 'Type' => $this->types[self::TX_REFUND], + 'InstallmentCnt' => $this->order->installment, + 'Amount' => $this->order->amount, + 'CurrencyCode' => $this->order->currency, + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + 'OriginalRetrefNum' => $this->order->ref_ret_num, + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createHistoryXML($customQueryData) + { + $requestData = [ + 'Mode' => $this->getMode(), + 'Version' => self::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $this->account->getUsername(), + 'UserID' => $this->account->getUsername(), + 'HashData' => $this->createHashData(), + 'ID' => $this->account->getTerminalId(), + 'MerchantID' => $this->account->getClientId(), + ], + 'Customer' => [ //TODO we need this data? + 'IPAddress' => $this->order->ip, + 'EmailAddress' => $this->order->email, + ], + 'Order' => [ + 'OrderID' => $this->order->id, + 'GroupID' => '', + ], + 'Card' => [ + 'Number' => '', + 'ExpireDate' => '', + 'CVV2' => '', + ], + 'Transaction' => [ + 'Type' => $this->types[self::TX_HISTORY], + 'InstallmentCnt' => $this->order->installment, + 'Amount' => $this->order->amount, + 'CurrencyCode' => $this->order->currency, //TODO we need it? + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createStatusXML() + { + $hashData = $this->createHashData(); + + $requestData = [ + 'Mode' => $this->getMode(), + 'Version' => self::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $this->account->getUsername(), + 'UserID' => $this->account->getUsername(), + 'HashData' => $hashData, + 'ID' => $this->account->getTerminalId(), + 'MerchantID' => $this->account->getClientId(), + ], + 'Customer' => [ //TODO we need this data? + 'IPAddress' => $this->order->ip, + 'EmailAddress' => $this->order->email, + ], + 'Order' => [ + 'OrderID' => $this->order->id, + 'GroupID' => '', + ], + 'Card' => [ + 'Number' => '', + 'ExpireDate' => '', + 'CVV2' => '', + ], + 'Transaction' => [ + 'Type' => $this->types[self::TX_STATUS], + 'InstallmentCnt' => $this->order->installment, + 'Amount' => $this->order->amount, //TODO we need it? + 'CurrencyCode' => $this->order->currency, //TODO we need it? + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + ], + ]; + + return $this->createXML($requestData); + } + + /** + * Make Hash Data + * + * @return string + */ + public function createHashData() + { + $map = [ + $this->order->id, + $this->account->getTerminalId(), + isset($this->card) ? $this->card->getNumber() : null, + $this->order->amount, + $this->createSecurityData(), + ]; + + return strtoupper(sha1(implode('', $map))); + } + + + /** + * Make 3d Hash Data + * + * @return string + */ + public function create3DHash() + { + $map = [ + $this->account->getTerminalId(), + $this->order->id, + $this->order->amount, + $this->order->success_url, + $this->order->fail_url, + $this->type, + $this->order->installment, + $this->account->getStoreKey(), + $this->createSecurityData(), + ]; + + return strtoupper(sha1(implode('', $map))); + } + + /** + * Amount Formatter + * + * @param double $amount + * + * @return int + */ + public static function amountFormat($amount) + { + return round($amount, 2) * 100; + } + + /** + * @return string + */ + protected function getMode() + { + return !$this->isTestMode() ? 'PROD' : 'TEST'; + } + + /** + * @inheritDoc + */ + protected function map3DPaymentData($raw3DAuthResponseData, $rawPaymentResponseData) + { + $status = 'declined'; + $response = 'Declined'; + $procReturnCode = '99'; + $transactionSecurity = 'MPI fallback'; + + if (in_array($raw3DAuthResponseData['mdstatus'], [1, 2, 3, 4])) { + if ($raw3DAuthResponseData['mdstatus'] == '1') { + $transactionSecurity = 'Full 3D Secure'; + } elseif (in_array($raw3DAuthResponseData['mdstatus'], [2, 3, 4])) { + $transactionSecurity = 'Half 3D Secure'; + } + + if ($rawPaymentResponseData->Transaction->Response->ReasonCode === '00') { + $response = 'Approved'; + $procReturnCode = $rawPaymentResponseData->Transaction->Response->ReasonCode; + $status = 'approved'; + } + } + + return (object) [ + 'id' => isset($rawPaymentResponseData->Transaction->AuthCode) ? $this->printData($rawPaymentResponseData->Transaction->AuthCode) : null, + 'order_id' => $raw3DAuthResponseData['oid'], + 'group_id' => isset($rawPaymentResponseData->Transaction->SequenceNum) ? $this->printData($rawPaymentResponseData->Transaction->SequenceNum) : null, + 'auth_code' => isset($rawPaymentResponseData->Transaction->AuthCode) ? $this->printData($rawPaymentResponseData->Transaction->AuthCode) : null, + 'host_ref_num' => isset($rawPaymentResponseData->Transaction->RetrefNum) ? $this->printData($rawPaymentResponseData->Transaction->RetrefNum) : null, + 'ret_ref_num' => isset($rawPaymentResponseData->Transaction->RetrefNum) ? $this->printData($rawPaymentResponseData->Transaction->RetrefNum) : null, + 'batch_num' => isset($rawPaymentResponseData->Transaction->BatchNum) ? $this->printData($rawPaymentResponseData->Transaction->BatchNum) : null, + 'error_code' => isset($rawPaymentResponseData->Transaction->Response->ErrorCode) ? $this->printData($rawPaymentResponseData->Transaction->Response->ErrorCode) : null, + 'error_message' => isset($rawPaymentResponseData->Transaction->Response->ErrorMsg) ? $this->printData($rawPaymentResponseData->Transaction->Response->ErrorMsg) : null, + 'reason_code' => isset($rawPaymentResponseData->Transaction->Response->ReasonCode) ? $this->printData($rawPaymentResponseData->Transaction->Response->ReasonCode) : null, + 'campaign_url' => isset($rawPaymentResponseData->Transaction->CampaignChooseLink) ? $this->printData($rawPaymentResponseData->Transaction->CampaignChooseLink) : null, + 'all' => $rawPaymentResponseData, + 'trans_id' => $raw3DAuthResponseData['transid'], + 'response' => $response, + 'transaction_type' => $this->type, + 'transaction' => $this->type, + 'transaction_security' => $transactionSecurity, + 'proc_return_code' => $procReturnCode, + 'code' => $procReturnCode, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'md_status' => $raw3DAuthResponseData['mdstatus'], + 'rand' => (string) $raw3DAuthResponseData['rnd'], + 'hash' => (string) $raw3DAuthResponseData['secure3dhash'], + 'hash_params' => (string) $raw3DAuthResponseData['hashparams'], + 'hash_params_val' => (string) $raw3DAuthResponseData['hashparamsval'], + 'secure_3d_hash' => (string) $raw3DAuthResponseData['secure3dhash'], + 'secure_3d_level' => (string) $raw3DAuthResponseData['secure3dsecuritylevel'], + 'masked_number' => (string) $raw3DAuthResponseData['MaskedPan'], + 'amount' => (string) $raw3DAuthResponseData['txnamount'], + 'currency' => (string) $raw3DAuthResponseData['txncurrencycode'], + 'tx_status' => (string) $raw3DAuthResponseData['txnstatus'], + 'eci' => (string) $raw3DAuthResponseData['eci'], + 'cavv' => (string) $raw3DAuthResponseData['cavv'], + 'xid' => (string) $raw3DAuthResponseData['xid'], + 'md_error_message' => (string) $raw3DAuthResponseData['mderrormessage'], + //'name' => (string) $raw3DAuthResponseData['firmaadi'], + 'email' => (string) $raw3DAuthResponseData['customeremailaddress'], + 'extra' => null, + '3d_all' => $raw3DAuthResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function map3DPayResponseData($raw3DAuthResponseData) + { + $status = 'declined'; + $response = 'Declined'; + $procReturnCode = $raw3DAuthResponseData['procreturncode']; + + $transactionSecurity = 'MPI fallback'; + if (in_array($raw3DAuthResponseData['mdstatus'], [1, 2, 3, 4])) { + if ($raw3DAuthResponseData['mdstatus'] == '1') { + $transactionSecurity = 'Full 3D Secure'; + } elseif (in_array($raw3DAuthResponseData['mdstatus'], [2, 3, 4])) { + $transactionSecurity = 'Half 3D Secure'; + } + + $status = 'approved'; + $response = 'Approved'; + } + + $this->response = (object) [ + 'id' => (string) $raw3DAuthResponseData['authcode'], + 'order_id' => (string) $raw3DAuthResponseData['oid'], + 'trans_id' => (string) $raw3DAuthResponseData['transid'], + 'auth_code' => (string) $raw3DAuthResponseData['authcode'], + 'host_ref_num' => (string) $raw3DAuthResponseData['hostrefnum'], + 'response' => $response, + 'transaction_type' => $this->type, + 'transaction' => $this->type, + 'transaction_security' => $transactionSecurity, + 'proc_return_code' => $procReturnCode, + 'code' => $procReturnCode, + 'md_status' => $raw3DAuthResponseData['mdStatus'], + 'status' => $status, + 'status_detail' => isset($this->codes[$raw3DAuthResponseData['ProcReturnCode']]) ? (string) $raw3DAuthResponseData['ProcReturnCode'] : null, + 'hash' => (string) $raw3DAuthResponseData['secure3dhash'], + 'rand' => (string) $raw3DAuthResponseData['rnd'], + 'hash_params' => (string) $raw3DAuthResponseData['hashparams'], + 'hash_params_val' => (string) $raw3DAuthResponseData['hashparamsval'], + 'masked_number' => (string) $raw3DAuthResponseData['MaskedPan'], + 'amount' => (string) $raw3DAuthResponseData['txnamount'], + 'currency' => (string) $raw3DAuthResponseData['txncurrencycode'], + 'tx_status' => (string) $raw3DAuthResponseData['txnstatus'], + 'eci' => (string) $raw3DAuthResponseData['eci'], + 'cavv' => (string) $raw3DAuthResponseData['cavv'], + 'xid' => (string) $raw3DAuthResponseData['xid'], + 'error_code' => (string) $raw3DAuthResponseData['errcode'], + 'error_message' => (string) $raw3DAuthResponseData['errmsg'], + 'md_error_message' => (string) $raw3DAuthResponseData['mderrormessage'], + 'campaign_url' => null, + //'name' => (string) $raw3DAuthResponseData['firmaadi'], + 'email' => (string) $raw3DAuthResponseData['customeremailaddress'], + 'extra' => $raw3DAuthResponseData['Extra'], + 'all' => $raw3DAuthResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapPaymentResponse($responseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + return (object) [ + 'id' => isset($responseData->Transaction->AuthCode) ? $this->printData($responseData->Transaction->AuthCode) : null, + 'order_id' => isset($responseData->Order->OrderID) ? $this->printData($responseData->Order->OrderID) : null, + 'group_id' => isset($responseData->Order->GroupID) ? $this->printData($responseData->Order->GroupID) : null, + 'trans_id' => isset($responseData->Transaction->AuthCode) ? $this->printData($responseData->Transaction->AuthCode) : null, + 'response' => isset($responseData->Transaction->Response->Message) ? $this->printData($responseData->Transaction->Response->Message) : null, + 'transaction_type' => $this->type, + 'transaction' => $this->type, + 'auth_code' => isset($responseData->Transaction->AuthCode) ? $this->printData($responseData->Transaction->AuthCode) : null, + 'host_ref_num' => isset($responseData->Transaction->RetrefNum) ? $this->printData($responseData->Transaction->RetrefNum) : null, + 'ret_ref_num' => isset($responseData->Transaction->RetrefNum) ? $this->printData($responseData->Transaction->RetrefNum) : null, + 'hash_data' => isset($responseData->Transaction->HashData) ? $this->printData($responseData->Transaction->HashData) : null, + 'proc_return_code' => $this->getProcReturnCode(), + 'code' => $this->getProcReturnCode(), + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => isset($responseData->Transaction->Response->Code) ? $this->printData($responseData->Transaction->Response->Code) : null, + 'error_message' => isset($responseData->Transaction->Response->ErrorMsg) ? $this->printData($responseData->Transaction->Response->ErrorMsg) : null, + 'campaign_url' => isset($responseData->Transaction->CampaignChooseLink) ? $this->printData($responseData->Transaction->CampaignChooseLink) : null, + 'extra' => isset($responseData->Extra) ? $responseData->Extra : null, + 'all' => $responseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapRefundResponse($rawResponseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + return (object) [ + 'id' => isset($rawResponseData->Transaction->AuthCode) ? $this->printData($rawResponseData->Transaction->AuthCode) : null, + 'order_id' => isset($rawResponseData->Order->OrderID) ? $this->printData($rawResponseData->Order->OrderID) : null, + 'group_id' => isset($rawResponseData->Order->GroupID) ? $this->printData($rawResponseData->Order->GroupID) : null, + 'trans_id' => isset($rawResponseData->Transaction->AuthCode) ? $this->printData($rawResponseData->Transaction->AuthCode) : null, + 'response' => isset($rawResponseData->Transaction->Response->Message) ? $this->printData($rawResponseData->Transaction->Response->Message) : null, + 'auth_code' => isset($rawResponseData->Transaction->AuthCode) ? $rawResponseData->Transaction->AuthCode : null, + 'host_ref_num' => isset($rawResponseData->Transaction->RetrefNum) ? $this->printData($rawResponseData->Transaction->RetrefNum) : null, + 'ret_ref_num' => isset($rawResponseData->Transaction->RetrefNum) ? $this->printData($rawResponseData->Transaction->RetrefNum) : null, + 'hash_data' => isset($rawResponseData->Transaction->HashData) ? $this->printData($rawResponseData->Transaction->HashData) : null, + 'proc_return_code' => $this->getProcReturnCode(), + 'code' => $this->getProcReturnCode(), + 'error_code' => isset($rawResponseData->Transaction->Response->Code) ? $this->printData($rawResponseData->Transaction->Response->Code) : null, + 'error_message' => isset($rawResponseData->Transaction->Response->ErrorMsg) ? $this->printData($rawResponseData->Transaction->Response->ErrorMsg) : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapCancelResponse($rawResponseData) + { + return $this->mapRefundResponse($rawResponseData); + } + + /** + * @inheritDoc + */ + protected function mapStatusResponse($rawResponseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + return (object) [ + 'id' => isset($rawResponseData->Order->OrderInqResult->AuthCode) ? $this->printData($rawResponseData->Order->OrderInqResult->AuthCode) : null, + 'order_id' => isset($rawResponseData->Order->OrderID) ? $this->printData($rawResponseData->Order->OrderID) : null, + 'group_id' => isset($rawResponseData->Order->GroupID) ? $this->printData($rawResponseData->Order->GroupID) : null, + 'trans_id' => isset($rawResponseData->Transaction->AuthCode) ? $this->printData($rawResponseData->Transaction->AuthCode) : null, + 'response' => isset($rawResponseData->Transaction->Response->Message) ? $this->printData($rawResponseData->Transaction->Response->Message) : null, + 'auth_code' => isset($rawResponseData->Order->OrderInqResult->AuthCode) ? $this->printData($rawResponseData->Order->OrderInqResult->AuthCode) : null, + 'host_ref_num' => isset($rawResponseData->Order->OrderInqResult->RetrefNum) ? $this->printData($rawResponseData->Order->OrderInqResult->RetrefNum) : null, + 'ret_ref_num' => isset($rawResponseData->Order->OrderInqResult->RetrefNum) ? $this->printData($rawResponseData->Order->OrderInqResult->RetrefNum) : null, + 'hash_data' => isset($rawResponseData->Transaction->HashData) ? $this->printData($rawResponseData->Transaction->HashData) : null, + 'proc_return_code' => $this->getProcReturnCode(), + 'code' => $this->getProcReturnCode(), + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => isset($rawResponseData->Transaction->Response->Code) ? $this->printData($rawResponseData->Transaction->Response->Code) : null, + 'error_message' => isset($rawResponseData->Transaction->Response->ErrorMsg) ? $this->printData($rawResponseData->Transaction->Response->ErrorMsg) : null, + 'extra' => isset($rawResponseData->Extra) ? $rawResponseData->Extra : null, + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapHistoryResponse($rawResponseData) + { + $status = 'declined'; + if ($this->getProcReturnCode() === '00') { + $status = 'approved'; + } + + return (object) [ + 'id' => isset($rawResponseData->Order->OrderHistInqResult->AuthCode) ? $this->printData($rawResponseData->Order->OrderHistInqResult->AuthCode) : null, + 'order_id' => isset($rawResponseData->Order->OrderID) ? $this->printData($rawResponseData->Order->OrderID) : null, + 'group_id' => isset($rawResponseData->Order->GroupID) ? $this->printData($rawResponseData->Order->GroupID) : null, + 'trans_id' => isset($rawResponseData->Transaction->AuthCode) ? $this->printData($rawResponseData->Transaction->AuthCode) : null, + 'response' => isset($rawResponseData->Transaction->Response->Message) ? $this->printData($rawResponseData->Transaction->Response->Message) : null, + 'auth_code' => isset($rawResponseData->Order->OrderHistInqResult->AuthCode) ? $this->printData($rawResponseData->Order->OrderHistInqResult->AuthCode) : null, + 'host_ref_num' => isset($rawResponseData->Order->OrderHistInqResult->RetrefNum) ? $this->printData($rawResponseData->Order->OrderHistInqResult->RetrefNum) : null, + 'ret_ref_num' => isset($rawResponseData->Order->OrderHistInqResult->RetrefNum) ? $this->printData($rawResponseData->Order->OrderHistInqResult->RetrefNum) : null, + 'hash_data' => isset($rawResponseData->Transaction->HashData) ? $this->printData($rawResponseData->Transaction->HashData) : null, + 'proc_return_code' => $this->getProcReturnCode(), + 'code' => $this->getProcReturnCode(), + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => isset($rawResponseData->Transaction->Response->Code) ? $this->printData($rawResponseData->Transaction->Response->Code) : null, + 'error_message' => isset($rawResponseData->Transaction->Response->ErrorMsg) ? $this->printData($rawResponseData->Transaction->Response->ErrorMsg) : null, + 'extra' => isset($rawResponseData->Extra) ? $rawResponseData->Extra : null, + 'order_txn' => isset($rawResponseData->Order->OrderHistInqResult->OrderTxnList->OrderTxn) ? $rawResponseData->Order->OrderHistInqResult->OrderTxnList->OrderTxn : [], + 'all' => $rawResponseData, + ]; + } + + /** + * Get ProcReturnCode + * + * @return string|null + */ + protected function getProcReturnCode() + { + return isset($this->data->Transaction->Response->Code) ? (string) $this->data->Transaction->Response->Code : null; + } + + /** + * Get Status Detail Text + * + * @return string|null + */ + protected function getStatusDetail() + { + $procReturnCode = $this->getProcReturnCode(); + + return $procReturnCode ? (isset($this->codes[$procReturnCode]) ? (string) $this->codes[$procReturnCode] : null) : null; + } + + /** + * @inheritDoc + */ + protected function preparePaymentOrder(array $order) + { + // Installment + $installment = ''; + if (isset($order['installment']) && $order['installment'] > 1) { + $installment = $order['installment']; + } + + // Order + return (object) array_merge($order, [ + 'installment' => $installment, + 'currency' => $this->mapCurrency($order['currency']), + 'amount' => self::amountFormat($order['amount']), + 'ip' => isset($order['ip']) ? $order['ip'] : '', + 'email' => isset($order['email']) ? $order['email'] : '', + ]); + } + + /** + * @inheritDoc + */ + protected function preparePostPaymentOrder(array $order) + { + return (object) [ + 'id' => $order['id'], + 'ref_ret_num' => $order['ref_ret_num'], + 'currency' => $this->mapCurrency($order['currency']), + 'amount' => self::amountFormat($order['amount']), + 'ip' => isset($order['ip']) ? $order['ip'] : '', + 'email' => isset($order['email']) ? $order['email'] : '', + ]; + } + + /** + * @inheritDoc + */ + protected function prepareStatusOrder(array $order) + { + return (object) [ + 'id' => $order['id'], + 'amount' => self::amountFormat(1), + 'currency' => $this->mapCurrency($order['currency']), + 'ip' => isset($order['ip']) ? $order['ip'] : '', + 'email' => isset($order['email']) ? $order['email'] : '', + 'installment' => '', + ]; + } + + /** + * @inheritDoc + */ + protected function prepareHistoryOrder(array $order) + { + return $this->prepareStatusOrder($order); + } + + /** + * @inheritDoc + */ + protected function prepareCancelOrder(array $order) + { + return (object) [ + 'id' => $order['id'], + 'amount' => self::amountFormat(1), + 'currency' => $this->mapCurrency($order['currency']), + 'ref_ret_num' => $order['ref_ret_num'], + 'ip' => isset($order['ip']) ? $order['ip'] : '', + 'email' => isset($order['email']) ? $order['email'] : '', + 'installment' => '', + ]; + } + + /** + * @inheritDoc + */ + protected function prepareRefundOrder(array $order) + { + return $this->prepareCancelOrder($order); + } + + /** + * Make Security Data + * @return string + */ + private function createSecurityData() + { + if ($this->type === $this->types[self::TX_REFUND] || $this->type === $this->types[self::TX_CANCEL]) { + $password = $this->account->getRefundPassword(); + } else { + $password = $this->account->getPassword(); + } + + $map = [ + $password, + str_pad((int) $this->account->getTerminalId(), 9, 0, STR_PAD_LEFT), + ]; + + return strtoupper(sha1(implode('', $map))); + } +} diff --git a/src/PayForPos.php b/src/Gateways/PayForPos.php similarity index 62% rename from src/PayForPos.php rename to src/Gateways/PayForPos.php index c3f69628..a9c99184 100644 --- a/src/PayForPos.php +++ b/src/Gateways/PayForPos.php @@ -1,27 +1,19 @@ 'invalid_credentials', ]; - private $config; - private $account; - /** * Transaction Types * * @var array */ - private $types = [ - 'pay' => 'Auth', - 'pre' => 'PreAuth', - 'post' => 'PostAuth', - 'cancel' => 'Void', - 'refund' => 'Refund', - 'history' => 'TxnHistory', - 'status' => 'OrderInquiry', + protected $types = [ + self::TX_PAY => 'Auth', + self::TX_PRE_PAY => 'PreAuth', + self::TX_POST_PAY => 'PostAuth', + self::TX_CANCEL => 'Void', + self::TX_REFUND => 'Refund', + self::TX_HISTORY => 'TxnHistory', + self::TX_STATUS => 'OrderInquiry', ]; /** - * Transaction Type + * currency mapping * - * @var string - */ - private $type; - - /** * @var array */ - private $currencies; - - /** - * @var object - */ - private $order; - - /** - * @var CreditCardPayFor - */ - private $card; + protected $currencies = [ + 'TRY' => 949, + 'USD' => 840, + 'EUR' => 978, + 'GBP' => 826, + 'JPY' => 392, + 'RUB' => 643, + ]; /** - * Processed Response Data + * @inheritDoc * - * @var object + * @param PayForAccount $account */ - private $response; - - public function __construct($config, $account, array $currencies) { - $this->config = $config; - $this->account = $account; - - if(!isset($this->account->lang)) $this->account->lang = self::LANG_TR; - - $this->currencies = $currencies; - - $this->url = isset($this->config['urls'][$this->account->env]) ? - $this->config['urls'][$this->account->env] : - $this->config['urls']['production']; - - $this->gateway = isset($this->config['urls']['gateway'][$this->account->env]) ? - $this->config['urls']['gateway'][$this->account->env] : - $this->config['urls']['gateway']['production']; - - $this->gateway3DHost = isset($this->config['urls']['gateway_3d_host'][$this->account->env]) ? - $this->config['urls']['gateway_3d_host'][$this->account->env] : - $this->config['urls']['gateway_3d_host']['production']; - } - - /** - * @return mixed - */ - public function getResponse() - { - return $this->response; - - } - - /** - * @return array - */ - public function getCurrencies() - { - return $this->currencies; + parent::__construct($config, $account, $currencies); } /** - * @return array - */ - public function getConfig() - { - return $this->config; - } - - /** - * @return mixed + * @return PayForAccount */ public function getAccount() { @@ -173,15 +112,7 @@ public function getAccount() } /** - * @return mixed - */ - public function getOrder() - { - return $this->order; - } - - /** - * @return CreditCardPayFor|null + * @return CreditCardPayFor */ public function getCard() { @@ -189,65 +120,7 @@ public function getCard() } /** - * @param AbstractCreditCard $card - * - * @return $this - * - * @throws GuzzleException - * @throws UnsupportedPaymentModelException - */ - public function payment($card) - { - $this->card = $card; - - $model = 'regular'; - if (isset($this->account->model) && $this->account->model) { - $model = $this->account->model; - } - - if ('regular' === $model) { - $this->makeRegularPayment(); - } elseif ('3d' === $model) { - $this->make3DPayment(); - } elseif ('3d_pay' === $model) { - $this->make3DPayPayment(); - } elseif ('3d_host' === $model) { - $this->make3DHostPayment(); - } else { - throw new UnsupportedPaymentModelException(); - } - - return $this; - } - - /** - * Regular Payment - * - * @return $this - * - * @throws GuzzleException - */ - public function makeRegularPayment() - { - $contents = ''; - if (in_array($this->order->transaction, ['pay', 'pre'])) { - $contents = $this->createRegularPaymentXML(); - } elseif ('post' === $this->order->transaction) { - $contents = $this->createRegularPostXML(); - } - - $this->send($contents); - - $this->response = (object) $this->mapPaymentResponse($this->data); - - return $this; - } - - /** - * Make 3D Payment - * @return $this - * - * @throws GuzzleException + * @inheritDoc */ public function make3DPayment() { @@ -266,9 +139,7 @@ public function make3DPayment() } /** - * Just returns formatted data of 3dPay payment response - * - * @return $this + * @inheritDoc */ public function make3DPayPayment() { @@ -280,9 +151,7 @@ public function make3DPayPayment() } /** - * Just returns formatted data of host payment response - * - * @return $this + * @inheritDoc */ public function make3DHostPayment() { @@ -297,91 +166,36 @@ public function make3DHostPayment() * Warning: You can not use refund for purchases made at the same date. * Instead, you need to use cancel. * - * @param array $meta - * - * @return $this - * - * @throws GuzzleException - */ - public function refund(array $meta) - { - $xml = $this->createRefundXML(); - $this->send($xml); - - $this->response = $this->mapRefundResponse($this->data); - - return $this; - } - - /** - * Cancel Order - * - * @param array $meta - * - * @return $this - * - * @throws GuzzleException - */ - public function cancel(array $meta) - { - $xml = $this->createCancelXML(); - $this->send($xml); - - $this->response = $this->mapCancelResponse($this->data); - - return $this; - } - - /** - * Order Status - * - * @param array $meta - * - * @return $this - * - * @throws GuzzleException + * @inheritDoc */ - public function status(array $meta) + public function refund() { - $xml = $this->createOrderStatusXML(); - - $this->send($xml); - - $this->response = $this->mapStatusResponse($this->data); - - return $this; + return parent::refund(); } /** * Fetches All Transaction/Action/Order history, both failed and successful, for the given date ReqDate - * or single order if orderId is given + * or transactions related to the queried order if orderId is given * Note: history request to gateway returns JSON response + * If both reqDate and orderId provided then finansbank will take into account only orderId * - * @param array $meta - * - * @return $this - * - * @throws GuzzleException + * returns list array or items for the given date, + * if orderId specified in request then return array of transactions (refund|pre|post|cancel) + * both successful and failed, for the related orderId + * @inheritDoc */ public function history(array $meta) { - $xml = $this->createHistoryXML($meta); - - $this->send($xml); - - //returns list array or items, if orderId specified in request then return array with single item - $this->response = (array) $this->data; - - return $this; + return parent::history($meta); } /** - * returns data needed for 3d, 3d_pay and 3d_host models + * returns form data needed for 3d, 3d_pay and 3d_host models * * @return array */ - public function get3dFormData() + public function get3DFormData() { if (!$this->order) { return []; @@ -389,58 +203,33 @@ public function get3dFormData() $this->order->hash = $this->create3DHash(); - if ('3d_pay' === $this->account->model) { + if ('3d_pay' === $this->account->getModel()) { $formData = $this->getCommon3DFormData(); $formData['inputs']['SecureType'] = '3DPay'; - $formData['gateway'] = $this->gateway; - } elseif ('3d' === $this->account->model) { + $formData['gateway'] = $this->get3DGatewayURL(); + } elseif ('3d' === $this->account->getModel()) { $formData = $this->getCommon3DFormData(); $formData['inputs']['SecureType'] = '3DModel'; - $formData['gateway'] = $this->gateway; + $formData['gateway'] = $this->get3DGatewayURL(); } else { $formData = $this->getCommon3DFormData(); $formData['inputs']['SecureType'] = '3DHost'; - $formData['gateway'] = $this->gateway3DHost; + $formData['gateway'] = $this->get3DHostGatewayURL(); } return $formData; } - /** - * @param object $order - * @param CreditCardPayFor $card - * - * @return void - * - * @throws UnsupportedTransactionTypeException - */ - public function prepare($order, $card = null) - { - $this->type = $this->types['pay']; - if (isset($order->transaction)) { - if (array_key_exists($order->transaction, $this->types)) { - $this->type = $this->types[$order->transaction]; - } else { - throw new UnsupportedTransactionTypeException('Unsupported transaction type!'); - } - } - $this->order = $order; - $this->card = $card; - } /** - * @param $postData - * - * @return $this|PayForPos - * - * @throws GuzzleException + * @inheritDoc */ public function send($postData) { $client = new Client(); - $response = $client->request('POST', $this->url, [ + $response = $client->request('POST', $this->getApiURL(), [ 'body' => $postData, ]); @@ -472,7 +261,7 @@ public function send($postData) */ public function createXML(array $data, $encoding = 'UTF-8'): string { - return $this->traitCreateXML(['PayforRequest' => $data], $encoding); + return parent::createXML(['PayforRequest' => $data], $encoding); } @@ -485,9 +274,9 @@ public function create3DHash() . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->type . $this->order->installment . $this->order->rand - . $this->account->store_key; + . $this->account->getStoreKey(); - return base64_encode(pack('H*', sha1($hashStr))); + return base64_encode(sha1($hashStr, true)); } /** @@ -500,21 +289,168 @@ public function create3DHash() public function check3DHash($data) { - $hashStr = $this->account->client_id . $this->account->store_key + $hashStr = $this->account->getClientId() . $this->account->getStoreKey() . $data['OrderId'] . $data['AuthCode'] . $data['ProcReturnCode'] . $data['3DStatus'] - . $data['ResponseRnd'] . $this->account->username; + . $data['ResponseRnd'] . $this->account->getUsername(); - $hash = base64_encode(pack('H*', sha1($hashStr))); + $hash = base64_encode(sha1($hashStr, true)); return $hash === $data['ResponseHash']; } /** - * @param array $raw3DAuthResponseData - * @param object $rawPaymentResponseData - * - * @return object + * @inheritDoc + */ + public function createRegularPaymentXML() + { + $requestData = [ + 'MbrId' => self::MBR_ID, + 'MerchantId' => $this->account->getClientId(), + 'UserCode' => $this->account->getUsername(), + 'UserPass' => $this->account->getPassword(), + 'MOTO' => self::MOTO, + 'OrderId' => $this->order->id, + 'SecureType' => 'NonSecure', + 'TxnType' => $this->type, + 'PurchAmount' => $this->order->amount, + 'Currency' => $this->order->currency, + 'InstallmentCount' => $this->order->installment, + 'Lang' => $this->getLang(), + 'CardHolderName' => $this->card->getHolderName(), + 'Pan' => $this->card->getNumber(), + 'Expiry' => $this->card->getExpirationDate(), + 'Cvv2' => $this->card->getCvv(), + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRegularPostXML() + { + $requestData = [ + 'MbrId' => self::MBR_ID, + 'MerchantId' => $this->account->getClientId(), + 'UserCode' => $this->account->getUsername(), + 'UserPass' => $this->account->getPassword(), + 'OrgOrderId' => $this->order->id, + 'SecureType' => 'NonSecure', + 'TxnType' => $this->type, + 'PurchAmount' => $this->order->amount, + 'Currency' => $this->order->currency, + 'Lang' => $this->getLang(), + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function create3DPaymentXML($responseData) + { + $requestData = [ + 'RequestGuid' => $responseData['RequestGuid'], + 'UserCode' => $this->account->getUsername(), + 'UserPass' => $this->account->getPassword(), + 'OrderId' => $this->order->id, + 'SecureType' => '3DModelPayment', + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createStatusXML() + { + $requestData = [ + 'MbrId' => self::MBR_ID, + 'MerchantId' => $this->account->getClientId(), + 'UserCode' => $this->account->getUsername(), + 'UserPass' => $this->account->getPassword(), + 'OrgOrderId' => $this->order->id, + 'SecureType' => 'Inquiry', + 'Lang' => $this->getLang(), + 'TxnType' => $this->types[self::TX_STATUS], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createHistoryXML($customQueryData) + { + $requestData = [ + 'MbrId' => self::MBR_ID, + 'MerchantId' => $this->account->getClientId(), + 'UserCode' => $this->account->getUsername(), + 'UserPass' => $this->account->getPassword(), + 'SecureType' => 'Report', + 'TxnType' => $this->types[self::TX_HISTORY], + 'Lang' => $this->getLang(), + ]; + + if (isset($customQueryData['orderId'])) { + $requestData['OrderId'] = $customQueryData['orderId']; + } elseif (isset($customQueryData['reqDate'])) { + //ReqData YYYYMMDD format + $requestData['ReqDate'] = $customQueryData['reqDate']; + } + + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRefundXML() + { + $requestData = [ + 'MbrId' => self::MBR_ID, + 'MerchantId' => $this->account->getClientId(), + 'UserCode' => $this->account->getUsername(), + 'UserPass' => $this->account->getPassword(), + 'SecureType' => 'NonSecure', + 'Lang' => $this->getLang(), + 'OrgOrderId' => $this->order->id, + 'TxnType' => $this->types[self::TX_REFUND], + 'PurchAmount' => $this->order->amount, + 'Currency' => $this->order->currency, + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createCancelXML() + { + $requestData = [ + 'MbrId' => self::MBR_ID, + 'MerchantId' => $this->account->getClientId(), + 'UserCode' => $this->account->getUsername(), + 'UserPass' => $this->account->getPassword(), + 'OrgOrderId' => $this->order->id, + 'SecureType' => 'NonSecure', + 'TxnType' => $this->types[self::TX_CANCEL], + 'Currency' => $this->order->currency, + 'Lang' => $this->getLang(), + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc */ protected function map3DPaymentData($raw3DAuthResponseData, $rawPaymentResponseData) { @@ -547,9 +483,7 @@ protected function map3DPaymentData($raw3DAuthResponseData, $rawPaymentResponseD } /** - * @param array $raw3DAuthResponseData - * - * @return object + * @inheritDoc */ protected function map3DPayResponseData($raw3DAuthResponseData) { @@ -568,7 +502,7 @@ protected function map3DPayResponseData($raw3DAuthResponseData) 'error_code' => ('approved' !== $status) ? $raw3DAuthResponseData['ProcReturnCode'] : null, 'error_message' => ('approved' !== $status) ? $raw3DAuthResponseData['ErrMsg'] : null, 'transaction_type' => array_search($raw3DAuthResponseData['TxnType'], $this->types, true), - 'transaction' => $this->order->transaction, + 'transaction' => $this->type, ]; return (object) array_merge($threeDResponse, $this->map3DCommonResponseData($raw3DAuthResponseData)); @@ -604,33 +538,18 @@ protected function map3DCommonResponseData($raw3DAuthResponseData) } /** - * @param $rawResponseData - * - * @return object + * @inheritDoc */ protected function mapRefundResponse($rawResponseData) { - - $status = 'declined'; - if ('00' === $rawResponseData->ProcReturnCode) { - $status = 'approved'; - } - - return (object) [ - 'order_id' => isset($rawResponseData->TransId) ? $rawResponseData->TransId : null, - 'auth_code' => ('declined' !== $status) ? $rawResponseData->AuthCode : null, - 'host_ref_num' => isset($rawResponseData->HostRefNum) ? $rawResponseData->HostRefNum : null, - 'proc_return_code' => isset($rawResponseData->ProcReturnCode) ? $rawResponseData->ProcReturnCode : null, - 'trans_id' => isset($rawResponseData->TransId) ? $rawResponseData->TransId : null, - 'error_code' => ('declined' === $status) ? $rawResponseData->ProcReturnCode : null, - 'error_message' => ('declined' === $status) ? $rawResponseData->ErrMsg : null, - 'status' => $status, - 'status_detail' => isset($this->codes[$rawResponseData->ProcReturnCode]) ? $this->codes[$rawResponseData->ProcReturnCode] : null, - 'all' => $rawResponseData, - ]; + return $this->mapCancelResponse($rawResponseData); } - protected function mapCancelResponse($rawResponseData){ + /** + * @inheritDoc + */ + protected function mapCancelResponse($rawResponseData) + { $status = 'declined'; if ('00' === $rawResponseData->ProcReturnCode) { @@ -651,13 +570,8 @@ protected function mapCancelResponse($rawResponseData){ ]; } - /** - * Processes payment response data - * - * @param object $responseData - * - * @return array + * @inheritDoc */ protected function mapPaymentResponse($responseData) { @@ -685,9 +599,7 @@ protected function mapPaymentResponse($responseData) } /** - * @param object $rawResponseData - * - * @return object + * @inheritDoc */ protected function mapStatusResponse($rawResponseData) { @@ -748,228 +660,121 @@ protected function getDefaultPaymentResponse() ]; } - /** - * Create Regular Payment XML + * returns common form data used by all 3D payment gates + * @param bool $withCrediCard * - * @return string + * @return array */ - protected function createRegularPaymentXML() + protected function getCommon3DFormData($withCrediCard = false) { - $requestData = [ + $inputs = [ 'MbrId' => self::MBR_ID, - 'MerchantId' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'UserPass' => $this->account->password, - 'MOTO' => self::MOTO, + 'MerchantID' => $this->account->getClientId(), + 'UserCode' => $this->account->getUsername(), 'OrderId' => $this->order->id, - 'SecureType' => 'NonSecure', + 'Lang' => $this->getLang(), + 'SecureType' => null, //to be filled by the caller 'TxnType' => $this->type, 'PurchAmount' => $this->order->amount, - 'Currency' => $this->order->currency, 'InstallmentCount' => $this->order->installment, - 'Lang' => $this->getLang(), - 'CardHolderName' => $this->card->getHolderName(), - 'Pan' => $this->card->getNumber(), - 'Expiry' => $this->card->getExpirationDate(), - 'Cvv2' => $this->card->getCvv(), + 'Currency' => $this->order->currency, + 'OkUrl' => $this->order->success_url, + 'FailUrl' => $this->order->fail_url, + 'Rnd' => $this->order->rand, + 'Hash' => $this->order->hash, ]; - return $this->createXML($requestData); - } + if ($withCrediCard) { + $inputs['CardHolderName'] = $this->card->getHolderName(); + $inputs['Pan'] = $this->card->getNumber(); + $inputs['Expiry'] = $this->card->getExpirationDate(); + $inputs['Cvv2'] = $this->card->getCvv(); + } - /** - * Create Regular Payment Post XML - * - * @return string - */ - protected function createRegularPostXML() - { - $requestData = [ - 'MbrId' => self::MBR_ID, - 'MerchantId' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'UserPass' => $this->account->password, - 'MOTO' => self::MOTO, - 'OrgOrderId' => $this->order->id, - 'SecureType' => 'NonSecure', - 'TxnType' => $this->type, - 'PurchAmount' => $this->order->amount, - 'Currency' => $this->order->currency, - 'Lang' => $this->getLang(), - 'CardHolderName' => $this->card->getHolderName(), - 'Pan' => $this->card->getNumber(), - 'Expiry' => $this->card->getExpirationDate(), - 'Cvv2' => $this->card->getCvv(), + return [ + 'gateway' => null, //to be filled by the caller + 'inputs' => $inputs, ]; - - return $this->createXML($requestData); } /** - * Creates 3D Payment XML - * @param $responseData - * - * @return string + * @inheritDoc */ - protected function create3DPaymentXML($responseData) + protected function mapHistoryResponse($rawResponseData) { - $requestData = [ - 'RequestGuid' => $responseData['RequestGuid'], - 'UserCode' => $this->account->username, - 'UserPass' => $this->account->password, - 'OrderId' => $this->order->id, - 'SecureType' => '3DModelPayment', - ]; - - return $this->createXML($requestData); + return $rawResponseData; } /** - * Creates XML string for order status inquiry - * - * @return string + * @inheritDoc */ - protected function createOrderStatusXML() + protected function preparePaymentOrder(array $order) { - $requestData = [ - 'MbrId' => self::MBR_ID, - 'MerchantId' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'UserPass' => $this->account->password, - 'OrgOrderId' => $this->order->id, - 'SecureType' => 'Inquiry', - 'Lang' => $this->getLang(), - 'TxnType' => $this->types['status'], - ]; + // Installment + $installment = 0; + if (isset($order['installment']) && $order['installment'] > 1) { + $installment = (int) $order['installment']; + } - return $this->createXML($requestData); + $currency = isset($order['currency']) ? $order['currency'] : 'TRY'; + + // Order + return (object) array_merge($order, [ + 'installment' => $installment, + 'currency' => $this->mapCurrency($currency), + ]); } /** - * Creates XML string for order refund operation - * - * @return string + * @inheritDoc */ - protected function createRefundXML() + protected function preparePostPaymentOrder(array $order) { - $requestData = [ - 'MbrId' => self::MBR_ID, - 'MerchantId' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'UserPass' => $this->account->password, - 'SecureType' => 'NonSecure', - 'Lang' => $this->getLang(), - 'OrgOrderId' => $this->order->id, - 'TxnType' => $this->types['refund'], - 'PurchAmount' => $this->order->amount, - 'Currency' => $this->order->currency, + return (object) [ + 'id' => $order['id'], + 'amount' => $order['amount'], + 'currency' => $this->mapCurrency($order['currency']), ]; - - return $this->createXML($requestData); } /** - * Creates XML string for order cancel operation - * - * @return string + * @inheritDoc */ - protected function createCancelXML() + protected function prepareStatusOrder(array $order) { - $requestData = [ - 'MbrId' => self::MBR_ID, - 'MerchantId' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'UserPass' => $this->account->password, - 'OrgOrderId' => $this->order->id, - 'SecureType' => 'NonSecure', - 'TxnType' => $this->types['cancel'], - 'Currency' => $this->order->currency, - 'Lang' => $this->getLang(), - ]; - - return $this->createXML($requestData); + return (object) $order; } - /** - * Creates XML string for history inquiry - * - * @param array $customQueryData - * - * @return string + * @inheritDoc */ - protected function createHistoryXML($customQueryData) + protected function prepareHistoryOrder(array $order) { - $requestData = [ - 'MbrId' => self::MBR_ID, - 'MerchantId' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'UserPass' => $this->account->password, - 'SecureType' => 'Report', - 'TxnType' => $this->types['history'], - 'Lang' => $this->getLang(), + return (object) [ + //reqDate or order id + 'reqDate' => isset($order['reqDate']) ? $order['reqDate'] : null, + 'id' => isset($order['id']) ? $order['id'] : null, ]; - - if (isset($customQueryData['orderId'])) { - $requestData['OrderId'] = $customQueryData['orderId']; - } elseif (isset($customQueryData['ReqDate'])) { - //ReqData YYYYMMDD format - $requestData['ReqDate'] = $customQueryData['reqDate']; - } - - - return $this->createXML($requestData); } /** - * returns common form data used by all 3D payment gates - * @return array + * @inheritDoc */ - protected function getCommon3DFormData($withCrediCard = false) + protected function prepareCancelOrder(array $order) { - $inputs = [ - 'MbrId' => self::MBR_ID, - 'MerchantID' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'OrderId' => $this->order->id, - 'Lang' => $this->getLang(), - 'SecureType' => null, - 'TxnType' => $this->type, - 'PurchAmount' => $this->order->amount, - 'InstallmentCount' => $this->order->installment, - 'Currency' => $this->order->currency, - 'OkUrl' => $this->order->success_url, - 'FailUrl' => $this->order->fail_url, - 'Rnd' => $this->order->rand, - 'Hash' => $this->order->hash, - ]; - - if ($withCrediCard) { - $inputs['CardHolderName'] = $this->card->getHolderName(); - $inputs['Pan'] = $this->card->getNumber(); - $inputs['Expiry'] = $this->card->getExpirationDate(); - $inputs['Cvv2'] = $this->card->getCvv(); - } + $order['currency'] = $this->mapCurrency($order['currency']); - return [ - 'gateway' => null, - 'inputs' => $inputs, - ]; + return (object) $order; } /** - * bank returns error messages for specified language value - * usually accepted values are tr,en - * @return string + * @inheritDoc */ - private function getLang() + protected function prepareRefundOrder(array $order) { - if ($this->order && isset($this->order->lang)) { - return $this->order->lang; - } + $order['currency'] = $this->mapCurrency($order['currency']); - return $this->account->lang; + return (object) $order; } - } \ No newline at end of file diff --git a/src/Gateways/PosNet.php b/src/Gateways/PosNet.php new file mode 100644 index 00000000..b73f7dca --- /dev/null +++ b/src/Gateways/PosNet.php @@ -0,0 +1,1054 @@ + 'declined', + '1' => 'approved', + '2' => 'declined', + '00' => 'approved', + '0001' => 'bank_call', + '0005' => 'reject', + '0007' => 'bank_call', + '0012' => 'reject', + '0014' => 'reject', + '0030' => 'bank_call', + '0041' => 'reject', + '0043' => 'reject', + '0051' => 'reject', + '0053' => 'bank_call', + '0054' => 'reject', + '0057' => 'reject', + '0058' => 'reject', + '0062' => 'reject', + '0065' => 'reject', + '0091' => 'bank_call', + '0123' => 'transaction_not_found', + '0444' => 'bank_call', + ]; + + /** + * Transaction Types + * + * @var array + */ + protected $types = [ + self::TX_PAY => 'Sale', + self::TX_PRE_PAY => 'Auth', + self::TX_POST_PAY => 'Capt', + self::TX_CANCEL => 'reverse', + self::TX_REFUND => 'return', + self::TX_STATUS => 'agreement', + ]; + + /** + * Fixed Currencies + * @var array + */ + protected $currencies = [ + 'TRY' => 'TL', + 'USD' => 'US', + 'EUR' => 'EU', + 'GBP' => 'GB', + 'JPY' => 'JP', + 'RUB' => 'RU', + ]; + + /** + * API Account + * + * @var PosNetAccount + */ + protected $account = []; + + /** + * @var CreditCardPosNet|null + */ + protected $card; + + /** + * Request + * + * @var Request + */ + protected $request; + + /** + * @var PosNetCrypt|null + */ + public $crypt; + + /** + * PosNet constructor. + * + * @param array $config + * @param PosNetAccount $account + * @param array $currencies + */ + public function __construct($config, $account, array $currencies) + { + $this->crypt = new PosNetCrypt(); + parent::__construct($config, $account, $currencies); + } + + /** + * @inheritDoc + */ + public function createXML(array $data, $encoding = 'ISO-8859-9'): string + { + return parent::createXML(['posnetRequest' => $data], $encoding); + } + + /** + * @inheritDoc + */ + public function make3DHostPayment() + { + throw new NotImplementedException(); + } + + /** + * Get OOS transaction data + * + * @return object + * + * @throws GuzzleException + */ + public function getOosTransactionData() + { + if (null === $this->card->getHolderName() && isset($this->order->name)) { + $this->card->setHolderName($this->order->name); + } + + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + 'oosRequestData' => [ + 'posnetid' => $this->account->getPosNetId(), + 'ccno' => $this->card->getNumber(), + 'expDate' => $this->card->getExpirationDate(), + 'cvc' => $this->card->getCvv(), + 'amount' => $this->order->amount, + 'currencyCode' => $this->order->currency, + 'installment' => $this->order->installment, + 'XID' => self::formatOrderId($this->order->id), + 'cardHolderName' => $this->card->getHolderName(), + 'tranType' => $this->type, + ], + ]; + $xml = $this->createXML($requestData); + + $this->send($xml); + + return $this->data; + } + + /** + * @inheritDoc + */ + public function make3DPayment() + { + $request = Request::createFromGlobals(); + + if ($this->check3DHash($request->request->all())) { + $contents = $this->create3DResolveMerchantDataXML($request->request->all()); + $this->send($contents); + } else { + goto end; + } + + if ($this->getProcReturnCode() !== '00') { + goto end; + } + + if (!$this->verifyResponseMAC($this->data->oosResolveMerchantDataResponse)) { + goto end; + } + + if ($this->getProcReturnCode() === '00' && $this->getStatusDetail() === 'approved') { + //if 3D Authentication is successful: + if (in_array($this->data->oosResolveMerchantDataResponse->mdStatus, [1, 2, 3, 4])) { + $contents = $this->create3DPaymentXML($request->request->all()); + $this->send($contents); + } + } + + end: + $this->response = $this->map3DPaymentData($request->request->all(), $this->data); + + return $this; + } + + /** + * Make 3D Pay Payment + * + * @return $this + */ + public function make3DPayPayment() + { + throw new NotImplementedException(); + } + + /** + * @inheritDoc + */ + public function get3DFormData() + { + $inputs = []; + $data = null; + + if ($this->card && $this->order) { + $data = $this->getOosTransactionData(); + + $inputs = [ + 'posnetData' => $data->oosRequestDataResponse->data1, + 'posnetData2' => $data->oosRequestDataResponse->data2, + 'mid' => $this->account->getClientId(), + 'posnetID' => $this->account->getPosNetId(), + 'digest' => $data->oosRequestDataResponse->sign, + 'vftCode' => isset($this->account->promotion_code) ? $this->account->promotion_code : null, + 'merchantReturnURL' => $this->order->success_url, + 'url' => '', + 'lang' => $this->getLang(), + ]; + } + + return [ + 'gateway' => $this->get3DGatewayURL(), + 'inputs' => $inputs, + ]; + } + + /** + * @inheritDoc + */ + public function send($contents) + { + $client = new Client(); + + $headers = [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ]; + + $response = $client->request('POST', $this->getApiURL(), [ + 'headers' => $headers, + 'body' => "xmldata=$contents", + ]); + + $this->data = $this->XMLStringToObject($response->getBody()->getContents()); + + return $this; + } + + /** + * @return PosNetAccount + */ + public function getAccount() + { + return $this->account; + } + + /** + * @return CreditCardPosNet|null + */ + public function getCard() + { + return $this->card; + } + + /** + * Hash string + * @param string $str + * + * @return string + */ + public function hashString(string $str) + { + return base64_encode(hash('sha256', $str, true)); + } + + /** + * Create 3D Hash (MAC) + * + * @return string + */ + public function create3DHash() + { + $hashStr = ''; + $glue = ';'; + $firstHash = $this->hashString($this->account->getStoreKey().$glue.$this->account->getTerminalId()); + + if ($this->account->getModel() === '3d' || $this->account->getModel() === '3d_pay') { + $hashStr = $this->hashString(implode($glue, [self::formatOrderId($this->order->id), $this->order->amount, $this->order->currency, $this->account->getClientId(), $firstHash])); + } + + return $hashStr; + } + + /** + * verifies the if request came from bank + * + * @param mixed $data oosResolveMerchantDataResponse + * + * @return boolean + */ + public function verifyResponseMAC($data) + { + $hashStr = ''; + $glue = ';'; + $firstHash = $this->hashString($this->account->getStoreKey().$glue.$this->account->getTerminalId()); + + if ($this->account->getModel() === '3d' || $this->account->getModel() === '3d_pay') { + $hashStr = $this->hashString(implode($glue, [$data->mdStatus, self::formatOrderId($this->order->id), $this->order->amount, $this->order->currency, $this->account->getClientId(), $firstHash])); + } + + return $hashStr === $data->mac; + } + + /** + * formats order id by adding 0 pad to the left + * + * @param $orderId + * @param int $padLength + * + * @return string + */ + public static function formatOrderId($orderId, int $padLength = null) + { + if (null === $padLength) { + $padLength = self::ORDER_ID_LENGTH; + } + + return str_pad($orderId, $padLength, '0', STR_PAD_LEFT); + } + + /** + * Get amount + * formats 10.01 to 1001 + * @param float $amount + * + * @return int + */ + public static function formatAmount($amount) + { + return (int) str_replace('.', '', number_format($amount, 2, '.', '')); + } + + /** + * Get PrefixedOrderId + * To check the status of an order or cancel/refund order Yapikredi + * - requires the order length to be 24 + * - and order id prefix which is "TDSC" for 3D payments + * + * @param string $orderId + * @param string $accountModel + * + * @return string + */ + public static function mapOrderIdToPrefixedOrderId(string $orderId, string $accountModel) + { + $prefix = self::ORDER_ID_REGULAR_PREFIX; + if ('3d' === $accountModel) { + $prefix = self::ORDER_ID_3D_PREFIX; + } elseif ('3d_pay' === $accountModel) { + $prefix = self::ORDER_ID_3D_PAY_PREFIX; + } + + return $prefix.self::formatOrderId($orderId, self::ORDER_ID_TOTAL_LENGTH - strlen($prefix)); + } + + + /** + * formats installment in 00, 02, 06 format + * @param int|string $installment + * + * @return string + */ + public static function formatInstallment($installment) + { + if ($installment > 1) { + return str_pad($installment, 2, '0', STR_PAD_LEFT); + } + + return '00'; + } + + /** + * @inheritDoc + */ + public function createRegularPaymentXML() + { + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + 'tranDateRequired' => '1', + strtolower($this->type) => [ + 'orderID' => self::formatOrderId($this->order->id), + 'installment' => $this->order->installment, + 'amount' => $this->order->amount, + 'currencyCode' => $this->order->currency, + 'ccno' => $this->card->getNumber(), + 'expDate' => $this->card->getExpirationDate(), + 'cvc' => $this->card->getCvv(), + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRegularPostXML() + { + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + 'tranDateRequired' => '1', + strtolower($this->types[self::TX_POST_PAY]) => [ + 'hostLogKey' => $this->order->host_ref_num, + 'amount' => $this->order->amount, + 'currencyCode' => $this->order->currency, + 'installment' => $this->order->installment, + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function create3DPaymentXML($responseData) + { + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + 'oosTranData' => [ + 'bankData' => $responseData['BankPacket'], + 'merchantData' => $responseData['MerchantPacket'], + 'sign' => $responseData['Sign'], + 'wpAmount' => 0, + 'mac' => $this->create3DHash(), + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @param $responseData + * + * @return string + */ + public function create3DResolveMerchantDataXML($responseData) + { + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + 'oosResolveMerchantData' => [ + 'bankData' => $responseData['BankPacket'], + 'merchantData' => $responseData['MerchantPacket'], + 'sign' => $responseData['Sign'], + 'mac' => $this->create3DHash(), + ], + ]; + + return $this->createXML($requestData); + } + + + /** + * @inheritDoc + */ + public function createHistoryXML($customQueryData) + { + throw new NotImplementedException(); + } + + + /** + * @inheritDoc + */ + public function createStatusXML() + { + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + $this->types[self::TX_STATUS] => [ + 'orderID' => $this->order->id, + ], + ]; + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createCancelXML() + { + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + 'tranDateRequired' => '1', + $this->types[self::TX_CANCEL] => [ + 'transaction' => 'sale', + ], + ]; + + if (isset($this->order->auth_code)) { + $requestData[$this->types[self::TX_CANCEL]]['authCode'] = $this->order->auth_code; + } + + //either will work + if (isset($this->order->host_ref_num)) { + $requestData[$this->types[self::TX_CANCEL]]['hostLogKey'] = $this->order->host_ref_num; + } else { + $requestData[$this->types[self::TX_CANCEL]]['orderID'] = $this->order->id; + } + + return $this->createXML($requestData); + } + + /** + * @inheritDoc + */ + public function createRefundXML() + { + $requestData = [ + 'mid' => $this->account->getClientId(), + 'tid' => $this->account->getTerminalId(), + 'tranDateRequired' => '1', + $this->types[self::TX_REFUND] => [ + 'amount' => $this->order->amount, + 'currencyCode' => $this->order->currency, + ], + ]; + + if (isset($this->order->host_ref_num)) { + $requestData[$this->types[self::TX_REFUND]]['hostLogKey'] = $this->order->host_ref_num; + } else { + $requestData[$this->types[self::TX_REFUND]]['orderID'] = $this->order->id; + } + + return $this->createXML($requestData); + } + + /** + * Get ProcReturnCode + * + * @return string|null + */ + protected function getProcReturnCode() + { + return (string) $this->data->approved == '1' ? '00' : $this->data->approved; + } + + /** + * Get Status Detail Text + * + * @return string|null + */ + protected function getStatusDetail() + { + $procReturnCode = $this->getProcReturnCode(); + + return isset($this->codes[$procReturnCode]) ? (string) $this->codes[$procReturnCode] : null; + } + + + /** + * Check 3D Hash + * + * @param array $data + * + * @return bool + */ + protected function check3DHash(array $data) + { + if ($this->crypt instanceof PosNetCrypt) { + $decryptedString = $this->crypt->decrypt($data['MerchantPacket'], $this->account->getStoreKey()); + + $decryptedData = explode(';', $decryptedString); + + $originalData = array_map('strval', [ + $this->account->getClientId(), + $this->account->getTerminalId(), + $this->order->amount, + $this->order->installment, + self::formatOrderId($this->order->id), + ]); + + $decryptedDataList = array_map('strval', [ + $decryptedData[0], + $decryptedData[1], + $decryptedData[2], + $decryptedData[3], + $decryptedData[4], + ]); + + return $originalData === $decryptedDataList; + } + + return false; + } + + /** + * @inheritDoc + */ + protected function map3DPaymentData($raw3DAuthResponseData, $rawPaymentResponseData) + { + $status = 'declined'; + $transactionSecurity = ''; + if ($this->getProcReturnCode() === '00' && $this->getStatusDetail() === 'approved') { + if ($rawPaymentResponseData->oosResolveMerchantDataResponse->mdStatus == '1') { + $transactionSecurity = 'Full 3D Secure'; + $status = 'approved'; + } elseif (in_array($rawPaymentResponseData->oosResolveMerchantDataResponse->mdStatus, [2, 3, 4])) { + $transactionSecurity = 'Half 3D Secure'; + $status = 'approved'; + } + } + + if ($rawPaymentResponseData->approved != 1) { + $status = 'declined'; + } + + return (object) [ + 'id' => isset($rawPaymentResponseData->authCode) ? $this->printData($rawPaymentResponseData->authCode) : null, + 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, + 'group_id' => isset($rawPaymentResponseData->groupID) ? $this->printData($rawPaymentResponseData->groupID) : null, + 'trans_id' => isset($rawPaymentResponseData->authCode) ? $this->printData($rawPaymentResponseData->authCode) : null, + 'response' => $this->getStatusDetail(), + 'transaction_type' => $this->type, + 'transaction' => $this->type, + 'transaction_security' => $transactionSecurity, + 'auth_code' => isset($rawPaymentResponseData->authCode) ? $this->printData($rawPaymentResponseData->authCode) : null, + 'host_ref_num' => isset($rawPaymentResponseData->hostlogkey) ? $this->printData($rawPaymentResponseData->hostlogkey) : null, + 'ret_ref_num' => isset($rawPaymentResponseData->transaction->hostlogkey) ? $this->printData($rawPaymentResponseData->transaction->hostlogkey) : null, + 'proc_return_code' => $this->getProcReturnCode(), + 'code' => $this->getProcReturnCode(), + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => !empty($rawPaymentResponseData->respCode) ? $this->printData($rawPaymentResponseData->respCode) : null, + 'error_message' => !empty($rawPaymentResponseData->respText) ? $this->printData($rawPaymentResponseData->respText) : null, + 'md_status' => isset($rawPaymentResponseData->oosResolveMerchantDataResponse->mdStatus) ? $this->printData($rawPaymentResponseData->oosResolveMerchantDataResponse->mdStatus) : null, + 'hash' => [ + 'merchant_packet' => $raw3DAuthResponseData['MerchantPacket'], + 'bank_packet' => $raw3DAuthResponseData['BankPacket'], + 'sign' => $raw3DAuthResponseData['Sign'], + ], + 'xid' => isset($rawPaymentResponseData->oosResolveMerchantDataResponse->xid) ? $rawPaymentResponseData->oosResolveMerchantDataResponse->xid : null, + 'md_error_message' => isset($rawPaymentResponseData->oosResolveMerchantDataResponse->mdErrorMessage) ? $rawPaymentResponseData->oosResolveMerchantDataResponse->mdErrorMessage : null, + 'campaign_url' => null, + 'all' => $rawPaymentResponseData, + '3d_all' => $raw3DAuthResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function map3DPayResponseData($raw3DAuthResponseData) + { + throw new NotImplementedException(); + } + + /** + * @inheritDoc + */ + protected function mapPaymentResponse($responseData) + { + $status = 'declined'; + $code = '1'; + $procReturnCode = '01'; + $errorCode = !empty($responseData->respCode) ? $responseData->respCode : null; + + if ($this->getProcReturnCode() === '00' && $this->getStatusDetail() === 'approved' && $responseData && !$errorCode) { + $status = 'approved'; + $code = isset($responseData->approved) ? $responseData->approved : null; + $procReturnCode = $this->getProcReturnCode(); + } + + return (object) [ + 'id' => isset($responseData->authCode) ? $this->printData($responseData->authCode) : null, + 'order_id' => $this->order->id, + 'fixed_order_id' => self::formatOrderId($this->order->id), + 'group_id' => isset($responseData->groupID) ? $this->printData($responseData->groupID) : null, + 'trans_id' => isset($responseData->authCode) ? $this->printData($responseData->authCode) : null, + 'response' => $this->getStatusDetail(), + 'transaction_type' => $this->type, + 'transaction' => $this->type, + 'auth_code' => isset($responseData->authCode) ? $this->printData($responseData->authCode) : null, + 'host_ref_num' => isset($responseData->hostlogkey) ? $this->printData($responseData->hostlogkey) : null, + 'ret_ref_num' => isset($responseData->hostlogkey) ? $this->printData($responseData->hostlogkey) : null, + 'proc_return_code' => $procReturnCode, + 'code' => $code, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => $errorCode, + 'error_message' => !empty($responseData->respText) ? $this->printData($responseData->respText) : null, + 'campaign_url' => null, + 'extra' => null, + 'all' => $responseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapRefundResponse($rawResponseData) + { + $status = 'declined'; + $code = '1'; + $procReturnCode = '01'; + $errorCode = !empty($rawResponseData->respCode) ? $rawResponseData->respCode : null; + + if ($this->getProcReturnCode() === '00' && $rawResponseData && !$errorCode) { + $status = 'approved'; + $code = isset($rawResponseData->approved) ? $rawResponseData->approved : null; + $procReturnCode = $this->getProcReturnCode(); + } + + $transaction = null; + $transactionType = null; + $state = isset($rawResponseData->state) ? $rawResponseData->state : null; + if ($state === 'Sale') { + $transaction = 'pay'; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Authorization') { + $transaction = 'pre'; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Capture') { + $transaction = 'post'; + $transactionType = $this->types[$transaction]; + } + + return (object) [ + 'id' => isset($rawResponseData->transaction->authCode) ? $this->printData($rawResponseData->transaction->authCode) : null, + 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, + 'fixed_order_id' => isset($rawResponseData->transaction->orderID) ? $this->printData($rawResponseData->transaction->orderID) : null, + 'group_id' => isset($rawResponseData->transaction->groupID) ? $this->printData($rawResponseData->transaction->groupID) : null, + 'trans_id' => isset($rawResponseData->transaction->authCode) ? $this->printData($rawResponseData->transaction->authCode) : null, + 'response' => $this->getStatusDetail(), + 'auth_code' => isset($rawResponseData->transaction->authCode) ? $this->printData($rawResponseData->transaction->authCode) : null, + 'host_ref_num' => isset($rawResponseData->transaction->hostlogkey) ? $this->printData($rawResponseData->transaction->hostlogkey) : null, + 'ret_ref_num' => isset($rawResponseData->transaction->hostlogkey) ? $this->printData($rawResponseData->transaction->hostlogkey) : null, + 'transaction' => $transaction, + 'transaction_type' => $transactionType, + 'state' => $state, + 'date' => isset($rawResponseData->transaction->tranDate) ? $this->printData($rawResponseData->transaction->tranDate) : null, + 'proc_return_code' => $procReturnCode, + 'code' => $code, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => $errorCode, + 'error_message' => !empty($rawResponseData->respText) ? $this->printData($rawResponseData->respText) : null, + 'extra' => null, + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapCancelResponse($rawResponseData) + { + return $this->mapRefundResponse($rawResponseData); + } + + /** + * @inheritDoc + */ + protected function mapStatusResponse($rawResponseData) + { + $status = 'declined'; + $code = '1'; + $procReturnCode = '01'; + $errorCode = !empty($rawResponseData->respCode) ? $rawResponseData->respCode : null; + + if ($this->getProcReturnCode() === '00' && isset($rawResponseData->transactions) && !$errorCode) { + $status = 'approved'; + $code = isset($rawResponseData->transactions->approved) ? $rawResponseData->transactions->approved : null; + $procReturnCode = $this->getProcReturnCode(); + } + + $transaction = null; + $transactionType = null; + + $state = null; + $authCode = null; + if (isset($rawResponseData->transactions->transaction)) { + $state = isset($rawResponseData->transactions->transaction->state) ? + $rawResponseData->transactions->transaction->state : + null; + + $authCode = isset($rawResponseData->transactions->transaction->authCode) ? $this->printData($rawResponseData->transactions->transaction->authCode) : null; + + if (is_array($rawResponseData->transactions->transaction) && count($rawResponseData->transactions->transaction)) { + $state = $rawResponseData->transactions->transaction[0]->state; + $authCode = $rawResponseData->transactions->transaction[0]->authCode; + } + } + + if ($state === 'Sale') { + $transaction = 'pay'; + $state = $transaction; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Authorization') { + $transaction = 'pre'; + $state = $transaction; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Capture') { + $transaction = 'post'; + $state = $transaction; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Bonus_Reverse') { + $state = 'cancel'; + } else { + $state = 'mixed'; + } + + return (object) [ + 'id' => $authCode, + 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, + 'fixed_order_id' => self::formatOrderId($this->order->id), + 'group_id' => isset($rawResponseData->transactions->transaction->groupID) ? $this->printData($rawResponseData->transactions->transaction->groupID) : null, + 'trans_id' => $authCode, + 'response' => $this->getStatusDetail(), + 'auth_code' => $authCode, + 'host_ref_num' => isset($rawResponseData->transactions->transaction->hostLogKey) ? $this->printData($rawResponseData->transactions->transaction->hostLogKey) : null, + 'ret_ref_num' => null, + 'transaction' => $transaction, + 'transaction_type' => $transactionType, + 'state' => $state, + 'date' => isset($rawResponseData->transactions->transaction->tranDate) ? $this->printData($rawResponseData->transactions->transaction->tranDate) : null, + 'proc_return_code' => $procReturnCode, + 'code' => $code, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => $errorCode, + 'error_message' => !empty($rawResponseData->respText) ? $this->printData($rawResponseData->respText) : null, + 'extra' => null, + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function mapHistoryResponse($rawResponseData) + { + $status = 'declined'; + $code = '1'; + $procReturnCode = '01'; + $errorCode = !empty($rawResponseData->respCode) ? $rawResponseData->respCode : null; + + if ($this->getProcReturnCode() === '00' && isset($rawResponseData->transactions) && !$errorCode) { + $status = 'approved'; + $code = isset($rawResponseData->transactions->approved) ? $rawResponseData->transactions->approved : null; + $procReturnCode = $this->getProcReturnCode(); + } + + $transaction = null; + $transactionType = null; + + $state = null; + $authCode = null; + $refunds = []; + if (isset($rawResponseData->transactions->transaction)) { + $state = isset($rawResponseData->transactions->transaction->state) ? + $rawResponseData->transactions->transaction->state : + null; + + $authCode = isset($rawResponseData->transactions->transaction->authCode) ? $this->printData($rawResponseData->transactions->transaction->authCode) : null; + + if (is_array($rawResponseData->transactions->transaction) && count($rawResponseData->transactions->transaction)) { + $state = $rawResponseData->transactions->transaction[0]->state; + $authCode = $rawResponseData->transactions->transaction[0]->authCode; + + if (count($rawResponseData->transactions->transaction) > 1) { + $currencies = array_flip($this->currencies); + + foreach ($rawResponseData->transactions->transaction as $key => $_transaction) { + if ($key > 0) { + $currency = isset($currencies[$_transaction->currencyCode]) ? + (string) $currencies[$_transaction->currencyCode] : + $_transaction->currencyCode; + $refunds[] = [ + 'amount' => (float) $_transaction->amount, + 'currency' => $currency, + 'auth_code' => $_transaction->authCode, + 'date' => $_transaction->tranDate, + ]; + } + } + } + } + } + + if ($state === 'Sale') { + $transaction = 'pay'; + $state = $transaction; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Authorization') { + $transaction = 'pre'; + $state = $transaction; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Capture') { + $transaction = 'post'; + $state = $transaction; + $transactionType = $this->types[$transaction]; + } elseif ($state === 'Bonus_Reverse') { + $state = 'cancel'; + } else { + $state = 'mixed'; + } + + return (object) [ + 'id' => $authCode, + 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, + 'group_id' => isset($rawResponseData->transactions->transaction->groupID) ? $this->printData($rawResponseData->transactions->transaction->groupID) : null, + 'trans_id' => $authCode, + 'response' => $this->getStatusDetail(), + 'auth_code' => $authCode, + 'host_ref_num' => isset($rawResponseData->transactions->transaction->hostLogKey) ? $this->printData($rawResponseData->transactions->transaction->hostLogKey) : null, + 'ret_ref_num' => null, + 'transaction' => $transaction, + 'transaction_type' => $transactionType, + 'state' => $state, + 'date' => isset($rawResponseData->transactions->transaction->tranDate) ? $this->printData($rawResponseData->transactions->transaction->tranDate) : null, + 'refunds' => $refunds, + 'proc_return_code' => $procReturnCode, + 'code' => $code, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => $errorCode, + 'error_message' => !empty($rawResponseData->respText) ? $this->printData($rawResponseData->respText) : null, + 'extra' => null, + 'all' => $rawResponseData, + ]; + } + + /** + * @inheritDoc + */ + protected function preparePaymentOrder(array $order) + { + // Installment + $installment = 0; + if (isset($order['installment']) && $order['installment'] > 1) { + $installment = $order['installment']; + } + + return (object) array_merge($order, [ + 'id' => self::formatOrderId($order['id']), + 'installment' => self::formatInstallment($installment), + 'amount' => self::formatAmount($order['amount']), + 'currency' => $this->mapCurrency($order['currency']), + ]); + } + + /** + * @inheritDoc + */ + protected function preparePostPaymentOrder(array $order) + { + // Installment + $installment = 0; + if (isset($order['installment']) && $order['installment'] > 1) { + $installment = $order['installment']; + } + + return (object) [ + 'host_ref_num' => $order['host_ref_num'], + 'amount' => self::formatAmount($order['amount']), + 'currency' => $this->mapCurrency($order['currency']), + 'installment' => self::formatInstallment($installment), + ]; + } + + /** + * @inheritDoc + */ + protected function prepareStatusOrder(array $order) + { + return (object) [ + 'id' => self::mapOrderIdToPrefixedOrderId($order['id'], $this->account->getModel()), + ]; + } + + /** + * @inheritDoc + */ + protected function prepareHistoryOrder(array $order) + { + return $this->prepareStatusOrder($order); + } + + /** + * @inheritDoc + */ + protected function prepareCancelOrder(array $order) + { + return (object) [ + //id or host_ref_num + 'id' => isset($order['id']) ? self::mapOrderIdToPrefixedOrderId($order['id'], $this->account->getModel()) : null, + 'host_ref_num' => isset($order['host_ref_num']) ? $order['host_ref_num'] : null, + //optional + 'auth_code' => isset($order['auth_code']) ? $order['auth_code'] : null, + ]; + } + + /** + * @inheritDoc + */ + protected function prepareRefundOrder(array $order) + { + return (object) [ + //id or host_ref_num + 'id' => isset($order['id']) ? self::mapOrderIdToPrefixedOrderId($order['id'], $this->account->getModel()) : null, + 'host_ref_num' => isset($order['host_ref_num']) ? $order['host_ref_num'] : null, + 'amount' => self::formatAmount($order['amount']), + 'currency' => self::mapCurrency($order['currency']), + ]; + } +} diff --git a/src/PosNetCrypt.php b/src/Gateways/PosNetCrypt.php similarity index 55% rename from src/PosNetCrypt.php rename to src/Gateways/PosNetCrypt.php index 649b3a10..3d87767e 100644 --- a/src/PosNetCrypt.php +++ b/src/Gateways/PosNetCrypt.php @@ -1,10 +1,9 @@ algo = 'des-ede3-cbc'; $this->block = 8; $this->ks = 24; @@ -46,7 +45,7 @@ public function __construct () * * @return string */ - public function getLastError () + public function getLastError() { return $this->error; } @@ -54,7 +53,7 @@ public function getLastError () /** * @return string */ - public function createIV () + public function createIV() { $temp = sprintf("%05d", rand()); $temp .= sprintf("%05d", rand()); @@ -67,6 +66,7 @@ public function createIV () /** * @param $data * @param $key + * * @return string */ public function encrypt($data, $key) @@ -75,65 +75,69 @@ public function encrypt($data, $key) $iv = $this->createIV(); // Encrypt Data - $encrypted_data = openssl_encrypt($data, $this->algo, $this->detKey($key), OPENSSL_RAW_DATA, $iv); + $encryptedData = openssl_encrypt($data, $this->algo, $this->detKey($key), OPENSSL_RAW_DATA, $iv); // Add IV and Convert to HEX - $hex_encrypted_data = strtoupper(bin2hex($iv)).strtoupper(bin2hex($encrypted_data)); + $hexEncryptedData = strtoupper(bin2hex($iv)).strtoupper(bin2hex($encryptedData)); // Add CRC - $hex_encrypted_data = $this->addCrc($hex_encrypted_data); + $hexEncryptedData = $this->addCrc($hexEncryptedData); - return $hex_encrypted_data; + return $hexEncryptedData; } /** * @param $data * @param $key + * * @return bool|string */ - public function decrypt($data, $key) { + public function decrypt($data, $key) + { - $parsed_data = $this->parseEncryptedData($data); + $parsedData = $this->parseEncryptedData($data); - if (!$parsed_data) return false; + if (!$parsedData) { + return false; + } // Check CRC - if (!$this->checkCrc($parsed_data['crc_data'], $parsed_data['crc'])) { - $this->error = "CRC is not valid! (" . $parsed_data['crc'] . ")"; - return FALSE; + if (!$this->checkCrc($parsedData['crc_data'], $parsedData['crc'])) { + $this->error = "CRC is not valid! (".$parsedData['crc'].")"; + + return false; } // Get IV - $iv = pack("H*", $parsed_data['iv']); + $iv = pack("H*", $parsedData['iv']); // Get Encrypted Data - $encrypted_data = pack("H*", $parsed_data['payload']); + $encryptedData = pack("H*", $parsedData['payload']); // Decrypt Data - $decrypted_data = openssl_decrypt($encrypted_data, $this->algo, $this->detKey($key), OPENSSL_RAW_DATA, $iv); - - return $decrypted_data; + return openssl_decrypt($encryptedData, $this->algo, $this->detKey($key), OPENSSL_RAW_DATA, $iv); } /** * @param $key + * * @return bool|string */ public function detKey($key) { - $deskey = substr(strtoupper(md5($key)), 0, $this->ks); - return $deskey; + return substr(strtoupper(md5($key)), 0, $this->ks); } /** * @param $data + * * @return string */ public function addCrc($data) { $crc = crc32($data); - $hex_crc = sprintf("%08x", $crc); - $data .= strtoupper($hex_crc); + $hexCrc = sprintf("%08x", $crc); + $data .= strtoupper($hexCrc); return $data; } @@ -141,30 +145,35 @@ public function addCrc($data) /** * @param $data * @param $crc + * * @return bool */ public function checkCrc($data, $crc) { - $crc_calc = crc32($data); - $hex_crc = sprintf("%08x", $crc_calc); - $crc_calc = strtoupper($hex_crc); + $crcCalc = crc32($data); + $hexCrc = sprintf("%08x", $crcCalc); + $crcCalc = strtoupper($hexCrc); - return strcmp($crc_calc, $crc) == 0 ? true : false; + return strcmp($crcCalc, $crc) === 0; } /** * @param string $data + * * @return array|bool */ - private function parseEncryptedData(string $data){ + private function parseEncryptedData(string $data) + { - if (strlen($data) < 16 + 8) return false; + if (strlen($data) < 16 + 8) { + return false; + } return [ 'crc' => substr($data, -8), 'crc_data' => substr($data, 0, strlen($data)-8), 'iv' => substr($data, 0, 16), - 'payload' => substr($data, 16, strlen($data)-16-8) + 'payload' => substr($data, 16, strlen($data)-16-8), ]; } } diff --git a/src/Pos.php b/src/Pos.php deleted file mode 100644 index 469e0d13..00000000 --- a/src/Pos.php +++ /dev/null @@ -1,230 +0,0 @@ -config = $config ? $config : require __DIR__ . '/../config/pos.php'; - - // API Account - $this->account = (object) $account; - - // Bank API Exist - if ( ! array_key_exists($this->account->bank, $this->config['banks'])) { - throw new BankNotFoundException(); - } - - // Instance Bank Class - $this->instance(); - } - - /** - * Instance Bank Class - * - * @throws BankClassNullException - */ - public function instance() - { - // Bank Class - $class = $this->config['banks'][$this->account->bank]['class']; - - if ( ! $class) throw new BankClassNullException(); - - // Create Bank Class Object - $this->bank = new $class($this->config['banks'][$this->account->bank], $this->account, $this->config['currencies']); - } - - /** - * Prepare Order - * - * @param array $order - * @param AbstractCreditCard|null $card - * - * @return Pos - */ - public function prepare(array $order, $card = null) - { - // Installment - $installment = 0; - if (isset($order['installment'])) { - $installment = $order['installment'] ? (int) $order['installment'] : 0; - } - - // Currency - $currency = null; - if (isset($order['currency'])) { - $currency = (int) $this->config['currencies'][$order['currency']]; - } - - // Order - $this->order = (object) array_merge($order, [ - 'installment' => $installment, - 'currency' => $currency, - ]); - - // Card - $this->card = $card; - - // Prepare Order - $this->bank->prepare($this->order, $this->card); - - return $this; - } - - /** - * Make Payment - * - * @param AbstractCreditCard $card - * - * @return PosInterface - */ - public function payment($card = null) - { - $this->card = $card; - - // Make Payment - return $this->bank->payment($this->card); - } - - /** - * Get gateway URL - * - * @return string|null - */ - public function getGatewayUrl() - { - return isset($this->bank->gateway) ? $this->bank->gateway : 'null'; - } - - /** - * @return array - */ - public function getConfig(){ - return $this->bank->getConfig(); - } - - /** - * @return mixed - */ - public function getAccount(){ - return $this->bank->getAccount(); - } - - /** - * @return array - */ - public function getCurrencies(){ - return $this->bank->getCurrencies(); - } - - /** - * @return mixed - */ - public function getOrder(){ - return $this->bank->getOrder(); - } - - /** - * @return AbstractCreditCard - */ - public function getCard() - { - return $this->bank->getCard(); - } - - /** - * @return string - */ - public function get3DHash() - { - return $this->bank->create3DHash(); - } - - /** - * Get 3d Form Data - * - * @return array - */ - public function get3dFormData() - { - $data = []; - - try { - $data = $this->bank->get3dFormData(); - } catch (Exception $e) {} - - return $data; - } - - /** - * Is success - * - * @return bool - */ - public function isSuccess() - { - return $this->bank->isSuccess(); - } - - /** - * Is error - * - * @return bool - */ - public function isError() - { - return $this->bank->isError(); - } -} diff --git a/src/PosHelpersTrait.php b/src/PosHelpersTrait.php deleted file mode 100644 index 627cb63e..00000000 --- a/src/PosHelpersTrait.php +++ /dev/null @@ -1,105 +0,0 @@ -encode($nodes[$rootNodeName], 'xml', [ - XmlEncoder::ROOT_NODE_NAME => $rootNodeName, - XmlEncoder::ENCODING => $encoding - ]); - return $xml; - } - - /** - * Print Data - * - * @param $data - * @return null|string - */ - public function printData($data) - { - if ((is_object($data) || is_array($data)) && !count((array)$data)) { - $data = null; - } - - return (string)$data; - } - - /** - * Is success - * - * @return bool - */ - public function isSuccess() - { - if (isset($this->response) && 'approved' === $this->response->status) { - return true; - } - - return false; - } - - /** - * Is error - * - * @return bool - */ - public function isError() - { - return !$this->isSuccess(); - } - - /** - * Converts XML string to object - * - * @param string data - * @return object - */ - public function XMLStringToObject($data) - { - $encoder = new XmlEncoder(); - $xml = $encoder->decode($data, 'xml'); - return (object)json_decode(json_encode($xml)); - } -} diff --git a/src/PosInterface.php b/src/PosInterface.php index 151ee0a1..b741b0d6 100644 --- a/src/PosInterface.php +++ b/src/PosInterface.php @@ -3,9 +3,11 @@ namespace Mews\Pos; use GuzzleHttp\Exception\GuzzleException; +use Mews\Pos\Entity\Account\AbstractPosAccount; use Mews\Pos\Entity\Card\AbstractCreditCard; use Mews\Pos\Exceptions\UnsupportedPaymentModelException; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; +use Mews\Pos\Gateways\AbstractGateway; /** * Interface PosInterface @@ -16,7 +18,7 @@ interface PosInterface * PosInterface constructor. * * @param object $config - * @param object $account + * @param AbstractPosAccount $account * @param array $currencies */ public function __construct($config, $account, array $currencies); @@ -24,8 +26,9 @@ public function __construct($config, $account, array $currencies); /** * Create XML DOM Document * - * @param array $nodes + * @param array $nodes * @param string $encoding + * * @return string the XML, or false if an error occurred. */ public function createXML(array $nodes, $encoding = 'UTF-8'); @@ -34,6 +37,7 @@ public function createXML(array $nodes, $encoding = 'UTF-8'); * Print Data * * @param $data + * * @return null|string */ public function printData($data); @@ -41,7 +45,8 @@ public function printData($data); /** * Regular Payment * - * @return $this + * @return AbstractGateway + * * @throws GuzzleException */ public function makeRegularPayment(); @@ -49,7 +54,8 @@ public function makeRegularPayment(); /** * Make 3D Payment * - * @return $this + * @return AbstractGateway + * * @throws GuzzleException */ public function make3DPayment(); @@ -57,15 +63,24 @@ public function make3DPayment(); /** * Make 3D Pay Payment * - * @return $this + * @return AbstractGateway */ public function make3DPayPayment(); + /** + * Just returns formatted data of host payment response + * + * @return AbstractGateway + */ + public function make3DHostPayment(); + /** * Send contents to WebService * * @param $contents - * @return $this + * + * @return AbstractGateway + * * @throws GuzzleException */ public function send($contents); @@ -73,18 +88,20 @@ public function send($contents); /** * Prepare Order * - * @param object $order - * @return mixed - * @throws UnsupportedTransactionTypeException + * @param array $order + * @param string $txType //txTypes from AbstractGateway + * @param AbstractCreditCard|null $card need when 3DFormData requested + * + * @return void */ - public function prepare($order); + public function prepare(array $order, string $txType, $card = null); /** * Make Payment * * @param AbstractCreditCard $card * - * @return mixed + * @return AbstractGateway * * @throws UnsupportedPaymentModelException * @throws GuzzleException @@ -94,35 +111,37 @@ public function payment($card); /** * Refund Order * - * @param array $meta - * @return $this + * @return AbstractGateway + * * @throws GuzzleException */ - public function refund(array $meta); + public function refund(); /** * Cancel Order * - * @param array $meta - * @return $this + * @return AbstractGateway + * * @throws GuzzleException */ - public function cancel(array $meta); + public function cancel(); /** * Order Status * - * @param array $meta - * @return $this + * @return AbstractGateway + * * @throws GuzzleException */ - public function status(array $meta); + public function status(); /** * Order History * * @param array $meta - * @return $this + * + * @return AbstractGateway + * * @throws GuzzleException */ public function history(array $meta); @@ -140,4 +159,20 @@ public function isSuccess(); * @return bool */ public function isError(); + + /** + * Enable/Disable test mode + * + * @param bool $testMode + * + * @return AbstractGateway + */ + public function setTestMode(bool $testMode); + + /** + * Enable/Disable test mode + * + * @return bool + */ + public function isTestMode(); } diff --git a/src/PosNet.php b/src/PosNet.php deleted file mode 100644 index 80af6e7e..00000000 --- a/src/PosNet.php +++ /dev/null @@ -1,1100 +0,0 @@ - 'declined', - '1' => 'approved', - '2' => 'declined', - '00' => 'approved', - '0001' => 'bank_call', - '0005' => 'reject', - '0007' => 'bank_call', - '0012' => 'reject', - '0014' => 'reject', - '0030' => 'bank_call', - '0041' => 'reject', - '0043' => 'reject', - '0051' => 'reject', - '0053' => 'bank_call', - '0054' => 'reject', - '0057' => 'reject', - '0058' => 'reject', - '0062' => 'reject', - '0065' => 'reject', - '0091' => 'bank_call', - '0123' => 'transaction_not_found', - '0444' => 'bank_call', - ]; - - /** - * Transaction Types - * - * @var array - */ - public $types = [ - 'pay' => 'Sale', - 'pre' => 'Auth', - 'post' => 'Capt', - ]; - - /** - * Currencies - * - * @var array - */ - public $currencies = []; - - /** - * Fixed Currencies - * @var array - */ - protected $_currencies = [ - 'TRY' => 'TL', - 'USD' => 'US', - 'EUR' => 'EU', - 'GBP' => 'GB', - 'JPY' => 'JP', - 'RUB' => 'RU', - ]; - - /** - * Transaction Type - * - * @var string - */ - public $type; - - /** - * API Account - * - * @var array - */ - protected $account = []; - - /** - * Order Details - * - * @var array - */ - protected $order = []; - - /** - * @var CreditCardPosNet|null - */ - protected $card; - - /** - * Request - * - * @var Request - */ - protected $request; - - /** - * Response Raw Data - * - * @var object - */ - protected $data; - - /** - * Processed Response Data - * - * @var mixed - */ - protected $response; - - /** - * Configuration - * - * @var array - */ - protected $config = []; - - /** - * @var PosNetCrypt|null - */ - public $crypt; - - /** - * PosNet constructor. - * - * @param array $config - * @param array $account - * @param array $currencies - */ - public function __construct($config, $account, array $currencies) - { - $request = Request::createFromGlobals(); - $this->request = $request->request; - - $this->crypt = new PosNetCrypt(); - - $this->config = $config; - $this->account = $account; - $this->currencies = $currencies; - - $this->url = isset($this->config['urls'][$this->account->env]) ? - $this->config['urls'][$this->account->env] : - $this->config['urls']['production']; - - $this->gateway = isset($this->config['urls']['gateway'][$this->account->env]) ? - $this->config['urls']['gateway'][$this->account->env] : - $this->config['urls']['gateway']['production']; - - return $this; - } - - /** - * @inheritDoc - */ - public function createXML(array $data, $encoding = 'ISO-8859-9'): string - { - return $this->traitCreateXML(['posnetRequest' => $data], $encoding); - } - - /** - * Get currency - * - * @return int|string - */ - protected function getCurrency() { - $search = array_search($this->order->currency, $this->currencies); - $currency = $this->order->currency; - if ($search) { - $currency = $this->_currencies[$search]; - } - - return $currency; - } - - /** - * Get amount - * - * @return int - */ - protected function getAmount() - { - return (int) str_replace('.', '', number_format($this->order->amount, 2, '.', '')); - } - - /** - * Get PrefixedOrderId - * To check the status of an order or cancel/refund order Yapikredi - * - requires the order length to be 24 - * - and order id prefix which is "TDSC" for 3D payments - * @return string - */ - protected function getPrefixedOrderId() - { - if($this->account->model == '3d'){ - return $this->config['order']['id_3d_prefix'] . $this->getOrderId($this->config['order']['id_total_length'] - strlen($this->config['order']['id_3d_prefix'])); - }elseif($this->account->model == '3d_pay') { - return $this->config['order']['id_3d_pay_prefix'] . $this->getOrderId($this->config['order']['id_total_length'] - strlen($this->config['order']['id_3d_pay_prefix'])); - } - return $this->config['order']['id_regular_prefix'] . $this->getOrderId($this->config['order']['id_total_length'] - strlen($this->config['order']['id_regular_prefix'])); - } - - /** - * Get orderId - * - * @param int $pad_length - * @return string - */ - protected function getOrderId(int $pad_length = null) - { - if($pad_length === null) $pad_length = $this->config['order']['id_length']; - return (string) str_pad($this->order->id, $pad_length, '0', STR_PAD_LEFT); - } - - /** - * Get Installment - * - * @return int|string - */ - protected function getInstallment() - { - $installment = (int) $this->order->installment; - if (!$this->order->installment) { - $installment = '00'; - } - - return $installment; - } - - /** - * Create Regular Payment XML - * - * @return string - */ - protected function createRegularPaymentXML() - { - $transaction = strtolower($this->type); - - $requestData = [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'tranDateRequired' => '1', - $transaction => [ - 'orderID' => $this->getOrderId(), - 'installment' => $this->getInstallment(), - 'amount' => $this->getAmount(), - 'currencyCode' => $this->getCurrency(), - 'ccno' => $this->card->getNumber(), - 'expDate' => $this->card->getExpirationDate(), - 'cvc' => $this->card->getCvv(), - ], - ]; - - return $this->createXML($requestData); - } - - /** - * Create Regular Payment Post XML - * - * @return string - */ - protected function createRegularPostXML() - { - $requestData = [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'tranDateRequired' => '1', - 'capt' => [ - 'hostLogKey' => $this->order->host_ref_num, - 'amount' => $this->getAmount(), - 'currencyCode' => $this->getCurrency(), - 'installment' => $this->order->installment ? $this->getInstallment() : null - ], - ]; - - return $this->createXML($requestData); - } - - /** - * Create 3D Payment XML - * @return string - */ - protected function create3DPaymentXML() - { - $requestData = [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'oosResolveMerchantData' => [ - 'bankData' => $this->request->get('BankPacket'), - 'merchantData' => $this->request->get('MerchantPacket'), - 'sign' => $this->request->get('Sign'), - 'mac' => $this->create3DHash() - ], - ]; - - return $this->createXML($requestData); - } - - /** - * Get ProcReturnCode - * - * @return string|null - */ - protected function getProcReturnCode() - { - return (string) $this->data->approved == '1' ? '00' : $this->data->approved; - } - - /** - * Get Status Detail Text - * - * @return string|null - */ - protected function getStatusDetail() - { - $proc_return_code = $this->getProcReturnCode(); - - return isset($this->codes[$proc_return_code]) ? (string) $this->codes[$proc_return_code] : null; - } - - /** - * @return mixed - */ - public function getResponse() - { - return $this->response; - } - - /** - * Get OOS transaction data - * - * @return object - * - * @throws GuzzleException - */ - public function getOosTransactionData() - { - if (null === $this->card->getHolderName() && isset($this->order->name)) { - $this->card->setHolderName($this->order->name); - } - - $requestData = [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'oosRequestData' => [ - 'posnetid' => $this->account->posnet_id, - 'ccno' => $this->card->getNumber(), - 'expDate' => $this->card->getExpirationDate(), - 'cvc' => $this->card->getCvv(), - 'amount' => $this->getAmount(), - 'currencyCode' => $this->getCurrency(), - 'installment' => $this->getInstallment(), - 'XID' => $this->getOrderId(), - 'cardHolderName' => $this->card->getHolderName(), - 'tranType' => $this->type, - ], - ]; - $xml = $this->createXML($requestData); - - $this->send($xml); - - return $this->data; - } - - /** - * Regular Payment - * - * @return $this - * @throws GuzzleException - */ - public function makeRegularPayment() - { - $contents = ''; - if (in_array($this->order->transaction, ['pay', 'pre'])) { - $contents = $this->createRegularPaymentXML(); - } elseif ($this->order->transaction == 'post') { - $contents = $this->createRegularPostXML(); - } - - $this->send($contents); - - $status = 'declined'; - $code = '1'; - $proc_return_code = '01'; - $obj = isset($this->data) ? $this->data : null; - $error_code = !empty($obj->respCode) ? $obj->respCode : null; - - if ($this->getProcReturnCode() == '00' && $this->getStatusDetail() == 'approved' && $obj && !$error_code) { - $status = 'approved'; - $code = isset($obj->approved) ? $obj->approved : null; - $proc_return_code = $this->getProcReturnCode(); - } - - $this->response = (object) [ - 'id' => isset($obj->authCode) ? $this->printData($obj->authCode) : null, - 'order_id' => $this->order->id, - 'fixed_order_id' => $this->getOrderId(), - 'group_id' => isset($obj->groupID) ? $this->printData($obj->groupID) : null, - 'trans_id' => isset($obj->authCode) ? $this->printData($obj->authCode) : null, - 'response' => $this->getStatusDetail(), - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'auth_code' => isset($obj->authCode) ? $this->printData($obj->authCode) : null, - 'host_ref_num' => isset($obj->hostlogkey) ? $this->printData($obj->hostlogkey) : null, - 'ret_ref_num' => isset($obj->hostlogkey) ? $this->printData($obj->hostlogkey) : null, - 'proc_return_code' => $proc_return_code, - 'code' => $code, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => $error_code, - 'error_message' => !empty($obj->respText) ? $this->printData($obj->respText) : null, - 'campaign_url' => null, - 'extra' => null, - 'all' => $this->data, - ]; - - return $this; - } - - /** - * Check 3D Hash - * - * @return bool - */ - protected function check3DHash() - { - $check = false; - - if ($this->crypt instanceof PosNetCrypt) { - $decrypted_data = $this->crypt->decrypt($this->request->get('MerchantPacket'), $this->account->store_key); - - $decrypted_data_array = explode(';', $decrypted_data); - - $original_data = array_map('strval', [ - $this->account->client_id, - $this->account->terminal_id, - $this->getAmount(), - $this->getInstallment(), - $this->getOrderId() - ]); - - $decrypted_data_list = array_map('strval', [ - $decrypted_data_array[0], - $decrypted_data_array[1], - $decrypted_data_array[2], - $decrypted_data_array[3], - $decrypted_data_array[4] - ]); - - if ($original_data == $decrypted_data_list) { - $check = true; - } - } else { - $check = false; - } - - return $check; - } - - /** - * Make 3D Payment - * - * @return $this - * @throws GuzzleException - */ - public function make3DPayment() - { - $status = 'declined'; - $transaction_security = 'MPI fallback'; - - if ($this->check3DHash()) { - $contents = $this->create3DPaymentXML(); - $this->send($contents); - }else{ - goto end; - } - - if($this->getProcReturnCode() != '00'){ - goto end; - } - - if(!$this->verifyResponseMAC($this->data->oosResolveMerchantDataResponse)) { - goto end; - } - - if ($this->getProcReturnCode() == '00' && $this->getStatusDetail() == 'approved') { - if ($this->data->oosResolveMerchantDataResponse->mdStatus == '1') { - $transaction_security = 'Full 3D Secure'; - $status = 'approved'; - } elseif (in_array($this->data->oosResolveMerchantDataResponse->mdStatus, [2, 3, 4])) { - $transaction_security = 'Half 3D Secure'; - $status = 'approved'; - } - - //if 3D Authentication is failed - if($status != 'approved') goto end; - - $requestData = [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'oosTranData' => [ - 'bankData' => $this->request->get('BankPacket'), - 'merchantData' => $this->request->get('MerchantPacket'), - 'sign' => $this->request->get('Sign'), - 'wpAmount' => 0, - 'mac' => $this->create3DHash() - ], - ]; - - $contents = $this->createXML($requestData); - $this->send($contents); - } - - if ($this->data->approved != 1) { - $status = 'declined'; - } - - end: - $this->response = (object) [ - 'id' => isset($this->data->authCode) ? $this->printData($this->data->authCode) : null, - 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, - 'fixed_order_id' => $this->getOrderId(), - 'group_id' => isset($this->data->groupID) ? $this->printData($this->data->groupID) : null, - 'trans_id' => isset($this->data->authCode) ? $this->printData($this->data->authCode) : null, - 'response' => $this->getStatusDetail(), - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'transaction_security' => $transaction_security, - 'auth_code' => isset($this->data->authCode) ? $this->printData($this->data->authCode) : null, - 'host_ref_num' => isset($this->data->hostlogkey) ? $this->printData($this->data->hostlogkey) : null, - 'ret_ref_num' => isset($this->data->transaction->hostlogkey) ? $this->printData($this->data->transaction->hostlogkey) : null, - 'proc_return_code' => $this->getProcReturnCode(), - 'code' => $this->getProcReturnCode(), - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => !empty($this->data->respCode) ? $this->printData($this->data->respCode) : null, - 'error_message' => !empty($this->data->respText) ? $this->printData($this->data->respText) : null, - 'md_status' => isset($this->data->oosResolveMerchantDataResponse->mdStatus) ? $this->printData($this->data->oosResolveMerchantDataResponse->mdStatus) : null, - 'hash' => [ - 'merchant_packet' => $this->request->get('MerchantPacket'), - 'bank_packet' => $this->request->get('BankPacket'), - 'sign' => $this->request->get('Sign'), - ], - 'xid' => isset($this->data->oosResolveMerchantDataResponse->xid) ? $this->data->oosResolveMerchantDataResponse->xid : null, - 'md_error_message' => isset($this->data->oosResolveMerchantDataResponse->mdErrorMessage) ? $this->data->oosResolveMerchantDataResponse->mdErrorMessage : null, - 'campaign_url' => null, - 'all' => $this->data, - ]; - - return $this; - } - - /** - * Make 3D Pay Payment - * - * @return $this - */ - public function make3DPayPayment() - { - //TODO - return $this; - } - - /** - * Get 3d Form Data - * - * @return array - * @throws GuzzleException - */ - public function get3DFormData() - { - $inputs = []; - $data = null; - - if ($this->card && $this->order) { - $data = $this->getOosTransactionData(); - - $inputs = [ - 'posnetData' => $data->oosRequestDataResponse->data1, - 'posnetData2' => $data->oosRequestDataResponse->data2, - 'mid' => $this->account->client_id, - 'posnetID' => $this->account->posnet_id, - 'digest' => $data->oosRequestDataResponse->sign, - 'vftCode' => isset($this->account->promotion_code) ? $this->account->promotion_code : null, - 'merchantReturnURL' => $this->order->success_url, - 'url' => '', - 'lang' => $this->getLang(), - ]; - } - - return [ - 'gateway' => $this->gateway, - 'success_url' => $this->order->success_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $data->oosRequestDataResponse->sign, - 'hash' => $data->oosRequestDataResponse->data1, - 'inputs' => $inputs, - ]; - } - - /** - * Send contents to WebService - * - * @param $contents - * @return $this - * @throws GuzzleException - */ - public function send($contents) - { - $client = new Client(); - - $headers = [ - 'Content-Type' => 'application/x-www-form-urlencoded' - ]; - - $response = $client->request('POST', $this->url, [ - 'headers' => $headers, - 'body' => "xmldata=" . $contents, - ]); - - $this->data = $this->XMLStringToObject($response->getBody()->getContents()); - - return $this; - } - - /** - * Prepare Order - * - * @param object $order - * @param CreditCardPosNet|null $card - * - * @throws UnsupportedTransactionTypeException - */ - public function prepare($order, $card = null) - { - $this->type = $this->types['pay']; - if (isset($order->transaction)) { - if (array_key_exists($order->transaction, $this->types)) { - $this->type = $this->types[$order->transaction]; - } else { - throw new UnsupportedTransactionTypeException('Unsupported transaction type!'); - } - } - - $this->order = $order; - $this->card = $card; - } - - /** - * Make Payment - * - * @param CreditCardPosNet $card - * - * @return mixed - * - * @throws UnsupportedPaymentModelException - * @throws GuzzleException - */ - public function payment($card) - { - $this->card = $card; - - $model = 'regular'; - if (isset($this->account->model) && $this->account->model) { - $model = $this->account->model; - } - - if ($model == 'regular') { - $this->makeRegularPayment(); - } elseif ($model == '3d') { - $this->make3DPayment(); - } elseif ($model == '3d_pay') { - $this->make3DPayPayment(); - } else { - throw new UnsupportedPaymentModelException(); - } - - return $this; - } - - /** - * Refund or Cancel Order - * - * @param array $meta - * @param string $type - * @return $this - * @throws GuzzleException - */ - protected function refundOrCancel(array $meta, $type = 'cancel') - { - $this->order = (object) [ - 'id' => $meta['order_id'], - 'host_ref_num' => isset($meta['host_ref_num']) ? $meta['host_ref_num'] : null, - 'auth_code' => isset($meta['auth_code']) ? $meta['auth_code'] : null, - 'amount' => isset($meta['amount']) ? $meta['amount'] : null, - 'currency' => isset($meta['currency']) ? $this->_currencies[$meta['currency']] : null, - ]; - - $nodes = [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'tranDateRequired' => '1', - ]; - - if ($type == 'refund') { - $return = [ - 'amount' => $this->getAmount(), - 'currencyCode' => $this->getCurrency(), - 'orderID' => $this->getPrefixedOrderId(), - ]; - - if ($this->order->host_ref_num) { - $return['hostLogKey'] = $this->order->host_ref_num; - unset($return['orderID']); - } - - $append = [ - 'return' => $return, - ]; - } else { - $reverse = [ - 'transaction' => 'pointUsage', - 'orderID' => $this->getPrefixedOrderId(), - 'authCode' => $this->order->auth_code, - ]; - - if ($this->order->host_ref_num) { - $reverse = [ - 'transaction' => 'pointUsage', - 'hostLogKey' => $this->order->host_ref_num, - 'authCode' => $this->order->auth_code, - ]; - } - - $append = [ - 'reverse' => $reverse, - ]; - } - - $requestData = array_merge($nodes, $append); - - $xml = $this->createXML($requestData); - - $this->send($xml); - - $status = 'declined'; - $code = '1'; - $proc_return_code = '01'; - $obj = isset($this->data) ? $this->data : null; - $error_code = !empty($obj->respCode) ? $obj->respCode : null; - - if ($this->getProcReturnCode() == '00' && $obj && !$error_code) { - $status = 'approved'; - $code = isset($obj->approved) ? $obj->approved : null; - $proc_return_code = $this->getProcReturnCode(); - } - - $transaction = null; - $transaction_type = null; - $state = isset($obj->state) ? $obj->state : null; - if ($state == 'Sale') { - $transaction = 'pay'; - $transaction_type = $this->types[$transaction]; - } elseif ($state == 'Authorization') { - $transaction = 'pre'; - $transaction_type = $this->types[$transaction]; - } elseif ($state == 'Capture') { - $transaction = 'post'; - $transaction_type = $this->types[$transaction]; - } - - $data = [ - 'id' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null, - 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, - 'fixed_order_id' => isset($obj->transaction->orderID) ? $this->printData($obj->transaction->orderID) : null, - 'group_id' => isset($obj->transaction->groupID) ? $this->printData($obj->transaction->groupID) : null, - 'trans_id' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null, - 'response' => $this->getStatusDetail(), - 'auth_code' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null, - 'host_ref_num' => isset($obj->transaction->hostlogkey) ? $this->printData($obj->transaction->hostlogkey) : null, - 'ret_ref_num' => isset($obj->transaction->hostlogkey) ? $this->printData($obj->transaction->hostlogkey) : null, - 'transaction' => $transaction, - 'transaction_type' => $transaction_type, - 'state' => $state, - 'date' => isset($obj->transaction->tranDate) ? $this->printData($obj->transaction->tranDate) : null, - 'proc_return_code' => $proc_return_code, - 'code' => $code, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => $error_code, - 'error_message' => !empty($obj->respText) ? $this->printData($obj->respText) : null, - 'extra' => null, - 'all' => $this->data, - ]; - - $this->response = (object) $data; - - return $this; - } - - /** - * Refund Order - * - * @param $meta - * @return $this - * @throws GuzzleException - */ - public function refund(array $meta) - { - return $this->refundOrCancel($meta, 'refund'); - } - - /** - * Cancel Order - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function cancel(array $meta) - { - return $this->refundOrCancel($meta, 'cancel'); - } - - /** - * Order Status - * - * @param array $meta - * @param bool $history - * @return $this - * @throws GuzzleException - */ - public function status(array $meta, $history = false) - { - $this->order = (object) [ - 'id' => isset($meta['order_id']) ? $meta['order_id'] : null, - ]; - - $requestData = [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'agreement' => [ - 'orderID' => $this->getPrefixedOrderId(), - ], - ]; - $xml = $this->createXML($requestData); - - $this->send($xml); - - $status = 'declined'; - $code = '1'; - $proc_return_code = '01'; - $obj = isset($this->data->transactions) ? $this->data->transactions : null; - $error_code = !empty($this->data->respCode) ? $this->data->respCode : null; - - if ($this->getProcReturnCode() == '00' && $obj && !$error_code) { - $status = 'approved'; - $code = isset($obj->approved) ? $obj->approved : null; - $proc_return_code = $this->getProcReturnCode(); - } - - $transaction = null; - $transaction_type = null; - - $state = null; - $auth_code = null; - $refunds = []; - if (isset($this->data->transactions->transaction)) { - $state = isset($this->data->transactions->transaction->state) ? - $this->data->transactions->transaction->state : - null; - - $auth_code = isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null; - - if (is_array($this->data->transactions->transaction) && count($this->data->transactions->transaction)) { - $state = $this->data->transactions->transaction[0]->state; - $auth_code = $this->data->transactions->transaction[0]->authCode; - - if (count($this->data->transactions->transaction) > 1 && $history) { - $_currencies = array_flip($this->_currencies); - - foreach ($this->data->transactions->transaction as $key => $_transaction) { - if ($key > 0) { - $currency = isset($_currencies[$_transaction->currencyCode]) ? - (string) $_currencies[$_transaction->currencyCode] : - $_transaction->currencyCode; - $refunds[] = [ - 'amount' => (double) $_transaction->amount, - 'currency' => $currency, - 'auth_code' => $_transaction->authCode, - 'date' => $_transaction->tranDate, - ]; - } - } - } - } - } - - if ($state == 'Sale') { - $transaction = 'pay'; - $state = $transaction; - $transaction_type = $this->types[$transaction]; - } elseif ($state == 'Authorization') { - $transaction = 'pre'; - $state = $transaction; - $transaction_type = $this->types[$transaction]; - } elseif ($state == 'Capture') { - $transaction = 'post'; - $state = $transaction; - $transaction_type = $this->types[$transaction]; - } elseif ($state == 'Bonus_Reverse') { - $state = 'cancel'; - } else { - $state = 'mixed'; - } - - $data = [ - 'id' => $auth_code, - 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, - 'fixed_order_id' => $this->getOrderId(), - 'group_id' => isset($obj->transaction->groupID) ? $this->printData($obj->transaction->groupID) : null, - 'trans_id' => $auth_code, - 'response' => $this->getStatusDetail(), - 'auth_code' => $auth_code, - 'host_ref_num' => isset($obj->transaction->hostLogKey) ? $this->printData($obj->transaction->hostLogKey) : null, - 'ret_ref_num' => null, - 'transaction' => $transaction, - 'transaction_type' => $transaction_type, - 'state' => $state, - 'date' => isset($obj->transaction->tranDate) ? $this->printData($obj->transaction->tranDate) : null, - 'refunds' => $refunds, - 'proc_return_code' => $proc_return_code, - 'code' => $code, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => $error_code, - 'error_message' => !empty($this->data->respText) ? $this->printData($this->data->respText) : null, - 'extra' => null, - 'all' => $this->data, - ]; - - if (!$history) { - unset($data['refunds']); - } - - $this->response = (object) $data; - - return $this; - } - - /** - * Order History - * - * @param array $meta - * @return $this - * @throws GuzzleException - */ - public function history(array $meta) - { - return $this->status($meta, true); - } - - /** - * @return array - */ - public function getConfig(){ - return $this->config; - } - - /** - * @return mixed - */ - public function getAccount(){ - return $this->account; - } - - /** - * @return array - */ - public function getCurrencies(){ - return $this->currencies; - } - - /** - * @return mixed - */ - public function getOrder(){ - return $this->order; - } - - /** - * @return CreditCardPosNet|null - */ - public function getCard(){ - return $this->card; - } - - /** - * Hash string - * - * @return string - */ - public function hashString(string $str) - { - return base64_encode(hash('sha256',$str,true)); - } - - /** - * Create 3D Hash (MAC) - * - * @return string - */ - public function create3DHash() - { - $hash_str = ''; - - $firstHash = $this->hashString($this->account->store_key . ";" . $this->account->terminal_id); - - if ($this->account->model == '3d' || $this->account->model == '3d_pay') { - $hash_str = $this->hashString($this->getOrderId() . ";" . $this->getAmount() . ";" . $this->getCurrency() . ";" . $this->account->client_id . ";" . $firstHash); - } - - return $hash_str; - } - - /** - * verifies the if request came from bank - * - * @param mixed $data oosResolveMerchantDataResponse - * @return boolean - */ - public function verifyResponseMAC($data) - { - $hash_str = ''; - - $firstHash = $this->hashString($this->account->store_key . ";" . $this->account->terminal_id); - - if ($this->account->model == '3d' || $this->account->model == '3d_pay') { - $hash_str = $this->hashString($data->mdStatus . ";" . $this->getOrderId() . ";" . $this->getAmount() . ";" . $this->getCurrency() . ";" . $this->account->client_id . ";" . $firstHash); - } - - return $hash_str == $data->mac; - } - - /** - * bank returns error messages for specified language value - * usually accepted values are tr,en - * @return string - */ - private function getLang() - { - if ($this->order && isset($this->order->lang)) { - return $this->order->lang; - } - if (isset($this->account->lang)) { - return $this->account->lang; - } - - return self::LANG_TR; - } -} diff --git a/tests/EstPostTest.php b/tests/EstPostTest.php deleted file mode 100644 index ab97c11b..00000000 --- a/tests/EstPostTest.php +++ /dev/null @@ -1,129 +0,0 @@ -config = require __DIR__ . '/../config/pos.php'; - - $this->account = (object)[ - 'bank' => 'akbank', - 'model' => '3d', - 'client_id' => 'XXXXXXX', - 'username' => 'XXXXXXX', - 'password' => 'XXXXXXX', - 'store_key' => 'VnM5WZ3sGrPusmWP', - 'env' => 'test', - ]; - - $this->card = new CreditCardEstPos('5555444433332222', '21', '12', '122', 'ahmet', 'visa'); - - $this->order = (object)[ - 'id' => 'order222', - 'name' => 'siparis veren', - 'email' => 'test@test.com', - 'amount' => '100.25', - 'installment' => 0, - 'currency' => 'TRY', - 'success_url' => 'https://domain.com/success', - 'fail_url' => 'https://domain.com/fail_url', - 'lang' => 'tr', - 'rand' => microtime() - ]; - - $this->estpos = new EstPos( - $this->config['banks'][$this->account->bank], - $this->account, - $this->config['currencies']); - } - - public function testInit() - { - $this->assertEquals($this->config['banks'][$this->account->bank], $this->estpos->getConfig()); - $this->assertEquals($this->account, $this->estpos->getAccount()); - $this->assertEquals($this->config['currencies'], $this->estpos->getCurrencies()); - } - - public function testPrepare() - { - - $this->estpos->prepare($this->order, $this->card); - $this->assertEquals($this->card, $this->estpos->getCard()); - $this->assertEquals($this->order, $this->estpos->getOrder()); - } - - public function testGet3DFormData() - { - $this->estpos->prepare($this->order, $this->card); - - $form = [ - 'gateway' => $this->config['banks'][$this->account->bank]['urls']['gateway'][$this->account->env], - 'success_url' => $this->order->success_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand, - 'hash' => $this->estpos->create3DHash(), - 'inputs' => [ - 'clientid' => $this->account->client_id, - 'storetype' => $this->account->model, - 'hash' => $this->estpos->create3DHash(), - 'cardType' => $this->card->getCardCode(), - 'pan' => $this->card->getNumber(), - 'Ecom_Payment_Card_ExpDate_Month' => $this->card->getExpireMonth(), - 'Ecom_Payment_Card_ExpDate_Year' => $this->card->getExpireYear(), - 'cv2' => $this->card->getCvv(), - 'firmaadi' => $this->order->name, - 'Email' => $this->order->email, - 'amount' => $this->order->amount, - 'oid' => $this->order->id, - 'okUrl' => $this->order->success_url, - 'failUrl' => $this->order->fail_url, - 'rnd' => $this->order->rand, - 'lang' => $this->order->lang, - 'currency' => $this->order->currency, - ] - ]; - $this->assertEquals($form, $this->estpos->get3DFormData()); - } - - public function testCheck3DHash() - { - $data = [ - "md" => "478719:0373D10CFD8BDED34FA0546D27D5BE76F8BA4A947D1EC499102AE97B880EB1B9:4242:##400902568", - "cavv" => "BwAQAhIYRwEAABWGABhHEE6v5IU=", - "AuthCode" => "", - "oid" => "880", - "mdStatus" => "4", - "eci" => "06", - "clientid" => "400902568", - "rnd" => "hDx50d0cq7u1vbpWQMae", - "ProcReturnCode" => "N7", - "Response" => "Declined", - "HASH" => "D+B5fFWXEWFqVSkwotyuTPUW800=", - "HASHPARAMS" => "clientid:oid:AuthCode:ProcReturnCode:Response:mdStatus:cavv:eci:md:rnd:", - "HASHPARAMSVAL" => "400902568880N7Declined4BwAQAhIYRwEAABWGABhHEE6v5IU=06478719:0373D10CFD8BDED34FA0546D27D5BE76F8BA4A947D1EC499102AE97B880EB1B9:4242:##400902568hDx50d0cq7u1vbpWQMae" - ]; - - $this->assertTrue($this->estpos->check3DHash($data)); - - $data['mdStatus'] = ''; - $this->assertFalse($this->estpos->check3DHash($data)); - } -} diff --git a/tests/GarantiPosTest.php b/tests/GarantiPosTest.php deleted file mode 100644 index 9b45570e..00000000 --- a/tests/GarantiPosTest.php +++ /dev/null @@ -1,95 +0,0 @@ -config = require __DIR__ . '/../config/pos.php'; - - $this->account = (object)[ - 'bank' => 'akbank', - 'model' => '3d', - 'client_id' => 'XXXXXXX', - 'terminal_id' => '13456', - 'username' => 'XXXXXXX', - 'password' => 'XXXXXXX', - 'store_key' => 'XXXXXXX', - 'env' => 'test', - ]; - - $this->card = new CreditCardGarantiPos('5555444433332222', '21', '12', '122'); - - $this->order = (object)[ - 'id' => 'order222', - 'name' => 'siparis veren', - 'email' => 'test@test.com', - 'amount' => '100.25', - 'installment' => 0, - 'currency' => 'TRY', - 'success_url' => 'https://domain.com/success', - 'fail_url' => 'https://domain.com/fail_url', - 'lang' => 'tr', - 'rand' => microtime(), - 'ip' => '156.155.154.153' - ]; - - $this->garantiPos = new GarantiPos( - $this->config['banks'][$this->account->bank], - $this->account, - $this->config['currencies']); - } - - public function testInit() - { - $this->assertEquals($this->config['banks'][$this->account->bank], $this->garantiPos->getConfig()); - $this->assertEquals($this->account, $this->garantiPos->getAccount()); - $this->assertEquals($this->config['currencies'], $this->garantiPos->getCurrencies()); - } - - public function testPrepare() - { - - $this->garantiPos->prepare($this->order, $this->card); - $this->assertEquals($this->card, $this->garantiPos->getCard()); - $this->assertEquals($this->order, $this->garantiPos->getOrder()); - } - - public function testGet3DFormData() - { - $this->garantiPos->prepare($this->order, $this->card); - - $form = [ - 'gateway' => $this->config['banks'][$this->account->bank]['urls']['gateway'][$this->account->env], - 'success_url' => $this->order->success_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand - ]; - $actualForm = $this->garantiPos->get3DFormData(); - $this->assertNotEmpty($actualForm['inputs']); - $this->assertNotEmpty($actualForm['hash']); - - unset($actualForm['inputs']); - unset($actualForm['hash']); - $this->assertEquals($form, $actualForm); - } - - -} diff --git a/tests/Gateways/EstPostTest.php b/tests/Gateways/EstPostTest.php new file mode 100644 index 00000000..8b3f9c96 --- /dev/null +++ b/tests/Gateways/EstPostTest.php @@ -0,0 +1,407 @@ +config = require __DIR__ . '/../../config/pos.php'; + + $this->account = AccountFactory::createEstPosAccount('akbank', 'XXXXXXX', 'XXXXXXX', 'XXXXXXX', '3d', 'VnM5WZ3sGrPusmWP', \Mews\Pos\Gateways\EstPos::LANG_TR); + + $this->card = new CreditCardEstPos('5555444433332222', '21', '12', '122', 'ahmet', 'visa'); + + $this->order = [ + 'id' => 'order222', + 'name' => 'siparis veren', + 'email' => 'test@test.com', + 'amount' => '100.25', + 'installment' => 0, + 'currency' => 'TRY', + 'success_url' => 'https://domain.com/success', + 'fail_url' => 'https://domain.com/fail_url', + 'lang' => 'tr', + 'rand' => microtime() + ]; + + $this->pos = PosFactory::createPosGateway($this->account); + + $this->pos->setTestMode(true); + + $this->xmlDecoder = new XmlEncoder(); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->account->getBank()], $this->pos->getConfig()); + $this->assertEquals($this->account, $this->pos->getAccount()); + $this->assertNotEmpty($this->pos->getCurrencies()); + } + + public function testPrepare() + { + + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + $this->assertEquals($this->card, $this->pos->getCard()); + } + + public function testGet3DFormData() + { + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + + $form = [ + 'gateway' => $this->config['banks'][$this->account->getBank()]['urls']['gateway']['test'], + 'inputs' => [ + 'clientid' => $this->account->getClientId(), + 'storetype' => $this->account->getModel(), + 'hash' => $this->pos->create3DHash(), + 'cardType' => $this->card->getCardCode(), + 'pan' => $this->card->getNumber(), + 'Ecom_Payment_Card_ExpDate_Month' => $this->card->getExpireMonth(), + 'Ecom_Payment_Card_ExpDate_Year' => $this->card->getExpireYear(), + 'cv2' => $this->card->getCvv(), + 'firmaadi' => $this->order['name'], + 'Email' => $this->order['email'], + 'amount' => $this->order['amount'], + 'oid' => $this->order['id'], + 'okUrl' => $this->order['success_url'], + 'failUrl' => $this->order['fail_url'], + 'rnd' => $this->order['rand'], + 'lang' => $this->order['lang'], + 'currency' => 949, + ], + ]; + $this->assertEquals($form, $this->pos->get3DFormData()); + } + + public function testCheck3DHash() + { + $data = [ + "md" => "478719:0373D10CFD8BDED34FA0546D27D5BE76F8BA4A947D1EC499102AE97B880EB1B9:4242:##400902568", + "cavv" => "BwAQAhIYRwEAABWGABhHEE6v5IU=", + "AuthCode" => "", + "oid" => "880", + "mdStatus" => "4", + "eci" => "06", + "clientid" => "400902568", + "rnd" => "hDx50d0cq7u1vbpWQMae", + "ProcReturnCode" => "N7", + "Response" => "Declined", + "HASH" => "D+B5fFWXEWFqVSkwotyuTPUW800=", + "HASHPARAMS" => "clientid:oid:AuthCode:ProcReturnCode:Response:mdStatus:cavv:eci:md:rnd:", + "HASHPARAMSVAL" => "400902568880N7Declined4BwAQAhIYRwEAABWGABhHEE6v5IU=06478719:0373D10CFD8BDED34FA0546D27D5BE76F8BA4A947D1EC499102AE97B880EB1B9:4242:##400902568hDx50d0cq7u1vbpWQMae", + ]; + + $this->assertTrue($this->pos->check3DHash($data)); + + $data['mdStatus'] = ''; + $this->assertFalse($this->pos->check3DHash($data)); + } + + public function testCreateRegularPaymentXML() + { + $order = [ + 'id' => '2020110828BC', + 'email' => 'samp@iexample.com', + 'name' => 'john doe', + 'user_id' => '1535', + 'ip' => '192.168.1.0', + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + ]; + + + $card = new CreditCardEstPos('5555444433332222', '22', '01', '123', 'ahmet'); + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_PAY, $card); + + $actualXML = $pos->createRegularPaymentXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPaymentXMLData($pos->getOrder(), $pos->getCard(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData); + } + + public function testCreateRegularPostXML() + { + $order = [ + 'id' => '2020110828BC', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_POST_PAY); + + $actualXML = $pos->createRegularPostXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPostXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData); + } + + public function testCreate3DPaymentXML() + { + + $order = [ + 'id' => '2020110828BC', + 'email' => 'samp@iexample.com', + 'name' => 'john doe', + 'user_id' => '1535', + 'ip' => '192.168.1.0', + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + 'success_url' => 'http://localhost/finansbank-payfor/3d/response.php', + 'fail_url' => 'http://localhost/finansbank-payfor/3d/response.php', + ]; + $responseData = [ + 'md' => '1', + 'xid' => '100000005xid', + 'eci' => '100000005eci', + 'cavv' => 'cavv', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_PAY); + + $actualXML = $pos->create3DPaymentXML($responseData); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSample3DPaymentXMLData($pos->getOrder(), $pos->getAccount(), $responseData); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData); + } + + public function testCreateStatusXML() + { + $order = [ + 'id' => '2020110828BC', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_STATUS); + + $actualXML = $pos->createStatusXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleStatusXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData); + } + + + public function testCreateCancelXML() + { + $order = [ + 'id' => '2020110828BC', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_CANCEL); + + $actualXML = $pos->createCancelXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleCancelXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData); + } + + public function testCreateRefundXML() + { + $order = [ + 'id' => '2020110828BC', + 'amount' => 50 + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_REFUND); + + $actualXML = $pos->createRefundXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRefundXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData); + } + + /** + * @param $order + * @param CreditCardEstPos $card + * @param EstPosAccount $account + * + * @return array + */ + private function getSampleRegularPaymentXMLData($order, $card, $account) + { + return [ + 'Name' => $account->getUsername(), + 'Password' => $account->getPassword(), + 'ClientId' => $account->getClientId(), + 'Type' => 'Auth', + 'IPAddress' => $order->ip, + 'Email' => $order->email, + 'OrderId' => $order->id, + 'UserId' => isset($order->user_id) ? $order->user_id : null, + 'Total' => $order->amount, + 'Currency' => $order->currency, + 'Taksit' => $order->installment, + 'CardType' => $card->getType(), + 'Number' => $card->getNumber(), + 'Expires' => $card->getExpirationDate(), + 'Cvv2Val' => $card->getCvv(), + 'Mode' => 'P', + 'GroupId' => '', + 'TransId' => '', + 'BillTo' => [ + 'Name' => $order->name ? $order->name : null, + ], + ]; + } + + /** + * @param $order + * @param EstPosAccount $account + * + * @return array + */ + private function getSampleRegularPostXMLData($order, $account) + { + return [ + 'Name' => $account->getUsername(), + 'Password' => $account->getPassword(), + 'ClientId' => $account->getClientId(), + 'Type' => 'PostAuth', + 'OrderId' => $order->id, + ]; + } + + /** + * @param $order + * @param EstPosAccount $account + * @param array $responseData + * + * @return array + */ + private function getSample3DPaymentXMLData($order, $account, array $responseData) + { + $requestData = [ + 'Name' => $account->getUsername(), + 'Password' => $account->getPassword(), + 'ClientId' => $account->getClientId(), + 'Type' => 'Auth', + 'IPAddress' => $order->ip, + 'Email' => $order->email, + 'OrderId' => $order->id, + 'UserId' => isset($order->user_id) ? $order->user_id : null, + 'Total' => $order->amount, + 'Currency' => $order->currency, + 'Taksit' => $order->installment, + 'Number' => $responseData['md'], + 'Expires' => '', + 'Cvv2Val' => '', + 'PayerTxnId' => $responseData['xid'], + 'PayerSecurityLevel' => $responseData['eci'], + 'PayerAuthenticationCode' => $responseData['cavv'], + 'CardholderPresentCode' => '13', + 'Mode' => 'P', + 'GroupId' => '', + 'TransId' => '', + ]; + if (isset($order->name)) { + $requestData['BillTo'] = [ + 'Name' => $order->name, + ]; + } + return $requestData; + } + + /** + * @param $order + * @param EstPosAccount $account + * + * @return array + */ + private function getSampleStatusXMLData($order, $account) + { + return [ + 'Name' => $account->getUsername(), + 'Password' => $account->getPassword(), + 'ClientId' => $account->getClientId(), + 'OrderId' => $order->id, + 'Extra' => [ + 'ORDERSTATUS' => 'QUERY', + ], + ]; + } + + /** + * @param $order + * @param EstPosAccount $account + * + * @return array + */ + private function getSampleCancelXMLData($order, $account) + { + return [ + 'Name' => $account->getUsername(), + 'Password' => $account->getPassword(), + 'ClientId' => $account->getClientId(), + 'OrderId' => $order->id, + 'Type' => 'Void', + ]; + } + + /** + * @param $order + * @param EstPosAccount $account + * + * @return array + */ + private function getSampleRefundXMLData($order, $account) + { + $data = [ + 'Name' => $account->getUsername(), + 'Password' => $account->getPassword(), + 'ClientId' => $account->getClientId(), + 'OrderId' => $order->id, + 'Type' => 'Credit', + ]; + + if ($order->amount) { + $data['Total'] = $order->amount; + } + + return $data; + } +} diff --git a/tests/Gateways/GarantiPosTest.php b/tests/Gateways/GarantiPosTest.php new file mode 100644 index 00000000..13321076 --- /dev/null +++ b/tests/Gateways/GarantiPosTest.php @@ -0,0 +1,519 @@ +config = require __DIR__ . '/../../config/pos.php'; + + $this->account = AccountFactory::createGarantiPosAccount('garanti', '7000679', 'PROVAUT', '123qweASD/', '30691298', '3d', '12345678', 'PROVRFN', '123qweASD/'); + + $this->card = new CreditCardGarantiPos('5555444433332222', '21', '12', '122'); + + $this->order = [ + 'id' => 'order222', + 'name' => 'siparis veren', + 'email' => 'test@test.com', + 'amount' => '100.25', + 'installment' => 0, + 'currency' => 'TRY', + 'success_url' => 'https://domain.com/success', + 'fail_url' => 'https://domain.com/fail_url', + 'lang' => 'tr', + 'rand' => microtime(), + 'ip' => '156.155.154.153', + ]; + + $this->pos = PosFactory::createPosGateway($this->account); + $this->pos->setTestMode(true); + + $this->xmlDecoder = new XmlEncoder(); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->account->getBank()], $this->pos->getConfig()); + $this->assertEquals($this->account, $this->pos->getAccount()); + $this->assertNotEmpty($this->pos->getCurrencies()); + } + + public function testPrepare() + { + + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + $this->assertEquals($this->card, $this->pos->getCard()); + } + + public function testGet3DFormData() + { + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + + $form = [ + 'gateway' => $this->config['banks'][$this->account->getBank()]['urls']['gateway']['test'], + ]; + $actualForm = $this->pos->get3DFormData(); + $this->assertNotEmpty($actualForm['inputs']); + + unset($actualForm['inputs']); + $this->assertEquals($form, $actualForm); + } + + public function testCreateRegularPaymentXML() + { + $order = [ + 'id' => '2020110828BC', + 'email' => 'samp@iexample.com', + 'name' => 'john doe', + 'user_id' => '1535', + 'ip' => '192.168.1.0', + 'amount' => 100.01, + 'installment' => 0, + 'currency' => 'TRY', + ]; + + + $card = new CreditCardGarantiPos('5555444433332222', '22', '01', '123', 'ahmet'); + $pos = PosFactory::createPosGateway($this->account); + $pos->setTestMode(true); + $pos->prepare($order, AbstractGateway::TX_PAY, $card); + + $actualXML = $pos->createRegularPaymentXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPaymentXMLData($pos->getOrder(), $pos->getCard(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreateRegularPostXML() + { + $order = [ + 'id' => '2020110828BC', + 'ref_ret_num' => '831803579226', + 'currency' => 'TRY', + 'amount' => 100.01, + 'email' => 'samp@iexample.com', + 'ip' => '192.168.1.0', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->setTestMode(true); + $pos->prepare($order, AbstractGateway::TX_POST_PAY); + + $actualXML = $pos->createRegularPostXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPostXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreate3DPaymentXML() + { + $order = [ + 'id' => '2020110828BC', + 'email' => 'samp@iexample.com', + 'name' => 'john doe', + 'user_id' => '1535', + 'ip' => '192.168.1.0', + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + 'success_url' => 'http://localhost/finansbank-payfor/3d/response.php', + 'fail_url' => 'http://localhost/finansbank-payfor/3d/response.php', + ]; + $responseData = [ + 'orderid' => '2020110828BC', + 'md' => '1', + 'xid' => '100000005xid', + 'eci' => '100000005eci', + 'cavv' => 'cavv', + 'txncurrencycode' => 'txncurrencycode', + 'txnamount' => 'txnamount', + 'txntype' => 'txntype', + 'customeripaddress' => 'customeripaddress', + 'customeremailaddress' => 'customeremailaddress', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->setTestMode(true); + $pos->prepare($order, AbstractGateway::TX_PAY); + + $actualXML = $pos->create3DPaymentXML($responseData); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSample3DPaymentXMLData($pos->getOrder(), $pos->getAccount(), $responseData); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreateStatusXML() + { + $order = [ + 'id' => '2020110828BC', + 'currency' => 'TRY', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->setTestMode(true); + $pos->prepare($order, AbstractGateway::TX_STATUS); + + $actualXML = $pos->createStatusXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleStatusXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + + public function testCreateCancelXML() + { + $order = [ + 'id' => '2020110828BC', + 'currency' => 'TRY', + 'amount' => 10.01, + 'ref_ret_num' => '831803579226', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->setTestMode(true); + $pos->prepare($order, AbstractGateway::TX_CANCEL); + + $actualXML = $pos->createCancelXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleCancelXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreateRefundXML() + { + $order = [ + 'id' => '2020110828BC', + 'currency' => 'TRY', + 'amount' => 10.01, + 'ref_ret_num' => '831803579226', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->setTestMode(true); + $pos->prepare($order, AbstractGateway::TX_REFUND); + + $actualXML = $pos->createRefundXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRefundXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['Transaction']); + } + + /** + * @param $order + * @param CreditCardGarantiPos $card + * @param GarantiPosAccount $account + * + * @return array + */ + private function getSampleRegularPaymentXMLData($order, $card, $account) + { + return [ + 'Mode' => 'TEST', + 'Version' => GarantiPos::API_VERSION, + 'Terminal' => [ + 'ProvUserID' => $account->getUsername(), + 'UserID' => $account->getUsername(), + 'HashData' => 'F0641E566B7B98260FD1608D1DF81E8D55461877', + 'ID' => $account->getTerminalId(), + 'MerchantID' => $account->getTerminalId(), + ], + 'Customer' => [ + 'IPAddress' => $order->ip, + 'EmailAddress' => $order->email, + ], + 'Card' => [ + 'Number' => $card->getNumber(), + 'ExpireDate' => $card->getExpirationDate(), + 'CVV2' => $card->getCvv(), + ], + 'Order' => [ + 'OrderID' => $order->id, + 'GroupID' => '', + 'AddressList' => [ + 'Address' => [ + 'Type' => 'S', + 'Name' => $order->name, + 'LastName' => '', + 'Company' => '', + 'Text' => '', + 'District' => '', + 'City' => '', + 'PostalCode' => '', + 'Country' => '', + 'PhoneNumber' => '', + ], + ], + ], + 'Transaction' => [ + 'Type' => 'sales', + 'InstallmentCnt' => $order->installment, + 'Amount' => $order->amount, + 'CurrencyCode' => $order->currency, + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + 'Description' => '', + 'OriginalRetrefNum' => '', + ], + ]; + } + + /** + * @param $order + * @param GarantiPosAccount $account + * + * @return array + */ + private function getSampleRegularPostXMLData($order, $account) + { + return [ + 'Mode' => 'TEST', + 'Version' => GarantiPos::API_VERSION, + 'Terminal' => [ + 'ProvUserID' => $account->getUsername(), + 'UserID' => $account->getUsername(), + 'HashData' => '7598B3D1A15C45095CD139E9CFD780B050D1C4AA', + 'ID' => $account->getTerminalId(), + 'MerchantID' => $account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $order->ip, + 'EmailAddress' => $order->email, + ], + 'Order' => [ + 'OrderID' => $order->id, + ], + 'Transaction' => [ + 'Type' => 'postauth', + 'Amount' => $order->amount, + 'CurrencyCode' => $order->currency, + 'OriginalRetrefNum' => $order->ref_ret_num, + ], + ]; + } + + /** + * @param $order + * @param GarantiPosAccount $account + * @param array $responseData + * + * @return array + */ + private function getSample3DPaymentXMLData($order, $account, array $responseData) + { + return [ + 'Mode' => 'TEST', + 'Version' => GarantiPos::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $account->getUsername(), + 'UserID' => $account->getUsername(), + 'HashData' => '7598B3D1A15C45095CD139E9CFD780B050D1C4AA', + 'ID' => $account->getTerminalId(), + 'MerchantID' => $account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $responseData['customeripaddress'], + 'EmailAddress' => $responseData['customeremailaddress'], + ], + 'Card' => [ + 'Number' => '', + 'ExpireDate' => '', + 'CVV2' => '', + ], + 'Order' => [ + 'OrderID' => $responseData['orderid'], + 'GroupID' => '', + 'AddressList' => [ + 'Address' => [ + 'Type' => 'B', + 'Name' => $order->name, + 'LastName' => '', + 'Company' => '', + 'Text' => '', + 'District' => '', + 'City' => '', + 'PostalCode' => '', + 'Country' => '', + 'PhoneNumber' => '', + ], + ], + ], + 'Transaction' => [ + 'Type' => $responseData['txntype'], + 'InstallmentCnt' => $order->installment, + 'Amount' => $responseData['txnamount'], + 'CurrencyCode' => $responseData['txncurrencycode'], + 'CardholderPresentCode' => '13', + 'MotoInd' => 'N', + 'Secure3D' => [ + 'AuthenticationCode' => $responseData['cavv'], + 'SecurityLevel' => $responseData['eci'], + 'TxnID' => $responseData['xid'], + 'Md' => $responseData['md'], + ], + ], + ]; + } + + /** + * @param $order + * @param GarantiPosAccount $account + * + * @return array + */ + private function getSampleStatusXMLData($order, $account) + { + return [ + 'Mode' => 'TEST', + 'Version' => GarantiPos::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $account->getUsername(), + 'UserID' => $account->getUsername(), + 'HashData' => '8DD74209DEEB7D333105E1C69998A827419A3B04', + 'ID' => $account->getTerminalId(), + 'MerchantID' => $account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $order->ip, + 'EmailAddress' => $order->email, + ], + 'Order' => [ + 'OrderID' => $order->id, + 'GroupID' => '', + ], + 'Card' => [ + 'Number' => '', + 'ExpireDate' => '', + 'CVV2' => '', + ], + 'Transaction' => [ + 'Type' => 'orderinq', + 'InstallmentCnt' => '', + 'Amount' => $order->amount, + 'CurrencyCode' => $order->currency, + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + ], + ]; + } + + /** + * @param $order + * @param GarantiPosAccount $account + * + * @return array + */ + private function getSampleCancelXMLData($order, $account) + { + return [ + 'Mode' => 'TEST', + 'Version' => GarantiPos::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $account->getRefundUsername(), + 'UserID' => $account->getRefundUsername(), + 'HashData' => '8DD74209DEEB7D333105E1C69998A827419A3B04', + 'ID' => $account->getTerminalId(), + 'MerchantID' => $account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $order->ip, + 'EmailAddress' => $order->email, + ], + 'Order' => [ + 'OrderID' => $order->id, + 'GroupID' => '', + ], + 'Transaction' => [ + 'Type' => 'void', + 'InstallmentCnt' => $order->installment, + 'Amount' => $order->amount, + 'CurrencyCode' => $order->currency, + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + 'OriginalRetrefNum' => $order->ref_ret_num, + ], + ]; + + } + + /** + * @param $order + * @param GarantiPosAccount $account + * + * @return array + */ + private function getSampleRefundXMLData($order, $account) + { + return [ + 'Mode' => 'TEST', + 'Version' => GarantiPos::API_VERSION, + 'ChannelCode' => '', + 'Terminal' => [ + 'ProvUserID' => $account->getRefundUsername(), + 'UserID' => $account->getRefundUsername(), + 'HashData' => '8DD74209DEEB7D333105E1C69998A827419A3B04', + 'ID' => $account->getTerminalId(), + 'MerchantID' => $account->getClientId(), + ], + 'Customer' => [ + 'IPAddress' => $order->ip, + 'EmailAddress' => $order->email, + ], + 'Order' => [ + 'OrderID' => $order->id, + 'GroupID' => '', + ], + 'Transaction' => [ + 'Type' => 'refund', + 'InstallmentCnt' => '', + 'Amount' => $order->amount, + 'CurrencyCode' => $order->currency, + 'CardholderPresentCode' => '0', + 'MotoInd' => 'N', + 'OriginalRetrefNum' => $order->ref_ret_num, + ], + ]; + } +} diff --git a/tests/Gateways/PayForTest.php b/tests/Gateways/PayForTest.php new file mode 100644 index 00000000..594b12a2 --- /dev/null +++ b/tests/Gateways/PayForTest.php @@ -0,0 +1,396 @@ +config = require __DIR__ . '/../../config/pos.php'; + + $this->threeDAccount = AccountFactory::createPayForAccount('qnbfinansbank-payfor', '085300000009704', 'QNB_API_KULLANICI_3DPAY', 'UcBN0', '3d', '12345678'); + + $this->card = new CreditCardPayFor('5555444433332222', '22', '01', '123', 'ahmet'); + + $this->order = [ + 'id' => '2020110828BC', + 'email' => 'mail@customer.com', // optional + 'name' => 'John Doe', // optional + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + 'success_url' => 'http://localhost/finansbank-payfor/3d/response.php', + 'fail_url' => 'http://localhost/finansbank-payfor/3d/response.php', + 'rand' => '0.43625700 1604831630', + 'hash' => 'zmSUxYPhmCj7QOzqpk/28LuE1Oc=', + 'lang' => PayForPos::LANG_TR, + ]; + + $this->pos = PosFactory::createPosGateway($this->threeDAccount); + + $this->pos->setTestMode(true); + + $this->xmlDecoder = new XmlEncoder(); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->threeDAccount->getBank()], $this->pos->getConfig()); + $this->assertEquals($this->threeDAccount, $this->pos->getAccount()); + $this->assertNotEmpty($this->pos->getCurrencies()); + $this->assertEquals($this->config['banks'][$this->threeDAccount->getBank()]['urls']['gateway']['test'], $this->pos->get3DGatewayURL()); + $this->assertEquals($this->config['banks'][$this->threeDAccount->getBank()]['urls']['test'], $this->pos->getApiURL()); + + } + + public function testSetTestMode() + { + $this->pos->setTestMode(false); + $this->assertFalse($this->pos->isTestMode()); + $this->pos->setTestMode(true); + $this->assertTrue($this->pos->isTestMode()); + } + + + public function testPrepare() + { + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + $this->assertEquals($this->card, $this->pos->getCard()); + } + + public function testGet3DFormData() + { + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + $order = $this->pos->getOrder(); + $form = [ + 'gateway' => $this->config['banks'][$this->threeDAccount->getBank()]['urls']['gateway']['test'], + 'inputs' => [ + 'MbrId' => PayForPos::MBR_ID, + 'MerchantID' => $this->threeDAccount->getClientId(), + 'UserCode' => $this->threeDAccount->getUsername(), + 'OrderId' => $order->id, + 'Lang' => $order->lang, + 'SecureType' => '3DModel', + 'TxnType' => 'Auth', + 'PurchAmount' => $order->amount, + 'InstallmentCount' => $order->installment, + 'Currency' => $order->currency, + 'OkUrl' => $order->success_url, + 'FailUrl' => $order->fail_url, + 'Rnd' => $order->rand, + 'Hash' => $this->pos->create3DHash(), + ] + ]; + $this->assertEquals($form, $this->pos->get3DFormData()); + } + + public function testCreate3DHash() + { + $order = [ + 'id' => '2020110828BC', + 'amount' => 100.01, + 'installment' => '0', + 'success_url' => 'http://localhost/finansbank-payfor/3d/response.php', + 'fail_url' => 'http://localhost/finansbank-payfor/3d/response.php', + 'rand' => '0.43625700 1604831630', + ]; + $hash = 'zmSUxYPhmCj7QOzqpk/28LuE1Oc='; + $this->pos->prepare($order, AbstractGateway::TX_PAY); + $this->assertEquals($hash, $this->pos->create3DHash()); + } + + public function testCheck3DHash() + { + $data = [ + "OrderId" => '2020110828BC', + "AuthCode" => "", + "3DStatus" => "1", + "ProcReturnCode" => "V033", + "ResponseRnd" => "PF637404392360825218", + "ResponseHash" => "ogupUOYY6vQ4+opqDqgLk3DLK7I=", + ]; + + $this->assertTrue($this->pos->check3DHash($data)); + + $data['3DStatus'] = ''; + $this->assertFalse($this->pos->check3DHash($data)); + } + + public function testCreateRegularPaymentXML() + { + + $order = [ + 'id' => '2020110828BC', + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + 'lang' => PayForPos::LANG_TR, + ]; + + $card = new CreditCardPayFor('5555444433332222', '22', '01', '123', 'ahmet'); + $pos = PosFactory::createPosGateway($this->threeDAccount); + $pos->prepare($order, AbstractGateway::TX_PAY, $card); + + $actualXML = $pos->createRegularPaymentXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPaymentXMLData($pos->getOrder(), $pos->getCard(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreateRegularPostXML() + { + $order = [ + 'id' => '2020110828BC', + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + 'lang' => PayForPos::LANG_TR, + ]; + + $pos = PosFactory::createPosGateway($this->threeDAccount); + $pos->prepare($order, AbstractGateway::TX_POST_PAY); + + $actualXML = $pos->createRegularPostXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPostXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreate3DPaymentXML() + { + + $order = [ + 'id' => '2020110828BC', + ]; + $responseData = ['RequestGuid' => '1000000057437884']; + + $pos = PosFactory::createPosGateway($this->threeDAccount); + $pos->prepare($order, AbstractGateway::TX_PAY); + + $actualXML = $pos->create3DPaymentXML($responseData); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSample3DPaymentXMLData($pos->getOrder(), $pos->getAccount(), $responseData); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreateStatusXML() + { + $order = [ + 'id' => '2020110828BC', + ]; + + $pos = PosFactory::createPosGateway($this->threeDAccount); + $pos->prepare($order, AbstractGateway::TX_STATUS); + + $actualXML = $pos->createStatusXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleStatusXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreateCancelXML() + { + $order = [ + 'id' => '2020110828BC', + 'currency' => 'TRY', + ]; + + $pos = PosFactory::createPosGateway($this->threeDAccount); + $pos->prepare($order, AbstractGateway::TX_CANCEL); + + $actualXML = $pos->createCancelXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleCancelXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + public function testCreateRefundXML() + { + $order = [ + 'id' => '2020110828BC', + 'currency' => 'TRY', + 'amount' => 10.1, + ]; + + $pos = PosFactory::createPosGateway($this->threeDAccount); + $pos->prepare($order, AbstractGateway::TX_REFUND); + + $actualXML = $pos->createRefundXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRefundXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + } + + /** + * @param $order + * @param AbstractCreditCard $card + * @param PayForAccount $account + * + * @return array + */ + private function getSampleRegularPaymentXMLData($order, $card, $account) + { + return [ + 'MbrId' => PayForPos::MBR_ID, + 'MerchantId' => $account->getClientId(), + 'UserCode' => $account->getUsername(), + 'UserPass' => $account->getPassword(), + 'MOTO' => PayForPos::MOTO, + 'OrderId' => $order->id, + 'SecureType' => 'NonSecure', + 'TxnType' => 'Auth', + 'PurchAmount' => $order->amount, + 'Currency' => $order->currency, + 'InstallmentCount' => $order->installment, + 'Lang' => 'tr', + 'CardHolderName' => $card->getHolderName(), + 'Pan' => $card->getNumber(), + 'Expiry' => $card->getExpirationDate(), + 'Cvv2' => $card->getCvv() + ]; + } + + /** + * @param $order + * @param PayForAccount $account + * + * @return array + */ + private function getSampleRegularPostXMLData($order, $account) + { + return [ + 'MbrId' => PayForPos::MBR_ID, + 'MerchantId' => $account->getClientId(), + 'UserCode' => $account->getUsername(), + 'UserPass' => $account->getPassword(), + 'OrgOrderId' => $order->id, + 'SecureType' => 'NonSecure', + 'TxnType' => 'PostAuth', + 'PurchAmount' => $order->amount, + 'Currency' => $order->currency, + 'Lang' => 'tr', + ]; + } + + /** + * @param $order + * @param PayForAccount $account + * @param array $responseData + * + * @return array + */ + private function getSample3DPaymentXMLData($order, $account, array $responseData) + { + return [ + 'RequestGuid' => $responseData['RequestGuid'], + 'UserCode' => $account->getUsername(), + 'UserPass' => $account->getPassword(), + 'OrderId' => $order->id, + 'SecureType' => '3DModelPayment', + ]; + } + + /** + * @param $order + * @param PayForAccount $account + * + * @return array + */ + private function getSampleStatusXMLData($order, $account) + { + return [ + 'MbrId' => PayForPos::MBR_ID, + 'MerchantId' => $account->getClientId(), + 'UserCode' => $account->getUsername(), + 'UserPass' => $account->getPassword(), + 'OrgOrderId' => $order->id, + 'SecureType' => 'Inquiry', + 'Lang' => 'tr', + 'TxnType' => 'OrderInquiry', + ]; + } + + /** + * @param $order + * @param PayForAccount $account + * + * @return array + */ + private function getSampleCancelXMLData($order, $account) + { + return [ + 'MbrId' => PayForPos::MBR_ID, + 'MerchantId' => $account->getClientId(), + 'UserCode' => $account->getUsername(), + 'UserPass' => $account->getPassword(), + 'OrgOrderId' => $order->id, + 'SecureType' => 'NonSecure', + 'Lang' => 'tr', + 'TxnType' => 'Void', + 'Currency' => $order->currency + ]; + } + + /** + * @param $order + * @param PayForAccount $account + * + * @return array + */ + private function getSampleRefundXMLData($order, $account) + { + return [ + 'MbrId' => PayForPos::MBR_ID, + 'MerchantId' => $account->getClientId(), + 'UserCode' => $account->getUsername(), + 'UserPass' => $account->getPassword(), + 'OrgOrderId' => $order->id, + 'SecureType' => 'NonSecure', + 'Lang' => 'tr', + 'TxnType' => 'Refund', + 'PurchAmount' => $order->amount, + 'Currency' => $order->currency + ]; + } +} diff --git a/tests/PosNetCryptTest.php b/tests/Gateways/PosNetCryptTest.php similarity index 67% rename from tests/PosNetCryptTest.php rename to tests/Gateways/PosNetCryptTest.php index 6ef38893..575e830a 100644 --- a/tests/PosNetCryptTest.php +++ b/tests/Gateways/PosNetCryptTest.php @@ -1,8 +1,8 @@ crypt->decrypt($data, $key); - $this->assertEquals($expected_output, $dc); + $this->assertEquals($expectedOutput, $dc); $data = '1974BC4B9984FF173F9DF305090E44241243CCC9648349C0D607AECACCB55C1F1ED47452B3AD90785F9BCC6AC7E65450D4E72F31B9FC8F9F55A7D109C2BE966C6DD3F3DE12B3457FF0C6FA8BCDBB4B8E5341C1C3DA327992C28354EB3B5C62472C06B8FB6DF34E351206D2CCD5E323FA26EC3EFF2C25656C74C836954F193E634AF391761F88B5D53DBE1FC61A89DB713DFC983D9605D080ACA857196DD3E7B3C52BFCC914CE47961D76B590ECB34B28113A6E4FAA572958D836B09546A9A62D24F829FE18628DF84504FB02'; - $expected_output = '6706598320;67825768;100;00;00000000000000000892;0;0;https://setmpos.ykb.com/PosnetWebService/YKBTransactionService;posnettest.ykb.com;2225;N;9;None 3D - Secure Transaction;1586175747626;TL'; + $expectedOutput = '6706598320;67825768;100;00;00000000000000000892;0;0;https://setmpos.ykb.com/PosnetWebService/YKBTransactionService;posnettest.ykb.com;2225;N;9;None 3D - Secure Transaction;1586175747626;TL'; $dc = $this->crypt->decrypt($data, $key); - $this->assertEquals($expected_output, $dc); + $this->assertEquals($expectedOutput, $dc); } } diff --git a/tests/Gateways/PosNetTest.php b/tests/Gateways/PosNetTest.php new file mode 100644 index 00000000..45127e08 --- /dev/null +++ b/tests/Gateways/PosNetTest.php @@ -0,0 +1,421 @@ +config = require __DIR__ . '/../../config/pos.php'; + + $this->account = AccountFactory::createPosNetAccount('yapikredi', '6706598320', 'XXXXXX', 'XXXXXX', '67005551', '27426', '3d', '10,10,10,10,10,10,10,10'); + + $this->card = new CreditCardPosNet('5555444433332222', '21', '12', '122', 'ahmet'); + + $this->order = [ + 'id' => 'YKB_TST_190620093100_024', + 'name' => 'siparis veren', + 'email' => 'test@test.com', + 'amount' => '1.75', + 'installment' => 0, + 'currency' => 'TL', + 'success_url' => 'https://domain.com/success', + 'fail_url' => 'https://domain.com/fail_url', + 'lang' => 'tr', + 'rand' => microtime(), + ]; + + $this->pos = PosFactory::createPosGateway($this->account); + + $this->pos->setTestMode(true); + + $this->xmlDecoder = new XmlEncoder(); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->account->getBank()], $this->pos->getConfig()); + $this->assertEquals($this->account, $this->pos->getAccount()); + $this->assertNotEmpty($this->pos->getCurrencies()); + } + + public function testPrepare() + { + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + $this->assertEquals($this->card, $this->pos->getCard()); + } + + public function testCreate3DHash(){ + + $this->pos->prepare($this->order, AbstractGateway::TX_PAY, $this->card); + $this->assertEquals('J/7/Xprj7F/KDf98luVfIGyUPRQzUCqGwpmvz3KT7oQ=', $this->pos->create3DHash()); + } + + public function testVerifyResponseMAC(){ + + $newOrder = $this->order; + $newOrder['id'] = '895'; + $newOrder['amount'] = 1; + $newOrder['currency'] = 'TL'; + + $account = AccountFactory::createPosNetAccount('yapikredi', '6706598320', 'XXXXXX', 'XXXXXX', '67825768', '27426', '3d', '10,10,10,10,10,10,10,10'); + + $pos = PosFactory::createPosGateway($account); + $pos->setTestMode(true); + + $pos->prepare($newOrder, AbstractGateway::TX_PAY); + $data = (object) [ + 'mdStatus' => '9', + 'mac' => 'U2kU/JWjclCvKZjILq8xBJUXhyB4DswKvN+pKfxl0u0=', + ]; + $this->assertTrue($pos->verifyResponseMAC($data)); + + $newOrder['id'] = '800'; + $pos->prepare($newOrder, AbstractGateway::TX_PAY); + $data = (object) [ + 'mdStatus' => '9', + 'mac' => 'U2kU/JWjclCvKZjILq8xBJUXhyB4DswKvN+pKfxl0u0=', + ]; + $this->assertFalse($pos->verifyResponseMAC($data)); + } + + public function testCreateRegularPaymentXML() + { + $order = [ + 'id' => '2020110828BC', + 'amount' => 100.01, + 'installment' => '2', + 'currency' => 'TRY', + ]; + + + $card = new CreditCardPosNet('5555444433332222', '22', '01', '123', 'ahmet'); + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_PAY, $card); + + $actualXML = $pos->createRegularPaymentXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPaymentXMLData($pos->getOrder(), $pos->getCard(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['sale']); + } + + public function testCreateRegularPostXML() + { + $order = [ + 'id' => '2020110828BC', + 'host_ref_num' => '019676067890000191', + 'amount' => 10.02, + 'currency' => 'TRY', + 'installment' => '2', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_POST_PAY); + + $actualXML = $pos->createRegularPostXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRegularPostXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['capt']); + } + + public function testCreate3DPaymentXML() + { + + $order = [ + 'id' => '2020110828BC', + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + ]; + $responseData = [ + 'BankPacket' => 'F61E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F30', + 'MerchantPacket' => 'E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F30', + 'Sign' => '9998F61E1D0C0FB6EC5203A748124F30', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_PAY); + + $actualXML = $pos->create3DPaymentXML($responseData); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSample3DPaymentXMLData($pos->getAccount(), $responseData); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['oosTranData']); + } + + public function testCreate3DResolveMerchantDataXML() + { + + $order = [ + 'id' => '2020110828BC', + 'amount' => 100.01, + 'installment' => '0', + 'currency' => 'TRY', + ]; + $responseData = [ + 'BankPacket' => 'F61E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F30', + 'MerchantPacket' => 'E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F30', + 'Sign' => '9998F61E1D0C0FB6EC5203A748124F30', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_PAY); + + $actualXML = $pos->create3DResolveMerchantDataXML($responseData); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleResolveMerchantDataXMLData($pos->getAccount(), $responseData); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['oosResolveMerchantData']); + } + + public function testCreateStatusXML() + { + $order = [ + 'id' => '2020110828BC', + 'type' => 'status', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_STATUS); + + $actualXML = $pos->createStatusXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleStatusXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['agreement']); + } + + + public function testCreateCancelXML() + { + $order = [ + 'id' => '2020110828BC', + 'host_ref_num' => '2020110828BCNUM', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_CANCEL); + + $actualXML = $pos->createCancelXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleCancelXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['reverse']); + } + + public function testCreateRefundXML() + { + $order = [ + 'id' => '2020110828BC', + 'amount' => 50, + 'currency' => 'TRY', + ]; + + $pos = PosFactory::createPosGateway($this->account); + $pos->prepare($order, AbstractGateway::TX_REFUND); + + $actualXML = $pos->createRefundXML(); + $actualData = $this->xmlDecoder->decode($actualXML, 'xml'); + + $expectedData = $this->getSampleRefundXMLData($pos->getOrder(), $pos->getAccount()); + $this->assertEquals($expectedData, $actualData); + //$this->assertEquals([], $actualData['return']); + } + + /** + * @param $order + * @param CreditCardPosNet $card + * @param PosNetAccount $account + * + * @return array + */ + private function getSampleRegularPaymentXMLData($order, $card, $account) + { + return [ + 'mid' => $account->getClientId(), + 'tid' => $account->getTerminalId(), + 'tranDateRequired' => '1', + 'sale' => [ + 'orderID' => $order->id, + 'installment' => $order->installment, + 'amount' => $order->amount, + 'currencyCode' => $order->currency, + 'ccno' => $card->getNumber(), + 'expDate' => $card->getExpirationDate(), + 'cvc' => $card->getCvv(), + ], + ]; + } + + /** + * @param $order + * @param PosNetAccount $account + * + * @return array + */ + private function getSampleRegularPostXMLData($order, $account) + { + return [ + 'mid' => $account->getClientId(), + 'tid' => $account->getTerminalId(), + 'tranDateRequired' => '1', + 'capt' => [ + 'hostLogKey' => $order->host_ref_num, + 'amount' => $order->amount, + 'currencyCode' => $order->currency, + 'installment' => $order->installment, + ], + ]; + } + + /** + * @param PosNetAccount $account + * @param array $responseData + * + * @return array + */ + private function getSample3DPaymentXMLData($account, array $responseData) + { + return [ + 'mid' => $account->getClientId(), + 'tid' => $account->getTerminalId(), + 'oosTranData' => [ + 'bankData' => $responseData['BankPacket'], + 'merchantData' => $responseData['MerchantPacket'], + 'sign' => $responseData['Sign'], + 'wpAmount' => 0, + 'mac' => 'oE7zwV87uOc2DFpGPlr4jQRQ0z9LsxGw56c7vaiZkTo=', + ], + ]; + } + + /** + * @param PosNetAccount $account + * @param array $responseData + * + * @return array + */ + private function getSampleResolveMerchantDataXMLData($account, array $responseData) + { + return [ + 'mid' => $account->getClientId(), + 'tid' => $account->getTerminalId(), + 'oosResolveMerchantData' => [ + 'bankData' => $responseData['BankPacket'], + 'merchantData' => $responseData['MerchantPacket'], + 'sign' => $responseData['Sign'], + 'mac' => 'oE7zwV87uOc2DFpGPlr4jQRQ0z9LsxGw56c7vaiZkTo=', + ], + ]; + } + + /** + * @param $order + * @param PosNetAccount $account + * + * @return array + */ + private function getSampleStatusXMLData($order, $account) + { + return [ + 'mid' => $account->getClientId(), + 'tid' => $account->getTerminalId(), + 'agreement' => [ + 'orderID' => $order->id, + ], + ]; + } + + /** + * @param $order + * @param PosNetAccount $account + * + * @return array + */ + private function getSampleCancelXMLData($order, $account) + { + $requestData = [ + 'mid' => $account->getClientId(), + 'tid' => $account->getTerminalId(), + 'tranDateRequired' => '1', + 'reverse' => [ + 'transaction' => 'sale', + ], + ]; + + //either will work + if (isset($order->host_ref_num)) { + $requestData['reverse']['hostLogKey'] = $order->host_ref_num; + } else { + $requestData['reverse']['orderID'] = PosNet::mapOrderIdToPrefixedOrderId($order->id, $account->getModel()); + } + return $requestData; + } + + /** + * @param $order + * @param PosNetAccount $account + * + * @return array + */ + private function getSampleRefundXMLData($order, $account) + { + $requestData = [ + 'mid' => $account->getClientId(), + 'tid' => $account->getTerminalId(), + 'tranDateRequired' => '1', + 'return' => [ + 'amount' => $order->amount, + 'currencyCode' => $order->currency, + ], + ]; + + if (isset($order->host_ref_num)) { + $requestData['return']['hostLogKey'] = $order->host_ref_num; + } else { + $requestData['return']['orderID'] = $order->id; + } + + return $requestData; + } +} diff --git a/tests/PayForTest.php b/tests/PayForTest.php deleted file mode 100644 index c58d1b87..00000000 --- a/tests/PayForTest.php +++ /dev/null @@ -1,131 +0,0 @@ -config = require __DIR__.'/../config/pos.php'; - - $this->account = (object) [ - 'bank' => 'qnbfinansbank-payfor', - 'model' => '3d', - 'client_id' => '085300000009704', - 'username' => 'QNB_API_KULLANICI_3DPAY', - 'password' => 'UcBN0', - 'store_key' => '12345678', //MerchantPass only needed for 3D payment - 'env' => 'test', - 'lang' => PayForPos::LANG_EN, - ]; - - $this->card = new CreditCardPayFor('5555444433332222', '22', '01', '123', 'ahmet'); - - $this->order = (object) [ - 'id' => '2020110828BC', - 'email' => 'mail@customer.com', // optional - 'name' => 'John Doe', // optional - 'amount' => 100.01, - 'installment' => '0', - 'currency' => 'TRY', - 'success_url' => 'http://localhost/finansbank-payfor/3d/response.php', - 'fail_url' => 'http://localhost/finansbank-payfor/3d/response.php', - 'transaction' => 'pay', // pay => Auth, pre PreAuth, - 'rand' => '0.43625700 1604831630', - 'hash' => 'zmSUxYPhmCj7QOzqpk/28LuE1Oc=', - 'lang' => PayForPos::LANG_TR, - ]; - - $this->payFor = new PayForPos( - $this->config['banks'][$this->account->bank], - $this->account, - $this->config['currencies'] - ); - } - - public function testInit() - { - $this->assertEquals($this->config['banks'][$this->account->bank], $this->payFor->getConfig()); - $this->assertEquals($this->account, $this->payFor->getAccount()); - $this->assertEquals($this->config['currencies'], $this->payFor->getCurrencies()); - } - - public function testPrepare() - { - - $this->payFor->prepare($this->order, $this->card); - $this->assertEquals($this->card, $this->payFor->getCard()); - $this->assertEquals($this->order, $this->payFor->getOrder()); - } - - public function testGet3DFormData() - { - $this->payFor->prepare($this->order, $this->card); - - $form = [ - 'gateway' => $this->config['banks'][$this->account->bank]['urls']['gateway'][$this->account->env], - 'inputs' => [ - 'MbrId' => PayForPos::MBR_ID, - 'MerchantID' => $this->account->client_id, - 'UserCode' => $this->account->username, - 'OrderId' => $this->order->id, - 'Lang' => $this->order->lang, - 'SecureType' => '3DModel', - 'TxnType' => 'Auth', - 'PurchAmount' => $this->order->amount, - 'InstallmentCount' => $this->order->installment, - 'Currency' => $this->order->currency, - 'OkUrl' => $this->order->success_url, - 'FailUrl' => $this->order->fail_url, - 'Rnd' => $this->order->rand, - 'Hash' => $this->payFor->create3DHash(), - ] - ]; - $this->assertEquals($form, $this->payFor->get3DFormData()); - } - - public function testCreate3DHash() - { - $this->payFor->prepare($this->order); - $this->assertEquals($this->order->hash, $this->payFor->create3DHash()); - } - - public function testCheck3DHash() - { - $data = [ - "OrderId" => $this->order->id, - "AuthCode" => "", - "3DStatus" => "1", - "ProcReturnCode" => "V033", - "ResponseRnd" => "PF637404392360825218", - "ResponseHash" => "ogupUOYY6vQ4+opqDqgLk3DLK7I=", - ]; - - $this->assertTrue($this->payFor->check3DHash($data)); - - $data['3DStatus'] = ''; - $this->assertFalse($this->payFor->check3DHash($data)); - } -} diff --git a/tests/PosNetTest.php b/tests/PosNetTest.php deleted file mode 100644 index fbe0a24a..00000000 --- a/tests/PosNetTest.php +++ /dev/null @@ -1,106 +0,0 @@ -account = (object)[ - 'bank' => 'yapikredi', - 'model' => 'regular', - 'client_id' => '6706598320', - 'terminal_id' => '67005551', - 'posnet_id' => '27426', - 'env' => 'test', - 'store_key' => '10,10,10,10,10,10,10,10', - 'model' => '3d' - ]; - - $this->card = new CreditCardPosNet('5555444433332222', '21', '12', '122', 'ahmet'); - - $this->order = (object)[ - 'id' => 'YKB_TST_190620093100_024', - 'name' => 'siparis veren', - 'email' => 'test@test.com', - 'amount' => '1.75', - 'installment' => 0, - 'currency' => 'TL', - 'success_url' => 'https://domain.com/success', - 'fail_url' => 'https://domain.com/fail_url', - 'lang' => 'tr', - 'rand' => microtime() - ]; - - - $this->config = require __DIR__ . '/../config/pos.php'; - $this->posnet = new PosNet( - $this->config['banks'][$this->account->bank], - $this->account, - $this->config['currencies']); - } - - public function testInit() - { - $this->assertEquals($this->config['banks'][$this->account->bank], $this->posnet->getConfig()); - $this->assertEquals($this->account, $this->posnet->getAccount()); - $this->assertEquals($this->config['currencies'], $this->posnet->getCurrencies()); - } - - public function testPrepare() - { - $this->posnet->prepare($this->order, $this->card); - $this->assertEquals($this->card, $this->posnet->getCard()); - $this->assertEquals($this->order, $this->posnet->getOrder()); - } - - public function testCreate3DHash(){ - - $this->posnet->prepare($this->order, $this->card); - $this->assertEquals('J/7/Xprj7F/KDf98luVfIGyUPRQzUCqGwpmvz3KT7oQ=', $this->posnet->create3DHash()); - } - - public function testVerifyResponseMAC(){ - - $order = $this->order; - $order->id = '895'; - $order->amount = 1; - $order->currency = 'TL'; - - $account = $this->account; - $account->client_id = '6706598320'; - $account->terminal_id = '67825768'; - $account->store_key = '10,10,10,10,10,10,10,10'; - - $this->posnet->prepare($order, $account); - $data = (object)[ - 'mdStatus' => '9', - 'mac' => 'U2kU/JWjclCvKZjILq8xBJUXhyB4DswKvN+pKfxl0u0=' - ]; - $this->assertTrue($this->posnet->verifyResponseMAC($data)); - - $order->id = '800'; - $this->posnet->prepare($order, $account); - $data = (object)[ - 'mdStatus' => '9', - 'mac' => 'U2kU/JWjclCvKZjILq8xBJUXhyB4DswKvN+pKfxl0u0=' - ]; - $this->assertFalse($this->posnet->verifyResponseMAC($data)); - } -} diff --git a/tests/PosTest.php b/tests/PosTest.php deleted file mode 100644 index ce5e1715..00000000 --- a/tests/PosTest.php +++ /dev/null @@ -1,89 +0,0 @@ -config = require __DIR__ . '/../config/pos.php'; - $this->account = [ - 'bank' => 'yapikredi', - 'model' => 'regular', - 'client_id' => '6706598320', - 'terminal_id' => '67322946', - 'posnet_id' => '27426', - 'env' => 'test', - 'store_key' => '10,10,10,10,10,10,10,10' - ]; - - $this->card = new CreditCardPosNet('5555444433332222', '21', '12', '122', 'ahmet', 'visa'); - - $this->order = [ - 'id' => 'order222', - 'name' => 'siparis veren', - 'email' => 'test@test.com', - 'amount' => '100.25', - 'installment' => 0, - 'currency' => 'TRY', - 'success_url' => 'https://domain.com/success', - 'fail_url' => 'https://domain.com/fail_url', - 'lang' => 'tr', - 'rand' => microtime() - ]; - - $this->pos = new Pos($this->account); - } - - public function testInit() - { - $this->assertEquals($this->config['banks'][$this->account['bank']], $this->pos->getConfig()); - $this->assertEquals((object)$this->account, $this->pos->getAccount()); - $this->assertEquals($this->config['currencies'], $this->pos->getCurrencies()); - $this->assertInstanceOf(PosNet::class, $this->pos->bank); - } - - public function testCreateXML() - { - $xml_str = $this->createXML($this->order); - $this->assertIsString($xml_str); - } - - public function testXMLStringToObject() - { - $xml_str = $this->createXML(['order' => $this->order]); - $this->assertEquals((object)$this->order, $this->XMLStringToObject($xml_str)); - } - - public function testPrepare() - { - $this->pos->prepare($this->order, $this->card); - $this->assertEquals($this->card, $this->pos->getCard()); - } - - public function testGetGatewayUrl() - { - $this->assertEquals($this->config['banks'][$this->account['bank']]['urls']['gateway'][$this->account['env']], $this->pos->getGatewayUrl()); - } -} From 4af43c1637594321e860afcf17a147629af4de55 Mon Sep 17 00:00:00 2001 From: nuryagdy Date: Sun, 22 Nov 2020 18:29:43 +0300 Subject: [PATCH 2/5] fix refund example --- examples/finansbank-payfor/regular/refund.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/finansbank-payfor/regular/refund.php b/examples/finansbank-payfor/regular/refund.php index b954806d..2a4fcb67 100644 --- a/examples/finansbank-payfor/regular/refund.php +++ b/examples/finansbank-payfor/regular/refund.php @@ -15,6 +15,8 @@ $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_REFUND); +$pos->refund(); + $response = $pos->getResponse(); ?> From 3fd2e34910c6aa1f8c0d7b21162c0e9398d90117 Mon Sep 17 00:00:00 2001 From: nuryagdy Date: Sun, 22 Nov 2020 20:20:37 +0300 Subject: [PATCH 3/5] fix yapikredi example --- examples/ykb/3d/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ykb/3d/index.php b/examples/ykb/3d/index.php index a7033fd8..a9c40364 100644 --- a/examples/ykb/3d/index.php +++ b/examples/ykb/3d/index.php @@ -15,7 +15,7 @@ 'currency' => 'TRY', 'success_url' => $success_url, 'fail_url' => $fail_url, - 'lang' => \Mews\Pos\PosNet::LANG_TR, + 'lang' => \Mews\Pos\Gateways\PosNet::LANG_TR, ]; ?> From b5472f466e35965b3983f5a68e20acd72ded68ef Mon Sep 17 00:00:00 2001 From: nuryagdy Date: Sun, 22 Nov 2020 20:21:18 +0300 Subject: [PATCH 4/5] update yapikredi URLs --- config/pos.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/pos.php b/config/pos.php index fe77d4df..e278646a 100644 --- a/config/pos.php +++ b/config/pos.php @@ -97,11 +97,11 @@ 'name' => 'Yapıkredi', 'class' => Mews\Pos\Gateways\PosNet::class, 'urls' => [ - 'production' => 'https://www.posnet.ykb.com/PosnetWebService/XML', - 'test' => 'https://setmpos.ykb.com/PosnetWebService/XML', + 'production' => 'https://posnet.yapikredi.com.tr/PosnetWebService/XML', + 'test' => 'https://setmpos.yapikredi.com.tr/PosnetWebService/XML', 'gateway' => [ - 'production' => 'https://www.posnet.ykb.com/3DSWebService/YKBPaymentService', - 'test' => 'https://setmpos.ykb.com/3DSWebService/YKBPaymentService', + 'production' => 'https://www.posnet.yapikredi.com.tr/3DSWebService/YKBPaymentService', + 'test' => 'https://setmpos.yapikredi.com.tr/3DSWebService/YKBPaymentService', ], ], ], From 436133db6e832ce03761f45bf1090fbd2c469bb1 Mon Sep 17 00:00:00 2001 From: nuryagdy Date: Mon, 23 Nov 2020 10:04:21 +0300 Subject: [PATCH 5/5] update README.md --- README.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1428dce2..6be6f2ab 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Finansbank'ın PayFor sanal pos sistemini desteklemektedir, Finansbank'ın IP k - Standart E-Commerce modeliyle ödeme (model => regular) - 3D modeliyle ödeme (model => 3d) - 3D Pay modeliyle ödeme (model => 3d_pay) - - Sipariş/Ödeme sorgulama (query) + - Sipariş/Ödeme sorgulama (status) - Sipariş/Ödeme geçmişi sorgulama (history) - Sipariş/Para iadesi yapma (refund) - Sipariş iptal etme (cancel) @@ -52,7 +52,7 @@ $account = \Mews\Pos\Factory\AccountFactory::createEstPosAccount('akbank', 'XXXX // API kullanıcı hesabı ile paket bir değişkene aktarılıyor try { $pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account); - // test veya production. test ise; API Test Url, production ise; API Production URL kullanılır. + //değere göre API URL'leri test veya production değerler kullanılır. $pos->setTestMode(true); } catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { dump($e->getCode(), $e->getMessage()); @@ -87,18 +87,14 @@ $card = new \Mews\Pos\Entity\Card\CreditCardEstPos('1111222233334444', '20', '01 // API kullanıcısı ile oluşturulan $pos değişkenine prepare metoduyla sipariş bilgileri gönderiliyor $pos->prepare($order, \Mews\Pos\Gateways\AbstractGateway::TX_PAY); -// Ödeme tamamlanıyor +// Ödeme tamamlanıyor, $card zorunlu değil. $pos->payment($card); // Ödeme başarılı mı? $pos->isSuccess(); -//veya -$pos->isSuccess(); // Ödeme başarısız mı? $pos->isError(); -//veya -$pos->isError(); // Sonuç çıktısı dump($pos->getResponse()); @@ -177,7 +173,6 @@ http://localhost/ URL projenin `examples` klasörünün içine bakar. ### Yol Haritası - Dökümantasyon hazırlanacak - - UnitTest yazılacak -> Bu hiçbir zaman olmayabilir, birisi el atarsa sevinirim :) > Değerli yorum, öneri ve katkılarınızı >