Jump to content

Need suggestion


Recommended Posts

Hello there,  
             How do i send a webhook request containing the location of the client when one of my client places a new order on WHMCS? I'm a newbie, i'll appreciate if you can give me step-by-step procedure on how it can be done.

Thanks,
Sai.

Link to comment
Share on other sites

Thank you @steven99 You are awesome! 

I created the webhook successfully, but it's generating some errors, can you please help me troubleshoot? 

 
Here's the folder containing webhook code and error_log: https://drive.google.com/open?id=102EegJQN97fTEU29rRdKIZWv6K_wMeIY
 
Also, please let me know how can i execute that webhook automatically as soon as a new order is created. Currently, the hook is only executed when i goto the file url, instead do i need to configure anywhere in my whmcs admin area? 
Link to comment
Share on other sites

Hooks can't be run directly because they do not have the needed files or variables.  Toss that file in to the includes/hooks directory of the WHMCS install directory.  After that when a client registered / new client orders, it will fire that hook. 

Link to comment
Share on other sites

I'm receiving this error log:

[23-May-2019 11:05:30 UTC] PHP Parse error:  syntax error, unexpected ''country'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in /home/myssdpage/public_html/includes/hooks/letconverthook.php on line 17
[23-May-2019 11:06:09 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function add_hook() in /home/myssdpage/public_html/includes/hooks/letconverthook.php:3
Stack trace:
#0 {main}
  thrown in /home/myssdpage/public_html/includes/hooks/letconverthook.php on line 3
[23-May-2019 11:11:07 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function add_hook() in /home/myssdpage/public_html/includes/hooks/letconverthook.php:3
Stack trace:
#0 {main}
  thrown in /home/myssdpage/public_html/includes/hooks/letconverthook.php on line 3
[23-May-2019 11:12:07 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function add_hook() in /home/myssdpage/public_html/includes/hooks/letconverthook.php:3
Stack trace:
#0 {main}
  thrown in /home/myssdpage/public_html/includes/hooks/letconverthook.php on line 3

when i excecute this code: 
 

Quote

<?php
add_hook('ClientAdd', 1, function($vars) {
    $country = $vars['country'];
    $firstname = $vars['firstname'];
    $email = $vars['email'];
//API Url
$url = 'https://app.letconvert.com/webhooks/5ce678145a914fd1108b456d';
 
//Initiate cURL.
$ch = curl_init($url);
 
//The JSON data.
$jsonData = array(
    'email' => '$email',
    'name' => '$firstname',
    'country' => '$country'
);
 
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
 
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
 
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
 
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
 
//Execute the request
$result = curl_exec($ch);
});

 

Link to comment
Share on other sites

Try replacing:

//The JSON data.
$jsonData = array(
    'email' => '$email',
    'name' => '$firstname',
    'country' => '$country'
);

With:

//The JSON data.
$jsonData = array(
    'email' => $vars['country'],
    'name' => $vars['firstname'],
    'country' => $vars['email']
);

If you quote PHP strings they will act as plaintext.

Edited by Kian
Link to comment
Share on other sites

@Kian thank you so much for your help! 

It worked great 🙂

One more question: If i want the hook to be executed using 'Order Paid' but i want to pass the parameters of 'ClientAdd', how can it be achieved? is it using nested function? is there a concept like that? 

 

I tried this code but it didn't work:


<?php
add_hook('OrderPaid', 1, function($vars) {
add_hook('ClientAdd', 1, function($vars) {
    $country = $vars['country'];
    $firstname = $vars['firstname'];
    $email = $vars['email'];
//API Url
$url = 'https://app.letconvert.com/webhooks/5ce678145a914fd1108b456d';
 
//Initiate cURL.
$ch = curl_init($url);
 
//The JSON data.
$jsonData = array(
    'email' => $vars['country'],
    'name' => $vars['firstname'],
    'country' => $vars['email']
);
 
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
 
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
 
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
 
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
 
//Execute the request
$result = curl_exec($ch);
});
});

 

 

Link to comment
Share on other sites

WHMCS has hundreds of hook points that can be used to fire your scripts. Here you can find the complete list of such hooks including OrderPaid that probably is the one that you are looking for. Keep in mind that each hook has its own set of parameters. They're all listed in documentation. I'm telling you this because ClientAdd and OrderPaid have different parameters. More in particular you need the following ones:

  • Email
  • Firstname
  • Country

These parameters exist in ClientAdd hook point but not in OrderPaid. You'll need to retreive them in a different way. As you can read from documentation, OrderPaid gives you userId parameter that can be used to query tblclients table in database and retreive firstname, email and country. Something like follows (untested):

<?php

use WHMCS\Database\Capsule;

add_hook('OrderPaid', 1, function($vars)
{
    $ClientData = Capsule::table('tblclients')->where('id', $vars['userId'])->first(['firstname', 'email', 'country']);

    $url = 'https://app.letconvert.com/webhooks/5ce678145a914fd1108b456d';

    //Initiate cURL.
    $ch = curl_init($url);

    //The JSON data.
    $jsonData = array(
        'email' => $ClientData->email,
        'name' => $ClientData->firstname,
        'country' => $ClientData->country
    );

    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);
    
    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);
    
    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    
    //Execute the request
    $result = curl_exec($ch);
});

 

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • 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