Skip to content

Commit 52d68cb

Browse files
authored
Verify Blocked Number Handling (#327)
* 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
1 parent 83c12a4 commit 52d68cb

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

src/Client/Exception/Request.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,27 @@ class Request extends Exception
1818
{
1919
use HasEntityTrait;
2020
use Psr7Trait;
21+
22+
protected string $requestId;
23+
protected string $networkId;
24+
25+
public function setRequestId(string $requestId): void
26+
{
27+
$this->requestId = $requestId;
28+
}
29+
30+
public function getRequestId(): string
31+
{
32+
return $this->requestId;
33+
}
34+
35+
public function setNetworkId(string $networkId): void
36+
{
37+
$this->networkId = $networkId;
38+
}
39+
40+
public function getNetworkId(): string
41+
{
42+
return $this->networkId;
43+
}
2144
}

src/Verify/Client.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,15 @@ protected function checkError($verification, $data)
341341
default:
342342
$e = new ClientException\Request($data['error_text'], (int)$data['status']);
343343
$e->setEntity($data);
344+
345+
if (array_key_exists('request_id', $data)) {
346+
$e->setRequestId($data['request_id']);
347+
}
348+
349+
if (array_key_exists('network', $data)) {
350+
$e->setNetworkId($data['network']);
351+
}
352+
344353
break;
345354
}
346355

test/Verify/ClientTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,63 @@ public function testStartThrowsException(): void
282282
}
283283
}
284284

285+
/**
286+
* @throws ClientExceptionInterface
287+
* @throws Client\Exception\Exception
288+
* @throws ServerException
289+
*/
290+
public function testStartThrowsExceptionAndHandlesBlock(): void
291+
{
292+
$response = $this->setupClientForStart('start-error-blocked');
293+
294+
try {
295+
@$this->client->start(
296+
[
297+
'number' => '14845551212',
298+
'brand' => 'Test Verify'
299+
]
300+
);
301+
302+
self::fail('did not throw exception');
303+
} catch (Client\Exception\Request $e) {
304+
$this->assertEquals('7', $e->getCode());
305+
$this->assertEquals(
306+
'The number you are trying to verify is blacklisted for verification',
307+
$e->getMessage()
308+
);
309+
$this->assertSame($response, @$e->getEntity()->getResponse());
310+
}
311+
}
312+
313+
/**
314+
* @throws ClientExceptionInterface
315+
* @throws Client\Exception\Exception
316+
* @throws ServerException
317+
*/
318+
public function testStartThrowsExceptionAndHandlesConcurrentVerifications(): void
319+
{
320+
$response = $this->setupClientForStart('start-error-concurrent');
321+
322+
try {
323+
@$this->client->start(
324+
[
325+
'number' => '14845551212',
326+
'brand' => 'Test Verify'
327+
]
328+
);
329+
330+
self::fail('did not throw exception');
331+
} catch (Client\Exception\Request $e) {
332+
$this->assertEquals('10', $e->getCode());
333+
$this->assertEquals(
334+
'Concurrent verifications to the same number are not allowed',
335+
$e->getMessage()
336+
);
337+
$this->assertEquals('abcdef0123456789abcdef0123456789', $e->getRequestId());
338+
$this->assertSame($response, @$e->getEntity()->getResponse());
339+
}
340+
}
341+
285342
/**
286343
* @throws ClientExceptionInterface
287344
* @throws Client\Exception\Exception
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"status": "7",
3+
"error_text": "The number you are trying to verify is blacklisted for verification",
4+
"network": "25503"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"status": "10",
3+
"request_id": "abcdef0123456789abcdef0123456789",
4+
"error_text": "Concurrent verifications to the same number are not allowed"
5+
}

0 commit comments

Comments
 (0)