PHP code example of mahdi-hejazi / laravel-ghasedak-sms

1. Go to this page and download the library: Download mahdi-hejazi/laravel-ghasedak-sms 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/ */

    

mahdi-hejazi / laravel-ghasedak-sms example snippets


// config/ghasedak.php
'templates' => [
    'phoneVerifyCode' => 'phoneVerifyCode',
    'loginCode' => 'loginCode', 
    'appointmentReminder' => 'appointmentReminder',
    'orderConfirmed' => 'orderConfirmed',
],

use MahdiHejazi\LaravelGhasedakSms\Facades\GhasedakSms;

// Send verification code using new OTP API
$response = GhasedakSms::sendOtp('09123456789', 'phoneVerifyCode', [
    'Code' => '1234'
]);

// Send login code with user name
$response = GhasedakSms::sendOtp('09123456789', 'loginCode', [
    'Code' => '5678',
    'Name' => 'احمد رضایی'
]);

use MahdiHejazi\LaravelGhasedakSms\Notifications\OtpSmsNotification;

// Single verification code
$user->notify(OtpSmsNotification::verificationCode('09123456789', '1234'));

// Appointment reminder
$user->notify(OtpSmsNotification::appointmentReminder(
    '09123456789', 
    'دکتر احمدی', 
    '1403/10/15', 
    '14:30'
));

// Order confirmation
$user->notify(OtpSmsNotification::orderConfirmation(
    '09123456789', 
    'ORD-12345', 
    '250000 تومان'
));

// Welcome message
$user->notify(OtpSmsNotification::welcome('09123456789', 'علی احمدی'));

// Password reset
$user->notify(OtpSmsNotification::passwordReset('09123456789', 'RESET123'));

// Send OTP at specific time (ISO 8601 format)
$sendDate = '2024-12-25T14:30:00Z';
$response = GhasedakSms::sendScheduledOtp(
    '09123456789', 
    'phoneVerifyCode', 
    ['Code' => '1234'], 
    $sendDate
);

// Using notification
$user->notify(OtpSmsNotification::scheduledVerificationCode(
    '09123456789', 
    '1234', 
    '2024-12-25T14:30:00Z'
));

// Send voice verification code
$response = GhasedakSms::sendVoiceOtp('09123456789', 'phoneVerifyCode', [
    'Code' => '1234'
]);

// Using notification
$user->notify(OtpSmsNotification::voiceVerificationCode('09123456789', '1234'));

// Track your OTP with custom reference ID
$clientRefId = 'USER_123_VERIFY_' . time();
$response = GhasedakSms::sendOtpVerificationCode('09123456789', '1234', $clientRefId);

// Later, you can use this reference ID to check status

// config/ghasedak.php
'templates' => [
    'phoneVerifyCode' => 'phoneVerifyCode',
    'loginCode' => 'loginCode', 
    'appointmentReminder' => 'appointmentReminder',
    'orderConfirmed' => 'orderConfirmed',
],

$response = [
    'isSuccess' => true,
    'statusCode' => 200,
    'message' => 'با موفقیت انجام شد',
    'data' => [
        'items' => [
            [
                'messageBody' => 'کد تایید شما: 1234',
                'receptor' => '09123456789',
                'cost' => 850,
                'messageId' => '23304980',
                'clientReferenceId' => 'USER_123_VERIFY_1703497185',
                'sendDate' => '2024-12-25T09:59:45.599126+03:30'
            ]
        ],
        'totalCost' => 850
    ]
];

use MahdiHejazi\LaravelGhasedakSms\Exceptions\GhasedakSmsException;

try {
    $response = GhasedakSms::sendOtpVerificationCode('09123456789', '1234');
    
    // Check response
    if ($response['isSuccess']) {
        $messageId = $response['data']['items'][0]['messageId'];
        $cost = $response['data']['totalCost'];
        echo "SMS sent successfully! ID: {$messageId}, Cost: {$cost}";
    }
    
} catch (GhasedakSmsException $e) {
    echo "Error: " . $e->getMessage();
    echo "Code: " . $e->getErrorCode();
}

// Old way with positional parameters
GhasedakSms::sendTemplate('09123456789', 'phoneVerifyCode', ['1234']);

// New way with named parameters
GhasedakSms::sendOtp('09123456789', 'phoneVerifyCode', ['Code' => '1234']);

// Or using the convenience method
GhasedakSms::sendOtpVerificationCode('09123456789', '1234');

// Built-in template SMS factory methods
SendSmsNotification::verificationCode($code, $phone);
SendSmsNotification::orderConfirmed($phone, $orderId, $amount, $date);
SendSmsNotification::thankYou($phone, $customerName);
SendSmsNotification::passwordReset($phone, $resetCode);
SendSmsNotification::welcome($phone, $userName);

// Simple SMS factory methods
SimpleSmsNotification::create($phone, $message, $sender);
SimpleSmsNotification::scheduled($phone, $message, $sendDate, $sender);



namespace App\Notifications;

use MahdiHejazi\LaravelGhasedakSms\Notifications\SendSmsNotification;

class CustomSmsNotification extends SendSmsNotification
{
    // Add your custom factory methods
    public static function appointmentReminder($phone, $doctorName, $date, $time)
    {
        return new self('appointmentReminder', $phone, [$doctorName, $date, $time]);
    }

    public static function paymentConfirmation($phone, $amount, $transactionId)
    {
        return new self('paymentConfirmed', $phone, [$amount, $transactionId]);
    }

    public static function productAvailable($phone, $productName, $price)
    {
        return new self('productAvailable', $phone, [$productName, $price]);
    }
}

use App\Notifications\CustomSmsNotification;

$user->notify(CustomSmsNotification::appointmentReminder(
    '09123456789', 
    'Dr. Smith', 
    '1403/10/15', 
    '14:30'
));



namespace App\Services;

use MahdiHejazi\LaravelGhasedakSms\Notifications\SendSmsNotification;
use Illuminate\Support\Facades\Notification;

class BusinessSmsService
{
    public function sendAppointmentReminder($phone, $doctorName, $date, $time)
    {
        return Notification::route('sms', $phone)
            ->notify(new SendSmsNotification('appointmentReminder', $phone, [
                $doctorName, $date, $time
            ]));
    }

    public function sendLowStockAlert($phone, $productName, $currentStock)
    {
        return Notification::route('sms', $phone)
            ->notify(new SendSmsNotification('lowStock', $phone, [
                $productName, $currentStock
            ]));
    }
}

use App\Services\BusinessSmsService;

$smsService = new BusinessSmsService();
$smsService->sendAppointmentReminder('09123456789', 'Dr. Smith', '1403/10/15', '14:30');

use MahdiHejazi\LaravelGhasedakSms\Notifications\SendSmsNotification;

public function boot()
{
    SendSmsNotification::macro('courseEnrollment', function ($phone, $courseName, $startDate) {
        return new SendSmsNotification('courseEnrollment', $phone, [$courseName, $startDate]);
    });
}

$user->notify(SendSmsNotification::courseEnrollment('09123456789', 'Laravel Course', '1403/11/01'));

'templates' => [
    'appointmentReminder' => 'appointmentReminder',
    // other templates...
],

CustomSmsNotification::appointmentReminder('09123456789', 'Dr. Smith', '1403/10/15', '14:30');

// Make notification queueable
$user->notify((new SendSmsNotification('phoneVerifyCode', $phone, [$code]))->delay(30));

use MahdiHejazi\LaravelGhasedakSms\Exceptions\GhasedakSmsException;

try {
    $user->notify(SendSmsNotification::verificationCode('1234', '09123456789'));
} catch (GhasedakSmsException $e) {
    // Handle specific errors
    $errorCode = $e->getErrorCode();
    $message = $e->getMessage(); // Persian error message
    
    if ($errorCode == 9) {
        // Insufficient balance
    }
}

'templates' => [
    'phoneVerifyCode' => 'your_template_name_in_ghasedak_panel', // ← Must exist in Ghasedak panel
    'orderConfirmed' => 'order_confirmed_template',              // ← Must exist in Ghasedak panel
    'passwordReset' => 'password_reset_template',                // ← Must exist in Ghasedak panel
    // Add more templates...
],

// config/ghasedak.php
'templates' => [
    'phoneVerifyCode' => 'verifyCodeTemplate', // ← Exact template name from panel
],

$user->notify(SendSmsNotification::verificationCode('1234', '09123456789'));

// Extend the notification class
class EcommerceSmsNotification extends SendSmsNotification
{
    public static function orderShipped($phone, $orderNumber, $trackingCode)
    {
        return new self('orderShipped', $phone, [$orderNumber, $trackingCode]);
    }
    
    public static function priceDropAlert($phone, $productName, $newPrice)
    {
        return new self('priceDropAlert', $phone, [$productName, $newPrice]);
    }
}

// Usage
$user->notify(EcommerceSmsNotification::orderShipped('09123456789', 'ORD-123', 'TR-456'));

class MedicalSmsNotification extends SendSmsNotification
{
    public static function appointmentConfirmed($phone, $doctorName, $date, $time)
    {
        return new self('appointmentConfirmed', $phone, [$doctorName, $date, $time]);
    }
}

use MahdiHejazi\LaravelGhasedakSms\Notifications\SimpleSmsNotification;

class BulkSmsService
{
    public function sendToMultipleUsers($phoneNumbers, $message)
    {
        foreach ($phoneNumbers as $phone) {
            Notification::route('sms', $phone)
                ->notify(new SimpleSmsNotification($phone, $message));
        }
    }
}

'logging' => [
    'enabled' => env('GHASEDAK_LOGGING', true),
    'channel' => env('LOG_CHANNEL', 'stack'),
],
bash
php artisan vendor:publish --provider="MahdiHejazi\LaravelGhasedakSms\GhasedakSmsServiceProvider" --tag="ghasedak-config"
bash
 docker-compose exec php vendor/bin/phpunit --group integration