From 15f008daa161000f17c6995b0f4c537666b98438 Mon Sep 17 00:00:00 2001 From: James Seconde Date: Mon, 15 May 2023 14:23:45 +0100 Subject: [PATCH 1/2] Added fraud check (#406) --- src/Verify2/Request/BaseVerifyRequest.php | 15 ++++++++++++++ test/Verify2/ClientTest.php | 24 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Verify2/Request/BaseVerifyRequest.php b/src/Verify2/Request/BaseVerifyRequest.php index a07eb584..2b8f6e54 100644 --- a/src/Verify2/Request/BaseVerifyRequest.php +++ b/src/Verify2/Request/BaseVerifyRequest.php @@ -16,6 +16,8 @@ abstract class BaseVerifyRequest implements RequestInterface protected int $timeout = 300; + protected bool $fraudCheck = true; + protected ?string $clientRef = null; protected int $length = 4; @@ -132,9 +134,22 @@ public function addWorkflow(VerificationWorkflow $verificationWorkflow): static return $this; } + public function getFraudCheck(): bool + { + return $this->fraudCheck; + } + + public function setFraudCheck(bool $fraudCheck): BaseVerifyRequest + { + $this->fraudCheck = $fraudCheck; + + return $this; + } + public function getBaseVerifyUniversalOutputArray(): array { $returnArray = [ + 'fraud_check' => $this->getFraudCheck(), 'locale' => $this->getLocale()->getCode(), 'channel_timeout' => $this->getTimeout(), 'code_length' => $this->getLength(), diff --git a/test/Verify2/ClientTest.php b/test/Verify2/ClientTest.php index 2b0b51f2..25a07e4e 100644 --- a/test/Verify2/ClientTest.php +++ b/test/Verify2/ClientTest.php @@ -98,6 +98,7 @@ public function testCanRequestSMS(): void ); $this->assertRequestJsonBodyContains('locale', 'en-us', $request); + $this->assertRequestJsonBodyContains('fraud_check', true, $request); $this->assertRequestJsonBodyContains('channel_timeout', 300, $request); $this->assertRequestJsonBodyContains('client_ref', $payload['client_ref'], $request); $this->assertRequestJsonBodyContains('code_length', 4, $request); @@ -115,6 +116,29 @@ public function testCanRequestSMS(): void $this->assertArrayHasKey('request_id', $result); } + public function testCanBypassFraudCheck(): void + { + $payload = [ + 'to' => '07785254785', + 'client_ref' => 'my-verification', + 'brand' => 'my-brand', + ]; + + $smsVerification = new SMSRequest($payload['to'], $payload['brand']); + $smsVerification->setFraudCheck(false); + + $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { + $this->assertRequestJsonBodyContains('fraud_check', false, $request); + + return true; + }))->willReturn($this->getResponse('verify-request-success', 202)); + + $result = $this->verify2Client->startVerification($smsVerification); + + $this->assertIsArray($result); + $this->assertArrayHasKey('request_id', $result); + } + /** * @dataProvider localeProvider */ From a8f6c33b6d5a25239524b4c176379cb844375640 Mon Sep 17 00:00:00 2001 From: James Seconde Date: Tue, 16 May 2023 11:34:31 +0100 Subject: [PATCH 2/2] Added ability to cancel request (#405) --- src/Verify2/Client.php | 10 +++++-- src/Verify2/ClientFactory.php | 2 +- test/Verify2/ClientTest.php | 26 +++++++++++++++++-- .../Responses/verify-cancel-success.json | 0 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 test/Verify2/Fixtures/Responses/verify-cancel-success.json diff --git a/src/Verify2/Client.php b/src/Verify2/Client.php index e70b5901..e8e9c1cc 100644 --- a/src/Verify2/Client.php +++ b/src/Verify2/Client.php @@ -2,7 +2,6 @@ namespace Vonage\Verify2; -use Laminas\Diactoros\Request; use Vonage\Client\APIClient; use Vonage\Client\APIResource; use Vonage\Client\Exception\Exception; @@ -27,7 +26,7 @@ public function startVerification(BaseVerifyRequest $request): ?array public function check(string $requestId, $code): bool { try { - $response = $this->getAPIResource()->create(['code' => $code], $requestId); + $response = $this->getAPIResource()->create(['code' => $code], '/' . $requestId); } catch (Exception $e) { // For horrible reasons in the API Error Handler, throw the error unless it's a 409. if ($e->getCode() === 409) { @@ -39,4 +38,11 @@ public function check(string $requestId, $code): bool return true; } + + public function cancelRequest(string $requestId): bool + { + $this->api->delete($requestId); + + return true; + } } \ No newline at end of file diff --git a/src/Verify2/ClientFactory.php b/src/Verify2/ClientFactory.php index 00daa750..fd3fe5d0 100644 --- a/src/Verify2/ClientFactory.php +++ b/src/Verify2/ClientFactory.php @@ -15,7 +15,7 @@ public function __invoke(ContainerInterface $container): Client $api->setIsHAL(false) ->setErrorsOn200(false) ->setAuthHandler([new KeypairHandler(), new BasicHandler()]) - ->setBaseUrl('https://api.nexmo.com/v2/verify/'); + ->setBaseUrl('https://api.nexmo.com/v2/verify'); return new Client($api); } diff --git a/test/Verify2/ClientTest.php b/test/Verify2/ClientTest.php index 25a07e4e..e9c8fa19 100644 --- a/test/Verify2/ClientTest.php +++ b/test/Verify2/ClientTest.php @@ -46,7 +46,7 @@ public function setUp(): void ->setErrorsOn200(false) ->setClient($this->vonageClient->reveal()) ->setAuthHandler([new Client\Credentials\Handler\BasicHandler(), new Client\Credentials\Handler\KeypairHandler()]) - ->setBaseUrl('https://api.nexmo.com/v2/verify/'); + ->setBaseUrl('https://api.nexmo.com/v2/verify'); $this->verify2Client = new Verify2Client($this->api); } @@ -93,7 +93,7 @@ public function testCanRequestSMS(): void $uri = $request->getUri(); $uriString = $uri->__toString(); $this->assertEquals( - 'https://api.nexmo.com/v2/verify/', + 'https://api.nexmo.com/v2/verify', $uriString ); @@ -615,6 +615,28 @@ public function testCheckHandlesThrottle(): void $result = $this->verify2Client->check('c11236f4-00bf-4b89-84ba-88b25df97315', '24525'); } + public function testWillCancelVerification(): void + { + $requestId = 'c11236f4-00bf-4b89-84ba-88b25df97315'; + + $this->vonageClient->send(Argument::that(function (Request $request) { + $uri = $request->getUri(); + $uriString = $uri->__toString(); + $this->assertEquals( + 'https://api.nexmo.com/v2/verify/c11236f4-00bf-4b89-84ba-88b25df97315', + $uriString + ); + + $this->assertEquals('DELETE', $request->getMethod()); + + return true; + }))->willReturn($this->getResponse('verify-cancel-success', 204)); + + $result = $this->verify2Client->cancelRequest($requestId); + + $this->assertTrue($result); + } + /** * This method gets the fixtures and wraps them in a Response object to mock the API */ diff --git a/test/Verify2/Fixtures/Responses/verify-cancel-success.json b/test/Verify2/Fixtures/Responses/verify-cancel-success.json new file mode 100644 index 00000000..e69de29b