midtrans pay, but not yet validated

This commit is contained in:
Ibnu Maksum
2022-09-09 16:46:39 +07:00
parent e9d67ce220
commit dd46273a04
10 changed files with 195 additions and 86 deletions

View File

@ -78,6 +78,66 @@ function xendit_get_invoice($xendittrxID){
*/
}
/** MIDTRANS */
function midtrans_create_payment($trxID, $amount){
global $midtrans_server,$_c;
$json = [
'transaction_details ' => [
'order_id' => $trxID,
'gross_amount' => $amount,
"payment_link_id" => alphanumeric(ucwords($_c['CompanyName']))."_".crc32($_c['CompanyName'])."_".$trxID
],
'enabled_payments' => explode(',',$_c['midtrans_channel']),
"usage_limit"=> 1,
"expiry" => [
"duration" => 24,
"unit" => "hour"
]
];
$json = json_decode(postJsonData($midtrans_server.'v1/payment-links', $json, ['Authorization: Basic '.base64_encode($_c['midtrans_server_key'].':')]),true);
if(!empty($json['error_messages'])){
sendTelegram(json_encode("Midtrans create Payment error:\n".alphanumeric($_c['CompanyName'])."_".crc32($_c['CompanyName'])."_".$trxID."\n".$json['error_messages']));
}
return $json;
/*
{
"order_id": "concert-ticket-05", //traxid
"payment_url": "https://app.sandbox.midtrans.com/payment-links/amazing-ticket-payment-123"
}
*/
}
function midtrans_check_payment($midtranstrxID){
global $midtrans_server,$_c;
return json_decode(getData($midtrans_server.'v2/'.$midtranstrxID.'/status', [
'Authorization: Basic '.base64_encode($_c['midtrans_server_key'].':')
]),true);
/*
{
"masked_card": "41111111-1111",
"approval_code": "1599493766402",
"bank": "bni",
"channel_response_code": "00",
"channel_response_message": "Approved",
"transaction_time": "2020-09-07 22:49:26",
"gross_amount": "10000.00",
"currency": "IDR",
"order_id": "SANDBOX-G710367688-806",
"payment_type": "credit_card",
"signature_key": "4d4abc70f5a88b09f48f3ab5cb91245feb0b3d89181117a677767b42f8cbe477f5a0d38af078487071311f97da646c1eb9542c1bbf0b19fa9f12e64605ac405e",
"status_code": "200",
"transaction_id": "3853c491-ca9b-4bcc-ac20-3512ff72a5d0",
"transaction_status": "cancel",
"fraud_status": "challenge",
"status_message": "Success, transaction is found",
"merchant_id": "G710367688",
"card_type": "credit"
}
*/
}
function getData($url, $headers)
{
$ch = curl_init();

View File

@ -91,43 +91,51 @@ switch ($action) {
$ui->assign('_title', 'TRX #' . $trxid . ' - ' . $config['CompanyName']);
$ui->display('user-orderView.tpl');
break;
case 'ppoe-buy':
case 'hotspot-buy':
if (empty($_c['xendit_secret_key'])) {
r2(U . "order/hotspot", 'e', Lang::T("Admin has not yet setup Xendit payment gateway, please tell admin"));
}
$back = "order/".str_replace('-buy','',$action);
$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)) {
r2(U . "order/hotspot", 'e', Lang::T("Plan Not found"));
r2(U . $back, 'e', Lang::T("Plan Not found"));
}
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
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']){
$id = $d['id'];
}else{
$d->status = 4;
$d->save();
}
}
if(empty($id)){
$d = ORM::for_table('tbl_payment_gateway')->create();
$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();
$id = $d->id();
}
if ($_c['payment_gateway'] == 'xendit') {
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
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."));
}
$id = $d['id'];
} else {
$d = ORM::for_table('tbl_payment_gateway')->create();
$d->username = $user['username'];
$d->gateway = 'xendit';
$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();
$id = $d->id();
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) {
$result = xendit_create_invoice($id, $plan['price'], $user['username'], $plan['name_plan']);
if (!$result['id']) {
r2(U . "order/hotspot", 'e', Lang::T("Failed to create transaction."));
r2(U . $back, 'e', Lang::T("Failed to create transaction."));
}
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
@ -144,7 +152,34 @@ switch ($action) {
r2(U . "order/view/" . $d['id'], 'w', Lang::T("Failed to create Transaction.."));
}
} else if ($_c['payment_gateway'] == 'midtrans') {
if (empty($_c['midtrans_server_key'])) {
sendTelegram("Midtrans payment gateway not configured");
r2(U . $back, 'e', Lang::T("Admin has not yet setup Midtrans payment gateway, please tell admin"));
}
if ($id) {
$result = midtrans_create_payment($id, $plan['price']);
if (!$result['payment_url']) {
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['order_id'];
$d->pg_url_payment = $result['payment_url'];
$d->pg_request = json_encode($result);
$d->expired_date = date('Y-m-d H:i:s', strtotime("+1 days"));
$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'] == '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"));
}
}
break;
default:

View File

@ -272,3 +272,5 @@ $_L['Transaction_still_unpaid'] = 'Transaction still unpaid.';
$_L['Paid_Date'] = 'Paid Date';
$_L['Transaction_has_been_paid'] = 'Transaction has been paid.';
$_L['PAID'] = 'PAID';
$_L['Buy_Hotspot_Plan'] = 'Buy Hotspot Plan';
$_L['Buy_PPOE_Plan'] = 'Buy PPOE Plan';