Jump to content

Auto Order Accept (Hook)


Kyo

Recommended Posts

Hello,

I am using this hook to accept automatically order and active if payment is paid 

to aim to provide hassle-free service to customers who already paid their amount, why should they wait, but I am facing issue in domain registration I have enabled automatic registration but still domain is not getting registrar, what I am doing then, manually go to customer domain and then select registrar and then click on register module infect I have already setup which registrar will be for automatic registration for different TLDs in WHMCS 

 

here is hook code

<?php
/*
*
* Auto Accept Orders
* Created By Idan Ben-Ezra
*
* Copyrights @ Jetserver Web Hosting
* www.jetserver.net
*
* Hook version 1.0.1
*
**/
if (!defined("WHMCS"))
	die("This file cannot be accessed directly");

/*********************
 Auto Accept Orders Settings
*********************/
function AutoAcceptOrders_settings()
{
return array(
'apiuser'	=> 'XXXXXX', // one of the admins username
'autosetup' => true, // determines whether product provisioning is performed
'sendregistrar' => true, // determines whether domain automation is performed
'sendemail' => true, // sets if welcome emails for products and registration confirmation emails for domains should be sent
'ispaid'	=> true, // set to true if you want to accept only paid orders
);
}


function AutoAcceptOrders_accept($vars)
{
$settings = AutoAcceptOrders_settings();

$ispaid = true;

if($vars['InvoiceID'])
{
    $result = localAPI('GetInvoice', array(
        'invoiceid'	=> $vars['invoiceId'],
    ), $settings['apiuser']);

    $ispaid = ($result['result'] == 'success' && $result['balance'] <= 0) ? true : false;

}
logActivity("Order isPaid: ".$ispaid,0);

if(( $settings['ispaid'] && $ispaid))
{
    $result = localAPI('AcceptOrder', array(
        'orderid' => $vars['orderId'],
        'autosetup'	=> $settings['autosetup'],
        'sendemail' => $settings['sendemail'],
    ), $settings['apiuser']);
    logActivity("Order Accept", 0);
    if(is_array($result)){
        foreach($result as $index=>$value){
            logActivity("$index:$value",0);
        }
    }

}
}
add_hook('OrderPaid', 1, 'AutoAcceptOrders_accept');

please guide me how can i achieve it i will be thankful to you guys

Link to comment
Share on other sites

Try this. Make a test order and check the Activity logs for errors.

<?php
/*
*
* Auto Accept Orders
* Created By Idan Ben-Ezra
*
* Copyrights @ Jetserver Web Hosting
* www.jetserver.net
*
* Hook version 1.0.1
*
**/
if (!defined("WHMCS")) die("This file cannot be accessed directly");

add_hook('OrderPaid', 1, function ($vars) {
    try {
        $settings = AutoAcceptOrders_settings();
        $ispaid = false;
        // Check if invoice
        if ($vars['invoiceId']) {
            $results = localAPI('GetInvoice', ['invoiceid' => $vars['invoiceId']]);
            if ($results['result'] != 'success') {
                throw new Exception("Unable to get Invoice #{$vars['invoiceId']}, {$results['message']}");
            } else {
                $ispaid = $results['status'] == 'Paid' && $results['balance'] <= 0;
            }
        }
        // Check if invoice id paid
        if ($ispaid) {
            logActivity("Order #{$vars['orderId']} is paid");
            $results = localAPI('AcceptOrder', [
                'orderid' => $vars['orderId'],
                'autosetup' => $settings['autosetup'],
                'sendemail' => $settings['sendemail'],
              	'sendregistrar' => $settings['sendregistrar']
              //'registrar' => $settings['registrar'] [optional]
            ]);
            // If success, log activity.
            if ($results['result'] != 'success') {
                throw new Exception("Unable to accept Order #{$vars['orderId']},{$results['message']}");
            } else {
                logActivity("Order #{$vars['orderId']} accepted");
            }
        }
    } catch (Exception $e) {
        logActivity("[Auto Accept Orders] {$e->getMessage()}");
    }
});

/*******************************
 * Auto Accept Orders Settings *
 ******************************/
function AutoAcceptOrders_settings(): array
{
    return [
        //'apiuser' => 'XXXXXX', // one of the admins username
        'autosetup' => true, // determines whether product provisioning is performed
      	//'registrar' =>'enom', // determines which registrar will get the registration request [optional]
        'sendregistrar' => true, // determines whether domain automation is performed
        'sendemail' => true, // sets if welcome emails for products and registration confirmation emails for domains should be sent
        'ispaid' => true, // set to true if you want to accept only paid orders
    ];
}

Also, if you have one registrar for your domains, you can set it on  your settings and pass it on AcceptOrder

Edited by pRieStaKos
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated