Skip to content

Commit a16cc30

Browse files
author
Vincent Mosoti
committed
Add support for AfricasTalking Gateway
1 parent 8907666 commit a16cc30

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/Config/sms.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
'username' => env('MICROMOBILE_USERNAME'),
4040
'password' => env('MICROMOBILE_PASSWORD'),
4141
'call_back_url' => env('MICROMOBILE_CALL_BACK_URL')
42+
],
43+
44+
'africastalking' => [
45+
'username' => env('AFRICASTALKING_USERNAME'),
46+
'api_key' => env('AFRICASTALKING_API_KEY'),
47+
'from' => env('AFRICASTALKING_FROM')
4248
]
4349
],
4450

@@ -58,5 +64,6 @@
5864
'map' => [
5965
'bongatech' => \CraftedSystems\LaravelSMS\Gateways\Bongatech::class,
6066
'micromobile' => \CraftedSystems\LaravelSMS\Gateways\MicroMobile::class,
67+
'africastalking' => \CraftedSystems\LaravelSMS\Gateways\AfricasTalking::class,
6168
]
6269
];

src/Gateways/AfricasTalking.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: vincent
5+
* Date: 12/14/17
6+
* Time: 6:04 PM
7+
*/
8+
9+
namespace CraftedSystems\LaravelSMS\Gateways;
10+
11+
use CraftedSystems\LaravelSMS\Contracts\SMSContract;
12+
use Illuminate\Http\Request;
13+
use AfricasTalking\AfricasTalkingGateway;
14+
15+
class AfricasTalking implements SMSContract
16+
{
17+
/**
18+
* @var AfricasTalkingGateway
19+
*/
20+
protected $class;
21+
22+
23+
/**
24+
* MicroMobile constructor.
25+
* @param $settings
26+
* @throws \Exception
27+
*/
28+
public function __construct($settings)
29+
{
30+
if (!class_exists('AfricasTalking\AfricasTalkingGateway')) {
31+
32+
throw new \Exception("Class 'AfricasTalking\AfricasTalkingGateway' does not exist");
33+
}
34+
35+
$s = (object)$settings;
36+
37+
$this->class = new AfricasTalkingGateway($s->username, $s->api_key);
38+
}
39+
40+
/**
41+
* @param $recipient
42+
* @param $message
43+
* @param null $params
44+
* @return mixed
45+
* @throws \Exception
46+
*/
47+
public function send($recipient, $message, $params = null)
48+
{
49+
$response = $this->class->sendMessage($recipient, $message, config('sms.gateways.africastalking.from'));
50+
51+
$data = [
52+
'is_success' => $response[0]->status === 'Success',
53+
'correlator' => '',
54+
'message_id' => $response[0]->messageId,
55+
'cost' => $response[0]->cost
56+
];
57+
58+
return (object)$data;
59+
}
60+
61+
62+
/**
63+
* @return mixed
64+
* @throws \AfricasTalking\AfricasTalkingGatewayException
65+
*/
66+
public function getBalance()
67+
{
68+
return trim(str_replace('KES', '', $this->class->getUserData()->balance));
69+
}
70+
71+
72+
/**
73+
* @param Request $request
74+
* @return mixed|object
75+
*/
76+
public function getDeliveryReports(Request $request)
77+
{
78+
$status = $request->status;
79+
80+
if ($status == "Failed" || $status == "Rejected") {
81+
82+
$fs = $request->failureReason;
83+
84+
} else {
85+
86+
$fs = $status;
87+
}
88+
89+
90+
$data = [
91+
'status' => $fs,
92+
'message_id' => $request->id,
93+
'phone_number' => ''
94+
];
95+
96+
return (object)$data;
97+
}
98+
}

0 commit comments

Comments
 (0)