Skip to content

Commit 008e03e

Browse files
feat(verify2): Add template_id support (#612)
1 parent c0f3497 commit 008e03e

5 files changed

Lines changed: 96 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
# Unreleasd
66
- Video: Added post-call transcription options
77
- Video: Added connections list calls
8+
- Verify v2: Added support for `template_id` parameter
89

910
# [9.8.0] - 2025-01-29
1011
- Added deprecation notice for Java 8/1.8 users. Version 9.x will be the final version to support Java 8.

src/main/java/com/vonage/client/verify2/VerificationRequest.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ public class VerificationRequest implements Jsonable {
4040
@JsonProperty("locale") protected final Locale locale;
4141
protected final Integer channelTimeout, codeLength;
4242
protected final Boolean fraudCheck;
43-
protected final String brand, code, clientRef;
43+
protected final String brand, code, clientRef, templateId;
4444
protected final List<Workflow> workflows;
4545

4646
VerificationRequest(Builder builder) {
4747
locale = builder.locale;
4848
clientRef = builder.clientRef;
49+
templateId = builder.templateId;
4950
fraudCheck = builder.fraudCheck != null && !builder.fraudCheck ? false : null;
5051
if ((brand = builder.brand) == null || brand.trim().isEmpty()) {
5152
throw new IllegalArgumentException("Brand name is required.");
@@ -158,6 +159,17 @@ public Boolean getFraudCheck() {
158159
return fraudCheck;
159160
}
160161

162+
/**
163+
* A custom template ID to use for the verification. This parameter works only when
164+
* the channel is SMS or RCS.
165+
*
166+
* @return The template ID, or {@code null} if not set.
167+
*/
168+
@JsonProperty("template_id")
169+
public String getTemplateId() {
170+
return templateId;
171+
}
172+
161173
/**
162174
* Workflows are a sequence of actions that Vonage use to reach the user you wish to verify with a PIN code.
163175
*
@@ -190,7 +202,7 @@ public static Builder builder() {
190202

191203
public static final class Builder {
192204
Boolean fraudCheck;
193-
String brand, code, clientRef;
205+
String brand, code, clientRef, templateId;
194206
Integer timeout, codeLength;
195207
Locale locale;
196208
final List<Workflow> workflows = new ArrayList<>(1);
@@ -364,6 +376,20 @@ public Builder fraudCheck() {
364376
return fraudCheck(false);
365377
}
366378

379+
/**
380+
* (OPTIONAL)
381+
* Set a custom template ID to use for the verification. This parameter works only when
382+
* the channel is SMS or RCS.
383+
*
384+
* @param templateId The template ID to use.
385+
*
386+
* @return This builder.
387+
*/
388+
public Builder templateId(String templateId) {
389+
this.templateId = templateId;
390+
return this;
391+
}
392+
367393
/**
368394
* Constructs a VerificationRequest with this builder's properties.
369395
*

src/test/java/com/vonage/client/verify2/VerificationRequestTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import com.vonage.client.verify2.VerificationRequest.Builder;
2121
import static org.junit.jupiter.api.Assertions.*;
2222
import org.junit.jupiter.api.*;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.nio.charset.StandardCharsets;
2326
import java.util.Arrays;
2427
import java.util.Collections;
2528
import java.util.Locale;
@@ -44,6 +47,39 @@ Builder newBuilder() {
4447
return VerificationRequest.builder().brand(BRAND);
4548
}
4649

50+
/**
51+
* Loads a JSON resource file and replaces placeholders with provided values.
52+
* This follows the pattern used in identityinsights tests for better maintainability.
53+
*
54+
* @param filename The resource filename (relative to this test class)
55+
* @param placeholders Variable arguments of key-value pairs (key, value, key, value, ...)
56+
* @return The JSON string with placeholders replaced
57+
* @throws IOException If the resource cannot be loaded
58+
*/
59+
private String loadJsonResource(String filename, String... placeholders) throws IOException {
60+
try (InputStream is = getClass().getResourceAsStream(filename)) {
61+
if (is == null) {
62+
throw new IOException("Could not find resource: " + filename);
63+
}
64+
byte[] buffer = new byte[1024];
65+
StringBuilder sb = new StringBuilder();
66+
int bytesRead;
67+
while ((bytesRead = is.read(buffer)) != -1) {
68+
sb.append(new String(buffer, 0, bytesRead, StandardCharsets.UTF_8));
69+
}
70+
String json = sb.toString().trim();
71+
72+
// Replace placeholders
73+
for (int i = 0; i < placeholders.length; i += 2) {
74+
if (i + 1 < placeholders.length) {
75+
json = json.replace(placeholders[i], placeholders[i + 1]);
76+
}
77+
}
78+
79+
return json;
80+
}
81+
}
82+
4783
Builder newBuilderAllParams() {
4884
return newBuilder().codeLength(CODE_LENGTH).clientRef(CLIENT_REF)
4985
.channelTimeout(CHANNEL_TIMEOUT).locale(LOCALE).fraudCheck();
@@ -366,6 +402,35 @@ public void testWhatsappWorkflowWithoutSender() {
366402
assertEquals(expectedJson, request.toJson());
367403
}
368404

405+
@Test
406+
public void testTemplateId() throws Exception {
407+
String templateId = "4ed3027d-8762-44a0-aa3f-c393717413a4";
408+
409+
// Test with SMS workflow - using JSON resource file
410+
String expectedSmsJson = loadJsonResource(
411+
"verification-request-with-template-id.json",
412+
"TEMPLATE_ID_PLACEHOLDER", templateId
413+
);
414+
415+
VerificationRequest smsRequest = newBuilder()
416+
.addWorkflow(new SmsWorkflow(TO_NUMBER))
417+
.templateId(templateId)
418+
.build();
419+
assertEquals(templateId, smsRequest.getTemplateId());
420+
assertEquals(expectedSmsJson, smsRequest.toJson());
421+
422+
// Test without template_id - using JSON resource file
423+
String expectedJsonWithoutTemplate = loadJsonResource(
424+
"verification-request-without-template-id.json"
425+
);
426+
427+
VerificationRequest requestWithoutTemplate = newBuilder()
428+
.addWorkflow(new SmsWorkflow(TO_NUMBER))
429+
.build();
430+
assertNull(requestWithoutTemplate.getTemplateId());
431+
assertEquals(expectedJsonWithoutTemplate, requestWithoutTemplate.toJson());
432+
}
433+
369434
@Test
370435
public void triggerJsonProcessingException() {
371436
class SelfRefrencing extends VerificationRequest {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"brand":"Vonage","template_id":"TEMPLATE_ID_PLACEHOLDER","workflow":[{"channel":"sms","to":"447700900000"}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"brand":"Vonage","workflow":[{"channel":"sms","to":"447700900000"}]}

0 commit comments

Comments
 (0)