Sai Mohit Posted May 22, 2019 Share Posted May 22, 2019 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. 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted May 22, 2019 Share Posted May 22, 2019 The orderpaid hook could be used and then you just do your webhook action as needed. 0 Quote Link to comment Share on other sites More sharing options...
Sai Mohit Posted May 24, 2019 Author Share Posted May 24, 2019 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? 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted May 24, 2019 Share Posted May 24, 2019 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. 0 Quote Link to comment Share on other sites More sharing options...
Sai Mohit Posted May 29, 2019 Author Share Posted May 29, 2019 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); }); 0 Quote Link to comment Share on other sites More sharing options...
Sai Mohit Posted May 29, 2019 Author Share Posted May 29, 2019 Can you please help @steven99 and @wp4all on what went wrong with my hook code? 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 29, 2019 Share Posted May 29, 2019 (edited) 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 May 29, 2019 by Kian 0 Quote Link to comment Share on other sites More sharing options...
Sai Mohit Posted May 30, 2019 Author Share Posted May 30, 2019 @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); }); }); 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 30, 2019 Share Posted May 30, 2019 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); }); 0 Quote Link to comment Share on other sites More sharing options...
Sai Mohit Posted May 30, 2019 Author Share Posted May 30, 2019 @Kian you are truly fantastic. Thank you so much for your help! 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.