From 50214942b7495610cbe3a273333503836789df68 Mon Sep 17 00:00:00 2001 From: James Seconde Date: Tue, 14 Nov 2023 15:56:57 +0000 Subject: [PATCH] Add ability to add optional redirect url (#454) --- src/Verify2/Request/SilentAuthRequest.php | 8 +++++- .../VerifyObjects/VerificationWorkflow.php | 21 ++++++++++++++- test/Verify2/ClientTest.php | 27 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/Verify2/Request/SilentAuthRequest.php b/src/Verify2/Request/SilentAuthRequest.php index 853291d0..4a74c046 100644 --- a/src/Verify2/Request/SilentAuthRequest.php +++ b/src/Verify2/Request/SilentAuthRequest.php @@ -9,8 +9,14 @@ class SilentAuthRequest extends BaseVerifyRequest public function __construct( protected string $to, protected string $brand, + protected ?string $redirectUrl = null ) { $workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_SILENT_AUTH, $to); + + if ($this->redirectUrl) { + $workflow->setCustomKeys(['redirect_url' => $this->redirectUrl]); + } + $this->addWorkflow($workflow); } @@ -21,4 +27,4 @@ public function toArray(): array 'workflow' => $this->getWorkflows() ]; } -} \ No newline at end of file +} diff --git a/src/Verify2/VerifyObjects/VerificationWorkflow.php b/src/Verify2/VerifyObjects/VerificationWorkflow.php index 129e7733..a823b2f6 100644 --- a/src/Verify2/VerifyObjects/VerificationWorkflow.php +++ b/src/Verify2/VerifyObjects/VerificationWorkflow.php @@ -25,7 +25,8 @@ class VerificationWorkflow implements ArrayHydrateInterface public function __construct( protected string $channel, protected string $to, - protected string $from = '' + protected string $from = '', + protected array $customKeys = [] ) { if (! in_array($channel, $this->allowedWorkflows, true)) { throw new \InvalidArgumentException($this->channel . ' is not a valid workflow'); @@ -91,6 +92,24 @@ public function toArray(): array $returnArray['from'] = $this->getFrom(); } + if (!empty($this->customKeys)) { + foreach ($this->customKeys as $key => $value) { + $returnArray[$key] = $value; + } + } + return $returnArray; } + + public function getCustomKeys(): array + { + return $this->customKeys; + } + + public function setCustomKeys(array $customKeys): self + { + $this->customKeys = $customKeys; + + return $this; + } } diff --git a/test/Verify2/ClientTest.php b/test/Verify2/ClientTest.php index 6d7be95a..2a4fe176 100644 --- a/test/Verify2/ClientTest.php +++ b/test/Verify2/ClientTest.php @@ -393,6 +393,33 @@ public function testCanRequestSilentAuth(): void $this->assertEquals('https://api.nexmo.com/v2/verify/c11236f4-00bf-4b89-84ba-88b25df97315/silent-auth/redirect', $result['check_url']); } + public function testCanRequestSilentAuthWithRedirectUrl(): void + { + $payload = [ + 'to' => '07784587411', + 'brand' => 'my-brand', + 'redirect_url' => 'https://my-app-endpoint/webhook' + ]; + + $silentAuthRequest = new SilentAuthRequest($payload['to'], $payload['brand'], $payload['redirect_url']); + + $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { + $this->assertRequestJsonBodyContains('brand', $payload['brand'], $request); + $this->assertRequestJsonBodyContains('to', $payload['to'], $request, true); + $this->assertRequestJsonBodyContains('channel', 'silent_auth', $request, true); + $this->assertRequestJsonBodyContains('redirect_url', 'https://my-app-endpoint/webhook', $request, true); + $this->assertEquals('POST', $request->getMethod()); + + return true; + }))->willReturn($this->getResponse('verify-silent-auth-request-success', 202)); + + $result = $this->verify2Client->startVerification($silentAuthRequest); + $this->assertIsArray($result); + $this->assertArrayHasKey('request_id', $result); + $this->assertArrayHasKey('check_url', $result); + $this->assertEquals('https://api.nexmo.com/v2/verify/c11236f4-00bf-4b89-84ba-88b25df97315/silent-auth/redirect', $result['check_url']); + } + public function testCannotSendConcurrentVerifications(): void { $this->expectException(Client\Exception\Request::class);