SmsService
in package
SMS notification service using Twilio.
Sends order-related text messages (ready for pickup, delay notices) to customers. All sends are logged to the sms_log table. Failures are logged but never block business operations.
Tags
Table of Contents
Properties
- $accountSid : string
- $authToken : string
- $fromNumber : string
Methods
- __construct() : mixed
- Initialize the SMS service with Twilio configuration.
- getLogForOrder() : array<string|int, mixed>
- Get SMS log entries for a specific order.
- isBusinessConfigured() : bool
- Check if a specific business has its own Twilio credentials configured.
- isConfigured() : bool
- Check whether Twilio credentials are configured.
- notifyOrderDelayed() : bool
- Send an "order delayed" SMS to the customer with a custom reason.
- notifyOrderReady() : bool
- Send an "order ready" SMS to the customer and log the result.
- send() : bool
- Send an SMS message via Twilio.
- log() : void
- Log an SMS send attempt to the sms_log table.
- normalizePhone() : string
- Normalize a phone number to E.164 format for US numbers.
Properties
$accountSid
private
string
$accountSid
Twilio Account SID
$authToken
private
string
$authToken
Twilio Auth Token
$fromNumber
private
string
$fromNumber
Twilio phone number (sender)
Methods
__construct()
Initialize the SMS service with Twilio configuration.
public
__construct([array<string|int, mixed>|null $business = null ]) : mixed
If a business array with Twilio credentials is provided, those are used. Otherwise falls back to platform-level environment variables.
Parameters
- $business : array<string|int, mixed>|null = null
-
Optional business record with twilio_* fields
getLogForOrder()
Get SMS log entries for a specific order.
public
getLogForOrder(int $orderId) : array<string|int, mixed>
Parameters
- $orderId : int
-
Order ID
Return values
array<string|int, mixed> —List of sms_log rows
isBusinessConfigured()
Check if a specific business has its own Twilio credentials configured.
public
static isBusinessConfigured(array<string|int, mixed> $business) : bool
Parameters
- $business : array<string|int, mixed>
-
Business record
Return values
bool —True if business has all three Twilio fields set
isConfigured()
Check whether Twilio credentials are configured.
public
isConfigured() : bool
Return values
bool —True if all three Twilio credentials are non-empty
notifyOrderDelayed()
Send an "order delayed" SMS to the customer with a custom reason.
public
notifyOrderDelayed(array<string|int, mixed> $order, array<string|int, mixed> $business, string $reason) : bool
Parameters
- $order : array<string|int, mixed>
-
Order record (must include id, order_number, customer_phone, business_id)
- $business : array<string|int, mixed>
-
Business record (must include id, name)
- $reason : string
-
The delay reason/message to include
Return values
bool —True if SMS was sent successfully
notifyOrderReady()
Send an "order ready" SMS to the customer and log the result.
public
notifyOrderReady(array<string|int, mixed> $order, array<string|int, mixed> $business) : bool
Parameters
- $order : array<string|int, mixed>
-
Order record (must include id, order_number, customer_phone, business_id)
- $business : array<string|int, mixed>
-
Business record (must include id, name)
Return values
bool —True if SMS was sent successfully
send()
Send an SMS message via Twilio.
public
send(string $to, string $message) : bool
Never throws — failures are logged and the method returns false.
Parameters
- $to : string
-
Recipient phone number (E.164 or domestic format)
- $message : string
-
SMS body text
Return values
bool —True if sent successfully, false otherwise
log()
Log an SMS send attempt to the sms_log table.
private
log(int $businessId, int $orderId, string $phone, string $messageType, string $status, string|null $twilioSid, string|null $errorMessage) : void
Parameters
- $businessId : int
-
Business ID
- $orderId : int
-
Order ID
- $phone : string
-
Recipient phone number
- $messageType : string
-
One of: ready, delayed, custom
- $status : string
-
One of: sent, failed
- $twilioSid : string|null
-
Twilio message SID (if sent)
- $errorMessage : string|null
-
Error message (if failed)
normalizePhone()
Normalize a phone number to E.164 format for US numbers.
private
normalizePhone(string $phone) : string
Parameters
- $phone : string
-
Raw phone number
Return values
string —Normalized phone number or empty string if invalid