PHP code example of sureshhemal / laravel-sms-sri-lanka

1. Go to this page and download the library: Download sureshhemal/laravel-sms-sri-lanka library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sureshhemal / laravel-sms-sri-lanka example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Sureshhemal\SmsSriLanka\Contracts\ReceivesSmsNotifications;

class User extends Authenticatable implements ReceivesSmsNotifications
{
    // ... your model code ...

    /**
     * Route notifications for the SMS channel.
     * This method should return the phone number for SMS delivery.
     */
    public function routeNotificationForSms(): ?string
    {
        return $this->phone_number; // or $this->telephone_number, etc.
    }
}

use Sureshhemal\SmsSriLanka\Notifications\SmsNotification;

// Send to single recipient
$user->notify(new SmsNotification('Your message here'));

// Send to multiple recipients
Notification::send($users, new SmsNotification('Your message here'));

// With custom options
$notification = new SmsNotification('Your message', [
    'delivery_report_request' => true,
    'campaign_name' => 'Custom Campaign'
]);
$user->notify($notification);



namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Sureshhemal\SmsSriLanka\Contracts\SendsSmsNotification;

class WelcomeSmsNotification extends Notification implements SendsSmsNotification, ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return ['sms'];
    }

    /**
     * Get the SMS representation of the notification.
     */
    public function toSms($notifiable): string
    {
        return "Welcome {$notifiable->name}! Your account has been created successfully.";
    }

    /**
     * Get the SMS options for the notification.
     */
    public function smsOptions(): array
    {
        return [
            'delivery_report_request' => true,
            'campaign_name' => 'Welcome Campaign',
        ];
    }
}

use Sureshhemal\SmsSriLanka\Contracts\SmsServiceContract;

// Inject the service
public function sendWelcomeSms(SmsServiceContract $smsService)
{
    // Send to single recipient
    $response = $smsService->sendSms(
        to: '94701234567',
        message: 'Welcome to our service!',
        options: [
            'campaign_name' => 'Welcome Campaign',
            'delivery_report_request' => true,
        ]
    );

    // Send to multiple recipients (comma-separated phone numbers)
    $response = $smsService->sendSms(
        to: '94701234567,94712345678,94723456789',
        message: 'Bulk message to multiple recipients!',
        options: [
            'campaign_name' => 'Bulk Campaign',
            'delivery_report_request' => true,
        ]
    );

    return $response;
}

use Sureshhemal\SmsSriLanka\Exceptions\SmsConfigurationException;
use Sureshhemal\SmsSriLanka\Exceptions\SmsSendException;
use Sureshhemal\SmsSriLanka\Exceptions\SmsProviderException;

try {
    $smsService->sendSms('94701234567', 'Test message');
} catch (SmsConfigurationException $e) {
    // Handle configuration errors (missing API keys, etc.)
    Log::error('SMS Configuration Error: ' . $e->getMessage());
} catch (SmsSendException $e) {
    // Handle sending errors (authentication, API errors, etc.)
    Log::error('SMS Send Error: ' . $e->getMessage());
} catch (SmsProviderException $e) {
    // Handle provider-related errors
    Log::error('SMS Provider Error: ' . $e->getMessage());
}

interface ReceivesSmsNotifications
{
    /**
     * Route notifications for the SMS channel.
     * Return the phone number for SMS delivery.
     */
    public function routeNotificationForSms(): ?string;
}

interface SendsSmsNotification
{
    /**
     * Get the SMS representation of the notification.
     */
    public function toSms($notifiable): string;

    /**
     * Get the SMS options for the notification.
     */
    public function smsOptions(): array;
}

interface SmsServiceContract
{
    /**
     * Send an SMS message
     * 
     * @param string $to Phone number(s) - single number or comma-separated list
     * @param string $message The SMS message content
     * @param array $options Additional options for the SMS
     * @return array The response from the SMS API
     */
    public function sendSms(string $to, string $message, array $options = []): array;
}

return [
    'default' => env('SMS_PROVIDER', 'hutch'),
    
    'providers' => [
        'hutch' => [
            'service' => \Sureshhemal\SmsSriLanka\Providers\Hutch\HutchSmsService::class,
            'authenticator' => \Sureshhemal\SmsSriLanka\Providers\Hutch\HutchSmsAuthenticator::class,
            'config' => [
                'base_url' => env('HUTCH_SMS_BASE_URL', 'https://bsms.hutch.lk/api'),
                'username' => env('HUTCH_SMS_USERNAME'),
                'password' => env('HUTCH_SMS_PASSWORD'),
                'default_mask' => env('HUTCH_SMS_DEFAULT_MASK'),
            ],
            'default_options' => [
                'delivery_report_request' => env('HUTCH_SMS_DELIVERY_REPORT_REQUEST', false),
                'campaign_name' => env('HUTCH_SMS_DEFAULT_CAMPAIGN_NAME', 'Laravel SMS'),
            ],
        ],
        // More providers coming soon...
    ],
];

   // src/Providers/NewProvider/
   - NewProviderSmsService.php
   - NewProviderSmsAuthenticator.php
   - NewProviderConfigurationValidator.php
   - NewProviderPayloadBuilder.php
   - NewProviderHttpClient.php
   

   'providers' => [
       'new-provider' => [
           'service' => \Sureshhemal\SmsSriLanka\Providers\NewProvider\NewProviderSmsService::class,
           'authenticator' => \Sureshhemal\SmsSriLanka\Providers\NewProvider\NewProviderSmsAuthenticator::class,
           // ... configuration
       ],
   ]
   

#[Test]
public function validatesSuccessfullyWithValidConfiguration()
{
    // Test implementation
}
bash
php artisan vendor:publish --provider="Sureshhemal\SmsSriLanka\Providers\SmsServiceProvider"

src/
├── Contracts/                    # Interface definitions
│   ├── SmsServiceContract.php
│   ├── SmsAuthenticatorContract.php
│   ├── SmsConfigurationValidator.php
│   ├── SmsPayloadBuilder.php
│   ├── SmsHttpClient.php
│   ├── ReceivesSmsNotifications.php
│   └── SendsSmsNotification.php
├── Exceptions/                   # Custom exception classes
│   ├── SmsException.php
│   ├── SmsConfigurationException.php
│   ├── SmsSendException.php
│   └── SmsProviderException.php
├── Notifications/                # Laravel notification integration
│   ├── SmsNotification.php
│   └── Channels/SmsChannel.php
└── Providers/                    # Provider implementations
    ├── SmsServiceProvider.php
    └── Hutch/
        ├── HutchSmsService.php
        ├── HutchSmsAuthenticator.php
        ├── HutchConfigurationValidator.php
        ├── HutchPayloadBuilder.php
        └── HutchHttpClient.php