plugin system for payment gateway

This commit is contained in:
Ibnu Maksum
2022-09-16 11:05:33 +07:00
parent 0bd6c9e3c7
commit 061224b469
17 changed files with 598 additions and 815 deletions

View File

@ -0,0 +1,170 @@
<?php
/**
* PHP Mikrotik Billing (https://ibnux.github.io/phpmixbill/)
*
* Payment Gateway duitku.com
**/
function duitku_validate_config()
{
global $config;
if (empty($config['duitku_merchant_key'])) {
sendTelegram("Duitku payment gateway not configured");
r2(U . 'order/package', 'w', Lang::T("Admin has not yet setup Duitku payment gateway, please tell admin"));
}
}
function duitku_show_config()
{
global $ui, $config;
$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');
}
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/';
}
}

View File

@ -0,0 +1,172 @@
<?php
/**
* PHP Mikrotik Billing (https://ibnux.github.io/phpmixbill/)
*
* Payment Gateway tripay.com
**/
function duitku_validate_config()
{
global $config;
if (empty($config['tripay_secret_key'])) {
sendTelegram("Tripay payment gateway not configured");
r2(U . 'order/package', 'w', Lang::T("Admin has not yet setup Tripay payment gateway, please tell admin"));
}
}
function tripay_show_config()
{
global $ui, $config;
$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');
}
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/';
}
}

View File

@ -0,0 +1,140 @@
<?php
/**
* PHP Mikrotik Billing (https://ibnux.github.io/phpmixbill/)
*
* Payment Gateway xendit.com
**/
function xendit_show_config()
{
global $ui, $config;
$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');
}
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/';
}
}