From 8c5f6fa826d441ebcde1648a2d4d1c49fd420358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muharrem=20ERI=CC=87N?= Date: Thu, 15 Nov 2018 05:30:37 +0300 Subject: [PATCH] Garanti ve YKB Posnet eklendi MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muharrem ERİN --- LICENSE.txt | 1 - README.md | 25 +- composer.json | 4 +- config/pos.php | 54 +- examples/akbank/3d-pay/_config.php | 41 +- examples/akbank/3d-pay/index.php | 49 +- examples/akbank/3d-pay/response.php | 5 +- examples/akbank/3d/_config.php | 45 +- examples/akbank/3d/form.php | 5 +- examples/akbank/3d/index.php | 47 +- examples/akbank/3d/response.php | 5 +- examples/akbank/regular/_config.php | 6 +- examples/akbank/regular/cancel.php | 8 +- examples/akbank/regular/history.php | 4 +- examples/akbank/regular/index.php | 4 + examples/akbank/regular/post.php | 8 +- examples/akbank/regular/refund.php | 9 +- examples/akbank/regular/response.php | 13 +- examples/akbank/regular/status.php | 4 +- examples/garanti/3d-pay/_config.php | 33 + examples/garanti/3d-pay/form.php | 68 ++ examples/garanti/3d-pay/index.php | 57 ++ examples/garanti/3d-pay/response.php | 109 ++++ examples/garanti/3d/_config.php | 3 +- examples/garanti/3d/form.php | 6 +- examples/garanti/regular/_config.php | 3 +- examples/garanti/regular/cancel.php | 4 +- examples/garanti/regular/history.php | 2 +- examples/garanti/regular/post.php | 4 +- examples/garanti/regular/refund.php | 4 +- examples/garanti/regular/response.php | 22 +- examples/garanti/regular/status.php | 4 +- examples/template/_footer.php | 8 + src/EstPos.php | 350 ++++++----- src/GarantiPos.php | 848 +++++++++++++++---------- src/Pos.php | 51 +- src/PosHelpersTrait.php | 63 +- src/PosInterface.php | 50 +- src/PosNet.php | 855 ++++++++++++++++---------- 39 files changed, 1851 insertions(+), 1030 deletions(-) create mode 100644 examples/garanti/3d-pay/_config.php create mode 100644 examples/garanti/3d-pay/form.php create mode 100644 examples/garanti/3d-pay/index.php create mode 100644 examples/garanti/3d-pay/response.php diff --git a/LICENSE.txt b/LICENSE.txt index df46ae58..54f5d5ee 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/README.md b/README.md index 1fc69a4c..6829b774 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # Türk bankaları için sanal pos paketi (PHP) -Bu paket ile amaçlanan; ortak bir arayüz sınıfı ile, tüm Türk banka sanal pos sistemlerinin kullanılabilmesidir. Şu an için EST altyapısı kullanan bankalar desteklenmektedir. +Bu paket ile amaçlanan; ortak bir arayüz sınıfı ile, tüm Türk banka sanal pos sistemlerinin kullanılabilmesidir. +EST altyapısı tam olarak test edilmiş ve kullanıma hazırdır. +Garanti Ödeme sistemi çalışmaktadır, fakat 3D ödeme kısmının üretim ortamında test edilmesi gerekiyor. +YapıKredi Posnet sistemi çalışmaktadır, fakat 3D ödeme kısmının üretim ortamında test edilmesi gerekiyor. -> Şu an elimde sadece Akbank test API ve kullanıcı bilgileri olduğu için, diğer EST altyapısı kullanan banklarda test yapamadım. +> EST altyapısında olan Akbank ve Ziraat bankası test edilmiştir. ### Özellikler - Standart E-Commerce modeliyle ödeme (model => regular) @@ -89,16 +92,21 @@ $card = [ ]; // API kullanıcısı ile oluşturulan $pos değişkenine prepare metoduyla sipariş bilgileri gönderiliyor -try { - $pos->prepare($order); -} catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { - var_dump($e->getCode(), $e->getMessage()); - exit(); -} +$pos->prepare($order); // Ödeme tamamlanıyor $payment = $pos->payment($card); +// Ödeme başarılı mı? +$payment->isSuccess(); +//veya +$pos->isSuccess(); + +// Ödeme başarısız mı? +$payment->isError(); +//veya +$pos->isError(); + // Sonuç çıktısı var_dump($payment->response); @@ -171,7 +179,6 @@ $pos = new \Mews\Pos\Pos($account, $yeni_ayarlar); ### Yol Haritası - Dökümantasyon hazırlanacak - - EST harici altyapılar için de geliştirme yapılacak - UnitTest yazılacak -> Bu hiçbir zaman olmayabilir, birisi el atarsa sevinirim :) > Değerli yorum, öneri ve katkılarınızı bekliyorum. diff --git a/composer.json b/composer.json index 6c1aa520..001be609 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,11 @@ "php": "^7.1.3", "ext-dom": "*", "ext-json": "*", + "ext-mcrypt": "*", "ext-SimpleXML": "*", "guzzlehttp/guzzle": "^6.3", - "symfony/http-foundation": "^4.1" + "symfony/http-foundation": "^4.1", + "symfony/serializer": "^4.1" }, "autoload": { "psr-4": { diff --git a/config/pos.php b/config/pos.php index 7da3156a..4936cf05 100644 --- a/config/pos.php +++ b/config/pos.php @@ -1,6 +1,8 @@ [ 'TRY' => 949, 'USD' => 840, @@ -10,6 +12,7 @@ 'RUB' => 643, ], + // Banks 'banks' => [ 'akbank' => [ 'name' => 'AKBANK T.A.S.', @@ -23,5 +26,54 @@ ], ] ], - ] + 'ziraat' => [ + 'name' => 'Ziraat Bankası', + 'class' => \Mews\Pos\EstPos::class, + 'urls' => [ + 'production' => 'https://sanalpos2.ziraatbank.com.tr/fim/api', + 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', + 'gateway' => [ + 'production' => 'https://sanalpos2.ziraatbank.com.tr/fim/est3dgate', + 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/est3Dgate', + ], + ] + ], + 'isbank' => [ + 'name' => 'İşbank', + 'class' => \Mews\Pos\EstPos::class, + 'urls' => [ + 'production' => 'https://sanalpos.isbank.com.tr/fim/api', + 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', + 'gateway' => [ + 'production' => 'https://sanalpos.isbank.com.tr/fim/est3Dgate', + 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/est3Dgate', + ], + ] + ], + 'yapikredi' => [ + 'name' => 'Yapıkredi', + 'class' => \Mews\Pos\PosNet::class, + 'urls' => [ + 'production' => 'https://posnet.yapikredi.com.tr/PosnetWebService/XML', + 'test' => 'http://setmpos.ykb.com/PosnetWebService/XML', + 'gateway' => [ + 'production' => 'http://posnet.ykb.com/3DSWebService/YKBPaymentService', + 'test' => 'http://setmpos.ykb.com/3DSWebService/YKBPaymentService', + ], + ] + ], + 'garanti' => [ + 'name' => 'Garanti', + 'class' => \Mews\Pos\GarantiPos::class, + 'urls' => [ + 'production' => 'https://sanalposprov.garanti.com.tr/VPServlet', + 'test' => 'https://sanalposprovtest.garanti.com.tr/VPServlet', + 'gateway' => [ + 'production' => 'https://sanalposprov.garanti.com.tr/servlet/gt3dengine', + 'test' => 'https://sanalposprovtest.garanti.com.tr/servlet/gt3dengine', + ], + ] + ], + ], + ]; diff --git a/examples/akbank/3d-pay/_config.php b/examples/akbank/3d-pay/_config.php index 2146bb42..b1fdefc4 100644 --- a/examples/akbank/3d-pay/_config.php +++ b/examples/akbank/3d-pay/_config.php @@ -1,5 +1,7 @@ 'akbank', 'model' => '3d_pay', - 'client_id' => 'xxx', - 'store_key' => 'xxx', + 'client_id' => 'XXXXXXX', + 'store_key' => 'XXXXXXX', 'env' => 'test', ]; @@ -25,39 +27,4 @@ var_dump($e->getCode(), $e->getMessage()); } -$amount = (double) 100; -$instalment = '0'; - -$ok_url = $base_url . 'response.php'; -$fail_url = $base_url . 'response.php'; - -$transaction = 'pay'; // pay => Auth, pre PreAuth -$transaction_type = $pos->bank->types[$transaction]; - -$rand = microtime(); - -$order = [ - 'id' => 'unique-order-id-006', - 'email' => 'mail@customer.com', // optional - 'name' => 'John Doe', // optional - 'amount' => $amount, - 'installment' => $instalment, - 'currency' => 'TRY', - 'ip' => $ip, - 'ok_url' => $ok_url, - 'fail_url' => $fail_url, - 'transaction' => $transaction, - 'transaction_type' => $transaction_type, - 'lang' => 'tr', - 'rand' => $rand, -]; - -$pos->prepare($order); - -$hash = $pos->bank->create3DHash(); -$order['hash'] = $hash; - -$currency = $pos->config['currencies'][$order['currency']]; -$gateway = $pos->bank->gateway; - $template_title = '3D Pay Model Payment'; diff --git a/examples/akbank/3d-pay/index.php b/examples/akbank/3d-pay/index.php index 532344b9..5ca2ca74 100644 --- a/examples/akbank/3d-pay/index.php +++ b/examples/akbank/3d-pay/index.php @@ -4,25 +4,30 @@ require '../../template/_header.php'; +$url = $base_url . 'form.php'; ?> -
+
+
+ + +
- - - - + +
- - + +
- - @@ -30,8 +35,8 @@
- - @@ -39,28 +44,10 @@
- - + +
- - - - - - - - - - - - - - - - - -
diff --git a/examples/akbank/3d-pay/response.php b/examples/akbank/3d-pay/response.php index 510a6591..ca4fe820 100644 --- a/examples/akbank/3d-pay/response.php +++ b/examples/akbank/3d-pay/response.php @@ -9,6 +9,9 @@ exit(); } +$order = $_SESSION['order']; + +$pos->prepare($order); $payment = $pos->payment(); $response = $payment->response; @@ -52,7 +55,7 @@
Order ID:
-
id; ?>
+
order_id ? $response->order_id : '-'; ?>

diff --git a/examples/akbank/3d/_config.php b/examples/akbank/3d/_config.php index a41b1af1..a3dc3c74 100644 --- a/examples/akbank/3d/_config.php +++ b/examples/akbank/3d/_config.php @@ -1,5 +1,7 @@ 'akbank', 'model' => '3d', - 'client_id' => 'xxx', - 'username' => 'xxx', - 'password' => 'xxx.', - 'store_key' => 'xxx', + 'client_id' => 'XXXXXXX', + 'username' => 'XXXXXXX', + 'password' => 'XXXXXXX', + 'store_key' => 'XXXXXXX', 'env' => 'test', ]; @@ -27,39 +29,4 @@ var_dump($e->getCode(), $e->getMessage()); } -$amount = (double) 320; -$instalment = '0'; - -$ok_url = $base_url . 'response.php'; -$fail_url = $base_url . 'response.php'; - -$transaction = 'pay'; // pay => Auth, pre PreAuth -$transaction_type = $pos->bank->types[$transaction]; - -$rand = microtime(); - -$order = [ - 'id' => 'unique-order-id-007', - 'email' => 'mail@customer.com', // optional - 'name' => 'John Doe', // optional - 'amount' => $amount, - 'installment' => $instalment, - 'currency' => 'TRY', - 'ip' => $ip, - 'ok_url' => $ok_url, - 'fail_url' => $fail_url, - 'transaction' => $transaction, - 'transaction_type' => $transaction_type, - 'lang' => 'tr', - 'rand' => $rand, -]; - -$pos->prepare($order); - -$hash = $pos->bank->create3DHash(); -$order['hash'] = $hash; - -$currency = $pos->config['currencies'][$order['currency']]; -$gateway = $pos->bank->gateway; - $template_title = '3D Model Payment'; diff --git a/examples/akbank/3d/form.php b/examples/akbank/3d/form.php index 8a56e722..4977e4ed 100644 --- a/examples/akbank/3d/form.php +++ b/examples/akbank/3d/form.php @@ -17,9 +17,6 @@ $success_url = $base_url . 'response.php'; $fail_url = $base_url . 'response.php'; -$transaction = 'pay'; // pay => Auth, pre PreAuth -$transaction_type = $pos->bank->types[$transaction]; - $rand = microtime(); $order = [ @@ -32,7 +29,7 @@ 'ip' => $ip, 'success_url' => $success_url, 'fail_url' => $fail_url, - 'transaction' => $transaction, + 'transaction' => 'pay', // pay => Auth, pre PreAuth, 'lang' => 'tr', 'rand' => $rand, ]; diff --git a/examples/akbank/3d/index.php b/examples/akbank/3d/index.php index fe1b4fd0..5ca2ca74 100644 --- a/examples/akbank/3d/index.php +++ b/examples/akbank/3d/index.php @@ -4,25 +4,30 @@ require '../../template/_header.php'; +$url = $base_url . 'form.php'; ?> - +
+
+ + +
- - - - + +
- - + +
- - @@ -30,8 +35,8 @@
- - @@ -39,26 +44,10 @@
- - + +
- - - - - - - - - - - - - - - -
diff --git a/examples/akbank/3d/response.php b/examples/akbank/3d/response.php index 2f9c24f6..890e2513 100644 --- a/examples/akbank/3d/response.php +++ b/examples/akbank/3d/response.php @@ -9,6 +9,9 @@ exit(); } +$order = $_SESSION['order']; + +$pos->prepare($order); $payment = $pos->payment(); $response = $payment->response; @@ -52,7 +55,7 @@
Order ID:
-
id; ?>
+
order_id ? $response->order_id : '-'; ?>

diff --git a/examples/akbank/regular/_config.php b/examples/akbank/regular/_config.php index 18c6fa15..f9a2238c 100644 --- a/examples/akbank/regular/_config.php +++ b/examples/akbank/regular/_config.php @@ -12,9 +12,9 @@ $account = [ 'bank' => 'akbank', 'model' => 'regular', - 'client_id' => 'xxx', - 'username' => 'xxx', - 'password' => 'xxx.', + 'client_id' => 'XXXXXXX', + 'username' => 'XXXXXXX', + 'password' => 'XXXXXXX', 'env' => 'test', ]; diff --git a/examples/akbank/regular/cancel.php b/examples/akbank/regular/cancel.php index adb6edc7..42a2f64d 100644 --- a/examples/akbank/regular/cancel.php +++ b/examples/akbank/regular/cancel.php @@ -7,15 +7,17 @@ require '../../template/_header.php'; // Cancel Order -$cancel = $pos->bank->cancel('unique-order-id'); +$cancel = $pos->bank->cancel([ + 'order_id' => '20181029A3C1', +]); $response = $cancel->response; $dump = get_object_vars($response); ?>
-

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

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

All Data Dump:
diff --git a/examples/akbank/regular/history.php b/examples/akbank/regular/history.php index 84b8e573..0b74cca0 100644 --- a/examples/akbank/regular/history.php +++ b/examples/akbank/regular/history.php @@ -7,7 +7,9 @@ require '../../template/_header.php'; // History Order -$query= $pos->bank->history('2018080532DD'); +$query= $pos->bank->history([ + 'order_id' => '201810297189', +]); $response = $query->response; $dump = get_object_vars($response); diff --git a/examples/akbank/regular/index.php b/examples/akbank/regular/index.php index f59a3066..3df48a69 100644 --- a/examples/akbank/regular/index.php +++ b/examples/akbank/regular/index.php @@ -8,6 +8,10 @@
+
+ + +
diff --git a/examples/akbank/regular/post.php b/examples/akbank/regular/post.php index e4a8fd75..5ef1b4d8 100644 --- a/examples/akbank/regular/post.php +++ b/examples/akbank/regular/post.php @@ -12,7 +12,7 @@ $account = [ 'bank' => 'akbank', 'model' => 'regular', - 'client_id' => '100200000', + 'client_id' => '100100000', 'username' => 'mewsapi', 'password' => 'ME12345.', 'env' => 'test', @@ -31,7 +31,7 @@ } $order = [ - 'id' => '2018080532DD', + 'id' => '201810297189', 'transaction' => 'post', ]; @@ -48,8 +48,8 @@ ?>
-

- proc_return_code == '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 c0a36e9e..198b9ac6 100644 --- a/examples/akbank/regular/refund.php +++ b/examples/akbank/regular/refund.php @@ -7,15 +7,18 @@ require '../../template/_header.php'; // Refund Order -$refund = $pos->bank->refund('unique-order-id', '100'); +$refund = $pos->bank->refund([ + 'order_id' => '201810297E8B', + 'amount' => '100', +]); $response = $refund->response; $dump = get_object_vars($response); ?>
-

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

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

All Data Dump:
diff --git a/examples/akbank/regular/response.php b/examples/akbank/regular/response.php index eec3f922..f67ab61c 100644 --- a/examples/akbank/regular/response.php +++ b/examples/akbank/regular/response.php @@ -21,14 +21,10 @@ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth + 'transaction' => 'pre', // pay => Auth, pre PreAuth ]; -try { - $pos->prepare($order); -} catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { - var_dump($e->getCode(), $e->getMessage()); -} +$pos->prepare($order); $card = [ 'number' => $request->get('number'), @@ -38,15 +34,14 @@ ]; $payment = $pos->payment($card); - $response = $payment->response; $dump = get_object_vars($response); ?>
-

- code == '00' ? 'Payment is successful!' : 'Payment is not successful!'; ?> +

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


diff --git a/examples/akbank/regular/status.php b/examples/akbank/regular/status.php index 261b33f2..f3aac1ee 100644 --- a/examples/akbank/regular/status.php +++ b/examples/akbank/regular/status.php @@ -7,7 +7,9 @@ require '../../template/_header.php'; // Query Order -$query= $pos->bank->status('2018080532DD'); +$query= $pos->bank->status([ + 'order_id' => '201810297189' +]); $response = $query->response; $dump = get_object_vars($response); diff --git a/examples/garanti/3d-pay/_config.php b/examples/garanti/3d-pay/_config.php new file mode 100644 index 00000000..4a904212 --- /dev/null +++ b/examples/garanti/3d-pay/_config.php @@ -0,0 +1,33 @@ +getClientIp(); + +$account = [ + 'bank' => 'garanti', + 'model' => '3d_pay', + 'client_id' => '7000679', + 'terminal_id' => '30691298', + 'username' => 'PROVAUT', + 'password' => '123qweASD/', + 'store_key' => '12345678', + 'env' => 'test', +]; + +try { + $pos = new \Mews\Pos\Pos($account); +} catch (\Mews\Pos\Exceptions\BankNotFoundException $e) { + var_dump($e->getCode(), $e->getMessage()); +} catch (\Mews\Pos\Exceptions\BankClassNullException $e) { + var_dump($e->getCode(), $e->getMessage()); +} + +$template_title = '3D Pay Model Payment'; diff --git a/examples/garanti/3d-pay/form.php b/examples/garanti/3d-pay/form.php new file mode 100644 index 00000000..1d066c34 --- /dev/null +++ b/examples/garanti/3d-pay/form.php @@ -0,0 +1,68 @@ +getMethod() !== 'POST') { + echo new \Symfony\Component\HttpFoundation\RedirectResponse($base_url); + exit(); +} + +$order_id = date('Ymd') . strtoupper(substr(uniqid(sha1(time())),0,4)); + +$amount = (double) 320; +$instalment = '0'; + +$success_url = $base_url . 'response.php'; +$fail_url = $base_url . 'response.php'; + +$transaction = 'pay'; // pay => Auth, pre PreAuth +$transaction_type = $pos->bank->types[$transaction]; + +$rand = microtime(); + +$order = [ + 'id' => $order_id, + 'email' => 'mail@customer.com', // optional + 'name' => 'John Doe', // optional + 'amount' => $amount, + 'installment' => $instalment, + 'currency' => 'TRY', + 'ip' => $ip, + 'success_url' => $success_url, + 'fail_url' => $fail_url, + 'transaction' => $transaction, + 'lang' => 'tr', + 'rand' => $rand, +]; + +$_SESSION['order'] = $order; + +$card = [ + 'name' => $request->get('name'), + 'type' => $request->get('type'), + 'number' => $request->get('number'), + 'month' => $request->get('month'), + 'year' => $request->get('year'), + 'cvv' => $request->get('cvv'), +]; + +$pos->prepare($order, $card); + +$form_data = $pos->get3dFormData(); +?> + + + $value): ?> + + +
Redirecting...
+
+
+
+ +
+ + + diff --git a/examples/garanti/3d-pay/index.php b/examples/garanti/3d-pay/index.php new file mode 100644 index 00000000..5ca2ca74 --- /dev/null +++ b/examples/garanti/3d-pay/index.php @@ -0,0 +1,57 @@ + + +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ +
+
+ + diff --git a/examples/garanti/3d-pay/response.php b/examples/garanti/3d-pay/response.php new file mode 100644 index 00000000..3fc015c1 --- /dev/null +++ b/examples/garanti/3d-pay/response.php @@ -0,0 +1,109 @@ +getMethod() !== 'POST') { + echo new \Symfony\Component\HttpFoundation\RedirectResponse($base_url); + exit(); +} + +$order = $_SESSION['order']; + +$pos->prepare($order); +$payment = $pos->payment(); +$response = $payment->response; + +$dump = get_object_vars($response); +?> + +
+
+

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

+
+
+
Response:
+
response ? $response->response : '-'; ?>
+
+
+
+
Status:
+
status; ?>
+
+
+
+
Transaction:
+
transaction; ?>
+
+
+
+
Transaction Type:
+
transaction_type; ?>
+
+
+
+
Transaction Security:
+
transaction_security; ?>
+
+
+
+
Hash:
+
hash; ?>
+
+
+
+
Order ID:
+
order_id ? $response->order_id : '-'; ?>
+
+
+
+
AuthCode:
+
auth_code ? $response->auth_code : '-'; ?>
+
+
+
+
HostRefNum:
+
host_ref_num ? $response->host_ref_num : '-'; ?>
+
+
+
+
ProcReturnCode:
+
code ? $response->code : '-'; ?>
+
+
+
+
mdStatus:
+
md_status ? $response->md_status : '-'; ?>
+
+
+
+
Error Code:
+
error_code ? $response->error_code : '-'; ?>
+
+
+
+
Error Message:
+
error_message ? $response->error_message : '-'; ?>
+
+
+
+
Md Error Message:
+
md_error_message ? $response->md_error_message : '-'; ?>
+
+
+
+
All Data Dump:
+
+
+
+
+
+ +
+ + diff --git a/examples/garanti/3d/_config.php b/examples/garanti/3d/_config.php index e0a5d6f0..cfe72c04 100644 --- a/examples/garanti/3d/_config.php +++ b/examples/garanti/3d/_config.php @@ -13,11 +13,10 @@ $account = [ 'bank' => 'garanti', - 'model' => '3s', + 'model' => '3d', 'client_id' => '7000679', 'terminal_id' => '30691298', 'username' => 'PROVAUT', - 'terminal_username' => 'PROVOOS', 'password' => '123qweASD/', 'store_key' => '12345678', 'env' => 'test', diff --git a/examples/garanti/3d/form.php b/examples/garanti/3d/form.php index 1d066c34..4977e4ed 100644 --- a/examples/garanti/3d/form.php +++ b/examples/garanti/3d/form.php @@ -17,9 +17,6 @@ $success_url = $base_url . 'response.php'; $fail_url = $base_url . 'response.php'; -$transaction = 'pay'; // pay => Auth, pre PreAuth -$transaction_type = $pos->bank->types[$transaction]; - $rand = microtime(); $order = [ @@ -32,7 +29,7 @@ 'ip' => $ip, 'success_url' => $success_url, 'fail_url' => $fail_url, - 'transaction' => $transaction, + 'transaction' => 'pay', // pay => Auth, pre PreAuth, 'lang' => 'tr', 'rand' => $rand, ]; @@ -59,7 +56,6 @@
Redirecting...

-
diff --git a/examples/garanti/regular/_config.php b/examples/garanti/regular/_config.php index d852dcdf..ccc6cb9a 100644 --- a/examples/garanti/regular/_config.php +++ b/examples/garanti/regular/_config.php @@ -13,8 +13,9 @@ 'bank' => 'garanti', 'model' => 'regular', 'client_id' => '7000679', - 'terminal_id' => '30691297', + 'terminal_id' => '30691298', 'username' => 'PROVAUT', + 'terminal_username' => 'PROVOOS', 'password' => '123qweASD/', 'refund_username' => 'PROVRFN', 'refund_password' => '123qweASD/', diff --git a/examples/garanti/regular/cancel.php b/examples/garanti/regular/cancel.php index 9ca32c7e..cc399b18 100644 --- a/examples/garanti/regular/cancel.php +++ b/examples/garanti/regular/cancel.php @@ -8,10 +8,10 @@ // Cancel Order $cancel = $pos->bank->cancel([ - 'order_id' => '20181023069E', + 'order_id' => '20181114DF2C', 'ip' => $ip, 'email' => 'mail@customer.com', - 'ref_ret_num' => '829603332867', + 'ref_ret_num' => '831803579226', 'amount' => 1, 'currency' => 'TRY', ]); diff --git a/examples/garanti/regular/history.php b/examples/garanti/regular/history.php index f30601d3..1628662a 100644 --- a/examples/garanti/regular/history.php +++ b/examples/garanti/regular/history.php @@ -8,7 +8,7 @@ // History Order $query= $pos->bank->history([ - 'order_id' => '20181023069E', + 'order_id' => '2018111377EF', 'currency' => 'TRY', 'ip' => $ip, ]); diff --git a/examples/garanti/regular/post.php b/examples/garanti/regular/post.php index 8739a708..0e1dda9d 100644 --- a/examples/garanti/regular/post.php +++ b/examples/garanti/regular/post.php @@ -32,10 +32,10 @@ } $order = [ - 'id' => '2018102377C1', + 'id' => '201810231553', 'transaction' => 'post', 'amount' => '1', - 'ref_ret_num' => '829603332757', + 'ref_ret_num' => '829603332856', 'ip' => $ip, ]; diff --git a/examples/garanti/regular/refund.php b/examples/garanti/regular/refund.php index e5901da4..efa20b63 100644 --- a/examples/garanti/regular/refund.php +++ b/examples/garanti/regular/refund.php @@ -8,10 +8,10 @@ // Refund Order $refund = $pos->bank->refund([ - 'order_id' => '20181023581A', + 'order_id' => '201811142A0A', 'ip' => $ip, 'email' => 'mail@customer.com', - 'ref_ret_num' => '829603332515', + 'ref_ret_num' => '831803586333', 'amount' => 1, 'currency' => 'TRY', ]); diff --git a/examples/garanti/regular/response.php b/examples/garanti/regular/response.php index eec3f922..57cb32f5 100644 --- a/examples/garanti/regular/response.php +++ b/examples/garanti/regular/response.php @@ -21,14 +21,10 @@ 'installment' => '0', 'currency' => 'TRY', 'ip' => $ip, - 'transaction' => 'pay', // pay => Auth, pre PreAuth + 'transaction' => 'pay', // pay => S, pre => preauth ]; -try { - $pos->prepare($order); -} catch (\Mews\Pos\Exceptions\UnsupportedTransactionTypeException $e) { - var_dump($e->getCode(), $e->getMessage()); -} +$pos->prepare($order); $card = [ 'number' => $request->get('number'), @@ -45,8 +41,8 @@ ?>
-

- code == '00' ? 'Payment is successful!' : 'Payment is not successful!'; ?> +

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


@@ -89,6 +85,16 @@
host_ref_num ? $response->host_ref_num : '-'; ?>

+
+
RetrefNum:
+
ret_ref_num ? $response->ret_ref_num : '-'; ?>
+
+
+
+
HashData:
+
hash_data ? $response->hash_data : '-'; ?>
+
+
ProcReturnCode:
code; ?>
diff --git a/examples/garanti/regular/status.php b/examples/garanti/regular/status.php index 1e27fc47..136720b2 100644 --- a/examples/garanti/regular/status.php +++ b/examples/garanti/regular/status.php @@ -8,9 +8,9 @@ // Query Order $query= $pos->bank->status([ - 'order_id' => '20181023069E', + 'order_id' => '2018111377EF', 'currency' => 'TRY', - 'ip' => $ip, + 'ip' => $ip ]); $response = $query->response; diff --git a/examples/template/_footer.php b/examples/template/_footer.php index 44e9b01a..50325810 100644 --- a/examples/template/_footer.php +++ b/examples/template/_footer.php @@ -3,5 +3,13 @@ + diff --git a/src/EstPos.php b/src/EstPos.php index 0bd042c1..e3f24d52 100644 --- a/src/EstPos.php +++ b/src/EstPos.php @@ -63,6 +63,13 @@ class EstPos implements PosInterface 'post' => 'PostAuth', ]; + /** + * Currencies + * + * @var array + */ + public $currencies = []; + /** * Transaction Type * @@ -124,12 +131,13 @@ class EstPos implements PosInterface * * @param array $config * @param array $account - * @return $this + * @param array $currencies */ - public function __construct($config, $account) + 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] : @@ -150,33 +158,32 @@ public function __construct($config, $account) protected function createRegularPaymentXML() { $nodes = [ - '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' => isset($this->card->type) ? $this->card->type : null, - 'Number' => $this->card->number, - 'Expires' => $this->card->month . '/' . $this->card->year, - 'Cvv2Val' => $this->card->cvv, - 'Mode' => 'P', - 'GroupId' => '', - 'TransId' => '', + 'CC5Request' => [ + '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' => isset($this->card->type) ? $this->card->type : null, + 'Number' => $this->card->number, + 'Expires' => $this->card->month . '/' . $this->card->year, + 'Cvv2Val' => $this->card->cvv, + 'Mode' => 'P', + 'GroupId' => '', + 'TransId' => '', + 'BillTo' => [ + 'Name' => $this->order->name ? $this->order->name : null, + ] + ] ]; - if ($this->order->name) { - $nodes['BillTo'] = [ - 'Name' => $this->order->name, - ]; - } - - return $this->createXML($nodes); + return $this->createXML($nodes, 'ISO-8859-9'); } /** @@ -187,14 +194,16 @@ protected function createRegularPaymentXML() protected function createRegularPostXML() { $nodes = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->types[$this->order->transaction], - 'OrderId' => $this->order->id, + 'CC5Request' => [ + '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($nodes); + return $this->createXML($nodes, 'ISO-8859-9'); } /** @@ -204,27 +213,29 @@ protected function createRegularPostXML() protected function create3DPaymentXML() { $nodes = [ - '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' => '', + 'CC5Request' => [ + '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) { @@ -233,7 +244,7 @@ protected function create3DPaymentXML() ]; } - return $this->createXML($nodes); + return $this->createXML($nodes, 'ISO-8859-9'); } /** @@ -268,9 +279,9 @@ public function create3DHash() $hash_str = ''; if ($this->account->model == '3d') { - $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->ok_url . $this->order->fail_url . $this->order->rand . $this->account->store_key; + $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->ok_url . $this->order->fail_url . $this->order->transaction_type . $this->order->installment . $this->order->rand . $this->account->store_key; + $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(pack('H*', sha1($hash_str))); @@ -293,7 +304,7 @@ public function check3DHash() $index2 = strpos($hash_params, ':', $index1); $value = $this->request->get(substr($hash_params, $index1, $index2 - $index1)); - if($value == null) $value = ''; + if ($value == null) $value = ''; $params_val = $params_val . $value; @@ -453,11 +464,12 @@ public function make3DPayPayment() } $this->response = (object) [ - 'id' => (string) $this->request->get('oid'), + '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, @@ -490,6 +502,68 @@ public function make3DPayPayment() return $this; } + /** + * Get 3d Form Data + * + * @return array + */ + public function get3DFormData() + { + $data = []; + + if ($this->order) { + $this->order->hash = $this->create3DHash(); + + var_dump($this->card); + $card_type = null; + if (isset($this->card->type)) { + if ($this->card->type == 'visa') { + $card_type = '1'; + } elseif ($this->card->type == 'master') { + $card_type = '2'; + } + } + + $inputs = [ + 'clientid' => $this->account->client_id, + 'storetype' => $this->account->model, + 'hash' => $this->order->hash, + 'cardType' => $card_type, + 'pan' => $this->card->number, + 'Ecom_Payment_Card_ExpDate_Month' => $this->card->month, + 'Ecom_Payment_Card_ExpDate_Year' => $this->card->year, + 'cv2' => $this->card->cvv, + '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, + ]; + + 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 * @@ -535,52 +609,6 @@ public function prepare($order, $card = null) $this->card = $card; } - /** - * Get 3d Form Data - * - * @return array - */ - public function get3DFormData() - { - $this->order->hash = $this->create3DHash(); - - $inputs = [ - 'cardType' => $this->card->type, - 'pan' => $this->card->number, - 'Ecom_Payment_Card_ExpDate_Month' => $this->card->month, - 'Ecom_Payment_Card_ExpDate_Year' => $this->card->year, - 'cv2' => $this->card->cvv, - 'firmaadi' => $this->order->name, - 'Email' => $this->order->email, - 'clientid' => $this->account->client_id, - 'amount' => $this->order->amount, - 'oid' => $this->order->id, - 'okUrl' => $this->order->ok_url, - 'failUrl' => $this->order->fail_url, - 'rnd' => $this->order->rand, - 'hash' => $this->order->hash, - 'storetype' => $this->account->model, - 'lang' => $this->order->lang, - 'currency' => $this->order->currency, - ]; - - if ($this->account->model == '3d_pay') { - $inputs = array_merge($inputs, [ - 'islemtipi' => $this->order->transaction_type, - 'taksit' => $this->order->installment, - ]); - } - - return [ - 'gateway' => $this->gateway, - 'ok_url' => $this->order->ok_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand, - 'hash' => $this->order->hash, - 'inputs' => $inputs, - ]; - } - /** * Make Payment * @@ -614,24 +642,25 @@ public function payment($card) /** * Refund Order * - * @param $order_id - * @param null $amount + * @param array $meta * @return $this * @throws GuzzleException */ - public function refund($order_id, $amount = null) + public function refund(array $meta) { $nodes = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Type' => 'Credit', + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Type' => 'Credit', + ] ]; - if ($amount) $nodes['Total'] = $amount; + if ($meta['amount']) $nodes['Total'] = $meta['amount']; - $xml = $this->createXML($nodes); + $xml = $this->createXML($nodes, 'ISO-8859-9'); $this->send($xml); $status = 'declined'; @@ -660,19 +689,21 @@ public function refund($order_id, $amount = null) /** * Cancel Order * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function cancel($order_id) + public function cancel(array $meta) { $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Type' => 'Void', - ]); + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Type' => 'Void', + ] + ], 'ISO-8859-9'); $this->send($xml); @@ -702,21 +733,23 @@ public function cancel($order_id) /** * Order Status * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function status($order_id) + public function status(array $meta) { $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Extra' => [ - 'ORDERSTATUS' => 'QUERY', - ], - ]); + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Extra' => [ + 'ORDERSTATUS' => 'QUERY', + ], + ] + ], 'ISO-8859-9'); $this->send($xml); @@ -725,21 +758,26 @@ public function status($order_id) $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->data->OrderId : null, - 'response' => isset($this->data->Response) ? $this->data->Response : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, - 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, - 'host_ref_num' => isset($this->data->Extra->HOST_REF_NUM) ? $this->data->Extra->HOST_REF_NUM : null, - 'order_status' => isset($this->data->Extra->ORDERSTATUS) ? $this->data->Extra->ORDERSTATUS : null, - 'process_type' => isset($this->data->Extra->CHARGE_TYPE_CD) ? $this->data->Extra->CHARGE_TYPE_CD : null, - 'pan' => isset($this->data->Extra->PAN) ? $this->data->Extra->PAN : null, - 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->data->Extra->NUMCODE : null, - 'first_amount' => isset($this->data->Extra->ORIG_TRANS_AMT) ? $this->data->Extra->ORIG_TRANS_AMT : null, - 'capture_amount' => isset($this->data->Extra->CAPTURE_AMT) ? $this->data->Extra->CAPTURE_AMT : null, + '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, ]; @@ -750,21 +788,23 @@ public function status($order_id) /** * Order History * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function history($order_id) + public function history(array $meta) { $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Extra' => [ - 'ORDERHISTORY' => 'QUERY', - ], - ]); + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Extra' => [ + 'ORDERHISTORY' => 'QUERY', + ], + ] + ], 'ISO-8859-9'); $this->send($xml); diff --git a/src/GarantiPos.php b/src/GarantiPos.php index fbe19c74..a375394d 100644 --- a/src/GarantiPos.php +++ b/src/GarantiPos.php @@ -58,11 +58,18 @@ class GarantiPos implements PosInterface * @var array */ public $types = [ - 'pay' => 'Auth', - 'pre' => 'PreAuth', - 'post' => 'PostAuth', + 'pay' => 'sales', + 'pre' => 'preauth', + 'post' => 'postauth', ]; + /** + * Currencies + * + * @var array + */ + public $currencies = []; + /** * Transaction Type * @@ -120,16 +127,33 @@ class GarantiPos implements PosInterface protected $config = []; /** - * EstPos constructor. + * Mode + * + * @var string + */ + protected $mode = 'PROD'; + + /** + * API version + * @var string + */ + protected $version = 'v0.01'; + + /** + * GarantiPost constructor. * * @param array $config * @param array $account - * @return $this + * @param array $currencies */ - public function __construct($config, $account) + 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] : @@ -139,9 +163,100 @@ public function __construct($config, $account) $this->config['urls']['gateway'][$this->account->env] : $this->config['urls']['gateway']['production']; + if ($this->account->env == 'test') { + $this->mode = 'TEST'; + } + return $this; } + /** + * 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->number) ? $this->card->number : 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))); + } + + /** + * Amount Formatter + * + * @param double $amount + * @return int + */ + protected function amountFormat($amount) + { + return (int) str_replace('.', '', number_format($amount, 2, '.', '')); + } + /** * Create Regular Payment XML * @@ -149,20 +264,58 @@ public function __construct($config, $account) */ protected function createRegularPaymentXML() { + $security_data = $this->makeSecurityData(); + $hash_data = $this->makeHashData($security_data); + $nodes = [ - 'posnetRequest' => [ - 'mid' => $this->account->client_id, - 'tid' => $this->account->terminal_id, - 'sale' => [ - 'amount' => $this->order->amount, - 'installment' => $this->order->installment, - 'ccno' => $this->card->number, - 'currencyCode' => 'YT', - 'cvc' => $this->card->cvv, - 'expDate' => $this->card->year . $this->card->month, - 'orderID' => $this->order->id, + 'GVPSRequest' => [ + '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->number, + 'ExpireDate' => $this->card->month . $this->card->year, + 'CVV2' => $this->card->cvv, ], - ], + '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($nodes); @@ -175,13 +328,33 @@ protected function createRegularPaymentXML() */ protected function createRegularPostXML() { + $security_data = $this->makeSecurityData(); + $hash_data = $this->makeHashData($security_data); + $nodes = [ - 'posnetRequest' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->types[$this->order->transaction], - 'OrderId' => $this->order->id, + 'GVPSRequest' => [ + '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, + ], ] ]; @@ -194,32 +367,62 @@ protected function createRegularPostXML() */ protected function create3DPaymentXML() { + $security_data = $this->makeSecurityData(); + $hash_data = $this->makeHashData($security_data); + $nodes = [ - 'posnetRequest' => [ - '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' => '', - 'BillTo' => [ - 'Name' => $this->order->name ? $this->order->name : null, - ] + 'GVPSRequest' => [ + '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'), + ], + ], ] ]; @@ -233,7 +436,7 @@ protected function create3DPaymentXML() */ protected function getProcReturnCode() { - return isset($this->data->ProcReturnCode) ? (string) $this->data->ProcReturnCode : null; + return isset($this->data->Transaction->Response->Code) ? (string) $this->data->Transaction->Response->Code : null; } /** @@ -258,49 +461,14 @@ public function create3DHash() $hash_str = ''; if ($this->account->model == '3d') { - $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->ok_url . $this->order->fail_url . $this->order->rand . $this->account->store_key; + $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->ok_url . $this->order->fail_url . $this->order->transaction_type . $this->order->installment . $this->order->rand . $this->account->store_key; + $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(pack('H*', sha1($hash_str))); } - /** - * Check 3D Hash - * - * @return bool - */ - public function check3DHash() - { - $hash_params = $this->request->get('HASHPARAMS'); - $hash_params_val = $this->request->get('HASHPARAMSVAL'); - $hash_param = $this->request->get('HASH'); - $params_val = ''; - $index1 = 0; - - while ($index1 < strlen($hash_params)) { - $index2 = strpos($hash_params, ':', $index1); - $value = $this->request->get(substr($hash_params, $index1, $index2 - $index1)); - - if($value == null) $value = ''; - - $params_val = $params_val . $value; - - $index1 = $index2 + 1; - } - - $hash_val = $params_val . $this->account->store_key; - $hash = base64_encode(pack('H*', sha1($hash_val))); - - $return = false; - if ($hash_params && !($params_val != $hash_params_val || $hash_param != $hash)) { - $return = true; - } - - return $return; - } - /** * Regular Payment * @@ -318,30 +486,29 @@ public function makeRegularPayment() $this->send($contents); - print_r($this->data); - exit(); - $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, + '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->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, + '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->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, - 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, + '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, 'original' => $this->data, @@ -358,62 +525,67 @@ public function makeRegularPayment() */ public function make3DPayment() { - $this->request = Request::createFromGlobals(); - $status = 'declined'; - if ($this->check3DHash()) { - $contents = $this->create3DPaymentXML(); - $this->send($contents); - } - + $response = 'Declined'; + $proc_return_code = '99'; $transaction_security = 'MPI fallback'; - if ($this->getProcReturnCode() == '00') { - if ($this->request->get('mdStatus') == '1') { + 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])) { + } elseif (in_array($this->request->get('mdstatus'), [2, 3, 4])) { $transaction_security = 'Half 3D Secure'; } - $status = 'approved'; + $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->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, + '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->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, + '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->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'), + '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, + 'md_status' => $this->request->get('mdstatus'), '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'), + '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('mdErrorMsg'), + 'md_error_message' => (string) $this->request->get('mderrormessage'), 'name' => (string) $this->request->get('firmaadi'), 'email' => (string) $this->request->get('Email'), - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, + 'extra' => null, 'all' => $this->data, - '3d_all' => $this->request->request->all(), + '3d_all' => $this->request->all(), ]; return $this; @@ -426,63 +598,104 @@ public function make3DPayment() */ public function make3DPayPayment() { - $this->request = Request::createFromGlobals(); - $status = 'declined'; - - if ($this->check3DHash() && (string) $this->request->get('ProcReturnCode') == '00') { - if (in_array($this->request->get('mdStatus'), [1, 2, 3, 4])) { - $status = 'approved'; - } - } + $response = 'Declined'; + $proc_return_code = $this->request->get('procreturncode'); $transaction_security = 'MPI fallback'; - if ($status == 'approved') { - if ($this->request->get('mdStatus') == '1') { + 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])) { + } 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('oid'), - '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'), + '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, - 'code' => (string) $this->request->get('ProcReturnCode'), + '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('HASH'), + '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('maskedCreditCard'), - 'month' => (string) $this->request->get('Ecom_Payment_Card_ExpDate_Month'), - 'year' => (string) $this->request->get('Ecom_Payment_Card_ExpDate_Year'), + '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('mdErrorMsg'), + 'error_code' => (string) $this->request->get('errcode'), + 'error_message' => (string) $this->request->get('errmsg'), + 'md_error_message' => (string) $this->request->get('mderrormessage'), 'name' => (string) $this->request->get('firmaadi'), 'email' => (string) $this->request->get('Email'), 'extra' => $this->request->get('Extra'), - 'all' => $this->request->request->all(), + '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->number, + 'cardexpiredatemonth' => $this->card->month, + 'cardexpiredateyear' => $this->card->year, + 'cardcvv2' => $this->card->cvv, + '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 * @@ -526,52 +739,10 @@ public function prepare($order, $card = null) $this->order = $order; $this->card = $card; - } - - /** - * Get 3d Form Data - * - * @return array - */ - public function get3DFormData() - { - $this->order->hash = $this->create3DHash(); - - $inputs = [ - 'cardType' => $this->card->type, - 'pan' => $this->card->number, - 'Ecom_Payment_Card_ExpDate_Month' => $this->card->month, - 'Ecom_Payment_Card_ExpDate_Year' => $this->card->year, - 'cv2' => $this->card->cvv, - 'firmaadi' => $this->order->name, - 'Email' => $this->order->email, - 'clientid' => $this->account->client_id, - 'amount' => $this->order->amount, - 'oid' => $this->order->id, - 'okUrl' => $this->order->ok_url, - 'failUrl' => $this->order->fail_url, - 'rnd' => $this->order->rand, - 'hash' => $this->order->hash, - 'storetype' => $this->account->model, - 'lang' => $this->order->lang, - 'currency' => $this->order->currency, - ]; - if ($this->account->model == '3d_pay') { - $inputs = array_merge($inputs, [ - 'islemtipi' => $this->order->transaction_type, - 'taksit' => $this->order->installment, - ]); + if ($this->card) { + $this->card->month = str_pad($this->card->month, 2, '0', STR_PAD_LEFT); } - - return [ - 'gateway' => $this->gateway, - 'ok_url' => $this->order->ok_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand, - 'hash' => $this->order->hash, - 'inputs' => $inputs, - ]; } /** @@ -605,24 +776,56 @@ public function payment($card) } /** - * Refund Order + * Refund or Cancel Order * - * @param $order_id - * @param null $amount + * @param array $meta + * @param $type * @return $this * @throws GuzzleException */ - public function refund($order_id, $amount = null) + protected function refundOrCancel(array $meta, $type) { - $nodes = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Type' => 'Credit', + $this->order = (object) [ + 'id' => $meta['order_id'], + 'amount' => isset($meta['amount']) ? $meta['amount'] : null, ]; - if ($amount) $nodes['Total'] = $amount; + $security_data = $this->makeSecurityData(true); + $hash_data = $this->makeHashData($security_data); + + $currency = (int) $this->currencies[$meta['currency']]; + + $nodes = [ + 'GVPSRequest' => [ + '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($nodes); $this->send($xml); @@ -633,15 +836,19 @@ public function refund($order_id, $amount = null) } $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, + '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, @@ -650,21 +857,88 @@ public function refund($order_id, $amount = null) return $this; } + /** + * Refund Order + * + * @param $meta + * @return $this + * @throws GuzzleException + */ + public function refund(array $meta) + { + return $this->refundOrCancel($meta, 'refund'); + } + /** * Cancel Order * - * @param $order_id + * @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 */ - public function cancel($order_id) + 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); + $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Type' => 'Void', + 'GVPSRequest' => [ + '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', + ], + ] ]); $this->send($xml); @@ -674,111 +948,59 @@ public function cancel($order_id) $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, + $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, + 'original' => $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 $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function status($order_id) + public function status(array $meta) { - $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Extra' => [ - 'ORDERSTATUS' => 'QUERY', - ], - ]); - - $this->send($xml); - - $status = 'declined'; - if ($this->getProcReturnCode() == '00') { - $status = 'approved'; - } - - $this->response = (object) [ - 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, - 'response' => isset($this->data->Response) ? $this->data->Response : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, - 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, - 'host_ref_num' => isset($this->data->Extra->HOST_REF_NUM) ? $this->data->Extra->HOST_REF_NUM : null, - 'order_status' => isset($this->data->Extra->ORDERSTATUS) ? $this->data->Extra->ORDERSTATUS : null, - 'process_type' => isset($this->data->Extra->CHARGE_TYPE_CD) ? $this->data->Extra->CHARGE_TYPE_CD : null, - 'pan' => isset($this->data->Extra->PAN) ? $this->data->Extra->PAN : null, - 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->data->Extra->NUMCODE : null, - 'first_amount' => isset($this->data->Extra->ORIG_TRANS_AMT) ? $this->data->Extra->ORIG_TRANS_AMT : null, - 'capture_amount' => isset($this->data->Extra->CAPTURE_AMT) ? $this->data->Extra->CAPTURE_AMT : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, - 'xml' => $xml, - ]; - - return $this; + return $this->statusOrHistory($meta, 'orderinq'); } /** * Order History * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function history($order_id) + public function history(array $meta) { - $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Extra' => [ - 'ORDERHISTORY' => 'QUERY', - ], - ]); - - $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 $this->statusOrHistory($meta, 'orderhistoryinq'); } } diff --git a/src/Pos.php b/src/Pos.php index e700b1da..82614060 100644 --- a/src/Pos.php +++ b/src/Pos.php @@ -2,6 +2,7 @@ namespace Mews\Pos; +use Exception; use Mews\Pos\Exceptions\BankClassNullException; use Mews\Pos\Exceptions\BankNotFoundException; @@ -84,7 +85,7 @@ public function instance() if ( ! $class) throw new BankClassNullException(); // Create Bank Class Object - $this->bank = new $class($this->config['banks'][$this->account->bank], $this->account); + $this->bank = new $class($this->config['banks'][$this->account->bank], $this->account, $this->config['currencies']); } /** @@ -104,7 +105,7 @@ public function prepare(array $order, array $card = []) // Currency $currency = null; - if ($order['transaction'] != 'post') { + if (isset($order['currency'])) { $currency = (int) $this->config['currencies'][$order['currency']]; } @@ -144,4 +145,50 @@ public function payment(array $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'; + } + + /** + * 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 index b45d1839..063b68cc 100644 --- a/src/PosHelpersTrait.php +++ b/src/PosHelpersTrait.php @@ -2,7 +2,7 @@ namespace Mews\Pos; -use DOMDocument; +use Symfony\Component\Serializer\Encoder\XmlEncoder; /** * Trait PosHelpersTrait @@ -14,35 +14,19 @@ trait PosHelpersTrait * Create XML DOM Document * * @param array $nodes - * @return string + * @param string $encoding + * @return string the XML, or false if an error occurred. */ - protected function createXML(array $nodes) + public function createXML(array $nodes, $encoding = 'UTF-8') { - $dom = new DOMDocument('1.0', 'ISO-8859-9'); - $root = $dom->createElement('CC5Request'); - - if (count($nodes)) { - foreach ($nodes as $key => $val) { - if (is_array($val)) { - $child = $dom->createElement($key); - - if (count($val)) { - foreach ($val as $_key => $_val) { - $_child = $dom->createElement($_key, $_val); - $child->appendChild($_child); - } - } - } else { - $child = $dom->createElement($key, $val); - } - - $root->appendChild($child); - } - } + $rootNodeName = array_keys($nodes)[0]; + $encoder = new XmlEncoder($rootNodeName); - $dom->appendChild($root); + $xml = $encoder->encode($nodes[$rootNodeName], 'xml', [ + 'xml_encoding' => $encoding + ]); - return $dom->saveXML(); + return $xml; } /** @@ -51,7 +35,7 @@ protected function createXML(array $nodes) * @param $data * @return null|string */ - protected function printData($data) + public function printData($data) { if ((is_object($data) || is_array($data)) && !count((array) $data)) { $data = null; @@ -59,4 +43,29 @@ protected function printData($data) return (string) $data; } + + /** + * Is success + * + * @return bool + */ + public function isSuccess() + { + $success = false; + if (isset($this->response) && $this->response->status == 'approved') { + $success = true; + } + + return $success; + } + + /** + * Is error + * + * @return bool + */ + public function isError() + { + return !$this->isSuccess(); + } } diff --git a/src/PosInterface.php b/src/PosInterface.php index 6a4efb63..4e49442f 100644 --- a/src/PosInterface.php +++ b/src/PosInterface.php @@ -17,23 +17,26 @@ interface PosInterface * * @param object $config * @param object $account - * @return $this + * @param array $currencies */ - public function __construct($config, $account); + public function __construct($config, $account, array $currencies); /** - * Create 3D Hash + * Create XML DOM Document * - * @return string + * @param array $nodes + * @param string $encoding + * @return string the XML, or false if an error occurred. */ - public function create3DHash(); + public function createXML(array $nodes, $encoding = 'UTF-8'); /** - * Check 3D Hash + * Print Data * - * @return bool + * @param $data + * @return null|string */ - public function check3DHash(); + public function printData($data); /** * Regular Payment @@ -89,37 +92,50 @@ public function payment($card); /** * Refund Order * - * @param $order_id - * @param null $amount + * @param array $meta * @return $this * @throws GuzzleException */ - public function refund($order_id, $amount = null); + public function refund(array $meta); /** * Cancel Order * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function cancel($order_id); + public function cancel(array $meta); /** * Order Status * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function status($order_id); + public function status(array $meta); /** * Order History * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function history($order_id); + public function history(array $meta); + + /** + * Is success + * + * @return bool + */ + public function isSuccess(); + + /** + * Is error + * + * @return bool + */ + public function isError(); } diff --git a/src/PosNet.php b/src/PosNet.php index 7d2361b6..f3e2afe9 100644 --- a/src/PosNet.php +++ b/src/PosNet.php @@ -37,19 +37,28 @@ class PosNet implements PosInterface * @var array */ public $codes = [ + '0' => 'declined', + '1' => 'approved', + '2' => 'declined', '00' => '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', + '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', ]; /** @@ -58,9 +67,29 @@ class PosNet implements PosInterface * @var array */ public $types = [ - 'pay' => 'Auth', - 'pre' => 'PreAuth', - 'post' => 'PostAuth', + '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', ]; /** @@ -120,16 +149,29 @@ class PosNet implements PosInterface protected $config = []; /** - * EstPos constructor. + * @var PosNetCrypt|null + */ + public $crypt; + + /** + * PosNet constructor. * * @param array $config * @param array $account - * @return $this + * @param array $currencies */ - public function __construct($config, $account) + public function __construct($config, $account, array $currencies) { + $request = Request::createFromGlobals(); + $this->request = $request->request; + + $this->crypt = function_exists('mcrypt_encrypt') ? + new PosNetCrypt : + null; + $this->config = $config; $this->account = $account; + $this->currencies = $currencies; $this->url = isset($this->config['urls'][$this->account->env]) ? $this->config['urls'][$this->account->env] : @@ -142,6 +184,57 @@ public function __construct($config, $account) return $this; } + /** + * 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 orderId + * + * @param int $pad_length + * @return string + */ + protected function getOrderId(int $pad_length = 24) + { + 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 * @@ -149,34 +242,26 @@ public function __construct($config, $account) */ protected function createRegularPaymentXML() { + $transaction = strtolower($this->type); + $nodes = [ - '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' => isset($this->card->type) ? $this->card->type : null, - 'Number' => $this->card->number, - 'Expires' => $this->card->month . '/' . $this->card->year, - 'Cvv2Val' => $this->card->cvv, - 'Mode' => 'P', - 'GroupId' => '', - 'TransId' => '', + 'posnetRequest' => [ + '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->number, + 'expDate' => $this->card->year . $this->card->month, + 'cvc' => $this->card->cvv, + ], + ] ]; - if ($this->order->name) { - $nodes['BillTo'] = [ - 'Name' => $this->order->name, - ]; - } - - return $this->createXML($nodes); + return $this->createXML($nodes, $encoding = 'ISO-8859-9'); } /** @@ -187,11 +272,17 @@ protected function createRegularPaymentXML() protected function createRegularPostXML() { $nodes = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->types[$this->order->transaction], - 'OrderId' => $this->order->id, + 'posnetRequest' => [ + '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($nodes); @@ -204,36 +295,18 @@ protected function createRegularPostXML() protected function create3DPaymentXML() { $nodes = [ - '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' => '', + 'posnetRequest' => [ + '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'), + ], + ] ]; - if ($this->order->name) { - $nodes['BillTo'] = [ - 'Name' => $this->order->name, - ]; - } - - return $this->createXML($nodes); + return $this->createXML($nodes, 'ISO-8859-9'); } /** @@ -243,7 +316,7 @@ protected function create3DPaymentXML() */ protected function getProcReturnCode() { - return isset($this->data->ProcReturnCode) ? (string) $this->data->ProcReturnCode : null; + return (string) $this->data->approved == '1' ? '00' : $this->data->approved; } /** @@ -253,62 +326,59 @@ protected function getProcReturnCode() */ protected function getStatusDetail() { - $proc_return_code = $this->getProcReturnCode(); + $proc_return_code = $this->getProcReturnCode(); - return $proc_return_code ? (isset($this->codes[$proc_return_code]) ? (string) $this->codes[$proc_return_code] : null) : null; + return isset($this->codes[$proc_return_code]) ? (string) $this->codes[$proc_return_code] : null; } /** - * Create 3D Hash + * Get card exp date * * @return string */ - public function create3DHash() + protected function getCardExpDate() { - $hash_str = ''; + $year = (string) str_pad($this->card->year, 2, '0', STR_PAD_LEFT); + $month = (string) str_pad($this->card->month, 2, '0', STR_PAD_LEFT); - if ($this->account->model == '3d') { - $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->ok_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->ok_url . $this->order->fail_url . $this->order->transaction_type . $this->order->installment . $this->order->rand . $this->account->store_key; - } - - return base64_encode(pack('H*', sha1($hash_str))); + return (string) $year . $month; } /** - * Check 3D Hash + * Get OOS transaction data * - * @return bool + * @return object + * @throws GuzzleException */ - public function check3DHash() + public function getOosTransactionData() { - $hash_params = $this->request->get('HASHPARAMS'); - $hash_params_val = $this->request->get('HASHPARAMSVAL'); - $hash_param = $this->request->get('HASH'); - $params_val = ''; - $index1 = 0; - - while ($index1 < strlen($hash_params)) { - $index2 = strpos($hash_params, ':', $index1); - $value = $this->request->get(substr($hash_params, $index1, $index2 - $index1)); - - if($value == null) $value = ''; - - $params_val = $params_val . $value; - - $index1 = $index2 + 1; + $name = isset($this->card->name) ? $this->card->name : null; + if (!$name) { + $name = isset($this->order->name) ? $this->order->name : null; } - $hash_val = $params_val . $this->account->store_key; - $hash = base64_encode(pack('H*', sha1($hash_val))); + $contents = $this->createXML([ + 'posnetRequest' => [ + 'mid' => $this->account->client_id, + 'tid' => $this->account->terminal_id, + 'oosRequestData' => [ + 'posnetid' => $this->account->posnet_id, + 'ccno' => $this->card->number, + 'expDate' => $this->getCardExpDate(), + 'cvc' => $this->card->cvv, + 'amount' => $this->getAmount(), + 'currencyCode' => $this->getCurrency(), + 'installment' => $this->getInstallment(), + 'XID' => $this->getOrderId(20), + 'cardHolderName' => $name, + 'tranType' => $this->type, + ] + ], + ]); - $return = false; - if ($hash_params && !($params_val != $hash_params_val || $hash_param != $hash)) { - $return = true; - } + $this->send($contents); - return $return; + return $this->data; } /** @@ -329,27 +399,37 @@ public function makeRegularPayment() $this->send($contents); $status = 'declined'; - if ($this->getProcReturnCode() == '00') { + $code = '1'; + $proc_return_code = '01'; + $obj = isset($this->data) ? $this->data : null; + $error_code = isset($obj->respCode) ? $obj->respCode : null; + $error_message = isset($obj->respText) ? $obj->respText : null; + + if ($this->getProcReturnCode() == '00' && $obj && !$error_code) { $status = 'approved'; + $code = isset($obj->approved) ? $obj->approved : null; + $proc_return_code = $this->getProcReturnCode(); } $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, + '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->Order->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($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, + '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' => 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, - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, + 'error_code' => $error_code, + 'error_message' => $error_message, + 'extra' => null, 'all' => $this->data, 'original' => $this->data, ]; @@ -357,6 +437,62 @@ public function makeRegularPayment() return $this; } + /** + * Get host name + * + * @param $url + * @return string + */ + public function getHostName($url) + { + $parse = parse_url($url); + + return $parse['host']; + } + + /** + * 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); + $this->crypt->deInit(); + + $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(20), + $this->getHostName($this->url), + ]); + + $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], + $this->getHostName($decrypted_data_array[7]), + ]); + + if ($original_data == $decrypted_data_list) { + $check = true; + } + } else { + $check = false; + } + + return $check; + } + /** * Make 3D Payment * @@ -365,28 +501,29 @@ public function makeRegularPayment() */ public function make3DPayment() { - $this->request = Request::createFromGlobals(); - $status = 'declined'; + $transaction_security = 'MPI fallback'; + if ($this->check3DHash()) { $contents = $this->create3DPaymentXML(); $this->send($contents); } - $transaction_security = 'MPI fallback'; if ($this->getProcReturnCode() == '00') { - if ($this->request->get('mdStatus') == '1') { + if ($this->data->oosResolveMerchantDataResponse->mdStatus == '1') { $transaction_security = 'Full 3D Secure'; - } elseif (in_array($this->request->get('mdStatus'), [2, 3, 4])) { + $status = 'approved'; + } elseif (in_array($this->data->oosResolveMerchantDataResponse->mdStatus, [2, 3, 4])) { $transaction_security = 'Half 3D Secure'; + $status = 'approved'; } - - $status = 'approved'; } + $this->response = (object) $this->data; + $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, + 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : 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, @@ -401,26 +538,15 @@ public function make3DPayment() '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'), - 'email' => (string) $this->request->get('Email'), - 'extra' => isset($this->data->Extra) ? $this->data->Extra : 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, 'all' => $this->data, - '3d_all' => $this->request->request->all(), ]; return $this; @@ -490,6 +616,35 @@ public function make3DPayPayment() return $this; } + /** + * Get 3d Form Data + * + * @return array + * @throws GuzzleException + */ + public function get3DFormData() + { + $data = []; + + if ($this->card && $this->order) { + $data = $this->getOosTransactionData(); + + $data = [ + '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->order->lang, + ]; + } + + return $data; + } + /** * Send contents to WebService * @@ -501,8 +656,13 @@ public function send($contents) { $client = new Client(); + $headers = [ + 'Content-Type' => 'application/x-www-form-urlencoded' + ]; + $response = $client->request('POST', $this->url, [ - 'body' => $contents + 'headers' => $headers, + 'body' => "xmldata=" . $contents, ]); $xml = new SimpleXMLElement($response->getBody()); @@ -535,52 +695,6 @@ public function prepare($order, $card = null) $this->card = $card; } - /** - * Get 3d Form Data - * - * @return array - */ - public function get3DFormData() - { - $this->order->hash = $this->create3DHash(); - - $inputs = [ - 'cardType' => $this->card->type, - 'pan' => $this->card->number, - 'Ecom_Payment_Card_ExpDate_Month' => $this->card->month, - 'Ecom_Payment_Card_ExpDate_Year' => $this->card->year, - 'cv2' => $this->card->cvv, - 'firmaadi' => $this->order->name, - 'Email' => $this->order->email, - 'clientid' => $this->account->client_id, - 'amount' => $this->order->amount, - 'oid' => $this->order->id, - 'okUrl' => $this->order->ok_url, - 'failUrl' => $this->order->fail_url, - 'rnd' => $this->order->rand, - 'hash' => $this->order->hash, - 'storetype' => $this->account->model, - 'lang' => $this->order->lang, - 'currency' => $this->order->currency, - ]; - - if ($this->account->model == '3d_pay') { - $inputs = array_merge($inputs, [ - 'islemtipi' => $this->order->transaction_type, - 'taksit' => $this->order->installment, - ]); - } - - return [ - 'gateway' => $this->gateway, - 'ok_url' => $this->order->ok_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand, - 'hash' => $this->order->hash, - 'inputs' => $inputs, - ]; - } - /** * Make Payment * @@ -612,180 +726,295 @@ public function payment($card) } /** - * Refund Order + * Refund or Cancel Order * - * @param $order_id - * @param null $amount + * @param array $meta + * @param string $type * @return $this * @throws GuzzleException */ - public function refund($order_id, $amount = null) + 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 = [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Type' => 'Credit', + 'mid' => $this->account->client_id, + 'tid' => $this->account->terminal_id, + 'tranDateRequired' => '1', ]; - if ($amount) $nodes['Total'] = $amount; + if ($type == 'refund') { + $return = [ + 'amount' => $this->getAmount(), + 'currencyCode' => $this->getCurrency(), + 'orderID' => $this->getOrderId(), + ]; + + 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->getOrderId(), + '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, + ]; + } + + $nodes = array_merge($nodes, $append); + + $xml = $this->createXML([ + 'posnetRequest' => $nodes + ]); - $xml = $this->createXML($nodes); $this->send($xml); $status = 'declined'; - if ($this->getProcReturnCode() == '00') { + $code = '1'; + $proc_return_code = '01'; + $obj = isset($this->data) ? $this->data : null; + $error_code = isset($obj->respCode) ? $obj->respCode : null; + $error_message = null; + + if ($this->getProcReturnCode() == '00' && $obj && !$error_code) { $status = 'approved'; + $code = isset($obj->approved) ? $obj->approved : null; + $proc_return_code = $this->getProcReturnCode(); } - $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, + $error_message = isset($obj->respText) ? $obj->respText : null; + + $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->authCode) ? $this->printData($obj->transaction->authCode) : null, + 'ret_ref_num' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : 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' => $error_message, + 'extra' => null, 'all' => $this->data, + 'original' => $this->data, ]; + $this->response = (object) $data; + return $this; } /** - * Cancel Order + * Refund Order * - * @param $order_id + * @param $meta * @return $this * @throws GuzzleException */ - public function cancel($order_id) + public function refund(array $meta) { - $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Type' => 'Void', - ]); - - $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->refundOrCancel($meta, 'refund'); + } - return $this; + /** + * Cancel Order + * + * @param array $meta + * @return $this + * @throws GuzzleException + */ + public function cancel(array $meta) + { + return $this->refundOrCancel($meta, 'cancel'); } /** * Order Status * - * @param $order_id + * @param array $meta + * @param bool $history * @return $this * @throws GuzzleException */ - public function status($order_id) + public function status(array $meta, $history = false) { + $this->order = (object) [ + 'id' => isset($meta['order_id']) ? $meta['order_id'] : null, + ]; + $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Extra' => [ - 'ORDERSTATUS' => 'QUERY', - ], + 'posnetRequest' => [ + 'mid' => $this->account->client_id, + 'tid' => $this->account->terminal_id, + 'agreement' => [ + 'orderID' => $this->getOrderId(), + ], + ] ]); $this->send($xml); $status = 'declined'; - if ($this->getProcReturnCode() == '00') { + $code = '1'; + $proc_return_code = '01'; + $obj = isset($this->data->transactions) ? $this->data->transactions : null; + $error_code = isset($this->data->respCode) ? $this->data->respCode : null; + $error_message = null; + + if ($this->getProcReturnCode() == '00' && $obj && !$error_code) { $status = 'approved'; + $code = isset($obj->approved) ? $obj->approved : null; + $proc_return_code = $this->getProcReturnCode(); } - $this->response = (object) [ - 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, - 'response' => isset($this->data->Response) ? $this->data->Response : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, - 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, - 'host_ref_num' => isset($this->data->Extra->HOST_REF_NUM) ? $this->data->Extra->HOST_REF_NUM : null, - 'order_status' => isset($this->data->Extra->ORDERSTATUS) ? $this->data->Extra->ORDERSTATUS : null, - 'process_type' => isset($this->data->Extra->CHARGE_TYPE_CD) ? $this->data->Extra->CHARGE_TYPE_CD : null, - 'pan' => isset($this->data->Extra->PAN) ? $this->data->Extra->PAN : null, - 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->data->Extra->NUMCODE : null, - 'first_amount' => isset($this->data->Extra->ORIG_TRANS_AMT) ? $this->data->Extra->ORIG_TRANS_AMT : null, - 'capture_amount' => isset($this->data->Extra->CAPTURE_AMT) ? $this->data->Extra->CAPTURE_AMT : null, + $error_message = isset($this->data->respText) ? $this->data->respText : null; + + $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' => 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' => $error_message, + 'extra' => null, 'all' => $this->data, - 'xml' => $xml, + 'original' => $this->data, ]; + if (!$history) { + unset($data['refunds']); + } + + $this->response = (object) $data; + return $this; } /** * Order History * - * @param $order_id + * @param array $meta * @return $this * @throws GuzzleException */ - public function history($order_id) + public function history(array $meta) { - $xml = $this->createXML([ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $order_id, - 'Extra' => [ - 'ORDERHISTORY' => 'QUERY', - ], - ]); - - $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 $this->status($meta, true); } }