Prepend country code to mobile numbers for SMS messaging. #658

This commit is contained in:
Korina Cordero 2022-04-12 09:30:00 +00:00
parent ab2ca02227
commit 466d6c124f

View file

@ -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;
}
}