<?php
namespace App\Modules\Notification\Channels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
/**
* SparrowSMS Channel — Nepal domestic SMS
*
* API: https://api.sparrowsms.com/v2/sms/
* Env: SPARROW_SMS_TOKEN, SPARROW_SMS_FROM
*
* Usage:
* SparrowSmsChannel::send('+9779XXXXXXXX', 'Your message here');
*/
class SparrowSmsChannel
{
private const API_URL = 'https://api.sparrowsms.com/v2/sms/';
public function send(string $to, string $message): bool
{
$token = config('notification.sparrow.token');
$from = config('notification.sparrow.from', 'STPStore');
if (! $token) {
Log::warning('SparrowSMS: token not configured');
return false;
}
// Normalise number — strip country code prefix if present, re-add +977
$to = $this->normaliseNepalNumber($to);
$response = Http::get(self::API_URL, [
'token' => $token,
'from' => $from,
'to' => $to,
'text' => $message,
]);
if (! $response->successful() || ($response->json('response_code') ?? 200) >= 300) {
Log::error('SparrowSMS send failed', [
'to' => $to,
'response' => $response->body(),
]);
return false;
}
Log::info('SparrowSMS sent', ['to' => $to]);
return true;
}
private function normaliseNepalNumber(string $phone): string
{
// Strip non-digits
$digits = preg_replace('/\D/', '', $phone);
// Remove leading 977 country code if present
if (str_starts_with($digits, '977') && strlen($digits) === 12) {
$digits = substr($digits, 3);
}
// Return with country code for international format
return '+977' . ltrim($digits, '0');
}
}