diff --git a/system/autoload/PGDuitku.php b/system/autoload/PGDuitku.php deleted file mode 100644 index 3278c37e..00000000 --- a/system/autoload/PGDuitku.php +++ /dev/null @@ -1,95 +0,0 @@ -user = $user; - $this->trx = $trx; - } - - - 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() - { - 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() - { - global $_app_stage; - if ($_app_stage == 'Live') { - return 'https://passport.duitku.com/webapi/api/merchant/'; - } else { - return 'https://sandbox.duitku.com/webapi/api/merchant/'; - } - } -} \ No newline at end of file diff --git a/system/autoload/PGTripay.php b/system/autoload/PGTripay.php deleted file mode 100644 index 76af265b..00000000 --- a/system/autoload/PGTripay.php +++ /dev/null @@ -1,123 +0,0 @@ -user = $user; - $this->trx = $trx; - } - - function getSignature() - { - global $_c; - return hash_hmac('sha256', $_c['tripay_merchant'] . $this->trx['id'] . $this->trx['price'], $_c['tripay_secret_key']); - } - - function createTransaction($channel) - { - global $_c; - $json = [ - 'method' => $channel, - 'amount' => $this->trx['price'], - 'merchant_ref' => $this->trx['id'], - 'customer_name' => $this->user['fullname'], - 'customer_email' => (empty($this->user['email'])) ? $this->user['username'] . '@' . $_SERVER['HTTP_HOST'] : $this->user['email'], - 'customer_phone' => $this->user['phonenumber'], - 'order_items' => [ - [ - 'name' => $this->trx['plan_name'], - 'price' => $this->trx['price'], - 'quantity' => 1 - ] - ], - 'return_url' => U . 'order/view/' . $this->trx['id'] . '/check', - 'signature' => $this->getSignature() - ]; - return json_decode(Http::postJsonData($this->getServer() . 'transaction/create', $json, ['Authorization: Bearer ' . $_c['tripay_api_key']]), true); - /* - { - "success": true, - "message": "", - "data": { - "reference": "T0001000000000000006", - "merchant_ref": "INV345675", - "payment_selection_type": "static", - "payment_method": "BRIVA", - "payment_name": "BRI Virtual Account", - "customer_name": "Nama Pelanggan", - "customer_email": "emailpelanggan@domain.com", - "customer_phone": "081234567890", - "callback_url": "https://domainanda.com/callback", - "return_url": "https://domainanda.com/redirect", - "amount": 1000000, - "fee_merchant": 1500, - "fee_customer": 0, - "total_fee": 1500, - "amount_received": 998500, - "pay_code": "57585748548596587", - "pay_url": null, - "checkout_url": "https://tripay.co.id/checkout/T0001000000000000006", - "status": "UNPAID", - "expired_time": 1582855837, - } - } - */ - } - - function getStatus($trxID) - { - global $_c; - return json_decode(Http::getData($this->getServer() . 'transaction/detail?'.http_build_query(['reference'=>$trxID]), [ - 'Authorization: Bearer ' . $_c['tripay_api_key'] - ]), true); - /* - { - "success": true, - "message": "", - "data": { - "reference": "T0001000000000000006", - "merchant_ref": "INV345675", - "payment_selection_type": "static", - "payment_method": "BRIVA", - "payment_name": "BRI Virtual Account", - "customer_name": "Nama Pelanggan", - "customer_email": "emailpelanggan@domain.com", - "customer_phone": "081234567890", - "callback_url": "https://domainanda.com/callback", - "return_url": "https://domainanda.com/redirect", - "amount": 1000000, - "fee_merchant": 1500, - "fee_customer": 0, - "total_fee": 1500, - "amount_received": 998500, - "pay_code": "57585748548596587", - "pay_url": null, - "checkout_url": "https://tripay.co.id/checkout/T0001000000000000006", - "status": "PAID", - "expired_time": 1582855837, - } - } - */ - } - - private function getServer() - { - global $_app_stage; - if ($_app_stage == 'Live') { - return 'https://tripay.co.id/api/'; - } else { - return 'https://tripay.co.id/api-sandbox/'; - } - } -} diff --git a/system/autoload/PGXendit.php b/system/autoload/PGXendit.php deleted file mode 100644 index 4d1b758e..00000000 --- a/system/autoload/PGXendit.php +++ /dev/null @@ -1,93 +0,0 @@ -user = $user; - $this->trx = $trx; - return $this; - } - - function createInvoice() - { - global $_c; - $json = [ - 'external_id' => $this->trx['id'], - 'amount' => $this->trx['price'], - 'description' => $this->trx['plan_name'], - 'customer' => [ - 'mobile_number' => $this->user['phonenumber'], - ], - 'customer_notification_preference' => [ - 'invoice_created' => ['whatsapp', 'sms'], - 'invoice_reminder' => ['whatsapp', 'sms'], - 'invoice_paid' => ['whatsapp', 'sms'], - 'invoice_expired' => ['whatsapp', 'sms'] - ], - 'payment_methods ' => explode(',', $_c['xendit_channel']), - 'success_redirect_url' => U . 'order/view/' . $this->trx['id'] . '/check', - 'failure_redirect_url' => U . 'order/view/' . $this->trx['id'] . '/check' - ]; - - return json_decode(Http::postJsonData($this->getServer() . 'invoices', $json, ['Authorization: Basic ' . base64_encode($_c['xendit_secret_key'] . ':')]), true); - /* - { - "id": "631597513897510bace2459d", #gateway_trx_id - "external_id": "test-va-success-1662359375", - "user_id": "599bd7f1ccab55b020bb1147", - "status": "PENDING", - "merchant_name": "Xendit", - "merchant_profile_picture_url": "https://xnd-companies.s3.amazonaws.com/prod/1538466380522_868.png", - "amount": 3000000, - "description": "Test - VA Successful invoice payment", - "expiry_date": "2022-09-06T06:29:37.251Z", - "invoice_url": "https://checkout-staging.xendit.co/web/631597513897510bace2459d" - "created": "2022-09-05T06:29:37.954Z", - "updated": "2022-09-05T06:29:37.954Z" - } - */ - } - - function getInvoice($xendittrxID) - { - global $_c; - return json_decode(Http::getData($this->getServer() . 'invoices/' . $xendittrxID, [ - 'Authorization: Basic ' . base64_encode($_c['xendit_secret_key'] . ':') - ]), true); - /* - { - "id": "631597513897510bace2459d", #gateway_trx_id - "external_id": "test-va-success-1662359375", - "user_id": "599bd7f1ccab55b020bb1147", - "status": "PENDING", // CHECK THIS - "merchant_name": "Xendit", - "merchant_profile_picture_url": "https://xnd-companies.s3.amazonaws.com/prod/1538466380522_868.png", - "amount": 3000000, - "description": "Test - VA Successful invoice payment", - "expiry_date": "2022-09-06T06:29:37.251Z", - "invoice_url": "https://checkout-staging.xendit.co/web/631597513897510bace2459d" - "created": "2022-09-05T06:29:37.954Z", - "updated": "2022-09-05T06:29:37.954Z" - } - */ - } - - private function getServer(){ - global $_app_stage; - if ($_app_stage == 'Live') { - return 'https://api.xendit.co/v2/'; - } else { - return 'https://api.xendit.co/v2/'; - } - } -} \ No newline at end of file diff --git a/system/controllers/order.php b/system/controllers/order.php index 06bb4f0a..b6aca185 100644 --- a/system/controllers/order.php +++ b/system/controllers/order.php @@ -18,8 +18,8 @@ switch ($action) { $d = ORM::for_table('tbl_payment_gateway') ->where('username', $user['username']) ->find_many(); - $paginator = Paginator::bootstrap('tbl_payment_gateway','username',$user['username']); - $ui->assign('paginator',$paginator); + $paginator = Paginator::bootstrap('tbl_payment_gateway', 'username', $user['username']); + $ui->assign('paginator', $paginator); $ui->assign('d', $d); $ui->assign('_title', Lang::T('Order History') . ' - ' . $config['CompanyName']); $ui->display('user-orderHistory.tpl'); @@ -37,13 +37,13 @@ switch ($action) { ->where('username', $user['username']) ->where('status', 1) ->find_one(); - if($d){ + if ($d) { if (empty($d['pg_url_payment'])) { - r2(U . "order/buy/" . $trx['routers_id'] .'/'.$trx['plan_id'], 'w', Lang::T("Checking payment")); - }else{ - r2(U . "order/view/" . $d['id'].'/check/', 's', Lang::T("You have unpaid transaction")); + r2(U . "order/buy/" . $trx['routers_id'] . '/' . $trx['plan_id'], 'w', Lang::T("Checking payment")); + } else { + r2(U . "order/view/" . $d['id'] . '/check/', 's', Lang::T("You have unpaid transaction")); } - }else{ + } else { r2(U . "order/package/", 's', Lang::T("You have no unpaid transaction")); } case 'view': @@ -51,99 +51,22 @@ switch ($action) { $trx = ORM::for_table('tbl_payment_gateway') ->where('username', $user['username']) ->find_one($trxid); + // jika tidak ditemukan, berarti punya orang lain + if (empty($trx)) { + r2(U . "order/package", 'w', Lang::T("Payment not found")); + } // jika url kosong, balikin ke buy if (empty($trx['pg_url_payment'])) { - r2(U . "order/buy/" . $trx['routers_id'] .'/'.$trx['plan_id'], 'w', Lang::T("Checking payment")); + r2(U . "order/buy/" . $trx['routers_id'] . '/' . $trx['plan_id'], 'w', Lang::T("Checking payment")); } if ($routes['3'] == 'check') { - if ($trx['gateway'] == 'xendit') { - $pg = new PGXendit($trx,$user); - $result = $pg->getInvoice($trx['gateway_trx_id']); - - if ($result['status'] == 'PENDING') { - r2(U . "order/view/" . $trxid, 'w', Lang::T("Transaction still unpaid.")); - } else if (in_array($result['status'],['PAID','SETTLED']) && $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->payment_method = $result['payment_method']; - $trx->payment_channel = $result['payment_channel']; - $trx->paid_date = date('Y-m-d H:i:s', strtotime($result['updated'])); - $trx->status = 2; - $trx->save(); - - r2(U . "order/view/" . $trxid, 's', Lang::T("Transaction has been paid.")); - } else if ($result['status'] == 'EXPIRED') { - $trx->pg_paid_response = json_encode($result); - $trx->status = 3; - $trx->save(); - r2(U . "order/view/" . $trxid, 'd', Lang::T("Transaction expired.")); - }else if($trx['status'] == 2){ - r2(U . "order/view/" . $trxid, 'd', Lang::T("Transaction has been paid..")); - } - r2(U . "order/view/" . $trxid, 'd', Lang::T("Unknown Command.")); - } else if ($trx['gateway'] == 'tripay') { - $pg = new PGTripay($trx,$user); - $result = $pg->getStatus($trx['gateway_trx_id']); - if ($result['success']!=1) { - sendTelegram("Tripay payment status failed\n\n".json_encode($result, JSON_PRETTY_PRINT)); - r2(U . "order/view/" . $trxid, 'w', Lang::T("Payment check failed.")); - } - $result = $result['data']; - if ($result['status'] == 'UNPAID') { - r2(U . "order/view/" . $trxid, 'w', Lang::T("Transaction still unpaid.")); - } else if (in_array($result['status'],['PAID','SETTLED']) && $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->payment_method = $result['payment_method']; - $trx->payment_channel = $result['payment_name']; - $trx->paid_date = date('Y-m-d H:i:s', $result['paid_at']); - $trx->status = 2; - $trx->save(); - - r2(U . "order/view/" . $trxid, 's', Lang::T("Transaction has been paid.")); - } else if (in_array($result['status'],['EXPIRED','FAILED','REFUND'])) { - $trx->pg_paid_response = json_encode($result); - $trx->status = 3; - $trx->save(); - r2(U . "order/view/" . $trxid, 'd', Lang::T("Transaction expired.")); - }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..")); - } + if (!file_exists('system/paymentgateway/' . $trx['gateway'] . '.php')) { + r2(U . 'order/view/' . $trxid, 'e', Lang::T("No Payment Gateway Available")); } + include 'system/paymentgateway/' . $trx['gateway'] . '.php'; + call_user_func($trx['gateway'] . '_validate_config'); + call_user_func($_c['payment_gateway'] . '_get_status', $trx, $user); + } else if ($routes['3'] == 'cancel') { $trx->pg_paid_response = '{}'; $trx->status = 4; @@ -152,7 +75,7 @@ switch ($action) { $trx = ORM::for_table('tbl_payment_gateway') ->where('username', $user['username']) ->find_one($trxid); - if('midtrans'==$trx['gateway']){ + if ('midtrans' == $trx['gateway']) { //Hapus invoice link } } @@ -173,7 +96,12 @@ switch ($action) { if ($_c['payment_gateway'] == 'none') { r2(U . 'home', 'e', Lang::T("No Payment Gateway Available")); } - $back = "order/package"; + if (!file_exists('system/paymentgateway/' . $_c['payment_gateway'] . '.php')) { + r2(U . 'home', 'e', Lang::T("No Payment Gateway Available")); + } + include 'system/paymentgateway/' . $_c['payment_gateway'] . '.php'; + call_user_func($_c['payment_gateway'] . '_validate_config'); + $router = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routes['2'] * 1); $plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3'] * 1); if (empty($router) || empty($plan)) { @@ -183,19 +111,19 @@ switch ($action) { ->where('username', $user['username']) ->where('status', 1) ->find_one(); - if($d){ + if ($d) { if ($d['pg_url_payment']) { r2(U . "order/view/" . $d['id'], 'w', Lang::T("You already have unpaid transaction, cancel it or pay it.")); - }else{ - if($_c['payment_gateway']==$d['gateway']){ + } else { + if ($_c['payment_gateway'] == $d['gateway']) { $id = $d['id']; - }else{ + } else { $d->status = 4; $d->save(); } } } - if(empty($id)){ + if (empty($id)) { $d = ORM::for_table('tbl_payment_gateway')->create(); $d->username = $user['username']; $d->gateway = $_c['payment_gateway']; @@ -208,7 +136,7 @@ switch ($action) { $d->status = 1; $d->save(); $id = $d->id(); - }else{ + } else { $d->username = $user['username']; $d->gateway = $_c['payment_gateway']; $d->plan_id = $plan['id']; @@ -220,109 +148,12 @@ switch ($action) { $d->status = 1; $d->save(); } - if ($_c['payment_gateway'] == 'xendit') { - if (empty($_c['xendit_secret_key'])) { - sendTelegram("Xendit payment gateway not configured"); - r2(U . $back, 'e', Lang::T("Admin has not yet setup Xendit payment gateway, please tell admin")); - } - if ($id) { - $pg = new PGXendit($d,$user); - $result = $pg->createInvoice($id, $plan['price'], $user['username'], $plan['name_plan']); - if (!$result['id']) { - 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['id']; - $d->pg_url_payment = $result['invoice_url']; - $d->pg_request = json_encode($result); - $d->expired_date = date('Y-m-d H:i:s', strtotime($result['expiry_date'])); - $d->save(); - header('Location: ' . $result['invoice_url']); - exit(); - } else { - r2(U . "order/view/" . $d['id'], 'w', Lang::T("Failed to create Transaction..")); - } - } else if ($_c['payment_gateway'] == 'tripay') { - if (empty($_c['tripay_secret_key'])) { - sendTelegram("Tripay payment gateway not configured"); - r2(U . $back, 'e', Lang::T("Admin has not yet setup Tripay payment gateway, please tell admin")); - } - if(!in_array($routes['4'],explode(",",$_c['tripay_channel']))){ - $ui->assign('_title', 'Tripay Channel - ' . $config['CompanyName']); - $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_tripay.json'), true)); - $ui->assign('tripay_channels', explode(",",$_c['tripay_channel'])); - $ui->assign('path', $routes['2'].'/'.$routes['3']); - $ui->display('tripay_channel.tpl'); - break; - } - if ($id) { - $pg = new PGTripay($d,$user); - $result = $pg->createTransaction($routes['4']); - if ($result['success']!=1) { - sendTelegram("Tripay 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['data']['reference']; - $d->pg_url_payment = $result['data']['checkout_url']; - $d->pg_request = json_encode($result); - $d->expired_date = date('Y-m-d H:i:s', $result['data']['expired_time']); - $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..")); - } - } 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..")); - } + if (!$id) { + r2(U . "order/package/" . $d['id'], 'e', Lang::T("Failed to create Transaction..")); + } else { + call_user_func($_c['payment_gateway'] . '_create_transaction', $d, $user); } break; default: $ui->display('404.tpl'); -} \ No newline at end of file +} diff --git a/system/controllers/paymentgateway.php b/system/controllers/paymentgateway.php index 66169e45..3aa73e6a 100644 --- a/system/controllers/paymentgateway.php +++ b/system/controllers/paymentgateway.php @@ -5,206 +5,51 @@ _admin(); $ui->assign('_system_menu', 'paymentgateway'); -$action = $routes['1']; +$action = alphanumeric($routes['1']); $admin = Admin::_info(); $ui->assign('_admin', $admin); -switch ($action) { - case 'xendit': - $ui->assign('_title', 'Xendit - Payment Gateway - '. $config['CompanyName']); - $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_xendit.json'), true)); - $ui->display('pg-xendit.tpl'); - break; - case 'xendit-post': - $xendit_secret_key = _post('xendit_secret_key'); - $xendit_verification_token = _post('xendit_verification_token'); - $d = ORM::for_table('tbl_appconfig')->where('setting', 'xendit_secret_key')->find_one(); - if($d){ - $d->value = $xendit_secret_key; - $d->save(); +if(file_exists('system/paymentgateway/'.$action.'.php')){ + include 'system/paymentgateway/'.$action.'.php'; + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if(function_exists($action.'_save_config')){ + call_user_func($action.'_save_config'); }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'xendit_secret_key'; - $d->value = $xendit_secret_key; - $d->save(); + $ui->display('a404.tpl'); } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'xendit_verification_token')->find_one(); - if($d){ - $d->value = $xendit_verification_token; - $d->save(); + }else{ + if(function_exists($action.'_show_config')){ + call_user_func($action.'_show_config'); }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'xendit_verification_token'; - $d->value = $xendit_verification_token; - $d->save(); + $ui->display('a404.tpl'); } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'xendit_channel')->find_one(); - if($d){ - $d->value = implode(',',$_POST['xendit_channel']); - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'xendit_channel'; - $d->value = implode(',',$_POST['xendit_channel']); - $d->save(); + } +}else{ + if(!empty($action)){ + r2(U . 'paymentgateway', 'w', Lang::T('Payment Gateway Not Found')); + }else{ + $files = scandir('system/paymentgateway/'); + foreach($files as $file){ + if(pathinfo($file, PATHINFO_EXTENSION)=='php'){ + $pgs[] = str_replace('.php','',$file); + } } - - _log('[' . $admin['username'] . ']: Xendit ' . $_L['Settings_Saved_Successfully'], 'Admin', $admin['id']); - - r2(U . 'paymentgateway/xendit', 's', $_L['Settings_Saved_Successfully']); - break; - case 'midtrans': - $ui->assign('_title', 'Midtrans - Payment Gateway - '. $config['CompanyName']); - $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_midtrans.json'), true)); - $ui->display('pg-midtrans.tpl'); - break; - case 'midtrans-post': - $midtrans_merchant_id = _post('midtrans_merchant_id'); - $midtrans_client_key = _post('midtrans_client_key'); - $midtrans_server_key = _post('midtrans_server_key'); - $d = ORM::for_table('tbl_appconfig')->where('setting', 'midtrans_merchant_id')->find_one(); - if($d){ - $d->value = $midtrans_merchant_id; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'midtrans_merchant_id'; - $d->value = $midtrans_merchant_id; - $d->save(); + if(isset($_POST['payment_gateway'])){ + $payment_gateway = _post('payment_gateway'); + $d = ORM::for_table('tbl_appconfig')->where('setting', 'payment_gateway')->find_one(); + if($d){ + $d->value = $payment_gateway; + $d->save(); + }else{ + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'payment_gateway'; + $d->value = $payment_gateway; + $d->save(); + } + r2(U . 'paymentgateway', 's', Lang::T('Payment Gateway saved successfully')); } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'midtrans_client_key')->find_one(); - if($d){ - $d->value = $midtrans_client_key; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'midtrans_client_key'; - $d->value = $midtrans_client_key; - $d->save(); - } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'midtrans_server_key')->find_one(); - if($d){ - $d->value = $midtrans_server_key; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'midtrans_server_key'; - $d->value = $midtrans_server_key; - $d->save(); - } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'midtrans_channel')->find_one(); - if($d){ - $d->value = implode(',',$_POST['midtrans_channel']); - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'midtrans_channel'; - $d->value = implode(',',$_POST['midtrans_channel']); - $d->save(); - } - - _log('[' . $admin['username'] . ']: Midtrans ' . $_L['Settings_Saved_Successfully'], 'Admin', $admin['id']); - - r2(U . 'paymentgateway/midtrans', 's', $_L['Settings_Saved_Successfully']); - break; - case 'tripay': - $ui->assign('_title', 'Tripay - Payment Gateway - '. $config['CompanyName']); - $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_tripay.json'), true)); - $ui->display('pg-tripay.tpl'); - break; - case 'tripay-post': - $tripay_merchant = _post('tripay_merchant'); - $tripay_api_key = _post('tripay_api_key'); - $tripay_secret_key = _post('tripay_secret_key'); - $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_merchant')->find_one(); - if($d){ - $d->value = $tripay_merchant; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'tripay_merchant'; - $d->value = $tripay_merchant; - $d->save(); - } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_api_key')->find_one(); - if($d){ - $d->value = $tripay_api_key; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'tripay_api_key'; - $d->value = $tripay_api_key; - $d->save(); - } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_secret_key')->find_one(); - if($d){ - $d->value = $tripay_secret_key; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'tripay_secret_key'; - $d->value = $tripay_secret_key; - $d->save(); - } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_channel')->find_one(); - if($d){ - $d->value = implode(',',$_POST['tripay_channel']); - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'tripay_channel'; - $d->value = implode(',',$_POST['tripay_channel']); - $d->save(); - } - - _log('[' . $admin['username'] . ']: Tripay ' . $_L['Settings_Saved_Successfully'].json_encode($_POST['tripay_channel']), 'Admin', $admin['id']); - - r2(U . 'paymentgateway/tripay', 's', $_L['Settings_Saved_Successfully']); - break; - case 'duitku': - $ui->assign('_title', 'Duitku - Payment Gateway - '. $config['CompanyName']); - $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_duitku.json'), true)); - $ui->display('pg-duitku.tpl'); - break; - case 'duitku-post': - $duitku_merchant_id = _post('duitku_merchant_id'); - $duitku_merchant_key = _post('duitku_merchant_key'); - $d = ORM::for_table('tbl_appconfig')->where('setting', 'duitku_merchant_id')->find_one(); - if($d){ - $d->value = $duitku_merchant_id; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'duitku_merchant_id'; - $d->value = $duitku_merchant_id; - $d->save(); - } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'duitku_merchant_key')->find_one(); - if($d){ - $d->value = $duitku_merchant_key; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'duitku_merchant_key'; - $d->value = $duitku_merchant_key; - $d->save(); - } - - $d = ORM::for_table('tbl_appconfig')->where('setting', 'duitku_channel')->find_one(); - if($d){ - $d->value = implode(',',$_POST['duitku_channel']); - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'duitku_channel'; - $d->value = implode(',',$_POST['duitku_channel']); - $d->save(); - } - - _log('[' . $admin['username'] . ']: Duitku ' . $_L['Settings_Saved_Successfully'], 'Admin', $admin['id']); - - r2(U . 'paymentgateway/duitku', 's', $_L['Settings_Saved_Successfully']); - break; - default: - $ui->display('a404.tpl'); -} + $ui->assign('_title', 'Payment Gateway Settings - '. $config['CompanyName']); + $ui->assign('pgs', $pgs); + $ui->display('paymentgateway.tpl'); + } +} \ No newline at end of file diff --git a/system/controllers/settings.php b/system/controllers/settings.php index f51248a0..d6259311 100644 --- a/system/controllers/settings.php +++ b/system/controllers/settings.php @@ -207,7 +207,6 @@ switch ($action) { $address = _post('address'); $tawkto = _post('tawkto'); $radius_mode = _post('radius_mode')*1; - $payment_gateway = _post('payment_gateway'); if ($company == '') { r2(U . 'settings/app', 'e', $_L['All_field_is_required']); } else { @@ -268,16 +267,6 @@ switch ($action) { $d->save(); } - $d = ORM::for_table('tbl_appconfig')->where('setting', 'payment_gateway')->find_one(); - if($d){ - $d->value = $payment_gateway; - $d->save(); - }else{ - $d = ORM::for_table('tbl_appconfig')->create(); - $d->setting = 'payment_gateway'; - $d->value = $payment_gateway; - $d->save(); - } $d = ORM::for_table('tbl_appconfig')->where('setting', 'tawkto')->find_one(); if($d){ diff --git a/system/lan/indonesia/common.lan.php b/system/lan/indonesia/common.lan.php index 8bc7c786..48853982 100644 --- a/system/lan/indonesia/common.lan.php +++ b/system/lan/indonesia/common.lan.php @@ -291,3 +291,5 @@ $_L['Order_History'] = 'Order History'; $_L['Gateway'] = 'Gateway'; $_L['Date_Done'] = 'Date Done'; $_L['Unpaid_Order'] = 'Unpaid Order'; +$_L['Payment_Gateway_Not_Found'] = 'Payment Gateway Not Found'; +$_L['Payment_Gateway_saved_successfully'] = 'Payment Gateway saved successfully'; diff --git a/system/paymentgateway/duitku.php b/system/paymentgateway/duitku.php new file mode 100644 index 00000000..421661fd --- /dev/null +++ b/system/paymentgateway/duitku.php @@ -0,0 +1,170 @@ +assign('_title', 'Duitku - Payment Gateway - ' . $config['CompanyName']); + $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_duitku.json'), true)); + $ui->display('pg-duitku.tpl'); +} + +function duitku_save_config() +{ + global $admin; + $duitku_merchant_id = _post('duitku_merchant_id'); + $duitku_merchant_key = _post('duitku_merchant_key'); + $d = ORM::for_table('tbl_appconfig')->where('setting', 'duitku_merchant_id')->find_one(); + if ($d) { + $d->value = $duitku_merchant_id; + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'duitku_merchant_id'; + $d->value = $duitku_merchant_id; + $d->save(); + } + $d = ORM::for_table('tbl_appconfig')->where('setting', 'duitku_merchant_key')->find_one(); + if ($d) { + $d->value = $duitku_merchant_key; + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'duitku_merchant_key'; + $d->value = $duitku_merchant_key; + $d->save(); + } + $d = ORM::for_table('tbl_appconfig')->where('setting', 'duitku_channel')->find_one(); + if ($d) { + $d->value = implode(',', $_POST['duitku_channel']); + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'duitku_channel'; + $d->value = implode(',', $_POST['duitku_channel']); + $d->save(); + } + _log('[' . $admin['username'] . ']: Duitku ' . Lang::T('Settings_Saved_Successfully'), 'Admin', $admin['id']); + r2(U . 'paymentgateway/duitku', 's', Lang::T('Settings_Saved_Successfully')); +} + +function duitku_create_transaction($trx, $user) +{ + global $config, $routes, $ui; + + $channels = json_decode(file_get_contents('system/paymentgateway/channel_duitku.json'), true); + if (!in_array($routes[4], explode(",", $config['duitku_channel']))) { + $ui->assign('_title', 'Duitku Channel - ' . $config['CompanyName']); + $ui->assign('channels', $channels); + $ui->assign('duitku_channels', explode(",", $config['duitku_channel'])); + $ui->assign('path', $routes['2'] . '/' . $routes['3']); + $ui->display('duitku_channel.tpl'); + die(); + } + + $json = [ + 'paymentMethod' => $routes[4], + 'paymentAmount' => $trx['price'], + 'merchantCode' => $config['duitku_merchant_id'], + 'merchantOrderId' => $trx['id'], + 'productDetails' => $trx['plan_name'], + 'merchantUserInfo' => $user['fullname'], + 'customerVaName' => $user['fullname'], + 'email' => (empty($user['email'])) ? $user['username'] . '@' . $_SERVER['HTTP_HOST'] : $user['email'], + 'phoneNumber' => $user['phonenumber'], + 'itemDetails' => [ + [ + 'name' => $trx['plan_name'], + 'price' => $trx['price'], + 'quantity' => 1 + ] + ], + 'returnUrl' => U . 'order/view/' . $trx['id'] . '/check', + 'signature' => md5($config['duitku_merchant_id'] . $trx['id'] . $trx['price'] . $config['duitku_merchant_key']) + ]; + + $result = json_decode(Http::postJsonData(duitku_get_server() . 'v2/inquiry', $json), true); + + if (empty($result['paymentUrl'])) { + sendTelegram("Duitku payment failed\n\n" . json_encode($result, JSON_PRETTY_PRINT)); + r2(U . 'order/package', '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/" . $d['id'], 's', Lang::T("Create Transaction Success")); +} + +function duitku_get_status($trx, $user) +{ + global $config; + $json = [ + 'merchantCode' => $config['duitku_merchant_id'], + 'merchantOrderId' => $trx['id'], + 'signature' => md5($config['duitku_merchant_id'] . $trx['id'] . $config['duitku_merchant_key']) + ]; + $result = json_decode(Http::postJsonData(duitku_get_server() . 'transactionStatus', $json), true); + if ($result['reference'] != $trx['gateway_trx_id']) { + sendTelegram("Duitku payment status failed\n\n" . json_encode($result, JSON_PRETTY_PRINT)); + r2(U . "order/view/" . $trx['id'], 'w', Lang::T("Payment check failed.")); + } + if ($result['statusCode'] == '01') { + r2(U . "order/view/" . $trx['id'], '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/" . $trx['id'], '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/" . $trx['id'], '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/" . $trx['id'], 'd', Lang::T("Transaction expired or Failed.")); + } else if ($trx['status'] == 2) { + r2(U . "order/view/" . $trx['id'], 'd', Lang::T("Transaction has been paid..")); + } +} + +function duitku_get_server() +{ + global $_app_stage; + if ($_app_stage == 'Live') { + return 'https://passport.duitku.com/webapi/api/merchant/'; + } else { + return 'https://sandbox.duitku.com/webapi/api/merchant/'; + } +} diff --git a/system/paymentgateway/tripay.php b/system/paymentgateway/tripay.php new file mode 100644 index 00000000..ad669d09 --- /dev/null +++ b/system/paymentgateway/tripay.php @@ -0,0 +1,172 @@ + +assign('_title', 'Tripay - Payment Gateway - ' . $config['CompanyName']); + $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_tripay.json'), true)); + $ui->display('pg-tripay.tpl'); +} + +function tripay_save_config() +{ + global $admin, $_L; + $tripay_merchant = _post('tripay_merchant'); + $tripay_api_key = _post('tripay_api_key'); + $tripay_secret_key = _post('tripay_secret_key'); + $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_merchant')->find_one(); + if ($d) { + $d->value = $tripay_merchant; + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'tripay_merchant'; + $d->value = $tripay_merchant; + $d->save(); + } + $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_api_key')->find_one(); + if ($d) { + $d->value = $tripay_api_key; + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'tripay_api_key'; + $d->value = $tripay_api_key; + $d->save(); + } + $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_secret_key')->find_one(); + if ($d) { + $d->value = $tripay_secret_key; + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'tripay_secret_key'; + $d->value = $tripay_secret_key; + $d->save(); + } + $d = ORM::for_table('tbl_appconfig')->where('setting', 'tripay_channel')->find_one(); + if ($d) { + $d->value = implode(',', $_POST['tripay_channel']); + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'tripay_channel'; + $d->value = implode(',', $_POST['tripay_channel']); + $d->save(); + } + + _log('[' . $admin['username'] . ']: Tripay ' . $_L['Settings_Saved_Successfully'] . json_encode($_POST['tripay_channel']), 'Admin', $admin['id']); + + r2(U . 'paymentgateway/tripay', 's', $_L['Settings_Saved_Successfully']); +} + + +function tripay_create_transaction($channel, $trx, $user) +{ + global $config, $routes, $ui; + $channels = json_decode(file_get_contents('system/paymentgateway/channel_tripay.json'), true); + if (!in_array($routes[4], explode(",", $config['tripay_channel']))) { + $ui->assign('_title', 'Tripay Channel - ' . $config['CompanyName']); + $ui->assign('channels', $channels); + $ui->assign('tripay_channels', explode(",", $config['tripay_channel'])); + $ui->assign('path', $routes[2] . '/' . $routes[3]); + $ui->display('tripay_channel.tpl'); + die(); + } + $json = [ + 'method' => $channel, + 'amount' => $trx['price'], + 'merchant_ref' => $trx['id'], + 'customer_name' => $user['fullname'], + 'customer_email' => (empty($user['email'])) ? $user['username'] . '@' . $_SERVER['HTTP_HOST'] : $user['email'], + 'customer_phone' => $user['phonenumber'], + 'order_items' => [ + [ + 'name' => $trx['plan_name'], + 'price' => $trx['price'], + 'quantity' => 1 + ] + ], + 'return_url' => U . 'order/view/' . $trx['id'] . '/check', + 'signature' => hash_hmac('sha256', $config['tripay_merchant'] . $trx['id'] . $trx['price'], $config['tripay_secret_key']) + ]; + $result = json_decode(Http::postJsonData(tripay_get_server() . 'transaction/create', $json, ['Authorization: Bearer ' . $config['tripay_api_key']]), true); + if ($result['success'] != 1) { + sendTelegram("Tripay payment failed\n\n" . json_encode($result, JSON_PRETTY_PRINT)); + r2(U . 'order/package', '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['data']['reference']; + $d->pg_url_payment = $result['data']['checkout_url']; + $d->pg_request = json_encode($result); + $d->expired_date = date('Y-m-d H:i:s', $result['data']['expired_time']); + $d->save(); + r2(U . "order/view/" . $d['id'], 's', Lang::T("Create Transaction Success")); + +} + +function tripay_get_status($trx, $user) +{ + global $config; + $result = json_decode(Http::getData(tripay_get_server() . 'transaction/detail?' . http_build_query(['reference' => $trx['id']]), [ + 'Authorization: Bearer ' . $config['tripay_api_key'] + ]), true); + if ($result['success'] != 1) { + sendTelegram("Tripay payment status failed\n\n" . json_encode($result, JSON_PRETTY_PRINT)); + r2(U . "order/view/" . $trx['id'], 'w', Lang::T("Payment check failed.")); + } + $result = $result['data']; + if ($result['status'] == 'UNPAID') { + r2(U . "order/view/" . $trx['id'], 'w', Lang::T("Transaction still unpaid.")); + } else if (in_array($result['status'], ['PAID', 'SETTLED']) && $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/" . $trx['id'], 'd', Lang::T("Failed to activate your Package, try again later.")); + } + + $trx->pg_paid_response = json_encode($result); + $trx->payment_method = $result['payment_method']; + $trx->payment_channel = $result['payment_name']; + $trx->paid_date = date('Y-m-d H:i:s', $result['paid_at']); + $trx->status = 2; + $trx->save(); + + r2(U . "order/view/" . $trx['id'], 's', Lang::T("Transaction has been paid.")); + } else if (in_array($result['status'], ['EXPIRED', 'FAILED', 'REFUND'])) { + $trx->pg_paid_response = json_encode($result); + $trx->status = 3; + $trx->save(); + r2(U . "order/view/" . $trx['id'], 'd', Lang::T("Transaction expired.")); + } else if ($trx['status'] == 2) { + r2(U . "order/view/" . $trx['id'], 'd', Lang::T("Transaction has been paid..")); + } +} + +function tripay_get_server() +{ + global $_app_stage; + if ($_app_stage == 'Live') { + return 'https://tripay.co.id/api/'; + } else { + return 'https://tripay.co.id/api-sandbox/'; + } +} diff --git a/system/paymentgateway/xendit.php b/system/paymentgateway/xendit.php new file mode 100644 index 00000000..5234ac22 --- /dev/null +++ b/system/paymentgateway/xendit.php @@ -0,0 +1,140 @@ + +assign('_title', 'Xendit - Payment Gateway - ' . $config['CompanyName']); + $ui->assign('channels', json_decode(file_get_contents('system/paymentgateway/channel_xendit.json'), true)); + $ui->display('pg-xendit.tpl'); +} + +function xendit_save_config() +{ + global $admin, $_L; + $xendit_secret_key = _post('xendit_secret_key'); + $xendit_verification_token = _post('xendit_verification_token'); + $d = ORM::for_table('tbl_appconfig')->where('setting', 'xendit_secret_key')->find_one(); + if ($d) { + $d->value = $xendit_secret_key; + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'xendit_secret_key'; + $d->value = $xendit_secret_key; + $d->save(); + } + $d = ORM::for_table('tbl_appconfig')->where('setting', 'xendit_verification_token')->find_one(); + if ($d) { + $d->value = $xendit_verification_token; + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'xendit_verification_token'; + $d->value = $xendit_verification_token; + $d->save(); + } + $d = ORM::for_table('tbl_appconfig')->where('setting', 'xendit_channel')->find_one(); + if ($d) { + $d->value = implode(',', $_POST['xendit_channel']); + $d->save(); + } else { + $d = ORM::for_table('tbl_appconfig')->create(); + $d->setting = 'xendit_channel'; + $d->value = implode(',', $_POST['xendit_channel']); + $d->save(); + } + + _log('[' . $admin['username'] . ']: Xendit ' . $_L['Settings_Saved_Successfully'], 'Admin', $admin['id']); + + r2(U . 'paymentgateway/xendit', 's', $_L['Settings_Saved_Successfully']); +} + + +function xendit_create_transaction($trx, $user) +{ + global $config; + $json = [ + 'external_id' => $trx['id'], + 'amount' => $trx['price'], + 'description' => $trx['plan_name'], + 'customer' => [ + 'mobile_number' => $user['phonenumber'], + ], + 'customer_notification_preference' => [ + 'invoice_created' => ['whatsapp', 'sms'], + 'invoice_reminder' => ['whatsapp', 'sms'], + 'invoice_paid' => ['whatsapp', 'sms'], + 'invoice_expired' => ['whatsapp', 'sms'] + ], + 'payment_methods ' => explode(',', $config['xendit_channel']), + 'success_redirect_url' => U . 'order/view/' . $trx['id'] . '/check', + 'failure_redirect_url' => U . 'order/view/' . $trx['id'] . '/check' + ]; + + $result = json_decode(Http::postJsonData(xendit_get_server() . 'invoices', $json, ['Authorization: Basic ' . base64_encode($config['xendit_secret_key'] . ':')]), true); + if (!$result['id']) { + r2(U . 'order/package', '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['id']; + $d->pg_url_payment = $result['invoice_url']; + $d->pg_request = json_encode($result); + $d->expired_date = date('Y-m-d H:i:s', strtotime($result['expiry_date'])); + $d->save(); + header('Location: ' . $result['invoice_url']); + exit(); +} + +function xendit_get_status($trx, $user) +{ + global $config; + $result = json_decode(Http::getData(xendit_get_server() . 'invoices/' . $trx['id'], [ + 'Authorization: Basic ' . base64_encode($config['xendit_secret_key'] . ':') + ]), true); + + if ($result['status'] == 'PENDING') { + r2(U . "order/view/" . $trx['id'], 'w', Lang::T("Transaction still unpaid.")); + } else if (in_array($result['status'], ['PAID', 'SETTLED']) && $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/" . $trx['id'], 'd', Lang::T("Failed to activate your Package, try again later.")); + } + $trx->pg_paid_response = json_encode($result); + $trx->payment_method = $result['payment_method']; + $trx->payment_channel = $result['payment_channel']; + $trx->paid_date = date('Y-m-d H:i:s', strtotime($result['updated'])); + $trx->status = 2; + $trx->save(); + + r2(U . "order/view/" . $trx['id'], 's', Lang::T("Transaction has been paid.")); + } else if ($result['status'] == 'EXPIRED') { + $trx->pg_paid_response = json_encode($result); + $trx->status = 3; + $trx->save(); + r2(U . "order/view/" . $trx['id'], 'd', Lang::T("Transaction expired.")); + } else if ($trx['status'] == 2) { + r2(U . "order/view/" . $trx['id'], 'd', Lang::T("Transaction has been paid..")); + } + r2(U . "order/view/" . $trx['id'], 'd', Lang::T("Unknown Command.")); +} + +function xendit_get_server() +{ + global $_app_stage; + if ($_app_stage == 'Live') { + return 'https://api.xendit.co/v2/'; + } else { + return 'https://api.xendit.co/v2/'; + } +} diff --git a/ui/ui/app-settings.tpl b/ui/ui/app-settings.tpl index f9f231ed..c9ee7196 100644 --- a/ui/ui/app-settings.tpl +++ b/ui/ui/app-settings.tpl @@ -26,18 +26,6 @@ -