diff --git a/README.md b/README.md index 437e121..d0f4571 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,20 @@ You can get changed permission by IYS IysManager::make()->createPermissionDriver()->getChangedPermissions(); ``` +You can get permission status by permission model + +```php + IysManager::make()->createPermissionDriver()->getPermissionStatus( + Permission::make() + ->setConsentDate('2022-02-10 09:50:02') + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipient('example@.com') + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL) + ); +``` + ## Testing ``` bash diff --git a/src/Drivers/Permission/PermissionDriver.php b/src/Drivers/Permission/PermissionDriver.php index dd4d5a6..194c0ba 100644 --- a/src/Drivers/Permission/PermissionDriver.php +++ b/src/Drivers/Permission/PermissionDriver.php @@ -67,6 +67,23 @@ public function getChangedPermissions(?string $after = null, SourceTypes $source ->throw(); } + /** + * @throws RequestException + */ + public function getPermissionStatus(Permission $permission) + { + $permissionData = $permission->toArray(); + + return Http::timeout($this->timeout) + ->withToken($this->bearer)->asJson()->acceptJson() + ->post($this->buildUrl("consents/status"), [ + "recipient" => $permissionData['recipient'], + "recipientType" => $permissionData['recipientType'], + "type" => $permissionData['type'] + ]) + ->throw(); + } + private function buildUrl(string $api): string { return sprintf('%s/sps/%s/brands/%s/%s', $this->baseUrl, $this->iysCode, $this->branchCode, $api); diff --git a/tests/PermissionDriverTest.php b/tests/PermissionDriverTest.php index 9f6dc44..d253e0b 100644 --- a/tests/PermissionDriverTest.php +++ b/tests/PermissionDriverTest.php @@ -433,4 +433,72 @@ public function test_throw_exception_get_changed_permissions() IysManager::make()->createPermissionDriver()->getChangedPermissions(); } + + /** + * @throws RequestException + */ + public function test_get_permission_status() + { + $recipient = 'test@example.com'; + + $consentDate = '2020-07-08 07:07:07'; + + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/status"; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response([ + 'consentDate' => $consentDate, + 'source' => ConsentSourceTypes::MOBILE, + 'recipientType' => RecipientTypes::INDIVIDUAL, + 'status' => StatusTypes::APPROVE, + 'type' => PermissionTypes::EMAIL, + 'recipient' => $recipient, + 'retailerCode' => 55550127, + 'creationDate' => '2020-08-06 15:50:23', + 'retailerTitle' => 'Test Company', + 'retailerAccessCount' => 3, + 'transactionId' => 'abc623z3cq4bhac9b88dadd49b767a2322be140a9n9cuc25abf1ac5392c4ca12', + ]) + ]); + + IysManager::make()->createPermissionDriver()->getPermissionStatus(Permission::make() + ->setConsentDate($consentDate) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipient($recipient) + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL)); + + Http::assertSent(function (Request $request) use ($endpoint, $recipient) { + return $request->url() == $this->url . $endpoint && + $request['recipient'] == $recipient && + $request['recipientType'] == RecipientTypes::INDIVIDUAL->value && + $request['type'] == PermissionTypes::EMAIL->value; + }); + } + + public function test_throw_exception_get_permission_status() + { + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/status"; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response([ + [ + "errors" => [ + [ + "code" => "H097", + "message" => "İzin sorgulama isteği geçerli olmalıdır." + ] + ] + ]], 400), + ]); + + $this->expectException(RequestException::class); + + IysManager::make()->createPermissionDriver()->getChangedPermissions(); + } }