Skip to content

Instantly share code, notes, and snippets.

@alaminfirdows
Created September 21, 2024 14:06
Show Gist options
  • Select an option

  • Save alaminfirdows/a9e0c95e80a6169a85fca09dc1a7fefc to your computer and use it in GitHub Desktop.

Select an option

Save alaminfirdows/a9e0c95e80a6169a85fca09dc1a7fefc to your computer and use it in GitHub Desktop.

Revisions

  1. alaminfirdows created this gist Sep 21, 2024.
    73 changes: 73 additions & 0 deletions sms-service.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?php

    namespace App\Services;

    use Illuminate\Http\Client\Response;
    use Illuminate\Support\Arr;
    use Illuminate\Support\Facades\Http;
    use Illuminate\Support\Facades\Log;

    class TextMessageService
    // implements TextMessageServiceInterface
    {
    private string $apiBase = 'http://bulksmsbd.net/api/';

    private array $responseCodes = [
    202 => 'SMS Submitted Successfully',
    1001 => 'Invalid Number',
    1002 => 'sender id not correct/sender id is disabled',
    1003 => 'Please Required all fields/Contact Your System Administrator',
    1005 => 'Internal Error',
    1006 => 'Balance Validity Not Available',
    1007 => 'Balance Insufficient',
    1011 => 'User Id not found',
    1012 => 'Masking SMS must be sent in Bengali',
    1013 => 'Sender Id has not found Gateway by api key',
    1014 => 'Sender Type Name not found using this sender by api key',
    1015 => 'Sender Id has not found Any Valid Gateway by api key',
    1016 => 'Sender Type Name Active Price Info not found by this sender id',
    1017 => 'Sender Type Name Price Info not found by this sender id',
    1018 => 'The Owner of this (username) Account is disabled',
    1019 => 'The (sender type name) Price of this (username) Account is disabled',
    1020 => 'The parent of this account is not found.',
    1021 => 'The parent active (sender type name) price of this account is not found.',
    ];

    private string $apiKey;
    private string $apiUser;

    public function __construct()
    {
    $this->apiKey = config('services.sms.api_key');
    $this->apiUser = config('services.sms.api_user');
    }

    public function sendSMS(string $message, array $recipient): array
    {
    $response = Http::post($this->apiBase. 'smsapi', [
    'senderid' => $this->apiUser,
    'api_key'=> $this->apiKey,
    'number'=> implode(',', array_unique($recipient)),
    'message'=> $message
    ]);

    // (new LogForSms)->handle($response);

    return $this->parseResponse($response);
    }

    private function parseResponse(Response $response): array
    {
    $status = false;
    $message = 'Something went wrong!';

    if ($response->successful()) {
    $outcome = json_decode($response->body());

    $status = $outcome->response_code === 202;
    $message = $status ? $outcome->success_message : $outcome->error_message;
    }

    return [$status, $message];
    }
    }