phpnuxbill/system/controllers/order.php

325 lines
15 KiB
PHP
Raw Normal View History

2022-09-07 15:44:04 +08:00
<?php
2022-09-08 18:09:21 +08:00
2022-09-07 15:44:04 +08:00
/**
2023-10-12 16:55:42 +08:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2022-09-08 18:09:21 +08:00
**/
2023-10-12 16:55:42 +08:00
2022-09-07 15:44:04 +08:00
_auth();
$action = $routes['1'];
$user = User::_info();
$ui->assign('_user', $user);
switch ($action) {
case 'voucher':
2022-10-13 16:19:51 +08:00
$ui->assign('_system_menu', 'voucher');
2022-10-13 15:00:54 +08:00
$ui->assign('_title', $_L['Order_Voucher']);
2022-09-18 01:00:40 +08:00
run_hook('customer_view_order'); #HOOK
2022-09-07 15:44:04 +08:00
$ui->display('user-order.tpl');
break;
2022-09-11 12:02:55 +08:00
case 'history':
2022-10-13 16:19:51 +08:00
$ui->assign('_system_menu', 'history');
2023-08-23 16:00:34 +08:00
$paginator = Paginator::bootstrap('tbl_payment_gateway', 'username', $user['username']);
2022-09-11 12:02:55 +08:00
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
2022-12-14 16:08:42 +08:00
->order_by_desc('id')
2023-08-23 16:00:34 +08:00
->offset($paginator['startpoint'])->limit($paginator['limit'])
2022-09-11 12:02:55 +08:00
->find_many();
2022-09-16 12:05:33 +08:00
$ui->assign('paginator', $paginator);
2022-09-11 12:02:55 +08:00
$ui->assign('d', $d);
2022-10-13 15:00:54 +08:00
$ui->assign('_title', Lang::T('Order History'));
2022-09-18 01:00:40 +08:00
run_hook('customer_view_order_history'); #HOOK
2022-09-11 12:02:55 +08:00
$ui->display('user-orderHistory.tpl');
break;
2022-09-10 13:17:38 +08:00
case 'package':
2023-06-15 17:46:36 +08:00
if (strpos($user['email'], '@') === false) {
2022-12-14 16:08:42 +08:00
r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address"));
}
2022-10-13 15:00:54 +08:00
$ui->assign('_title', 'Order Plan');
2022-10-13 16:19:51 +08:00
$ui->assign('_system_menu', 'package');
2023-10-04 18:29:11 +08:00
if (!empty($_SESSION['nux-router'])) {
if ($_SESSION['nux-router'] == 'radius') {
$radius_pppoe = ORM::for_table('tbl_plans')->where('enabled', '1')->where('is_radius', 1)->where('type', 'PPPOE')->find_many();
$radius_hotspot = ORM::for_table('tbl_plans')->where('enabled', '1')->where('is_radius', 1)->where('type', 'Hotspot')->find_many();
} else {
$routers = ORM::for_table('tbl_routers')->where('id', $_SESSION['nux-router'])->find_many();
2023-10-04 18:07:13 +08:00
$rs = [];
2023-10-04 18:29:11 +08:00
foreach ($routers as $r) {
2023-10-04 18:07:13 +08:00
$rs[] = $r['name'];
}
2023-10-04 18:29:11 +08:00
$plans_pppoe = ORM::for_table('tbl_plans')->where('enabled', '1')->where_in('routers', $rs)->where('is_radius', 0)->where('type', 'PPPOE')->find_many();
$plans_hotspot = ORM::for_table('tbl_plans')->where('enabled', '1')->where_in('routers', $rs)->where('is_radius', 0)->where('type', 'Hotspot')->find_many();
2023-09-21 14:45:28 +08:00
}
2023-10-04 18:29:11 +08:00
} else {
$radius_pppoe = ORM::for_table('tbl_plans')->where('enabled', '1')->where('is_radius', 1)->where('type', 'PPPOE')->find_many();
$radius_hotspot = ORM::for_table('tbl_plans')->where('enabled', '1')->where('is_radius', 1)->where('type', 'Hotspot')->find_many();
2023-10-04 18:07:13 +08:00
2023-09-21 14:45:28 +08:00
$routers = ORM::for_table('tbl_routers')->find_many();
2023-10-04 18:29:11 +08:00
$plans_pppoe = ORM::for_table('tbl_plans')->where('enabled', '1')->where('is_radius', 0)->where('type', 'PPPOE')->find_many();
$plans_hotspot = ORM::for_table('tbl_plans')->where('enabled', '1')->where('is_radius', 0)->where('type', 'Hotspot')->find_many();
2023-09-21 14:45:28 +08:00
}
2023-08-14 16:59:53 +08:00
$plans_balance = ORM::for_table('tbl_plans')->where('enabled', '1')->where('type', 'Balance')->find_many();
2022-09-08 18:09:21 +08:00
$ui->assign('routers', $routers);
2023-10-04 18:07:13 +08:00
$ui->assign('radius_pppoe', $radius_pppoe);
$ui->assign('radius_hotspot', $radius_hotspot);
2023-08-14 16:59:53 +08:00
$ui->assign('plans_pppoe', $plans_pppoe);
$ui->assign('plans_hotspot', $plans_hotspot);
$ui->assign('plans_balance', $plans_balance);
2022-09-18 01:00:40 +08:00
run_hook('customer_view_order_plan'); #HOOK
$ui->display('user-orderPlan.tpl');
2022-09-07 15:44:04 +08:00
break;
2022-09-10 13:17:38 +08:00
case 'unpaid':
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
2022-09-18 01:00:40 +08:00
run_hook('customer_find_unpaid'); #HOOK
2022-09-16 12:05:33 +08:00
if ($d) {
2022-09-10 13:17:38 +08:00
if (empty($d['pg_url_payment'])) {
2022-09-16 12:05:33 +08: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 13:17:38 +08:00
}
2022-09-16 12:05:33 +08:00
} else {
2022-09-10 13:17:38 +08:00
r2(U . "order/package/", 's', Lang::T("You have no unpaid transaction"));
}
2023-07-07 16:51:48 +08:00
break;
2022-09-08 18:09:21 +08:00
case 'view':
2023-08-23 17:46:05 +08:00
$trxid = $routes['2'];
2022-09-08 18:09:21 +08:00
$trx = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->find_one($trxid);
2022-09-18 01:00:40 +08:00
run_hook('customer_view_payment'); #HOOK
2022-09-16 12:05:33 +08:00
// jika tidak ditemukan, berarti punya orang lain
if (empty($trx)) {
r2(U . "order/package", 'w', Lang::T("Payment not found"));
}
2022-09-10 13:17:38 +08:00
// jika url kosong, balikin ke buy
if (empty($trx['pg_url_payment'])) {
2023-10-04 18:29:11 +08: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 13:17:38 +08:00
}
2022-09-08 18:09:21 +08:00
if ($routes['3'] == 'check') {
2022-09-16 12:05:33 +08:00
if (!file_exists('system/paymentgateway/' . $trx['gateway'] . '.php')) {
r2(U . 'order/view/' . $trxid, 'e', Lang::T("No Payment Gateway Available"));
2022-09-08 18:09:21 +08:00
}
2022-09-18 01:00:40 +08:00
run_hook('customer_check_payment_status'); #HOOK
2022-09-16 12:05:33 +08:00
include 'system/paymentgateway/' . $trx['gateway'] . '.php';
call_user_func($trx['gateway'] . '_validate_config');
2022-09-21 15:15:00 +08:00
call_user_func($config['payment_gateway'] . '_get_status', $trx, $user);
2022-09-08 18:09:21 +08:00
} else if ($routes['3'] == 'cancel') {
2022-09-18 01:00:40 +08:00
run_hook('customer_cancel_payment'); #HOOK
2022-09-10 13:17:38 +08:00
$trx->pg_paid_response = '{}';
2022-09-08 18:09:21 +08:00
$trx->status = 4;
2022-09-10 13:17:38 +08:00
$trx->paid_date = date('Y-m-d H:i:s');
2022-09-08 18:09:21 +08:00
$trx->save();
$trx = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->find_one($trxid);
2022-09-16 12:05:33 +08:00
if ('midtrans' == $trx['gateway']) {
2022-09-10 13:17:38 +08:00
//Hapus invoice link
}
2022-09-08 18:09:21 +08:00
}
if (empty($trx)) {
2023-09-13 16:38:56 +08:00
r2(U . "order/package", 'e', Lang::T("Transaction Not found"));
2022-09-08 18:09:21 +08:00
}
$router = ORM::for_table('tbl_routers')->find_one($trx['routers_id']);
$plan = ORM::for_table('tbl_plans')->find_one($trx['plan_id']);
$bandw = ORM::for_table('tbl_bandwidth')->find_one($plan['id_bw']);
$ui->assign('trx', $trx);
$ui->assign('router', $router);
$ui->assign('plan', $plan);
$ui->assign('bandw', $bandw);
2022-10-13 15:00:54 +08:00
$ui->assign('_title', 'TRX #' . $trxid);
2022-09-08 18:09:21 +08:00
$ui->display('user-orderView.tpl');
break;
2023-08-18 15:01:59 +08:00
case 'pay':
2023-09-13 16:38:56 +08:00
if ($_c['enable_balance'] != 'yes' && $config['allow_balance_transfer'] != 'yes') {
r2(U . "order/package", 'e', Lang::T("Balance not enabled"));
2023-08-18 15:01:59 +08:00
}
$plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']);
2023-10-04 18:29:11 +08:00
if (empty($plan)) {
2023-08-18 15:01:59 +08:00
r2(U . "order/package", 'e', Lang::T("Plan Not found"));
}
2023-10-04 18:29:11 +08:00
if ($routes['2'] == 'radius') {
$router_name = 'radius';
} else {
$router_name = $plan['routers'];
}
2023-08-18 15:01:59 +08:00
if ($plan && $plan['enabled'] && $user['balance'] >= $plan['price']) {
2023-10-04 18:29:11 +08:00
if (Package::rechargeUser($user['id'], $router_name, $plan['id'], 'Customer', 'Balance')) {
2023-08-18 15:01:59 +08:00
// if success, then get the balance
Balance::min($user['id'], $plan['price']);
r2(U . "home", 's', Lang::T("Success to buy package"));
} 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 .
"\nPrice: " . $p['price']);
}
2023-09-13 16:38:56 +08:00
} else {
2023-08-18 15:01:59 +08:00
echo "no renewall | plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
}
break;
2023-09-13 16:38:56 +08:00
case 'send':
if ($_c['enable_balance'] != 'yes') {
r2(U . "order/package", 'e', Lang::T("Balance not enabled"));
}
$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)) {
r2(U . "order/package", 'e', Lang::T("Plan Not found"));
}
2023-10-04 18:29:11 +08:00
if ($routes['2'] == 'radius') {
$router_name = 'radius';
} else {
$router_name = $plan['routers'];
}
2023-09-13 16:38:56 +08:00
if (isset($_POST['send']) && $_POST['send'] == 'plan') {
$target = ORM::for_table('tbl_customers')->where('username', _post('username'))->find_one();
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 18:03:27 +08:00
if ($active && $active['plan_id'] != $plan['id']) {
2023-10-04 18:29:11 +08:00
r2(U . "order/package", 'e', Lang::T("Target has active plan, different with current plant.") . " [ <b>$active[namebp]</b> ]");
2023-09-13 16:38:56 +08:00
}
2023-10-04 18:29:11 +08:00
if (Package::rechargeUser($target['id'], $router_name, $plan['id'], $user['fullname'], 'Balance')) {
2023-09-13 16:38:56 +08: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 18:29:11 +08:00
$d->routers = $router_name;
2023-09-13 16:38:56 +08: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->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 18:29:11 +08:00
$d->routers = $router_name;
2023-09-13 16:38:56 +08: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->status = 2;
$d->save();
r2(U . "order/view/$trx_id", 's', Lang::T("Success to send package"));
} else {
r2(U . "order/package", 'e', Lang::T("Failed to Send package"));
Message::sendTelegram("Send Package with Balance Failed\n\n#u$user[username] #send \n" . $plan['name_plan'] .
2023-10-04 18:29:11 +08:00
"\nRouter: " . $router_name .
2023-09-13 16:38:56 +08:00
"\nPrice: " . $plan['price']);
}
}
$ui->assign('username', $_GET['u']);
2023-10-04 18:29:11 +08:00
$ui->assign('router', $router_name);
2023-09-13 16:38:56 +08:00
$ui->assign('plan', $plan);
$ui->display('user-sendPlan.tpl');
break;
2022-09-10 13:17:38 +08:00
case 'buy':
2023-06-15 17:46:36 +08:00
if (strpos($user['email'], '@') === false) {
2022-12-14 16:08:42 +08:00
r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address"));
}
2022-09-21 15:15:00 +08:00
if ($config['payment_gateway'] == 'none') {
2022-09-10 17:01:51 +08:00
r2(U . 'home', 'e', Lang::T("No Payment Gateway Available"));
}
2022-09-21 15:15:00 +08:00
if (!file_exists('system/paymentgateway/' . $config['payment_gateway'] . '.php')) {
2022-09-16 12:05:33 +08:00
r2(U . 'home', 'e', Lang::T("No Payment Gateway Available"));
}
2022-09-18 01:00:40 +08:00
run_hook('customer_buy_plan'); #HOOK
2022-09-21 15:15:00 +08:00
include 'system/paymentgateway/' . $config['payment_gateway'] . '.php';
call_user_func($config['payment_gateway'] . '_validate_config');
2023-10-04 18:29:11 +08:00
if ($routes['2'] == 'radius') {
$router['id'] = 0;
$router['name'] = 'radius';
} else if ($routes['2'] > 0) {
2023-08-14 16:59:53 +08:00
$router = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routes['2']);
2023-09-13 16:38:56 +08:00
} else {
2023-08-14 16:59:53 +08:00
$router['id'] = 0;
$router['name'] = 'balance';
2022-09-08 18:09:21 +08:00
}
2023-08-14 16:59:53 +08:00
$plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']);
2023-08-16 14:41:34 +08:00
if (empty($router) || empty($plan)) {
r2(U . "order/package", 'e', Lang::T("Plan Not found"));
}
2022-09-09 17:46:39 +08:00
$d = ORM::for_table('tbl_payment_gateway')
->where('username', $user['username'])
->where('status', 1)
->find_one();
2022-09-16 12:05:33 +08:00
if ($d) {
2022-09-10 13:17:38 +08: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 12:05:33 +08:00
} else {
2022-09-21 15:15:00 +08:00
if ($config['payment_gateway'] == $d['gateway']) {
2022-09-10 13:17:38 +08:00
$id = $d['id'];
2022-09-16 12:05:33 +08:00
} else {
2022-09-10 13:17:38 +08:00
$d->status = 4;
$d->save();
}
2022-09-09 17:46:39 +08:00
}
}
2022-09-16 12:05:33 +08:00
if (empty($id)) {
2022-09-09 17:46:39 +08:00
$d = ORM::for_table('tbl_payment_gateway')->create();
$d->username = $user['username'];
2022-09-21 15:15:00 +08:00
$d->gateway = $config['payment_gateway'];
2022-09-09 17:46:39 +08:00
$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();
2022-09-16 12:05:33 +08:00
} else {
2022-09-14 17:54:29 +08:00
$d->username = $user['username'];
2022-09-21 15:15:00 +08:00
$d->gateway = $config['payment_gateway'];
2022-09-14 17:54:29 +08:00
$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();
2022-09-09 17:46:39 +08:00
}
2022-09-16 12:05:33 +08:00
if (!$id) {
r2(U . "order/package/" . $d['id'], 'e', Lang::T("Failed to create Transaction.."));
} else {
2022-09-21 15:15:00 +08:00
call_user_func($config['payment_gateway'] . '_create_transaction', $d, $user);
2022-09-08 18:09:21 +08:00
}
break;
2022-09-07 15:44:04 +08:00
default:
2023-09-13 16:38:56 +08:00
r2(U . "order/package/", 's', '');
2022-09-16 12:05:33 +08:00
}