708 lines
30 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
2024-10-10 15:13:29 +01:00
$ui->display('customer/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');
$query = ORM::for_table('tbl_payment_gateway')->where('user_id', $user['id'])->order_by_desc('id');
2024-03-27 09:44:48 +07:00
$d = Paginator::findMany($query);
if (empty($order) || $order < 5) {
$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
2024-10-10 15:13:29 +01:00
$ui->display('customer/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) {
2025-01-31 16:22:58 +07:00
r2(getUrl('accounts/profile'), 'e', Lang::T("Please enter your email address"));
2024-02-26 14:38:04 +07:00
}
$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);
2024-10-10 15:13:29 +01:00
$ui->display('customer/orderBalance.tpl');
2024-02-26 14:38:04 +07:00
break;
case 'package':
if (strpos($user['email'], '@') === false) {
2025-01-31 16:22:58 +07:00
r2(getUrl('accounts/profile'), 'e', Lang::T("Please enter your email address"));
2024-02-26 14:38:04 +07:00
}
$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
2024-10-10 15:13:29 +01:00
$ui->display('customer/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'])) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/buy/') . $trx['routers_id'] . '/' . $trx['plan_id'], 'w', Lang::T("Checking payment"));
2022-09-16 11:05:33 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('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 {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package/'), 's', Lang::T("You have no unpaid transaction"));
2022-09-10 12:17:38 +07:00
}
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)) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'w', Lang::T("Payment not found"));
2022-09-16 11:05:33 +07:00
}
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') {
2025-01-31 16:22:58 +07:00
r2(getUrl('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')) {
2025-01-31 16:22:58 +07:00
r2(getUrl('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)) {
2025-01-31 16:22:58 +07:00
r2(getUrl('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);
2024-10-10 15:13:29 +01:00
$ui->display('customer/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') {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'e', Lang::T("Balance not enabled"));
2023-08-18 14:01:59 +07:00
}
if (!empty(App::getTokenValue($_GET['stoken']))) {
2025-01-31 16:22:58 +07:00
r2(getUrl('voucher/invoice/'));
die();
}
if ($user['status'] != 'Active') {
_alert(Lang::T('This account status') . ' : ' . Lang::T($user['status']), 'danger', "");
}
$plan = ORM::for_table('tbl_plans')->find_one($routes[3]);
if (!$plan) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'e', Lang::T("Plan Not found"));
2023-08-18 14:01:59 +07:00
}
if ($plan['is_radius'] == '1') {
2023-10-04 17:29:11 +07:00
$router_name = 'radius';
$router = 'radius';
2023-10-04 17:29:11 +07:00
} 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
$total_cost = $plan['price'] + $add_cost + $tax;
if ($plan && $plan['enabled'] && $user['balance'] >= $total_cost) {
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
Balance::min($user['id'], $total_cost);
App::setToken($_GET['stoken'], "success");
2025-01-31 16:22:58 +07:00
r2(getUrl('voucher/invoice/'), 's', Lang::T("Success to buy package"));
2023-08-18 14:01:59 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'e', Lang::T("Failed to buy package"));
2023-08-18 14:01:59 +07:00
Message::sendTelegram("Buy Package with Balance Failed\n\n#u$c[username] #buy \n" . $plan['name_plan'] .
"\nRouter: " . $router_name .
"\nPrice: " . $total_cost);
2023-08-18 14:01:59 +07:00
}
2023-09-13 15:38:56 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/gateway/$routes[2]/$routes[3]'), 'e', Lang::T("Insufficient balance"));
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') {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'e', Lang::T("Balance not enabled"));
2023-09-13 15:38:56 +07:00
}
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)) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'e', Lang::T("Plan Not found"));
2023-09-13 15:38:56 +07:00
}
2024-02-26 14:38:04 +07:00
if (!$plan['enabled']) {
2025-01-31 16:22:58 +07:00
r2(getUrl('home'), 'e', 'Plan is not exists');
}
2024-10-11 18:59:38 +07:00
if ($plan['is_radius'] == '1') {
2024-10-10 17:25:21 +07:00
$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) {
2025-01-31 16:22:58 +07:00
r2(getUrl('home'), 'd', Lang::T('Username not found'));
2023-09-13 15:38:56 +07:00
}
if ($user['balance'] < $plan['price']) {
2025-01-31 16:22:58 +07:00
r2(getUrl('home'), 'd', Lang::T('insufficient balance'));
2023-09-13 15:38:56 +07:00
}
if ($user['username'] == $target['username']) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/pay/$routes[2]/$routes[3]'), 's', '^_^ v');
2023-09-13 15:38:56 +07:00
}
$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']) {
2025-01-31 16:22:58 +07:00
r2(getUrl('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->user_id = $user['id'];
2023-09-13 15:38:56 +07:00
$d->gateway = $target['username'];
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
2024-10-10 17:25:21 +07:00
$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->user_id = $target['id'];
2023-09-13 15:38:56 +07:00
$d->gateway = $user['username'];
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
2024-10-10 17:25:21 +07:00
$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(getUrl("order/view/$trx_id"), 's', Lang::T("Success to send package"));
2023-09-13 15:38:56 +07:00
} 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;
}
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'e', Lang::T("Failed to Send package"));
2024-05-17 14:47:12 +01:00
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);
2024-10-10 15:13:29 +01:00
$ui->display('customer/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) {
2025-01-31 16:22:58 +07:00
r2(getUrl('accounts/profile'), 'e', Lang::T("Please enter your email address"));
2022-12-14 15:08:42 +07:00
}
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);
}
$add_inv = User::getAttribute("Invoice", $id_customer);
if (!empty($add_inv)) {
$plan['price'] = $add_inv;
}
if($config['enable_coupons']){
if (!isset($_SESSION['coupon_attempts'])) {
$_SESSION['coupon_attempts'] = 0;
$_SESSION['last_attempt_time'] = time();
}
if ($_SESSION['coupon_attempts'] >= 5) {
$timeout = 10 * 60; // 10 minutes in seconds
$time_diff = time() - $_SESSION['last_attempt_time'];
if ($time_diff >= $timeout) {
$_SESSION['coupon_attempts'] = 0;
$_SESSION['last_attempt_time'] = time();
} else {
$remaining_time = ceil(($timeout - $time_diff) / 60);
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("Too many invalid attempts. Please try again after $remaining_time minutes."));
}
}
if (_post('coupon')) {
if ($plan['routers'] === 'balance') {
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("Coupon not available for Balance"));
}
$coupon = ORM::for_table('tbl_coupons')->where('code', _post('coupon'))->find_one();
if (!$coupon) {
$_SESSION['coupon_attempts']++;
$_SESSION['last_attempt_time'] = time();
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("Coupon not found"));
}
if ($coupon['status'] != 'active') {
$_SESSION['coupon_attempts']++;
$_SESSION['last_attempt_time'] = time();
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("Coupon is not active"));
}
// Reset attempts after a successful coupon validation
$_SESSION['coupon_attempts'] = 0;
$_SESSION['last_attempt_time'] = time();
$today = date('Y-m-d');
if ($today < $coupon['start_date'] || $today > $coupon['end_date']) {
$_SESSION['coupon_attempts']++;
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("Coupon is not valid for today"));
}
if ($coupon['max_usage'] > 0 && $coupon['usage_count'] >= $coupon['max_usage']) {
$_SESSION['coupon_attempts']++;
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("Coupon usage limit reached"));
}
if ($plan['price'] < $coupon['min_order_amount']) {
$_SESSION['coupon_attempts']++;
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("The order amount does not meet the minimum requirement for this coupon"));
}
// Calculate discount value
$discount = 0;
switch ($coupon['type']) {
case 'percent':
$discount = ($coupon['value'] / 100) * $plan['price'];
if ($discount > $coupon['max_discount_amount']) {
$discount = $coupon['max_discount_amount'];
}
break;
case 'fixed':
$discount = $coupon['value'];
break;
}
// Ensure discount does not exceed the plan price
if ($discount >= $plan['price']) {
r2($_SERVER['HTTP_REFERER'], 'e', Lang::T("Discount value exceeds the plan price"));
}
$plan['price'] -= $discount;
$coupon->usage_count = $coupon['usage_count'] + 1;
$coupon->save();
$ui->assign('discount', $discount);
$ui->assign('notify', Lang::T("Coupon applied successfully. You saved " . Lang::moneyFormat($discount)));
$ui->assign('notify_t', 's');
}
}
$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"));
2025-01-31 16:22:58 +07:00
r2(getUrl('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);
}
if (_post('custom') == '1') {
if (_post('amount') > 0) {
$ui->assign('custom', '1');
$ui->assign('amount', _post('amount'));
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/balance'), 'e', Lang::T("Please enter amount"));
}
}
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);
2024-10-10 15:13:29 +01:00
$ui->display('customer/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"));
2025-01-31 16:22:58 +07:00
r2(getUrl('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');
$discount = _post('discount') ?: 0;
if ($gateway == 'balance') {
unset($_SESSION['gateway']);
2025-01-31 16:22:58 +07:00
r2(getUrl('order/pay/') . $routes[2] . '/' . $routes[3]);
}
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)) {
2025-01-31 16:22:58 +07:00
r2(getUrl('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-10-10 10:18:35 +07:00
switch (_post('custom')) {
case '1':
$amount = _post('amount');
$amount = (float) $amount;
if ($amount <= 0) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/gateway/') . $routes[2] . '/' . $routes[3], 'w', Lang::T("Please enter amount"));
}
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
if ($d) {
if ($d['pg_url_payment']) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/view/') . $d['id'], 'w', Lang::T("You already have unpaid transaction, cancel it or pay it."));
} else {
if ($gateway == $d['gateway']) {
$id = $d['id'];
} else {
$d->status = 4;
$d->save();
}
}
}
$d = ORM::for_table('tbl_payment_gateway')->create();
$d->username = $user['username'];
$d->user_id = $user['id'];
$d->gateway = $gateway;
$d->plan_id = 0;
$d->plan_name = 'Custom';
$d->routers_id = '0';
$d->routers = 'Custom Balance';
$d->price = $amount;
$d->created_date = date('Y-m-d H:i:s');
$d->status = 1;
$d->save();
$id = $d->id;
break;
default:
$plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']);
if ($plan['is_radius'] == '1') {
$router['id'] = 0;
$router['name'] = 'radius';
} else if ($routes['2'] > 0) {
$router = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routes['2']);
2022-09-16 11:05:33 +07:00
} else {
$router['id'] = 0;
$router['name'] = 'balance';
2022-09-10 12:17:38 +07:00
}
if (empty($router) || empty($plan)) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package'), 'e', Lang::T("Plan Not found"));
}
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
if ($d) {
if ($d['pg_url_payment']) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/view/') . $d['id'], 'w', Lang::T("You already have unpaid transaction, cancel it or pay it."));
} else {
if ($gateway == $d['gateway']) {
$id = $d['id'];
} else {
$d->status = 4;
$d->save();
}
}
}
$add_cost = 0;
$tax = 0;
if ($router['name'] != 'balance') {
list($bills, $add_cost) = User::getBills($id_customer);
}
// 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;
2024-03-18 23:12:23 +03:00
} else {
$tax_rate = $tax_rate_setting;
2024-03-18 23:12:23 +03:00
}
if ($tax_enable === 'yes') {
$tax = Package::tax($plan['price'], $tax_rate);
}
// Tax calculation stop
if (empty($id)) {
$d = ORM::for_table('tbl_payment_gateway')->create();
$d->username = $user['username'];
$d->user_id = $user['id'];
$d->gateway = $gateway;
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
$d->routers_id = $router['id'];
$d->routers = $router['name'];
if ($plan['validity_unit'] == 'Period') {
// Postpaid price from field
$add_inv = User::getAttribute("Invoice", $id_customer);
if (empty($add_inv) or $add_inv == 0) {
$d->price = $plan['price'] + $add_cost + $tax - $discount;
} else {
$d->price = $add_inv + $add_cost + $tax - $discount;
}
} else {
$d->price = $plan['price'] + $add_cost + $tax - $discount;
}
$d->created_date = date('Y-m-d H:i:s');
$d->status = 1;
$d->save();
$id = $d->id();
2024-03-18 23:12:23 +03:00
} else {
$d->username = $user['username'];
$d->user_id = $user['id'];
$d->gateway = $gateway;
$d->plan_id = $plan['id'];
$d->plan_name = $plan['name_plan'];
$d->routers_id = $router['id'];
$d->routers = $router['name'];
if ($plan['validity_unit'] == 'Period') {
// Postpaid price from field
$add_inv = User::getAttribute("Invoice", $id_customer);
if (empty($add_inv) or $add_inv == 0) {
$d->price = ($plan['price'] + $add_cost + $tax - $discount);
} else {
$d->price = ($add_inv + $add_cost + $tax - $discount);
}
} else {
$d->price = ($plan['price'] + $add_cost + $tax - $discount);
}
//$d->price = ($plan['price'] + $add_cost);
$d->created_date = date('Y-m-d H:i:s');
$d->status = 1;
$d->save();
2024-03-18 23:12:23 +03:00
}
break;
2022-09-09 16:46:39 +07:00
}
2022-09-16 11:05:33 +07:00
if (!$id) {
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package/') . $d['id'], 'e', Lang::T("Failed to create Transaction.."));
2022-09-16 11:05:33 +07:00
} 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:
2025-01-31 16:22:58 +07:00
r2(getUrl('order/package/'), 's', '');
2022-09-16 11:05:33 +07:00
}