Hooks

Some useful hooks

How To Use

Copy the hook module you want to use and add it to the file V2RaySocks/hooks.php. Some modules may need simple settings, please set it as required

Automatically Accept Order

Let whmcs automatically review the purchase of new products that have been paid!

Before using, please modify the administrator username in the second line!

Work on WHMCS 7.x and 8.X

add_hook('AfterCronJob', 1, function($vars) {
    $adminUser = 'adminname';//Administrator username
    $command = 'GetOrders';
    $postData = [
        'limitnum' => 10000,
        'status' => 'Pending'
    ];
    $results = localAPI($command, $postData, $adminUser);
    if (isset($results['orders']['order'])) {
        foreach ($results['orders']['order'] as $order) {
            if ($order['paymentstatus'] === 'Paid' || $order['amount'] === '0.00') {
                $command = 'AcceptOrder';
                $postData = [
                    'orderid' => $order['id']
                ];
                $results = localAPI($command, $postData, $adminUser);
                if ($results['result'] !== 'success') {
                    $postData = array(
                        'description' => 'Accept order failed #' . $order['id'],
                    );
                    $command = 'LogActivity';
                    localAPI($command, $postData, $adminUser);
                }
            }
        }
    }
});

Bill payment control and guidance

Guide customers to correctly renew fees and control customer misoperations to generate abandoned bills

Work on WHMCS 7.x and 8.X

add_hook('ShoppingCartViewCartOutput', 1, function ($vars) {
  $unpaidinvoices = new WHMCS_ClientArea();
  if ($unpaidinvoices->isLoggedIn()) {
    $active_product_no = Capsule::table('tblhosting')->where('userid', $unpaidinvoices->getUserID())->where('domainstatus', 'Active')->count();
    $unpaid_invoice_no = Capsule::table('tblinvoices')->where('userid', $unpaidinvoices->getUserID())->where('status', 'Unpaid')->count();
    if ($active_product_no > 0 && $unpaid_invoice_no > 0) {
      echo '<script>if(confirm("You have ' . $active_product_no . ' effective products and ' . $unpaid_invoice_no . ' pending bills!\nBefore purchasing a new product, you must pay these bills first!\nPlease click confirm to pay the bill!")){location.href=\'clientarea.php?action=invoices\';}else{location.href=\'index.php\';};</script>';
    } elseif ($active_product_no > 0 && $unpaid_invoice_no == 0) {
      $return = '<div class="alert alert-warning text-center" role="alert">You have ' . $active_product_no . ' effective products, If you want to renew an existing product, please cancel this purchase, and the system will generate an order and notify you before the existing product expires!</br>If you want to purchase the product again (that is, you own two or more products at the same time), please complete this purchase!</div>';
      return $return;
    } elseif ($active_product_no = 0 && $unpaid_invoice_no > 0) {
      echo '<script>if(confirm("You have ' . $unpaid_invoice_no . ' pending bills!\nPlease click Confirm to end this purchase and check & pay the corresponding bill to complete the purchase!\nIf you want to purchase other products (products not corresponding to the generated bill), please click Cancel!")){location.href=\'clientarea.php?action=invoices\';};</script>';
    }
  }
});

Automatically cancel overdue orders and bills

Let WHMCS automatically cancel overdue orders and bills

Before using, please set the administrator username in the code and the time of automatic cancellation

Not tested yet

add_hook('DailyCronJob', 1, function ($vars) {
    $adminUser = 'adminname';//Administrator username
    $orderOverdueDays = 1;//Allow order overdue time(day)
    $invoiceOverdueDays = 10;//Allow bill overdue time(day)

    $now = Carbon::now();
    $now->hour = 0;
    $now->minute = 0;
    $now->second = 0;

    //Cancel order start
    $command = 'GetOrders';
    $postData = [
        'limitnum' => 10000,
        'status' => 'Pending'
    ];
    $results = localAPI($command, $postData, $adminUser);
    if (isset($results['orders']['order'])) {
        foreach ($results['orders']['order'] as $order) {
            $createDate = Carbon::parse($order['date']);
            $createDate->hour = 0;
            $createDate->minute = 0;
            $createDate->second = 0;
            if ($now->diffInDays($createDate) >= $orderOverdueDays) {
                $command = 'CancelOrder';
                $postData = [
                    'orderid' => $order['id']
                ];
                $results = localAPI($command, $postData, $adminUser);
                if ($results['result'] !== 'success') {
                    $postData = array(
                        'description' => 'Cancel order failed #' . $order['id'],
                    );
                    $command = 'LogActivity';
                    localAPI($command, $postData, $adminUser);
                }
            }
        }
    }
    //Cancel order end


    //Cancel invoices start
    $command = 'GetInvoices';
    $postData = [
        'limitnum' => 10000,
        'status' => 'Overdue'
    ];
    $results = localAPI($command, $postData, $adminUser);
    if (isset($results['invoices']['invoice'])) {
        foreach ($results['invoices']['invoice'] as $invoice) {
            $dueDate = Carbon::parse($invoice['duedate']);
            $dueDate->hour = 0;
            $dueDate->minute = 0;
            $dueDate->second = 0;
            if ($now->diffInDays($dueDate) >= $invoiceOverdueDays) {
                $command = 'UpdateInvoice';
                $postData = [
                    'invoiceid' => $invoice['id'],
                    'status' => 'Cancelled'
                ];
                $results = localAPI($command, $postData, $adminUser);
                if ($results['result'] === 'success') {
                    $postData = array(
                        'description' => 'Cancel invoice succeed #' . $invoice['id'],
                    );
                } else {
                    $postData = array(
                        'description' => 'Cancel invoice failed #' . $invoice['id'] . ';' . $results['message'],
                    );
                }
                $command = 'LogActivity';
                localAPI($command, $postData, $adminUser);
            }
        }
    }
    //Cancel invoices end
});

Mandatory account email authentication

Force users to verify their email address, otherwise they will refuse to access the product details page

Work on WHMCS 7.x and 8.X

add_hook('ClientAreaPageProductDetails', 1, function($var)
{
	$email_ca = new WHMCS_ClientArea();
	if ($email_ca->isLoggedIn()) {
		$adminuser = "NutterChen";
		$result = Capsule::table('tblclients')->where('id', '=', $email_ca->getUserID())->get()[0]->email_verified;
		if ($result == "0") { 
			echo '<script>if(confirm("You have not verified your email, access is denied!\nPlease click confirm to back to homepage and verify email!\nPlease click cancel to modify account information!")){location.href=\'clientarea.php\';}else{location.href=\'clientarea.php?action=details\';};</script>';
			exit();
		}
	}
});

Restrict email suffix

Restrict users' registered email suffixes so that users can only use specific email addresses to register

Please set the available email suffix before use, use commas to separate the email suffix (in the third line of the code)

Work on WHMCS 7.x and 8.X

add_hook('ClientDetailsValidation', 1, function($vars) {
	$EmailAddress = explode('@',trim($vars['email']));
	$AllowAddress = 'baidu.com,qq.com,ter.com';
	$AllowAddressArray = array();
	$AllowAddressArray = explode(',',$AllowAddress);
	if(!@$EmailAddress[1]){
			return array('Incorrect email');
	} else {
			foreach ($AllowAddressArray as $AllowAddressForeach){
				if(strpos($EmailAddress[1],$AllowAddressForeach) !== true){
					return array('Email suffix is not allowed');
				}
			}
	}
});

Prohibit immediate cancellation

Prohibit immediate cancellation through hidden options

add_hook('ClientAreaFooterOutput', 1, function($vars) {
return <<<HTML
<script>$("option[value='Immediate']").remove(); </script>
HTML;
});

Last updated