Integrating SMS api with woocommerce , Not sending messages

I’m integrating SMS API with WooCommerce to send automatic order updates to customers mobiles whenever the make any purchase on site.

below is my code for the same

Read More
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($billing_phone)
{
$username = "my username";
$hash = "d761fbd7bd31c5eeec2a5b2556d6b9d3b1a1ae51";
//Multiple mobiles numbers separated by comma
$mobileNumber = "$billing_phone";


$senderId = "ORNGMT";
$message = urlencode("Dear Customer");
$postData = array(
    'hash' => $hash,
    'mobiles' => $$billing_phone,
    'message' => $message,
    'sender' => $senderId,

);
$url='http://api.textlocal.in/send/?';

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData
    //,CURLOPT_FOLLOWLOCATION => true
));

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$output = curl_exec($ch);

if(curl_errno($ch))
{
    echo 'error:' . curl_error($ch);
}

curl_close($ch);

echo $output;
}

is it correct ?
I’ve added this code to functions.php page

my sms gateway provider has sent me below example code to send sms with PHP

<?php
// Authorisation details.
$username = "your login id";
$hash = "your hash key";

// Configuration variables. Consult http://api.textlocal.in/docs for more info.
$test = "0";

// Data for text message. This is the text message data.
$sender = "API Test"; // This is who the message appears to be from.
$numbers = "44777000000"; // A single number or a comma-seperated list of numbers
$message = "This is a test message from the PHP API script.";
// 612 chars or less
// A single number or a comma-seperated list of numbers
$message = urlencode($message);
$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
$ch = curl_init('http://api.textlocal.in/send/?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // This is the result from the API
curl_close($ch);

?>

Related posts

5 comments

  1. Tested and working
    Custom api Integration with Woocommerce wordpress

    Ufone pakistan sms integration with woocommerce wordpress

    if you are looking for integration with ufone pakistan sms api bsms ufone pakistan service provider with woocommerce wordpress then use the following code in your functions file

    sms api integration ufone bsms with wordpress woocommerce thanks to the author on this page Integrating custom SMS API with woocommerce

    //add this line for calling your function on creation of order in woocommerce 
    add_action('woocommerce_order_status_processing', 'custom_func', 10, 3);
    
    function custom_func ($order_id) {
    
    $order_details = new WC_Order($order_id);
    
    //fetch all required fields
    $billing_phone = $order_details->get_billing_phone();
    $billing_name = $order_details->get_billing_first_name();
    $billing_last_name = $order_details->get_billing_last_name();
    $total_bill = $order_details->get_total();
    
    $textmessage = rawurlencode("Dear $billing_name, Thank you for your order. Your order #$order_id is being processed. Please wait for confirmation call Total Bill = $total_bill");
    
    // Now put HTTP ufone BSMS API URL
    $url = "https://bsms.ufone.com/bsms_v8_api/sendapi-0.3.jsp?id=msisdn&message=$textmessage&shortcode=SHORTCODE&lang=English&mobilenum=$billing_phone&password=password&messagetype=Nontransactional";
    
    // NOW WILL CALL FUNCTION CURL  
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);
    
    return $order_id;
    }
    
  2. $billingphone in your function is ID. Try this below code. It works

    add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
    function custom_process_order($order_id) {
    //Lets get data about the order made
    $order = new WC_Order( $order_id );
    
    //Now will fetch customer/buyer id here
    $customer_id = $order->user_id;
    
    //now finally we fetch phone number 
    $billing_phone = get_user_meta( $customer_id, 'billing_phone', true );
    
    // Now put your HTTP SMS API URL . I PUT WHICH WE ARE USING
    $jsonurl = "http://tsms.thirdeyegoa.com/api/sendmsg.php?user=USERNAME&pass=PASSWORD&sender=MYSENDERID&phone=".$billing_phone."&priority=ndnd&stype=normal&text=MY MESSAGE TO CUSTOMER.";
    
    // NOW WILL CALL FUNCTION CURL  
    $json = curl($jsonurl);
    
    return $order_id;
    }
    

    Same you can do for Order Completed too with respective hook.

  3. i write this piece of code and it is working.

    add_action('woocommerce_order_status_completed','payment_complete');
      function payment_complete($order_id)
    {
    
    $order=new Wc_order($order_id);
    $order_meta = get_post_meta($order_id);
    $shipping_first_name = $order_meta['_shipping_first_name'][0];
    $phone=$order_meta['_billing_phone'][0];
    $first_name=$order->billing_first_name;
    $last_name=$order->billing_last_name;
    $name=$first_name."".$last_name;
    $mobile=$order->billing_mobile;
    $items = $order->get_items();
    foreach ( $items as $item ) {
    $product_name = $item['name'];
    $product_id = $item['product_id'];
    $product_variation_id = $item['variation_id'];
    $time=$item['timings'];
    $slot=$item['time_slot'];
    $qty= $item['qty'];
    $date=$item['tour_date'];
    }
    $text="your message here";
    $data="user=username&pass=password&sender=sendername&phone=".$phone."&priority=ndnd&stype=normal&text=".$text;
    $ch = curl_init('http://bhashsms.com/api/sendmsg.php?');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
     echo $result; 
    curl_close($ch);
    }
    
  4. I am from Textlocal. Let me help you out with the code.

    Firstly the error that you are getting – ‘Unexpected Token’ is generally due to an invalid JSON response. If you are unaware of the source of the issue, I would suggest you to look in the Javascript console in the Dev section (triggering the error). For reference, you can check Mike’s blog out here.

    In the code you have shared, Textlocal API is not receiving the parameters that are mandatory for a successful POST call. You can rephrase your function like this:

    add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
    function custom_process_order($order_id) {
    
    $order = new WC_Order( $order_id );
    $customer_id = $order->user_id; 
    $billing_phone = get_user_meta( $customer_id, 'billing_phone', true );
    
    $data="username=USERNAME&hash=hash&sender=ORNGMT&numbers=".$billing_phone."&message=MY MESSAGE TO CUSTOMER.";
    $jsonurl = curl_init('https://api.textlocal.in/send?');
    
    $json = curl($jsonurl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($json);
    echo $result; 
    curl_close($json);
    
    return $order_id;
    }
    

    If you need any further assistance, please reach out to our support team (support@textlocal.in)

Comments are closed.