Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 61
SmsHelper
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 61
 sendSms
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 61
<?php
namespace App\Helpers;
use App\Models\Auth\User;
class SmsHelper{
    public static function sendSms($user_id, $mobile, $message){
        
        $smsProvider = config('settings.sms_provider');
        
        if($smsProvider == 'gup_shup' && !empty(config('settings.gup_shup_username')) && !empty(config('settings.gup_shup_password')) ){
            $curl = curl_init();
            $post_fields = array();
            $post_fields["method"] = "sendMessage";
            $post_fields["send_to"] = $mobile;
            $post_fields["msg"] = $message;
            $post_fields["msg_type"] = "TEXT";
            $post_fields["userid"] = config('settings.gup_shup_username');
            //37
            //©Gupshup Technology India Pvt. Ltd. 2020GupShup Enterprise API Help Document
            $post_fields["password"] =  config('settings.gup_shup_password');
            $post_fields["auth_scheme"] = "PLAIN";
            $post_fields["format"] = "JSON";
            //return   CURL_HTTP_VERSION_1_1;
            //return CURLOPT_HTTP_VERSION;
            //return $post_fields;
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://enterprise.smsgupshup.com/GatewayAPI/rest",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => $post_fields
            ));
            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            if ($err) {
                return response()->json([
                    'status' =>422,
                    'error' => "cURL Error #:" . $err
                ]);
            } else {
                return response()->json([
                    'status' =>200,
                    'response' =>  $response
                ]);
            }
              // $request =""; //initialise the request variable
              // $param['userid'] = "2000168903";
              // $param['password'] = "DLS286vdt";
              // $param['method']= "sendMessage";
              // $param['v'] = "1.1";
              // $param['msg_type'] = "TEXT"; //Can be "FLASH”/"UNICODE_TEXT"/”BINARY”
              // $param['auth_scheme'] = "PLAIN";
              // $param['send_to'] = $mobile;
              // $param['msg'] =  'Hi, Thank you for calling Vayudoot helpdesk. Our team will call back as soon as our office opens tomorrow morning.';
              // //Have to URL encode the values
              // foreach($param as $key=>$val) {
              //   $request.= $key."=".urlencode($val);
              //   //we have to urlencode the values
              //   $request.= "&";
              //   //append the ampersand (&) sign after each parameter/value pair
              // }
              // //return $request;
              // $request = substr($request, 0, strlen($request)-1);
              // //remove final (&) sign from the request
              // $url ="http://enterprise.smsgupshup.com/GatewayAPI/rest?".$request;
              // $ch = curl_init($url);
              // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              // $result = curl_exec($ch);      curl_close($ch);
              // //$response = curl_exec($curl);
              // if (preg_match('/error/',$result))
              //         return response()->json([
               //               'status' =>422,
               //               'error' => "cURL Error #"
               //           ]);
              // else
              //     return response()->json([
               //           'status' =>200,
               //           'response' =>  $result
               //       ]);
           
        }else if($smsProvider== 'message_91' && !empty(config('settings.msg91_auth_key')) && !empty(config('settings.msg91_sender_id')) && !empty(config('settings.msg91_route')) ) {
            $authKey = config('settings.msg91_auth_key');
            //Multiple mobiles numbers separated by comma
            $mobileNumber = $to;
            //Sender ID,While using route4 sender id should be 6 characters long.
            $senderId = config('settings.msg91_sender_id');
            //Your message to send, Add URL encoding here.
            $message = $message;/*urlencode("Test message");*/
            //Define route 
            $route = config('settings.msg91_route');
            //Prepare you post parameters
            $postData = array(
                'authkey' => $authKey,
                'mobiles' => $mobileNumber,
                'message' => $message,
                'sender' => $senderId,
                'route' => $route
            );
            //API URL
            $url="http://api.msg91.com/api/sendhttp.php";
            // init the resource
            $ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => $postData
                //,CURLOPT_FOLLOWLOCATION => true
            ));
            //Ignore SSL certificate verification
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            //get response
            $output = curl_exec($ch);
            //Print error if any
            if(curl_errno($ch))
            {   
               return response()->json([
                    'status' =>422,
                    'error' => "some error occur"
                ]);
                /*echo 'error:' . curl_error($ch);*/
            }
            curl_close($ch);
            return response()->json([
                    'status' =>200,
                    'response' =>  $output
            ]);
        }else{
            return response()->json([
                    'status' =>422,
                    'error' => "please setup sms configuration"
                ]);
        }
    }
}