From cef6e2a2f68a5370f658ff0087bb0ca788d88d08 Mon Sep 17 00:00:00 2001
From: Ibnu Maksum <ibnumaksum@gmail.com>
Date: Wed, 14 Sep 2022 16:54:29 +0700
Subject: [PATCH] Duitku ok

---
 system/autoload/PGDuitku.php              | 64 +++++++++++++++--
 system/controllers/order.php              | 83 ++++++++++++++++++++++-
 system/paymentgateway/channel_duitku.json |  8 ---
 ui/ui/duitku_channel.tpl                  | 19 ++++++
 4 files changed, 157 insertions(+), 17 deletions(-)
 create mode 100644 ui/ui/duitku_channel.tpl

diff --git a/system/autoload/PGDuitku.php b/system/autoload/PGDuitku.php
index c5af3363..3278c37e 100644
--- a/system/autoload/PGDuitku.php
+++ b/system/autoload/PGDuitku.php
@@ -18,19 +18,69 @@ class PGDuitku
         $this->trx = $trx;
     }
 
-    function getSignature($amount,$datetime)
-    {
-        global $_c;
-        return hash('sha256', $_c['duitku_merchant_id'] . $amount . $datetime . $_c['duitku_merchant_key']);
-    }
-
 
     function createTransaction($channel)
     {
+        global $_c;
+        $json = [
+            'paymentMethod' => $channel,
+            'paymentAmount' => $this->trx['price'],
+            'merchantCode' => $_c['duitku_merchant_id'],
+            'merchantOrderId' => $this->trx['id'],
+            'productDetails' => $this->trx['plan_name'],
+            'merchantUserInfo' =>  $this->user['fullname'],
+            'customerVaName' =>  $this->user['fullname'],
+            'email' => (empty($this->user['email'])) ? $this->user['username'] . '@' . $_SERVER['HTTP_HOST'] : $this->user['email'],
+            'phoneNumber' => $this->user['phonenumber'],
+            'itemDetails' => [
+                [
+                    'name' => $this->trx['plan_name'],
+                    'price' => $this->trx['price'],
+                    'quantity' => 1
+                ]
+            ],
+            'returnUrl' => U . 'order/view/' . $this->trx['id'] . '/check',
+            'signature' => md5($_c['duitku_merchant_id'] . $this->trx['id'] . $this->trx['price'] . $_c['duitku_merchant_key'])
+        ];
+        return json_decode(Http::postJsonData($this->getServer() . 'v2/inquiry', $json), true);
+        /*
+        {
+            "merchantCode": "DXXXX",
+            "reference": "DXXXXCX80TZJ85Q70QCI",
+            "paymentUrl": "https://sandbox.duitku.com/topup/topupdirectv2.aspx?ref=BCA7WZ7EIDXXXXWEC",
+            "vaNumber": "7007014001444348",
+            "qrString": "",
+            "amount": "40000",
+            "statusCode": "00",
+            "statusMessage": "SUCCESS"
+        }
+        00 - Success
+        01 - Pending
+        02 - Canceled
+         */
     }
 
-    function getStatus($trxID)
+    function getStatus()
     {
+        global $_c;
+        $json = [
+            'merchantCode' => $_c['duitku_merchant_id'],
+            'merchantOrderId' => $this->trx['id'],
+            'signature' => md5($_c['duitku_merchant_id'] . $this->trx['id'] . $_c['duitku_merchant_key'])
+        ];
+        return json_decode(Http::postJsonData($this->getServer() . 'transactionStatus', $json), true);
+        /*
+        {
+            "merchantOrderId": "abcde12345",
+            "reference": "DXXXXCX80TZJ85Q70QCI",
+            "amount": "100000",
+            "statusCode": "00",
+            "statusMessage": "SUCCESS"
+        }
+        00 - Success
+        01 - Pending
+        02 - Canceled
+         */
     }
 
     private function getServer()
diff --git a/system/controllers/order.php b/system/controllers/order.php
index 70be0461..06bb4f0a 100644
--- a/system/controllers/order.php
+++ b/system/controllers/order.php
@@ -88,8 +88,6 @@ switch ($action) {
                 $pg = new PGTripay($trx,$user);
                 $result = $pg->getStatus($trx['gateway_trx_id']);
                 if ($result['success']!=1) {
-                    print_r($result);
-                    die();
                     sendTelegram("Tripay payment status failed\n\n".json_encode($result, JSON_PRETTY_PRINT));
                     r2(U . "order/view/" . $trxid, 'w', Lang::T("Payment check failed."));
                 }
@@ -117,6 +115,34 @@ switch ($action) {
                 }else if($trx['status'] == 2){
                     r2(U . "order/view/" . $trxid, 'd', Lang::T("Transaction has been paid.."));
                 }
+            } else if ($trx['gateway'] == 'duitku') {
+                $pg = new PGDuitku($trx,$user);
+                $result = $pg->getStatus($trx['id']);
+                if ($result['reference']!=$trx['gateway_trx_id']) {
+                    sendTelegram("Duitku payment status failed\n\n".json_encode($result, JSON_PRETTY_PRINT));
+                    r2(U . "order/view/" . $trxid, 'w', Lang::T("Payment check failed."));
+                }
+                if ($result['statusCode'] == '01') {
+                    r2(U . "order/view/" . $trxid, 'w', Lang::T("Transaction still unpaid."));
+                } else if ($result['statusCode']=='00' && $trx['status'] != 2) {
+                    if (!Package::rechargeUser($user['id'], $trx['routers'], $trx['plan_id'], $trx['gateway'],  $result['payment_method'] . ' ' . $result['payment_channel'])) {
+                        r2(U . "order/view/" . $trxid, 'd', Lang::T("Failed to activate your Package, try again later."));
+                    }
+
+                    $trx->pg_paid_response = json_encode($result);
+                    $trx->paid_date = date('Y-m-d H:i:s');
+                    $trx->status = 2;
+                    $trx->save();
+
+                    r2(U . "order/view/" . $trxid, 's', Lang::T("Transaction has been paid."));
+                } else if ($result['statusCode']=='02') {
+                    $trx->pg_paid_response = json_encode($result);
+                    $trx->status = 3;
+                    $trx->save();
+                    r2(U . "order/view/" . $trxid, 'd', Lang::T("Transaction expired or Failed."));
+                }else if($trx['status'] == 2){
+                    r2(U . "order/view/" . $trxid, 'd', Lang::T("Transaction has been paid.."));
+                }
             }
         } else if ($routes['3'] == 'cancel') {
             $trx->pg_paid_response = '{}';
@@ -182,6 +208,17 @@ switch ($action) {
             $d->status = 1;
             $d->save();
             $id = $d->id();
+        }else{
+            $d->username = $user['username'];
+            $d->gateway = $_c['payment_gateway'];
+            $d->plan_id = $plan['id'];
+            $d->plan_name = $plan['name_plan'];
+            $d->routers_id = $router['id'];
+            $d->routers = $router['name'];
+            $d->price = $plan['price'];
+            $d->created_date = date('Y-m-d H:i:s');
+            $d->status = 1;
+            $d->save();
         }
         if ($_c['payment_gateway'] == 'xendit') {
             if (empty($_c['xendit_secret_key'])) {
@@ -242,6 +279,48 @@ switch ($action) {
             } else {
                 r2(U . "order/view/" . $d['id'], 'w', Lang::T("Failed to create Transaction.."));
             }
+        } else if ($_c['payment_gateway'] == 'duitku') {
+            if (empty($_c['duitku_merchant_key'])) {
+                sendTelegram("Duitku payment gateway not configured");
+                r2(U . $back, 'e', Lang::T("Admin has not yet setup Duitku payment gateway, please tell admin"));
+            }
+            $channels = json_decode(file_get_contents('system/paymentgateway/channel_duitku.json'), true);
+            if(!in_array($routes['4'],explode(",",$_c['duitku_channel']))){
+                $ui->assign('_title', 'Duitku Channel - ' . $config['CompanyName']);
+                $ui->assign('channels', $channels);
+                $ui->assign('duitku_channels', explode(",",$_c['duitku_channel']));
+                $ui->assign('path', $routes['2'].'/'.$routes['3']);
+                $ui->display('duitku_channel.tpl');
+                break;
+            }
+            if ($id) {
+                $pg = new PGDuitku($d,$user);
+                $result = $pg->createTransaction($routes['4']);
+                if (empty($result['paymentUrl'])) {
+                    sendTelegram("Duitku payment failed\n\n".json_encode($result, JSON_PRETTY_PRINT));
+                    r2(U . $back, 'e', Lang::T("Failed to create transaction."));
+                }
+                $d = ORM::for_table('tbl_payment_gateway')
+                    ->where('username', $user['username'])
+                    ->where('status', 1)
+                    ->find_one();
+                $d->gateway_trx_id = $result['reference'];
+                $d->pg_url_payment = $result['paymentUrl'];
+                $d->payment_method = $routes['4'];
+                foreach($channels as $channel){
+                    if($channel['id']==$routes['4']){
+                        $d->payment_channel = $channel['name'];
+                        break;
+                    }
+                }
+                $d->pg_request = json_encode($result);
+                $d->expired_date = date('Y-m-d H:i:s', strtotime("+1 day"));
+                $d->save();
+                r2(U . "order/view/" . $id, 'w', Lang::T("Create Transaction Success"));
+                exit();
+            } else {
+                r2(U . "order/view/" . $d['id'], 'w', Lang::T("Failed to create Transaction.."));
+            }
         }
         break;
     default:
diff --git a/system/paymentgateway/channel_duitku.json b/system/paymentgateway/channel_duitku.json
index e2e4c569..a0e5cc7b 100644
--- a/system/paymentgateway/channel_duitku.json
+++ b/system/paymentgateway/channel_duitku.json
@@ -15,10 +15,6 @@
 		"id": "I1",
 		"name": "BNI"
 	},
-	{
-		"id": "MANDIRIVA",
-		"name": "Mandiri"
-	},
 	{
 		"id": "B1",
 		"name": "CIMB Niaga"
@@ -35,10 +31,6 @@
 		"id": "AG",
 		"name": "Artha Graha"
 	},
-	{
-		"id": "NC",
-		"name": "Bank Neo Commerce/BNC"
-	},
 	{
 		"id": "BR",
 		"name": "BRIVA"
diff --git a/ui/ui/duitku_channel.tpl b/ui/ui/duitku_channel.tpl
new file mode 100644
index 00000000..96038896
--- /dev/null
+++ b/ui/ui/duitku_channel.tpl
@@ -0,0 +1,19 @@
+{include file="sections/user-header.tpl"}
+<div class="row">
+    <div class="col-sm-12">
+            <div class="panel panel-info panel-hovered">
+            <div class="panel-heading">Duitku {Lang::T('Payment Channel')}</div>
+            <div class="panel-body row">
+                {foreach $channels as $channel}
+                    {if in_array($channel['id'], $duitku_channels)}
+                        <div class="col-sm-4 mb20">
+                            <a href="{$_url}order/buy/{$path}/{$channel['id']}"
+                            onclick="return confirm('{$channel['name']}')"
+                            class="btn btn-block btn-default">{$channel['name']}</a>
+                        </div>
+                    {/if}
+                {/foreach}
+            </div>
+    </div>
+</div>
+{include file="sections/user-footer.tpl"}