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/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..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,11 +93,12 @@ 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 ); $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 */ @@ -591,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