Skip to content

Commit

Permalink
Merge pull request #7 from tarfin-labs/add-avaliable-credit-package-s…
Browse files Browse the repository at this point in the history
…ervice

Add avaliable credit package service
  • Loading branch information
frkcn authored Mar 3, 2020
2 parents 53c94a1 + 4a55ec2 commit 478c81e
Show file tree
Hide file tree
Showing 29 changed files with 650 additions and 177 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
All notable changes to `netgsm` will be documented in this file


## 2.0.0 - 2020-03-02

- namespaces and folder structure changed
- available credit and packages service added
- tests updated

## 1.0.1 - 2020-02-17

- default sms sending method changed to get

## 1.0.0 - 2020-02-17

- initial release

70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Also, this package provides simple reporting.
- [Available SMS Interfaces](#available-sms-interfaces)
- [Reporting](#reporting)
- [Available Reporting Interfaces](#available-report-interfaces)
- [Account Balance](#account-balance)
- [Remaining Balance](#remaining-balance)
- [Remaining Package Credits](#remaining-package-credits)
- [Testing](#testing)
- [Security](#security)
- [Contributing](#contributing)
Expand Down Expand Up @@ -225,6 +228,73 @@ Report Results
| total | All | No | Yes


### Account Balance

With this service, you can inquire the remaining balance of your netgsm account and the credit balances of your packages.

#### Remaining Balance

Returns the remaining money balance on netgsm account. (TL)

Usage:

```php
Netgsm::getCredit();
```

Output:

```php
2,7
```

#### Remaining Package Credits

Returns the credit balances of the packages defined in the relevant netgsm account.

Usage:

```php
Netgsm::getAvailablePackages();
```

Output:

```php
class Illuminate\Support\Collection#105 (1) {
protected $items =>
array(3) {
[0] =>
array(3) {
'amount' =>
int(1000)
'amountType' =>
string(14) "Adet Flash Sms"
'packageType' =>
string(0) ""
}
[1] =>
array(3) {
'amount' =>
int(953)
'amountType' =>
string(12) "Adet OTP Sms"
'packageType' =>
string(0) ""
}
[2] =>
array(3) {
'amount' =>
int(643)
'amountType' =>
string(4) "Adet"
'packageType' =>
string(3) "SMS"
}
}
}
```

### Testing

``` bash
Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
'language' => env('NETGSM_LANGUAGE', 'tr'),
'header' => env('NETGSM_HEADER', null),
'sms_sending_method' => env('NETGSM_SMS_SENDING_METHOD', 'get'),
'base_uri' => env('NETGSM_BASE_URI', 'https://api.netgsm.com.tr'),
'timeout' => env('NETGSM_TIMEOUT', 60),
],
];
75 changes: 75 additions & 0 deletions src/Balance/NetgsmAvailableCredit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace TarfinLabs\Netgsm\Balance;

use GuzzleHttp\Exception\GuzzleException;
use TarfinLabs\Netgsm\Exceptions\NetgsmException;
use TarfinLabs\Netgsm\NetgsmApiClient;
use TarfinLabs\Netgsm\NetgsmErrors;

class NetgsmAvailableCredit extends NetgsmApiClient
{
/**
* @var string
*/
protected $response;

/**
* @var array
*/
protected $successCodes = [
'00',
];

/**
* @var string
*/
protected $url = 'balance/list/get';

/**
* @var array
*/
protected $errorCodes = [
'30' => NetgsmErrors::CREDENTIALS_INCORRECT,
'40' => NetgsmErrors::CREDENTIALS_INCORRECT,
'100' => NetgsmErrors::SYSTEM_ERROR,
];

/**
* extracts credit from the returned response.
*
* @return string
* @throws NetgsmException
*/
public function parseResponse(): ?string
{
$result = explode(' ', $this->response);

if (empty($result[0])) {
throw new NetgsmException(NetgsmErrors::NETGSM_GENERAL_ERROR);
}

$code = $result[0];

if (! in_array($code, $this->successCodes)) {
$message = $this->errorCodes[$code];
throw new NetgsmException($message, $code);
}

return $result[1];
}

/**
* returns the credits amount for associated netgsm account.
*
* @throws NetgsmErrors
* @throws GuzzleException
* @throws NetgsmException
*/
public function getCredit(): ?string
{
$this->response = $this->callApi('GET', $this->url);

return $this->parseResponse();
}
}
73 changes: 73 additions & 0 deletions src/Balance/NetgsmPackages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace TarfinLabs\Netgsm\Balance;

use GuzzleHttp\Exception\GuzzleException;
use TarfinLabs\Netgsm\Exceptions\NetgsmException;
use TarfinLabs\Netgsm\NetgsmApiClient;
use TarfinLabs\Netgsm\NetgsmErrors;

class NetgsmPackages extends NetgsmApiClient
{
/**
* @var string
*/
protected $response;

/**
* @var string
*/
protected $url = 'balance/list/get';

/**
* @var array
*/
protected $errorCodes = [
'30' => NetgsmErrors::MESSAGE_TOO_LONG,
'40' => NetgsmErrors::CREDENTIALS_INCORRECT,
'100' => NetgsmErrors::SYSTEM_ERROR,
];

/**
* handles the response and return the package list as an array.
*
* @return array
* @throws NetgsmException
*/
public function parseResponse(): array
{
$availablePackages = [];

if (array_key_exists($this->response, $this->errorCodes)) {
$message = $this->errorCodes[$this->response];
throw new NetgsmException($message, $this->response);
}

$rows = array_filter(explode('<BR>', $this->response));
foreach ($rows as $row) {
$columns = array_filter(explode('|', $row));
$columns = array_map('trim', $columns);
$availablePackages[] = [
'amount' => (int) $columns[0],
'amountType' => $columns[1] ?? null,
'packageType' => $columns[2] ?? null,
];
}

return $availablePackages;
}

/**
* returns the packages list for associated netgsm account.
*
* @return array
* @throws GuzzleException
* @throws NetgsmException
*/
public function getPackages(): array
{
$this->response = $this->callApi('GET', $this->url, ['tip' => 1]);

return $this->parseResponse();
}
}
15 changes: 2 additions & 13 deletions src/Exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@

namespace TarfinLabs\Netgsm\Exceptions;

use Exception;
use GuzzleHttp\Exception\ClientException;

class CouldNotSendNotification extends \Exception
class CouldNotSendNotification extends Exception
{
const MESSAGE_TOO_LONG = 'Mesaj metnindeki bir problemden dolayı gönderilemedi veya standart maksimum mesaj karakter sayısını geçtiniz.';
const START_DATE_INCORRECT = 'Mesaj gönderim baslangıç tarihinde hata var. Sistem tarihi ile değiştirilip işleme alındı.';
const END_DATE_INCORRECT = 'Mesaj gönderim sonlandırılma tarihinde hata var. Sistem tarihi ile değiştirilip işleme alındı.Bitiş tarihi başlangıç tarihinden küçük girilmiş ise, sistem bitiş tarihine içinde bulunduğu tarihe 24 saat ekler.';
const SENDER_INCORRECT = 'Mesaj başlığınız (gönderici adınızın) sistemde tanımlı değil.';
const CREDENTIALS_INCORRECT = 'Geçersiz kullanıcı adı, şifre veya kullanıcınızın API erişim izni yok.';
const PARAMETERS_INCORRECT = 'Hatalı sorgulama. Gönderdiğiniz parametrelerden birisi hatalı veya zorunlu alanlardan biri eksik.';
const RECEIVER_INCORRECT = 'Gönderilen numara hatalı.';
const OTP_ACCOUNT_NOT_DEFINED = 'Hesabınızda OTP SMS Paketi tanımlı değildir.';
const QUERY_LIMIT_EXCEED = 'Hesabınızda OTP SMS Paketi tanımlı değildir.';
const SYSTEM_ERROR = 'Sistem hatası.';
const NETGSM_GENERAL_ERROR = 'Netgsm responded with an error :';

/**
* Thrown when there's a bad request and an error is responded.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/IncorrectPhoneNumberFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TarfinLabs\Netgsm\Exceptions;

class IncorrectPhoneNumberFormatException extends \Exception
use Exception;

class IncorrectPhoneNumberFormatException extends Exception
{
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class InvalidConfiguration extends Exception
*/
public static function configurationNotSet()
{
return new static('In order to send notification via netgsm you need to add credentials in the `netgsm` key of `config.services`.');
return new static('In order to send notification via netgsm you need to add credentials in the `credentials` key of `config.netgsm`.');
}
}
9 changes: 9 additions & 0 deletions src/Exceptions/NetgsmException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace TarfinLabs\Netgsm\Exceptions;

use Exception;

class NetgsmException extends Exception
{
}
43 changes: 40 additions & 3 deletions src/Netgsm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Collection;
use TarfinLabs\Netgsm\Balance\NetgsmAvailableCredit;
use TarfinLabs\Netgsm\Balance\NetgsmPackages;
use TarfinLabs\Netgsm\Exceptions\CouldNotSendNotification;
use TarfinLabs\Netgsm\Report\AbstractNetgsmReport;
use TarfinLabs\Netgsm\Sms\AbstractNetgsmMessage;

class Netgsm
{
Expand All @@ -31,7 +36,7 @@ public function __construct(Client $client, array $credentials = [], array $defa
* @return mixed
* @throws CouldNotSendNotification
* @throws Exceptions\IncorrectPhoneNumberFormatException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function sendSms(AbstractNetgsmMessage $netgsmMessage)
{
Expand All @@ -49,14 +54,14 @@ public function sendSms(AbstractNetgsmMessage $netgsmMessage)
}

/**
* Get sending status report for messages between given dates.
* Get sms status report between given dates.
*
* @param AbstractNetgsmReport $report
* @param $startDate
* @param $endDate
* @param array $filters
* @return Collection
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
* @throws Exceptions\ReportException
*/
public function getReports(
Expand Down Expand Up @@ -89,4 +94,36 @@ public function getReports(

return $report->getReports();
}

/**
* Returns the remaining credits amount (TL) on the netgsm account.
*
* @return string
* @throws Exceptions\NetgsmException
* @throws GuzzleException
*/
public function getCredit()
{
$creditService = new NetgsmAvailableCredit();
$creditService->setClient($this->client);
$creditService->setCredentials($this->credentials);

return $creditService->getCredit();
}

/**
* Returns the available package list and their balances on the netgsm account.
*
* @return array
* @throws Exceptions\NetgsmException
* @throws GuzzleException
*/
public function getAvailablePackages(): Collection
{
$packageService = new NetgsmPackages();
$packageService->setClient($this->client);
$packageService->setCredentials($this->credentials);

return collect($packageService->getPackages());
}
}
Loading

0 comments on commit 478c81e

Please sign in to comment.