Advertisement
Guest User

sms function foodomaa

a guest
Dec 16th, 2021
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use App\SmsOtp;
  6. use App\User;
  7. use Exception;
  8. use Illuminate\Http\Request;
  9. use Ixudra\Curl\Facades\Curl;
  10. use JWTAuth;
  11. use JWTAuthException;
  12. use Session;
  13. use Twilio\Rest\Client;
  14.  
  15. class Sms
  16. {
  17.  
  18.     /**
  19.      * @param $actionType
  20.      * @param $phone
  21.      * @param $otp
  22.      * @param null $message
  23.      * @return mixed
  24.      */
  25.     public function processSmsAction($actionType, $phone, $otp = null, $message = null, $smsForDelivery = null)
  26.     {
  27.         // Selects Default Gateway
  28.         $gateway = config('setting.defaultSmsGateway');
  29.  
  30.         switch ($gateway) {
  31.  
  32.             case '1':
  33.                 try {
  34.                     $response = $this->msg91($actionType, $phone, $otp, $message, $smsForDelivery);
  35.                 } catch (Exception $e) {
  36.                     $response = [
  37.                         'success' => false,
  38.                         'type' => 'MSG91',
  39.                     ];
  40.                 }
  41.  
  42.                 break;
  43.  
  44.             case '2':
  45.                 try {
  46.                     $response = $this->twilio($actionType, $phone, $otp, $message);
  47.                 } catch (Exception $e) {
  48.                     $response = [
  49.                         'success' => false,
  50.                         'type' => 'TWILIO',
  51.                     ];
  52.                 }
  53.  
  54.                 break;
  55.         }
  56.         return $response;
  57.     }
  58.  
  59.     /**
  60.      * @param $actionType
  61.      * @param $phone
  62.      * @param $otp
  63.      * @param $message
  64.      * @return mixed
  65.      */
  66.     private function msg91($actionType, $phone, $otp, $message, $smsForDelivery = null)
  67.     {
  68.         $authkey = config('setting.msg91AuthKey');
  69.         $sender_id = config('setting.msg91SenderId');
  70.  
  71.         switch ($actionType) {
  72.  
  73.             case 'OTP':
  74.                 $otp = rand(111111, 999999);
  75.                 $message = config('setting.otpMessage') . ' ' . $otp;
  76.                 $this->saveOtp($phone, $otp);
  77.  
  78.                 $msg_dlt_template_id = config('setting.msg91OtpDltTemplateId');
  79.                 if ($msg_dlt_template_id == null) {
  80.                     $curlPost = "{ \"sender\": \"$sender_id\", \"route\": \"4\", \"sms\": [ { \"message\": \"$message\", \"to\": [ \"$phone\" ] } ] }";
  81.                
  82.  
  83.  
  84.                 $paramsArr = [];
  85.                 /* Required fields */
  86.                 $paramsArr['token'] = "put here your token";
  87.                 $paramsArr['sendto'] = "$phone";
  88.                 $paramsArr['body'] = "$message"; // also you can send long messages
  89.                 $paramsArr['device_id'] = "put here your device id";
  90.                 $paramsArr['sim'] = "0";  // 0 or 1. try first 0.
  91.  
  92.  
  93.                 } else {
  94.                     $curlPost = "{ \"DLT_TE_ID\": \"$msg_dlt_template_id\", \"sender\": \"$sender_id\", \"route\": \"4\", \"sms\": [ { \"message\": \"$message\", \"to\": [ \"$phone\" ] } ] }";
  95.                 }
  96.  
  97.                 break;
  98.  
  99.             case 'VERIFY':
  100.                 $response = $this->verifyOtp($phone, $otp);
  101.                 return $response;
  102.                 break;
  103.  
  104.             case 'OD_NOTIFY':
  105.  
  106.                 if ($smsForDelivery) {
  107.                     $msg_dlt_template_id = config('setting.msg91NewOrderDeliveryDltTemplateId');
  108.                 } else {
  109.                     $msg_dlt_template_id = config('setting.msg91NewOrderDltTemplateId');
  110.                 }
  111.  
  112.                 if ($msg_dlt_template_id == null) {
  113.                     $curlPost = "{ \"sender\": \"$sender_id\", \"route\": \"4\", \"sms\": [ { \"message\": \"$message\", \"to\": [ \"$phone\" ] } ] }";
  114.                 } else {
  115.                     $curlPost = "{ \"DLT_TE_ID\": \"$msg_dlt_template_id\", \"sender\": \"$sender_id\", \"route\": \"4\", \"sms\": [ { \"message\": \"$message\", \"to\": [ \"$phone\" ] } ] }";
  116.                 }
  117.  
  118.                 break;
  119.         }
  120.  
  121.         $phone = preg_replace('/[^0-9]/', '', $phone);
  122.         $curl = curl_init();
  123.  
  124.         curl_setopt_array($curl, array(
  125.             CURLOPT_URL => 'https://smsgateway24.com/getdata/addsms',
  126.             //CURLOPT_URL => 'https://api.msg91.com/api/v2/sendsms',
  127.             CURLOPT_POST => 1,
  128.             CURLOPT_POSTFIELDS => $paramsArr,
  129.             CURLOPT_RETURNTRANSFER => true,
  130.  
  131.         // curl_setopt_array($curl, array(
  132.         //     CURLOPT_URL => 'https://api.msg91.com/api/v2/sendsms',
  133.         //     CURLOPT_RETURNTRANSFER => true,
  134.         //     CURLOPT_ENCODING => '',
  135.         //     CURLOPT_MAXREDIRS => 10,
  136.         //     CURLOPT_TIMEOUT => 30,
  137.         //     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  138.         //     CURLOPT_CUSTOMREQUEST => 'POST',
  139.         //     CURLOPT_POSTFIELDS => $curlPost,
  140.         //     CURLOPT_SSL_VERIFYHOST => 0,
  141.         //     CURLOPT_SSL_VERIFYPEER => 0,
  142.         //     CURLOPT_HTTPHEADER => array(
  143.         //         "authkey: $authkey",
  144.         //         'content-type: application/json',
  145.         //     ),
  146.         ));
  147.  
  148.         $response = curl_exec($curl);
  149.         $err = curl_error($curl);
  150.         curl_close($curl);
  151.  
  152.         if ($err) {
  153.             return false;
  154.         } else {
  155.             return true;
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * @param $actionType
  161.      * @param $phone
  162.      * @param $otp
  163.      * @param $message
  164.      * @return mixed
  165.      */
  166.     private function twilio($actionType, $phone, $otp, $message)
  167.     {
  168.         $sid = config('setting.twilioSid');
  169.         $token = config('setting.twilioAccessToken');
  170.         $from = config('setting.twilioFromPhone');
  171.  
  172.         switch ($actionType) {
  173.  
  174.             case 'OTP':
  175.                 $otp = rand(111111, 999999);
  176.                 $message = config('setting.otpMessage') . ' ' . $otp;
  177.                 $this->saveOtp($phone, $otp);
  178.  
  179.                 break;
  180.  
  181.             case 'VERIFY':
  182.                 $response = $this->verifyOtp($phone, $otp);
  183.                 return $response;
  184.                 break;
  185.  
  186.             case 'OD_NOTIFY':
  187.                 // Do Nothing Just Send
  188.                 break;
  189.         }
  190.         // Send Api Request
  191.  
  192.         $twilio = new Client($sid, $token);
  193.  
  194.         try {
  195.             $twilio->messages->create(
  196.                 // Where to send a text message (your cell phone?)
  197.                 $phone,
  198.                 array(
  199.                     'From' => $from,
  200.                     'body' => $message,
  201.                 )
  202.             );
  203.             return true;
  204.         } catch (Exception $e) {
  205.             \Log::error($e->getMessage());
  206.             throw new Exception('Twilio Error');
  207.         } catch (\Twilio\Rest\RestException $e) {
  208.             \Log::error($e);
  209.             throw new Exception('Twilio Error');
  210.         } catch (\Twilio\Exceptions\RestException $e) {
  211.             \Log::error($e);
  212.             throw new Exception('Twilio Error');
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * @param $phone
  218.      * @param $otp
  219.      */
  220.     private function saveOtp($phone, $otp)
  221.     {
  222.  
  223.         $otpTable = SmsOtp::where('phone', $phone)->first();
  224.  
  225.         if ($otpTable) {
  226.             //phone exists, just update the otp
  227.             $otpTable->otp = $otp;
  228.             $otpTable->save();
  229.             # code...
  230.        } else {
  231.             //create new entry
  232.             $otpTable = new SmsOtp();
  233.             $otpTable->phone = $phone;
  234.             $otpTable->otp = $otp;
  235.             $otpTable->save();
  236.         }
  237.         // dd($phone);
  238.     }
  239.  
  240.     /**
  241.      * @param $phone
  242.      * @param $otp
  243.      */
  244.     private function verifyOtp($phone, $otp)
  245.     {
  246.         // dd($otp);
  247.         $otpTable = SmsOtp::where('phone', $phone)->first();
  248.  
  249.         if ($otpTable) {
  250.             if ($otpTable->otp == $otp) {
  251.  
  252.                 $response = [
  253.                     'valid_otp' => true,
  254.                 ];
  255.             } else {
  256.                 $response = [
  257.                     'valid_otp' => false,
  258.                 ];
  259.             }
  260.         } else {
  261.             $response = [
  262.                 'valid_otp' => false,
  263.             ];
  264.         }
  265.         return $response;
  266.     }
  267. }
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement