Viewing file: SmppBuilder.php (5.06 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\SmsProvider;
use App\Events\ProcessSendSuccessEvent; use App\Models\DynamicGateway; use App\Models\SmsQueue; use smpp\{Address, Client as SmppClient,Smpp as SMPP, transport\Socket}; use Illuminate\Support\Facades\Log;
class SmppBuilder {
private $messages; private $send_fail=[]; private $message_log; private $queue; private $sms_type; //sms or mms /** * @var mixed */ private $gateway;
public function setType($type){ $this->sms_type=$type; return $this; } public function setMessages($messages){ $this->messages=$messages; return $this; } public function setGateway(int $gatewayId){ if($gatewayId){ $this->gateway=DynamicGateway::find($gatewayId); } return $this; }
public function sendMessage() {
$gateway=$this->gateway; if(!$gateway){ throw new \Exception('gateway not found for smpp'); } $parameters = []; if ($gateway->inputs) { foreach (json_decode($gateway->inputs) as $key => $gt) { $parameters[$key] = $gt; } }
if(!isset($parameters['smpp_ip_address']) || !isset($parameters['smpp_username']) || !isset($parameters['smpp_password']) || !isset($parameters['smpp_port'])){ throw new \Exception('Provider Not Configured'); }
$transport = new Socket(array($parameters['smpp_ip_address']),(int) $parameters['smpp_port']); $transport->setRecvTimeout(10000); $transport->setSendTimeout(10000); $transport->debug = true; $transport->open();
SmppClient::$smsNullTerminateOctetstrings = false; Socket::$forceIpv4 = true; SmppClient::$csmsMethod = SmppClient::CSMS_8BIT_UDH; SmppClient::$smsRegisteredDeliveryFlag = SMPP::REG_DELIVERY_SMSC_BOTH; SmppClient::$smsNullTerminateOctetstrings = false;
$smpp =new SmppClient($transport); $smpp->bindTransmitter($parameters['smpp_username'], $parameters['smpp_password']);
$smsQueues=collect([]); foreach($this->messages as $queue){ $from=$queue->from; $to=str_replace("+","",$queue->to); $body=$queue->body; if (ctype_digit($from) && strlen($from) <= 8) { $from = new Address($from, SMPP::TON_NETWORKSPECIFIC); } elseif (ctype_digit($from) && (strlen($from) <= 15 && strlen($from) >= 10)) { $from = new Address($from, SMPP::TON_INTERNATIONAL, SMPP::NPI_E164); } else { $from = new Address($from, SMPP::TON_ALPHANUMERIC); } $to = new Address($to, SMPP::TON_INTERNATIONAL, SMPP::NPI_E164); $tags = null; try{ $output = $smpp->sendSMS($from, $to, $body, $tags); if($output){ $smsQueues->push([ 'id'=>$queue->id, 'sms_gateway_message_id'=>$output, 'delivered_at'=>now() ]); }else{ $this->setSendFail([ 'queue_id' => $queue->id, 'customer_id' => $queue->customer_id, 'to' => $queue->to, 'from' => $queue->from, 'sms_count'=>$this->countTotalSms($body), 'reason' => "empty response id", ]); } }catch(\Exception $ex){ $this->setSendFail([ 'queue_id' => $queue->id, 'customer_id' => $queue->customer_id, 'to' => $queue->to, 'from' => $queue->from, 'sms_count'=>$this->countTotalSms($body), 'reason' => $ex->getMessage(), ]); } } // Close connection $smpp->close(); if($smsQueues->isNotEmpty()){ foreach($smsQueues->chunk(100) as $smsQueueChunks){ ProcessSendSuccessEvent::dispatch($smsQueueChunks); } } }
public function get_failed_sms() { return $this->send_fail; }
private function setSendFail($sendFailArray) { $this->send_fail[] = $sendFailArray; }
private function countTotalSms($requestCharacters){ $totalCount=1; $characters=mb_strlen($requestCharacters, "UTF-8"); if (strlen($requestCharacters) != strlen(utf8_decode($requestCharacters))) { if($characters && $characters > 70){ $grandTotal=ceil($characters / 70); if($grandTotal > 1) $totalCount= $grandTotal - 1; } }else { if($characters && $characters > 160){ $grandTotal=ceil($characters / 160); if($grandTotal > 1) $totalCount= $grandTotal; } } return $totalCount; } }
|