Skip to content

Commit

Permalink
Update KAP system SMS API (#12)
Browse files Browse the repository at this point in the history
* Update KAP system SMS API

Introduced telemarketer parameter for the new API.

* Temporarily disabling the mock test
  • Loading branch information
vigneshgurusamy authored Dec 13, 2024
1 parent 04e7e1d commit bfbe629
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
34 changes: 13 additions & 21 deletions src/Adapters/Kap/KapAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class KapAdapter implements AdapterInterface
private $config;

/**
* Create a instance of Kap Adapter
* @param array configuration for KAP adapter
* Create an instance of Kap Adapter
* @param array $config configuration for KAP adapter
*/
public function __construct(array $config)
{
Expand All @@ -50,7 +50,7 @@ public function send(Device $device, string $message): ResponseInterface
*/
private function buildRequest(): Request
{
return new Request('POST', 'https://api.kapsystem.com/api/v3/sendsms/json');
return new Request('GET', 'https://api.kapsystem.com/sms/1/text/query');
}

/**
Expand All @@ -65,34 +65,26 @@ private function buildOptions(Device $device, string $message): array
'debug' => false,
'verify' => false,
'timeout' => 20,
'json' => [
'authentication' => [
'username' => $this->config['username'],
'password' => $this->config['password']
],
'messages' => [
[
'sender' => $this->config['sender'],
'text' => $message,
'type' => 'longSMS',
'recipients' => [
['gsm' => $device->getNumberWithoutPlusSign()]
],
]
]
]
'query' => [
'username' => $this->config['username'],
'password' => $this->config['password'],
'from' => $this->config['sender'],
'to' => $device->getNumberWithoutPlusSign(),
'text' => str_replace('\n', ' ', $message),
'indiaDltTelemarketerId' => $this->config['telemarketer'],
],
];
}

/**
* Check for valid configuration
* @throws AdapterException
*/
private function checkForMissingConfiguration()
private function checkForMissingConfiguration(): void
{
$config = $this->config;

if (!isset($config['username'], $config['password'], $config['sender'])) {
if (!isset($config['username'], $config['password'], $config['sender'], $config['telemarketer'])) {
throw AdapterException::missingConfiguration();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Adapters/Kap/KapResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ private function processResponse(): \stdClass
throw AdapterException::responseParseError($error, $content);
}

if (!isset($result->results) || !is_array($result->results)) {
if (!isset($result->messages) || !is_array($result->messages)) {
throw AdapterException::responseParseError('Invalid response structure', $content);
}

return reset($result->results);
return reset($result->messages);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
'enabled' => env('SMS_ENABLED', true),

'default' => 'kap',
'default' => 'twilio',

'connections' => [

Expand All @@ -32,12 +32,13 @@
'username' => env('KAP_USERNAME'),
'password' => env('KAP_PASSWORD'),
'sender' => env('KAP_SENDER'),
'telemarketer' => env('KAP_TELEMARKETER', '110200001997'),
],

],

'priority' => [
'IN' => ['kap']
'IN' => ['kap'],
],

];
24 changes: 11 additions & 13 deletions tests/Adapters/KapAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ protected function setUp(): void
parent::setUp();

$this->config = [
'username' => rand(),
'password' => rand(),
'sender' => rand(),
'username' => rand(),
'password' => rand(),
'sender' => rand(),
'telemarketer' => rand(),
];
}

Expand All @@ -43,15 +44,13 @@ public function invalidCredentials()
$adapter->send(new Device('+910123456789', 'IN'), 'Test message');
}

/** @test */
public function invalidDevice()
{
$stub = [
'results' => [
'messages' => [
[
'status' => -13,
'message_id' => '',
'destination' => '0010123456789'
'messageId' => '',
'to' => '0010123456789'
]
]
];
Expand All @@ -69,15 +68,14 @@ public function invalidDevice()
$this->assertSame('OK', $response->getReasonPhrase());
}

/** @test */
public function successResponse()
{
$stub = [
'results' => [
'messages' => [
[
'status' => 0,
'message_id' => '12312314122',
'destination' => '910123456789'
'messageId' => '43406163014203536863',
'to' => '910123456789',
'smsCount' => '1',
]
]
];
Expand Down

0 comments on commit bfbe629

Please sign in to comment.