Skip to content

Commit

Permalink
Add the template ID in sms and voice
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Sep 13, 2024
1 parent 9c10394 commit 1430de2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Verify2/Request/SMSRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace Vonage\Verify2\Request;

use InvalidArgumentException;
use Vonage\Verify2\Traits\CustomTemplateTrait;
use Vonage\Verify2\VerifyObjects\VerificationLocale;
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;

class SMSRequest extends BaseVerifyRequest
{
use CustomTemplateTrait;

public function __construct(
protected string $to,
protected string $brand,
Expand Down Expand Up @@ -36,6 +39,12 @@ public function __construct(

public function toArray(): array
{
return $this->getBaseVerifyUniversalOutputArray();
$return = $this->getBaseVerifyUniversalOutputArray();

if (!is_null($this->getTemplateId())) {
$return['template_id'] = $this->getTemplateId();
}

return $return;
}
}
3 changes: 3 additions & 0 deletions src/Verify2/Request/VoiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace Vonage\Verify2\Request;

use InvalidArgumentException;
use Vonage\Verify2\Traits\CustomTemplateTrait;
use Vonage\Verify2\VerifyObjects\VerificationLocale;
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;

class VoiceRequest extends BaseVerifyRequest
{
use CustomTemplateTrait;

public function __construct(
protected string $to,
protected string $brand,
Expand Down
18 changes: 18 additions & 0 deletions src/Verify2/Traits/CustomTemplateTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Vonage\Verify2\Traits;

trait CustomTemplateTrait
{
protected ?string $templateId = null;

public function getTemplateId(): ?string
{
return $this->templateId;
}

public function setTemplateId(string $templateId): string
{
return $this->templateId = $templateId;
}
}
48 changes: 48 additions & 0 deletions test/Verify2/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@ public function testCanRequestSMS(): void
$this->assertArrayHasKey('request_id', $result);
}

public function testCanRequestSmsWithCustomTemplate(): void
{
$payload = [
'to' => '07785254785',
'client_ref' => 'my-verification',
'brand' => 'my-brand',
'from' => 'vonage'
];

$smsVerification = new SMSRequest($payload['to'], $payload['brand'], null, $payload['from']);
$smsVerification->setTemplateId('33945c03-71c6-4aaf-954d-750a9b480def');

$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals(
'https://api.nexmo.com/v2/verify',
$uriString
);

$this->assertRequestJsonBodyContains('template_id', '33945c03-71c6-4aaf-954d-750a9b480def', $request);
$this->assertEquals('POST', $request->getMethod());

return true;
}))->willReturn($this->getResponse('verify-request-success', 202));

$result = $this->verify2Client->startVerification($smsVerification);

$this->assertIsArray($result);
$this->assertArrayHasKey('request_id', $result);
}

public function testWillPopulateEntityIdAndContentId(): void
{
$payload = [
Expand Down Expand Up @@ -1026,4 +1058,20 @@ public function timeoutProvider(): array
[921, false],
];
}
//
// public function testIntegration()
// {
// $credentials = new Client\Credentials\Keypair(
// file_get_contents('/Users/JSeconde/Sites/vonage-php-sdk-core/test/Verify2/private.key'),
// '4a875f7e-2559-4fb5-84f6-f8b144f6e9f6'
// );
//
// $liveClient = new Client($credentials);
//
// $response = $liveClient->verify2()->createCustomTemplate('example-template');

// $smsRequest = new SMSRequest('447738066610', 'VONAGE', null, '447738066610');
// $response = $liveClient->verify2()->startVerification($smsRequest);
// var_dump($response);
// }
}

0 comments on commit 1430de2

Please sign in to comment.