Skip to content

Commit

Permalink
Verify Blocked Number Handling (#327)
Browse files Browse the repository at this point in the history
* Make sure that the library can handle the new number blocked error thrown in verify

* Allow user to get requestID out of an exception when present
  • Loading branch information
SecondeJK committed Aug 30, 2022
1 parent 83c12a4 commit 52d68cb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Client/Exception/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,27 @@ class Request extends Exception
{
use HasEntityTrait;
use Psr7Trait;

protected string $requestId;
protected string $networkId;

public function setRequestId(string $requestId): void
{
$this->requestId = $requestId;
}

public function getRequestId(): string
{
return $this->requestId;
}

public function setNetworkId(string $networkId): void
{
$this->networkId = $networkId;
}

public function getNetworkId(): string
{
return $this->networkId;
}
}
9 changes: 9 additions & 0 deletions src/Verify/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ protected function checkError($verification, $data)
default:
$e = new ClientException\Request($data['error_text'], (int)$data['status']);
$e->setEntity($data);

if (array_key_exists('request_id', $data)) {
$e->setRequestId($data['request_id']);
}

if (array_key_exists('network', $data)) {
$e->setNetworkId($data['network']);
}

break;
}

Expand Down
57 changes: 57 additions & 0 deletions test/Verify/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,63 @@ public function testStartThrowsException(): void
}
}

/**
* @throws ClientExceptionInterface
* @throws Client\Exception\Exception
* @throws ServerException
*/
public function testStartThrowsExceptionAndHandlesBlock(): void
{
$response = $this->setupClientForStart('start-error-blocked');

try {
@$this->client->start(
[
'number' => '14845551212',
'brand' => 'Test Verify'
]
);

self::fail('did not throw exception');
} catch (Client\Exception\Request $e) {
$this->assertEquals('7', $e->getCode());
$this->assertEquals(
'The number you are trying to verify is blacklisted for verification',
$e->getMessage()
);
$this->assertSame($response, @$e->getEntity()->getResponse());
}
}

/**
* @throws ClientExceptionInterface
* @throws Client\Exception\Exception
* @throws ServerException
*/
public function testStartThrowsExceptionAndHandlesConcurrentVerifications(): void
{
$response = $this->setupClientForStart('start-error-concurrent');

try {
@$this->client->start(
[
'number' => '14845551212',
'brand' => 'Test Verify'
]
);

self::fail('did not throw exception');
} catch (Client\Exception\Request $e) {
$this->assertEquals('10', $e->getCode());
$this->assertEquals(
'Concurrent verifications to the same number are not allowed',
$e->getMessage()
);
$this->assertEquals('abcdef0123456789abcdef0123456789', $e->getRequestId());
$this->assertSame($response, @$e->getEntity()->getResponse());
}
}

/**
* @throws ClientExceptionInterface
* @throws Client\Exception\Exception
Expand Down
5 changes: 5 additions & 0 deletions test/Verify/responses/start-error-blocked.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"status": "7",
"error_text": "The number you are trying to verify is blacklisted for verification",
"network": "25503"
}
5 changes: 5 additions & 0 deletions test/Verify/responses/start-error-concurrent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"status": "10",
"request_id": "abcdef0123456789abcdef0123456789",
"error_text": "Concurrent verifications to the same number are not allowed"
}

0 comments on commit 52d68cb

Please sign in to comment.