From 466d6c124f6fbac876093f06bb4b31cc6a8d1545 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 12 Apr 2022 09:30:00 +0000 Subject: [PATCH] Prepend country code to mobile numbers for SMS messaging. #658 --- src/Service/RisingTideGateway.php | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index acd7d4d5..bcbc1073 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -32,6 +32,10 @@ class RisingTideGateway public function sendSMS($mobile_num, $mask, $message) { + // make sure number is acceptable to RT + // at this point, assume that mobile is numeric and valid mobile number + $clean_num = $this->cleanPhoneNumber($mobile_num); + $headers = [ 'Content-Type: application/vnd.net.wyrls.Document-v3+json' ]; @@ -39,7 +43,7 @@ class RisingTideGateway $sms = new SMSMessage(); $sms->setFrom($this->shortcode) ->setFromAlias($mask) - ->setTo($mobile_num) + ->setTo($clean_num) ->setMessage($message) ->setStatus('sent'); @@ -54,7 +58,7 @@ class RisingTideGateway 'id' => $sms->getID(), 'from' => $this->shortcode, 'from_alias' => $mask, - 'to' => $mobile_num, + 'to' => $clean_num, 'content_type' => 'text/plain', 'body' => $message, 'date' => $date_string, @@ -107,4 +111,32 @@ class RisingTideGateway return true; } + + protected function cleanPhoneNumber($mobile) + { + $num = trim($mobile); + + // at this point, assume that mobile is numeric and valid mobile number + + // check if number begins with 63 and length is 12 for the format 639XXXXXXXXX + if ((strlen($num) == 12) && (substr($num, 0, 2) == '63')) + return $num; + + if ($num[0] == '0') + { + // remove the 0, prepend 63 + $stripped_num = substr($num, 1); + $clean_num = '63' . $stripped_num; + return $clean_num; + } + + if ($num[0] == '9') + { + // prepend 63 + $clean_num = '63' . $num; + return $clean_num; + } + + return $num; + } }