Skip to content

Commit

Permalink
Add ability to add optional redirect url (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK authored Nov 14, 2023
1 parent bac6ebc commit 5021494
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Verify2/Request/SilentAuthRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -21,4 +27,4 @@ public function toArray(): array
'workflow' => $this->getWorkflows()
];
}
}
}
21 changes: 20 additions & 1 deletion src/Verify2/VerifyObjects/VerificationWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
}
27 changes: 27 additions & 0 deletions test/Verify2/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5021494

Please sign in to comment.