550 lines
23 KiB
PHP
Raw Normal View History

2022-09-07 14:44:04 +07:00
<?php
2022-09-08 17:09:21 +07:00
2022-09-07 14:44:04 +07:00
/**
2023-10-12 15:55:42 +07:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2022-09-08 17:09:21 +07:00
**/
2023-10-12 15:55:42 +07:00
2022-09-07 14:44:04 +07:00
_auth();
$action = $routes['1'];
$user = User::_info();
$ui->assign('_user', $user);
switch ($action) {
case 'voucher':
2022-10-13 15:19:51 +07:00
$ui->assign('_system_menu', 'voucher');
2024-02-13 13:54:01 +07:00
$ui->assign('_title', Lang::T('Order Voucher'));
2022-09-18 00:00:40 +07:00
run_hook('customer_view_order'); #HOOK
$ui->display('user-ui/order.tpl');
2022-09-07 14:44:04 +07:00
break;
2022-09-11 11:02:55 +07:00
case 'history':
2022-10-13 15:19:51 +07:00
$ui->assign('_system_menu', 'history');
2024-03-27 09:44:48 +07:00
$query = ORM::for_table('tbl_payment_gateway')->where('username', $user['username'])->order_by_desc('id');
$d = Paginator::findMany($query);
2022-09-11 11:02:55 +07:00
$ui->assign('d', $d);
2022-10-13 14:00:54 +07:00
$ui->assign('_title', Lang::T('Order History'));
2022-09-18 00:00:40 +07:00
run_hook('customer_view_order_history'); #HOOK
$ui->display('user-ui/orderHistory.tpl');
2022-09-11 11:02:55 +07:00
break;
2024-02-26 14:38:04 +07:00
case 'balance':
if (strpos($user['email'], '@') === false) {
r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address"));
}
$ui->assign('_title', 'Top Up');
$ui->assign('_system_menu', 'balance');
2024-03-13 14:32:10 +07:00
$plans_balance = ORM::for_table('tbl_plans')->where('enabled', '1')->where('type', 'Balance')->where('prepaid', 'yes')->find_many();
2024-02-26 14:38:04 +07:00
$ui->assign('plans_balance', $plans_balance);
$ui->display('user-ui/orderBalance.tpl');
2024-02-26 14:38:04 +07:00
break;
case 'package':
if (strpos($user['email'], '@') === false) {
r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address"));
}
$ui->assign('_title', 'Order Plan');
$ui->assign('_system_menu', 'package');
2024-03-19 19:05:35 +07:00
$account_type = $user['account_type'];
if (empty($account_type)) {
2024-03-19 19:05:35 +07:00
$account_type = 'Personal';
}
if (!empty($_SESSION['nux-router'])) {
2024-02-26 14:38:04 +07:00
if ($_SESSION['nux-router'] == 'radius') {
2024-08-19 15:58:51 +07:00
$radius_pppoe = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')
->where('is_radius', 1)
->where('type', 'PPPOE')
->where('prepaid', 'yes')->find_many();
$radius_hotspot = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')
->where('is_radius', 1)
->where('type', 'Hotspot')
->where('prepaid', 'yes')->find_many();
2024-02-26 14:38:04 +07:00
} else {
$routers = ORM::for_table('tbl_routers')->where('id', $_SESSION['nux-router'])->find_many();
$rs = [];
foreach ($routers as $r) {
$rs[] = $r['name'];
}
2024-08-19 15:58:51 +07:00
$plans_pppoe = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')
->where_in('routers', $rs)
->where('is_radius', 0)
->where('type', 'PPPOE')
->where('prepaid', 'yes')
->find_many();
$plans_hotspot = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')
->where_in('routers', $rs)
->where('is_radius', 0)
->where('type', 'Hotspot')
->where('prepaid', 'yes')
->find_many();
2023-10-20 10:34:24 +07:00
}
2024-02-26 14:38:04 +07:00
} else {
2024-08-19 15:58:51 +07:00
$radius_pppoe = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')
->where('is_radius', 1)
->where('type', 'PPPOE')
->where('prepaid', 'yes')
->find_many();
$radius_hotspot = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')
->where('is_radius', 1)
->where('type', 'Hotspot')
->where('prepaid', 'yes')
->find_many();
2024-02-26 14:38:04 +07:00
$routers = ORM::for_table('tbl_routers')->find_many();
2024-08-19 15:58:51 +07:00
$plans_pppoe = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')
->where('is_radius', 0)
->where('type', 'PPPOE')
->where('prepaid', 'yes')
->find_many();
$plans_hotspot = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')->where('is_radius', 0)
->where('type', 'Hotspot')
->where('prepaid', 'yes')
->find_many();
2024-09-13 00:43:46 +07:00
$plans_vpn = ORM::for_table('tbl_plans')
->where('plan_type', $account_type)
->where('enabled', '1')->where('is_radius', 0)
->where('type', 'VPN')
->where('prepaid', 'yes')
->find_many();
2024-02-26 14:38:04 +07:00
}
$ui->assign('routers', $routers);
$ui->assign('radius_pppoe', $radius_pppoe);
$ui->assign('radius_hotspot', $radius_hotspot);
$ui->assign('plans_pppoe', $plans_pppoe);
$ui->assign('plans_hotspot', $plans_hotspot);
2024-09-13 00:43:46 +07:00
$ui->assign('plans_vpn', $plans_vpn);
2024-02-26 14:38:04 +07:00
run_hook('customer_view_order_plan'); #HOOK
$ui->display('user-ui/orderPlan.tpl');
2024-02-26 14:38:04 +07:00
break;
2022-09-10 12:17:38 +07:00
case 'unpaid':
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
2023-10-20 13:57:12 +07:00
run_hook('custome
r_find_unpaid'); #HOOK
2022-09-16 11:05:33 +07:00
if ($d) {
if (empty($d['pg_url_payment'])) {
2022-09-16 11:05:33 +07:00
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"));
2022-09-10 12:17:38 +07:00
}
2022-09-16 11:05:33 +07:00
} else {
2022-09-10 12:17:38 +07:00
r2(U . "order/package/", 's', Lang::T("You have no unpaid transaction"));
}
2023-07-07 15:51:48 +07:00
break;
2022-09-08 17:09:21 +07:00
case 'view':
2023-08-23 16:46:05 +07:00
$trxid = $routes['2'];
2022-09-08 17:09:21 +07:00
$trx = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->find_one($trxid);
2022-09-18 00:00:40 +07:00
run_hook('customer_view_payment'); #HOOK
2022-09-16 11:05:33 +07:00
// jika tidak ditemukan, berarti punya orang lain
if (empty($trx)) {
2022-09-16 11:05:33 +07:00
r2(U . "order/package", 'w', Lang::T("Payment not found"));
}
2024-03-25 10:27:06 +07:00
// jika url kosong, balikin ke buy, kecuali cancel
2024-08-02 16:03:20 +07:00
if ($trx['status'] == 1 && empty($trx['pg_url_payment']) && $routes['3'] != 'cancel') {
2023-10-04 17:29:11 +07:00
r2(U . "order/buy/" . (($trx['routers_id'] == 0) ? $trx['routers'] : $trx['routers_id']) . '/' . $trx['plan_id'], 'w', Lang::T("Checking payment"));
2022-09-10 12:17:38 +07:00
}
2022-09-08 17:09:21 +07:00
if ($routes['3'] == 'check') {
2024-02-26 14:38:04 +07:00
if (!file_exists($PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR . $trx['gateway'] . '.php')) {
2022-09-16 11:05:33 +07:00
r2(U . 'order/view/' . $trxid, 'e', Lang::T("No Payment Gateway Available"));
2022-09-08 17:09:21 +07:00
}
2022-09-18 00:00:40 +07:00
run_hook('customer_check_payment_status'); #HOOK
2024-02-26 14:38:04 +07:00
include $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR . $trx['gateway'] . '.php';
2022-09-16 11:05:33 +07:00
call_user_func($trx['gateway'] . '_validate_config');
2024-03-12 09:33:45 +07:00
call_user_func($trx['gateway'] . '_get_status', $trx, $user);
2022-09-08 17:09:21 +07:00
} else if ($routes['3'] == 'cancel') {
2022-09-18 00:00:40 +07:00
run_hook('customer_cancel_payment'); #HOOK
2022-09-10 12:17:38 +07:00
$trx->pg_paid_response = '{}';
2022-09-08 17:09:21 +07:00
$trx->status = 4;
2022-09-10 12:17:38 +07:00
$trx->paid_date = date('Y-m-d H:i:s');
2022-09-08 17:09:21 +07:00
$trx->save();
$trx = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->find_one($trxid);
}
if (empty($trx)) {
2023-09-13 15:38:56 +07:00
r2(U . "order/package", 'e', Lang::T("Transaction Not found"));
2022-09-08 17:09:21 +07:00
}
2024-04-05 09:34:29 +07:00
2024-06-07 17:21:29 +07:00
$router = ORM::for_table('tbl_routers')->where('name', $trx['routers'])->find_one();
2022-09-08 17:09:21 +07:00
$plan = ORM::for_table('tbl_plans')->find_one($trx['plan_id']);
$bandw = ORM::for_table('tbl_bandwidth')->find_one($plan['id_bw']);
2024-05-17 10:59:41 +01:00
$invoice = ORM::for_table('tbl_transactions')->where("invoice", $trx['trx_invoice'])->find_one();
2024-04-05 11:18:28 +07:00
$ui->assign('invoice', $invoice);
2022-09-08 17:09:21 +07:00
$ui->assign('trx', $trx);
$ui->assign('router', $router);
$ui->assign('plan', $plan);
$ui->assign('bandw', $bandw);
2022-10-13 14:00:54 +07:00
$ui->assign('_title', 'TRX #' . $trxid);
$ui->display('user-ui/orderView.tpl');
2022-09-08 17:09:21 +07:00
break;
2023-08-18 14:01:59 +07:00
case 'pay':
2024-02-29 09:25:26 +07:00
if ($config['enable_balance'] != 'yes') {
2023-09-13 15:38:56 +07:00
r2(U . "order/package", 'e', Lang::T("Balance not enabled"));
2023-08-18 14:01:59 +07:00
}
if (!empty(App::getTokenValue($_GET['stoken']))) {
r2(U . "voucher/invoice/");
die();
}
if ($user['status'] != 'Active') {
_alert(Lang::T('This account status') . ' : ' . Lang::T($user['status']), 'danger', "");
}
2023-08-18 14:01:59 +07:00
$plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']);
if (empty($plan)) {
2023-08-18 14:01:59 +07:00
r2(U . "order/package", 'e', Lang::T("Plan Not found"));
}
2024-02-26 14:38:04 +07:00
if (!$plan['enabled']) {
r2(U . "home", 'e', 'Plan is not exists');
}
if ($plan['is_radius'] == '1') {
2023-10-04 17:29:11 +07:00
$router_name = 'radius';
} else {
$router_name = $plan['routers'];
}
2024-05-17 14:47:12 +01:00
2024-03-15 10:38:05 +07:00
list($bills, $add_cost) = User::getBills($id_customer);
2024-05-17 14:47:12 +01:00
// Tax calculation start
$tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no';
$tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null;
$custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null;
if ($tax_rate_setting === 'custom') {
$tax_rate = $custom_tax_rate;
} else {
$tax_rate = $tax_rate_setting;
}
if ($tax_enable === 'yes') {
$tax = Package::tax($plan['price'], $tax_rate);
} else {
$tax = 0;
}
// Tax calculation stop
if ($plan && $plan['enabled'] && $user['balance'] >= $plan['price'] + $tax) {
2023-10-04 17:29:11 +07:00
if (Package::rechargeUser($user['id'], $router_name, $plan['id'], 'Customer', 'Balance')) {
2023-08-18 14:01:59 +07:00
// if success, then get the balance
2024-05-17 14:47:12 +01:00
Balance::min($user['id'], $plan['price'] + $add_cost + $tax);
App::setToken($_GET['stoken'], "success");
2024-03-14 14:42:20 +07:00
r2(U . "voucher/invoice/", 's', Lang::T("Success to buy package"));
2023-08-18 14:01:59 +07:00
} else {
r2(U . "order/package", 'e', Lang::T("Failed to buy package"));
Message::sendTelegram("Buy Package with Balance Failed\n\n#u$c[username] #buy \n" . $plan['name_plan'] .
"\nRouter: " . $router_name .
2024-05-17 14:47:12 +01:00
"\nPrice: " . $plan['price'] + $tax);
2023-08-18 14:01:59 +07:00
}
2023-09-13 15:38:56 +07:00
} else {
r2(U . "home", 'e', 'Plan is not exists');
2023-08-18 14:01:59 +07:00
}
break;
2024-05-17 14:47:12 +01:00
2023-09-13 15:38:56 +07:00
case 'send':
2024-02-29 09:25:26 +07:00
if ($config['enable_balance'] != 'yes') {
2023-09-13 15:38:56 +07:00
r2(U . "order/package", 'e', Lang::T("Balance not enabled"));
}
if ($user['status'] != 'Active') {
_alert(Lang::T('This account status') . ' : ' . Lang::T($user['status']), 'danger', "");
}
2023-09-13 15:38:56 +07:00
$ui->assign('_title', Lang::T('Buy for friend'));
$ui->assign('_system_menu', 'package');
$plan = ORM::for_table('tbl_plans')->find_one($routes['3']);
if (empty($plan)) {
2023-09-13 15:38:56 +07:00
r2(U . "order/package", 'e', Lang::T("Plan Not found"));
}
2024-02-26 14:38:04 +07:00
if (!$plan['enabled']) {
r2(U . "home", 'e', 'Plan is not exists');
}
if ($plan['is_radius'] == '1') {
$routes['2'] = '0';
2023-10-04 17:29:11 +07:00
$router_name = 'radius';
} else {
$router_name = $plan['routers'];
}
2024-05-17 14:47:12 +01:00
$tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null;
$custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null;
if ($tax_rate_setting === 'custom') {
$tax_rate = $custom_tax_rate;
} else {
$tax_rate = $tax_rate_setting;
}
$tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no';
if ($tax_enable === 'yes') {
$tax = Package::tax($plan['price'], $tax_rate);
$ui->assign('tax', $tax);
} else {
$tax = 0;
}
// Add tax to plan price
$plan['price'] += $tax;
if (isset($_POST['send']) && $_POST['send'] == 'plan') {
2023-09-13 15:38:56 +07:00
$target = ORM::for_table('tbl_customers')->where('username', _post('username'))->find_one();
2024-03-15 10:38:05 +07:00
list($bills, $add_cost) = User::getBills($target['id']);
if (!empty($add_cost)) {
2024-03-15 10:38:05 +07:00
$ui->assign('bills', $bills);
$ui->assign('add_cost', $add_cost);
$plan['price'] += $add_cost;
}
2024-05-17 14:47:12 +01:00
2023-09-13 15:38:56 +07:00
if (!$target) {
r2(U . 'home', 'd', Lang::T('Username not found'));
}
if ($user['balance'] < $plan['price']) {
r2(U . 'home', 'd', Lang::T('insufficient balance'));
}
if ($user['username'] == $target['username']) {
r2(U . "order/pay/$routes[2]/$routes[3]", 's', '^_^ v');
}
$active = ORM::for_table('tbl_user_recharges')
->where('username', _post('username'))
->where('status', 'on')
->find_one();
2023-09-14 17:03:27 +07:00
if ($active && $active['plan_id'] != $plan['id']) {
2023-10-04 17:29:11 +07:00
r2(U . "order/package", 'e', Lang::T("Target has active plan, different with current plant.") . " [ <b>$active[namebp]</b> ]");
2023-09-13 15:38:56 +07:00
}
$result = Package::rechargeUser($target['id'], $router_name, $plan['id'], $user['username'], 'Balance');
if (!empty($result)) {
2023-09-13 15:38:56 +07:00
// if success, then get the balance
Balance::min($user['id'], $plan['price']);
//sender
$d = ORM::for_table('tbl_payment_gateway')->create();
$d->username = $user['username'];
$d->gateway = $target['username'];
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
$d->routers_id = $routes['2'];
2023-10-04 17:29:11 +07:00
$d->routers = $router_name;
2023-09-13 15:38:56 +07:00
$d->price = $plan['price'];
$d->payment_method = "Balance";
$d->payment_channel = "Send Plan";
$d->created_date = date('Y-m-d H:i:s');
$d->paid_date = date('Y-m-d H:i:s');
$d->expired_date = date('Y-m-d H:i:s');
$d->pg_url_payment = 'balance';
$d->trx_invoice = $result;
2023-09-13 15:38:56 +07:00
$d->status = 2;
$d->save();
$trx_id = $d->id();
//receiver
$d = ORM::for_table('tbl_payment_gateway')->create();
$d->username = $target['username'];
$d->gateway = $user['username'];
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
$d->routers_id = $routes['2'];
2023-10-04 17:29:11 +07:00
$d->routers = $router_name;
2023-09-13 15:38:56 +07:00
$d->price = $plan['price'];
$d->payment_method = "Balance";
$d->payment_channel = "Received Plan";
$d->created_date = date('Y-m-d H:i:s');
$d->paid_date = date('Y-m-d H:i:s');
$d->expired_date = date('Y-m-d H:i:s');
$d->pg_url_payment = 'balance';
$d->trx_invoice = $result;
2023-09-13 15:38:56 +07:00
$d->status = 2;
$d->save();
r2(U . "order/view/$trx_id", 's', Lang::T("Success to send package"));
} else {
2024-05-17 14:47:12 +01:00
$errorMessage = "Send Package with Balance Failed\n\n#u$user[username] #send \n" . $plan['name_plan'] .
2023-10-04 17:29:11 +07:00
"\nRouter: " . $router_name .
2024-05-17 14:47:12 +01:00
"\nPrice: " . $plan['price'];
if ($tax_enable === 'yes') {
$errorMessage .= "\nTax: " . $tax;
}
r2(U . "order/package", 'e', Lang::T("Failed to Send package"));
Message::sendTelegram($errorMessage);
2023-09-13 15:38:56 +07:00
}
}
$ui->assign('username', $_GET['u']);
2023-10-04 17:29:11 +07:00
$ui->assign('router', $router_name);
2023-09-13 15:38:56 +07:00
$ui->assign('plan', $plan);
2024-05-17 14:47:12 +01:00
$ui->assign('tax', $tax);
$ui->display('user-ui/sendPlan.tpl');
2023-09-13 15:38:56 +07:00
break;
2024-03-12 09:33:45 +07:00
case 'gateway':
$ui->assign('_title', Lang::T('Select Payment Gateway'));
$ui->assign('_system_menu', 'package');
2023-06-15 16:46:36 +07:00
if (strpos($user['email'], '@') === false) {
2022-12-14 15:08:42 +07:00
r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address"));
}
2024-05-17 10:59:41 +01:00
$tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no';
$tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null;
$custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null;
if ($tax_rate_setting === 'custom') {
$tax_rate = $custom_tax_rate;
} else {
$tax_rate = $tax_rate_setting;
}
$plan = ORM::for_table('tbl_plans')->find_one($routes['3']);
$add_cost = 0;
if ($router['name'] != 'balance') {
list($bills, $add_cost) = User::getBills($id_customer);
}
$tax = Package::tax($plan['price'] + $add_cost, $tax_rate);
2024-03-12 11:17:05 +07:00
$pgs = array_values(explode(',', $config['payment_gateway']));
2024-03-14 14:42:20 +07:00
if (count($pgs) == 0) {
2024-03-12 11:17:05 +07:00
sendTelegram("Payment Gateway not set, please set it in Settings");
_log(Lang::T("Payment Gateway not set, please set it in Settings"));
r2(U . "home", 'e', Lang::T("Failed to create Transaction.."));
}
2024-08-19 16:13:51 +07:00
if (count($pgs) > 0) {
2024-03-14 14:42:20 +07:00
$ui->assign('pgs', $pgs);
2024-05-17 10:59:41 +01:00
if ($tax_enable === 'yes') {
$ui->assign('tax', $tax);
}
2024-03-12 11:17:05 +07:00
$ui->assign('route2', $routes[2]);
$ui->assign('route3', $routes[3]);
$ui->assign('add_cost', $add_cost);
$ui->assign('bills', $bills);
2024-05-17 10:59:41 +01:00
$ui->assign('plan', $plan);
$ui->display('user-ui/selectGateway.tpl');
2024-03-12 11:17:05 +07:00
break;
2024-03-14 14:42:20 +07:00
} else {
2024-08-19 16:13:51 +07:00
sendTelegram("Payment Gateway not set, please set it in Settings");
_log(Lang::T("Payment Gateway not set, please set it in Settings"));
r2(U . "home", 'e', Lang::T("Failed to create Transaction.."));
2024-03-12 11:17:05 +07:00
}
2024-03-12 09:33:45 +07:00
case 'buy':
$gateway = _post('gateway');
if (empty($gateway) && !empty($_SESSION['gateway'])) {
2024-03-12 09:33:45 +07:00
$gateway = $_SESSION['gateway'];
} else if (!empty($gateway)) {
2024-03-12 09:33:45 +07:00
$_SESSION['gateway'] = $gateway;
}
if ($user['status'] != 'Active') {
_alert(Lang::T('This account status') . ' : ' . Lang::T($user['status']), 'danger', "");
}
if (empty($gateway)) {
2024-03-12 09:33:45 +07:00
r2(U . 'order/gateway/' . $routes[2] . '/' . $routes[3], 'w', Lang::T("Please select Payment Gateway"));
}
2022-09-18 00:00:40 +07:00
run_hook('customer_buy_plan'); #HOOK
include $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR . $gateway . '.php';
call_user_func($gateway . '_validate_config');
2024-08-26 20:09:45 +07:00
$plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']);
if ($plan['is_radius'] == '1') {
2023-10-04 17:29:11 +07:00
$router['id'] = 0;
$router['name'] = 'radius';
} else if ($routes['2'] > 0) {
2023-08-14 15:59:53 +07:00
$router = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routes['2']);
2023-09-13 15:38:56 +07:00
} else {
2023-08-14 15:59:53 +07:00
$router['id'] = 0;
$router['name'] = 'balance';
2022-09-08 17:09:21 +07:00
}
if (empty($router) || empty($plan)) {
2023-08-16 13:41:34 +07:00
r2(U . "order/package", 'e', Lang::T("Plan Not found"));
}
2022-09-09 16:46:39 +07:00
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
2022-09-16 11:05:33 +07:00
if ($d) {
2022-09-10 12:17:38 +07:00
if ($d['pg_url_payment']) {
r2(U . "order/view/" . $d['id'], 'w', Lang::T("You already have unpaid transaction, cancel it or pay it."));
2022-09-16 11:05:33 +07:00
} else {
2024-03-12 09:33:45 +07:00
if ($gateway == $d['gateway']) {
2022-09-10 12:17:38 +07:00
$id = $d['id'];
2022-09-16 11:05:33 +07:00
} else {
2022-09-10 12:17:38 +07:00
$d->status = 4;
$d->save();
}
2022-09-09 16:46:39 +07:00
}
}
2024-03-14 14:42:20 +07:00
$add_cost = 0;
2024-05-17 14:47:12 +01:00
$tax = 0;
2024-03-14 14:42:20 +07:00
if ($router['name'] != 'balance') {
2024-03-15 10:38:05 +07:00
list($bills, $add_cost) = User::getBills($id_customer);
2024-03-14 14:42:20 +07:00
}
2024-05-17 14:47:12 +01:00
// Tax calculation start
$tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no';
$tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null;
$custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null;
if ($tax_rate_setting === 'custom') {
$tax_rate = $custom_tax_rate;
} else {
$tax_rate = $tax_rate_setting;
}
if ($tax_enable === 'yes') {
$tax = Package::tax($plan['price'], $tax_rate);
}
// Tax calculation stop
if (empty($id)) {
2022-09-09 16:46:39 +07:00
$d = ORM::for_table('tbl_payment_gateway')->create();
$d->username = $user['username'];
2024-03-12 09:33:45 +07:00
$d->gateway = $gateway;
2022-09-09 16:46:39 +07:00
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
$d->routers_id = $router['id'];
$d->routers = $router['name'];
2024-03-18 23:12:23 +03:00
if ($plan['validity_unit'] == 'Period') {
// Postpaid price from field
$add_inv = User::getAttribute("Invoice", $id_customer);
if (empty($add_inv) or $add_inv == 0) {
2024-05-17 14:47:12 +01:00
$d->price = ($plan['price'] + $add_cost + $tax);
2024-03-18 23:12:23 +03:00
} else {
2024-05-17 14:47:12 +01:00
$d->price = ($add_inv + $add_cost + $tax);
2024-03-18 23:12:23 +03:00
}
} else {
2024-05-17 14:47:12 +01:00
$d->price = ($plan['price'] + $add_cost + $tax);
}
2022-09-09 16:46:39 +07:00
$d->created_date = date('Y-m-d H:i:s');
$d->status = 1;
$d->save();
$id = $d->id();
2022-09-16 11:05:33 +07:00
} else {
2022-09-14 16:54:29 +07:00
$d->username = $user['username'];
2024-03-12 09:33:45 +07:00
$d->gateway = $gateway;
2022-09-14 16:54:29 +07:00
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
$d->routers_id = $router['id'];
$d->routers = $router['name'];
2024-03-18 23:12:23 +03:00
if ($plan['validity_unit'] == 'Period') {
// Postpaid price from field
$add_inv = User::getAttribute("Invoice", $id_customer);
if (empty($add_inv) or $add_inv == 0) {
2024-05-17 14:47:12 +01:00
$d->price = ($plan['price'] + $add_cost + $tax);
2024-03-18 23:12:23 +03:00
} else {
2024-05-17 14:47:12 +01:00
$d->price = ($add_inv + $add_cost + $tax);
2024-03-18 23:12:23 +03:00
}
} else {
2024-05-17 14:47:12 +01:00
$d->price = ($plan['price'] + $add_cost + $tax);
}
//$d->price = ($plan['price'] + $add_cost);
2022-09-14 16:54:29 +07:00
$d->created_date = date('Y-m-d H:i:s');
$d->status = 1;
$d->save();
2022-09-09 16:46:39 +07:00
}
2022-09-16 11:05:33 +07:00
if (!$id) {
r2(U . "order/package/" . $d['id'], 'e', Lang::T("Failed to create Transaction.."));
} else {
2024-03-12 09:33:45 +07:00
call_user_func($gateway . '_create_transaction', $d, $user);
2022-09-08 17:09:21 +07:00
}
break;
2022-09-07 14:44:04 +07:00
default:
2023-09-13 15:38:56 +07:00
r2(U . "order/package/", 's', '');
2022-09-16 11:05:33 +07:00
}