Search code examples
phpapismsprestashopsms-gateway

How to integrate msg91 php api with Prestasms or Prestashop?


<?php
error_reporting(E_ALL ^ E_NOTICE);ini_set('error_reporting', E_ALL ^ E_NOTICE);
    
define('IS_ADMIN_FLAG', false);

include_once(dirname(__FILE__).'/../../config/config.inc.php');
include_once(dirname(__FILE__).'/../../config/setting.inc.php');

include_once('includes/model/smsAdapter.php');
include_once('includes/model/sms.php');
include_once('includes/model/variables.php');

class ControllerSmsApi
{
    public function __construct()
    {
        $this->index();
    }                  

    public function index()
    {
        die("DISABLED");
                         
        $to = $this->getVar("to");
        $text = $this->getVar("text");
        $unicode = $this->getVar("unicode");
        $type = $this->getVar("type");
        $transaction = $this->getVar("transaction");
        
        if(isset($to) && strlen($to) > 4 && strlen($text) > 0)
        {   
            $sms = new SmsModel(true, SmsModel::TYPE_SIMPLE, $type, ($transaction ? SmsModel::SMS_TRANSACTION : SmsModel::SMS_BULK));
            
            $sms->number($to)->text($text)->unicode($unicode)->send();

            if(!$sms->isError())
            {
                echo "SMSSTATUS:OK";
            }
            else
            {
                echo "SMSSTATUS:ERROR";
            }
        }
        else
        {
            echo "SMSSTATUS:ERROR";
        }
    }
    
    private function getVar($var)
    {
        if(filter_input(INPUT_POST, $var))
        {
            return filter_input(INPUT_POST, $var);
        }
        elseif(filter_input(INPUT_GET, $var))
        {
            return filter_input(INPUT_GET, $var);
        }
        else
        {
            return null;
        }
    }
}

new ControllerSmsApi();
  
?>

I have an ecommers website in which customer placed order and get all updation via email service ,but now i want to make it for sms service also for which i have sms api in msg91 for php. But unfortunately i am unable to integrate it with prestashop via prestasms or any other free module.


Solution

  • Actually making a Module should do the job and adding various hooks might do the job, you can generate one with nearly all you will need here : https://validator.prestashop.com/

    Based on your answer you will certainly need two hooks : actionOrderStatusUpdate and actionValidateOrder. You can also get an updated list here http://www.prestarocket.com/blog/prestashop-1-7-hook-list-liste-des-hooks/.

    If you need example of a module well working, you can take a look at modules/dashactivity/ which one of the most compliant to Prestashops guidelines.

    Your code might look like this in the end :

    <?php
    
    
    class Msg91SMS extends Module
    {
        public function __construct()
        {
            $this->name = 'msg91sms';
            $this->tab = 'front_office';
            $this->version = '1.0.1';
            $this->author = 'YourName';
    
            $this->displayName = $this->l('MSG91SMS');
            $this->description = $this->l('Description');
    
            // Hooks you need, setup on install so you might do it again
            $this->hooks = array(
                'actionValidateOrder',
                'actionOrderStatusUpdate',
            );
        }
    
        public function install() {
            if (!parent::install()) {
                return false;
            } else {
                if (isset($this->hooks) && !empty($this->hooks)) {
                    foreach ($this->hooks as $v) {
                        if (!$this->registerHook($v)) {
                            return false;
                        }
                    }
                }
            }
    
        }
    
        public function hookActionValidateOrder($params) {
           $order = $params['order'];
           // Do your magic here
        }
    
        public function hookActionOrderStatusUpdate($params) {
            // Same as above, remember to check order state to see if it interests you some ways with $order->id_state and a switch / case
        }
    
    }