diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f2f0758..191951b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: true matrix: - php: [7.3, 7.4] + php: [7.4] laravel: [8.*] dependency-version: [prefer-lowest, prefer-stable] @@ -17,13 +17,16 @@ jobs: steps: - name: Checkout the code - uses: actions/checkout@v1 + uses: actions/checkout@v2 - - name: Disable Xdebug - run: sudo phpdismod xdebug + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: simplexml - name: Install dependencies - run: composer install --no-progress --no-ansi --profile --no-interaction --no-scripts --no-suggest --prefer-dist + run: composer install --no-progress --no-ansi --profile --no-interaction --no-scripts --prefer-dist - name: Execute tests run: vendor/bin/phpunit --testdox --colors=always --exclude-group skipped diff --git a/CHANGELOG.md b/CHANGELOG.md index 11c3a52..fe7e780 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to `netgsm` will be documented in this file ## Unreleased +## [4.0.0] - 2021-04-26 + +- IYS integration (add, search) added. + ## [3.0.1] - 2020-10-08 - Upgrade packages for Laravel 8. diff --git a/README.md b/README.md index 6a954b9..3e5ba06 100755 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ This package also provides some simple reporting. - [Account Balance](#account-balance) - [Remaining Balance](#remaining-balance) - [Remaining Package Credits](#remaining-package-credits) + - [IYS Integration](#iys-integration) + - [Add Address](#add-address) + - [Search Address](#search-address) - [Testing](#testing) - [Security](#security) - [Contributing](#contributing) @@ -56,6 +59,7 @@ NETGSM_USER_CODE= NETGSM_SECRET= NETGSM_LANGUAGE= NETGSM_HEADER= +NETGSM_BRANDCODE= ], ... ``` @@ -298,6 +302,105 @@ class Illuminate\Support\Collection#105 (1) { } ``` +### IYS Integration + +With these services you can add and search any address to IYS. + +#### Add Address + +This service is used to add a phone number or email address to IYS using NetGsm IYS service. + +##### Object Parameters + +| Method | Description | Type | Required | +| --------------------- | ----------- | ---- | -------- | +| setRefId() | Reference id to query your request | String | No +| setType() | Communication type | String | Yes +| setSource() | Source of permission | String | Yes +| setRecipient() | phone number or email address | String | Yes +| setStatus() | Permission status | String | Yes +| setConsentDate() | Permission date | Datetime (YYYY-MM-DD H:i:s) | Yes +| setRecipientType() | Recipient type | String | Yes +| setRetailerCode() | Retailer code | Integer | No +| setRetailerAccess() | Retailer access | Integer | No + +##### Usage + +```php +$address = new \TarfinLabs\Netgsm\Iys\Requests\Add(); +$address->setRefId(999999) + ->setType('MESAJ') + ->setSource('HS_WEB') + ->setRecipient('+905XXXXXXXXX') + ->setStatus('ONAY') + ->setConsentDate(now()->toDateTimeString()) + ->setRecipientType('TACIR'); + +\TarfinLabs\Netgsm\Netgsm::iys()->addAddress($address)->send(); +``` + +##### Response Parameters +```json +{ + "code": "0", + "error": "false", + "uid": "73113cb9-dff0-415b-9491-xxxxxxxxxx" +} +``` + +#### Search Address + +This service is used to search a phone number or email address on IYS using NetGsm IYS service. + +##### Object Parameters + +| Method | Description | Type | Required | +| --------------------- | ----------- | ---- | -------- | +| setType() | Communication type | String | Yes (if refId is set to null) +| setRecipient() | phone number or email address | String | Yes (if refId is set to null) +| setRecipientType() | Recipient type | String | Yes (if refId is set to null) +| setRefId() | Reference id to query your request | String | No + +##### Usage + +```php +$address = new \TarfinLabs\Netgsm\Iys\Requests\Search(); +$address->setType('MESAJ') + ->setRecipient('+905XXXXXXXXX') + ->setRecipientType('TACIR') + ->setRefId(999999); + +\TarfinLabs\Netgsm\Netgsm::iys()->searchAddress($address)->send(); +``` + +##### Response Parameters +* Response with a matched address. +```json +{ + "code": "0", + "error": "false", + "query": { + "consentDate": "2020-11-06 11:22:34", + "source": "HS_FIZIKSEL_ORTAM", + "recipient": "+905XXXXXXXXX", + "recipientType": "BIREYSEL", + "type": "MESAJ", + "status": "ONAY", + "creationDate": "2020-11-06 11:23:49", + "retailerAccessCount": 0 + // "querystatus": null + } +} +``` + +* Response without any matched address +```json +{ + "code": "50", + "error": "Kayıt bulunamadi." +} +``` + ### Testing ``` bash diff --git a/composer.json b/composer.json index 7a3847a..1c9193d 100755 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ } ], "require": { - "php": "^7.3", + "php": "^7.4", "guzzlehttp/guzzle": "^7.1", "illuminate/support": "^8.0", "illuminate/notifications": "^8.0", @@ -40,7 +40,7 @@ "ext-simplexml": "*" }, "require-dev": { - "fzaninotto/faker": "^1.9", + "fakerphp/faker": "^1.14", "phpunit/phpunit": "^9.4", "mockery/mockery": "^1.4", "orchestra/testbench": "^6.0" diff --git a/config/config.php b/config/config.php index 48f29e9..9eed397 100755 --- a/config/config.php +++ b/config/config.php @@ -4,6 +4,7 @@ 'credentials' => [ 'user_code' => env('NETGSM_USERCODE'), 'secret' => env('NETGSM_SECRET'), + 'brand_code'=> env('NETGSM_BRANDCODE'), ], 'defaults' => [ 'language' => env('NETGSM_LANGUAGE', 'tr'), diff --git a/src/Iys/AbstractNetgsmIys.php b/src/Iys/AbstractNetgsmIys.php new file mode 100644 index 0000000..c7c7533 --- /dev/null +++ b/src/Iys/AbstractNetgsmIys.php @@ -0,0 +1,42 @@ +client->request($this->method, $this->url, [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'header' => [ + 'username' => $this->credentials['user_code'], + 'password' => $this->credentials['secret'], + 'brandCode' => $this->credentials['brand_code'], + ], + 'body' => [ + 'data' => [ + $this->body, + ], + ], + ], + ]); + + return $response->getBody()->getContents(); + } +} diff --git a/src/Iys/NetgsmIys.php b/src/Iys/NetgsmIys.php new file mode 100644 index 0000000..a8dde89 --- /dev/null +++ b/src/Iys/NetgsmIys.php @@ -0,0 +1,39 @@ +url = $request->getUrl(); + $this->body = $request->body(); + $this->method = 'POST'; + + return $this; + } + + /** + * Search address request. + * + * @param Search $request + * @return $this + */ + public function searchAddress(Search $request): NetgsmIys + { + $this->url = $request->getUrl(); + $this->body = $request->body(); + $this->method = 'POST'; + + return $this; + } +} diff --git a/src/Iys/Requests/Add.php b/src/Iys/Requests/Add.php new file mode 100644 index 0000000..b64b7aa --- /dev/null +++ b/src/Iys/Requests/Add.php @@ -0,0 +1,170 @@ +refId = $refId; + + return $this; + } + + /** + * @param string $type + * @return $this + */ + public function setType(string $type): self + { + $this->type = $type; + + return $this; + } + + /** + * @param string $source + * @return $this + */ + public function setSource(string $source): self + { + $this->source = $source; + + return $this; + } + + /** + * @param string $recipient + * @return $this + */ + public function setRecipient(string $recipient): self + { + $this->recipient = $recipient; + + return $this; + } + + /** + * @param string $status + * @return $this + */ + public function setStatus(string $status): self + { + $this->status = $status; + + return $this; + } + + /** + * @param string $consentDate + * @return $this + */ + public function setConsentDate(string $consentDate): self + { + $this->consentDate = $consentDate; + + return $this; + } + + /** + * @param string $recipientType + * @return $this + */ + public function setRecipientType(string $recipientType): self + { + $this->recipientType = $recipientType; + + return $this; + } + + /** + * @param int|null $retailerCode + * @return $this + */ + public function setRetailerCode(?int $retailerCode): self + { + $this->retailerCode = $retailerCode; + + return $this; + } + + /** + * @param int|null $retailerAccess + * @return $this + */ + public function setRetailerAccess(?int $retailerAccess): self + { + $this->retailerAccess = $retailerAccess; + + return $this; + } + + /** + * @param array $defaults + * @return $this + */ + public function setDefaults(array $defaults): self + { + foreach ($defaults as $key => $value) { + if (method_exists($this, 'set'.ucfirst($key))) { + call_user_func([$this, 'set'.ucfirst($key)], $value); + } + } + + return $this; + } + + /** + * Get request body. + * + * @return array + */ + public function body(): array + { + return [ + 'refid' => $this->refId ?? null, + 'type' => $this->type ?? null, + 'source' => $this->source ?? null, + 'recipient' => $this->recipient ?? null, + 'status' => $this->status ?? null, + 'consentDate' => $this->consentDate ?? null, + 'recipientType' => $this->recipientType ?? null, + 'retailerCode' => $this->retailerCode ?? null, + 'retailerAccess' => $this->retailerAccess ?? null, + ]; + } + + /** + * Get request url. + * + * @return string + */ + public function getUrl(): string + { + return $this->url; + } +} diff --git a/src/Iys/Requests/Search.php b/src/Iys/Requests/Search.php new file mode 100644 index 0000000..0b16d89 --- /dev/null +++ b/src/Iys/Requests/Search.php @@ -0,0 +1,100 @@ +type = $type; + + return $this; + } + + /** + * @param string $recipient + * @return $this + */ + public function setRecipient(string $recipient): self + { + $this->recipient = $recipient; + + return $this; + } + + /** + * @param string $recipientType + * @return $this + */ + public function setRecipientType(string $recipientType): self + { + $this->recipientType = $recipientType; + + return $this; + } + + /** + * @param string $refId + * @return $this + */ + public function setRefId(string $refId): self + { + $this->refId = $refId; + + return $this; + } + + /** + * @param array $defaults + * @return $this + */ + public function setDefaults(array $defaults): self + { + foreach ($defaults as $key => $value) { + if (method_exists($this, 'set'.ucfirst($key))) { + call_user_func([$this, 'set'.ucfirst($key)], $value); + } + } + + return $this; + } + + /** + * Get request body. + * + * @return array + */ + public function body(): array + { + return [ + 'type' => $this->type ?? null, + 'recipient' => $this->recipient ?? null, + 'recipientType' => $this->recipientType ?? null, + 'refid' => $this->refId ?? null, + ]; + } + + /** + * Get request url. + * + * @return string + */ + public function getUrl(): string + { + return $this->url; + } +} diff --git a/src/Netgsm.php b/src/Netgsm.php index c9104a4..c6d6cee 100644 --- a/src/Netgsm.php +++ b/src/Netgsm.php @@ -9,6 +9,7 @@ use TarfinLabs\Netgsm\Balance\NetgsmAvailableCredit; use TarfinLabs\Netgsm\Balance\NetgsmPackages; use TarfinLabs\Netgsm\Exceptions\CouldNotSendNotification; +use TarfinLabs\Netgsm\Iys\NetgsmIys; use TarfinLabs\Netgsm\Report\AbstractNetgsmReport; use TarfinLabs\Netgsm\Sms\AbstractNetgsmMessage; @@ -126,4 +127,15 @@ public function getAvailablePackages(): Collection return collect($packageService->getPackages()); } + + /** + * @return NetgsmIys + */ + public function iys(): NetgsmIys + { + $iysService = new NetgsmIys(); + $iysService->setClient($this->client)->setCredentials($this->credentials); + + return $iysService; + } } diff --git a/tests/NetGsmIysTest.php b/tests/NetGsmIysTest.php new file mode 100644 index 0000000..35ad786 --- /dev/null +++ b/tests/NetGsmIysTest.php @@ -0,0 +1,129 @@ +httpClient = Mockery::mock(new Client()); + $this->response = Mockery::mock(ResponseInterface::class); + + $this->netgsm = new Netgsm($this->httpClient, [ + 'user_code' => 'test', + 'secret' => 'test', + 'brand_code' => 123456, + ]); + } + + /** @test */ + public function it_makes_add_requests_for_iys_addresses() + { + $data = [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'header' => [ + 'username' => 'test', + 'password' => 'test', + 'brandCode' => 123456, + ], + 'body' => [ + 'data' => [ + [ + 'refid' => $this->faker->numerify('####'), + 'type' => $this->faker->randomElement(['MESAJ', 'ARAMA']), + 'source' => $this->faker->randomElement([ + 'HS_WEB', + 'HS_FIZIKSEL_ORTAM', + 'HS_ISLAK_IMZA', + 'HS_CAGRI_MERKEZI', + 'HS_SOSYAL_MEDYA', + 'HS_EPOSTA', + 'HS_MESAJ', + 'HS_MOBIL', + 'HS_EORTAM', + 'HS_ETKINLIK', + 'HS_2015', + 'HS_ATM', + 'HS_KARAR', + ]), + 'recipient' => $this->faker->numerify('+905#########'), + 'status' => $this->faker->randomElement(['ONAY', 'RET']), + 'consentDate' => $this->faker->date('Y-m-d H:i:s'), + 'recipientType' => $this->faker->randomElement(['BIREYSEL', 'TACIR']), + 'retailerCode' => null, + 'retailerAccess'=> null, + ], + ], + ], + ], + ]; + + $iysAddress = new Add(); + $iysAddress->setDefaults($data['json']['body']['data'][0]); + + $this->httpClient + ->shouldReceive('request') + ->withSomeOfArgs('POST', 'iys/add', $data) + ->once() + ->andReturn(new Response()); + + $this->netgsm->iys()->addAddress($iysAddress)->send(); + } + + /** @test */ + public function it_makes_search_requests_for_iys_addresses() + { + $data = [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'header' => [ + 'username' => 'test', + 'password' => 'test', + 'brandCode' => 123456, + ], + 'body' => [ + 'data' => [ + [ + 'type' => $this->faker->randomElement(['MESAJ', 'ARAMA']), + 'recipient' => $this->faker->numerify('+905#########'), + 'recipientType' => $this->faker->randomElement(['BIREYSEL', 'TACIR']), + 'refid' => $this->faker->numerify('####'), + ], + ], + ], + ], + ]; + + $iysSearch = new Search(); + $iysSearch->setDefaults($data['json']['body']['data'][0]); + + $this->httpClient + ->shouldReceive('request') + ->withSomeOfArgs('POST', 'iys/search', $data) + ->once() + ->andReturn(new Response()); + + $this->netgsm->iys()->searchAddress($iysSearch)->send(); + } +}