Skip to content

Commit

Permalink
Add Number Verification demo UI
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Oct 8, 2024
1 parent 8232a04 commit 48a8fa8
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ private URI resolveEndpoint(String endpoint) {
return configuration.serverUrl.resolve(endpoint);
}

@PostConstruct
//@PostConstruct
public void init() {
var ac = configuration.vonageClient.getApplicationClient();
var appIdStr = configuration.applicationId.toString();
try {
var existing = ac.getApplication(appIdStr);
var application = ac.updateApplication(
Application.builder(existing)
.improveAi(true)
.addCapability(Verify.builder()
.addWebhook(Webhook.Type.STATUS, buildWebhook(VERIFY_STATUS_ENDPOINT)).build()
).addCapability(Messages.builder()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.vonage.sample.serversdk.springboot;

import com.vonage.client.camara.numberverification.NumberVerificationClient;
import static com.vonage.sample.serversdk.springboot.ApplicationConfiguration.NUMBER_VERIFICATION_REDIRECT_ENDPOINT;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;
import java.net.URI;
import java.util.UUID;

@Controller
public final class NumberVerificationController extends VonageController {
private static final String
NUMBER_VERIFICATION_TEMPLATE_NAME = "number_verification",
NUMBER_VERIFICATION_PARAMS_NAME = "numberVerificationParams",
NUMBER_VERIFICATION_URL = "/numberVerification";

private NumberVerificationClient getNumberVerificationClient() {
return getVonageClient().getNumberVerificationClient();
}

@GetMapping(NUMBER_VERIFICATION_URL)
public String numberVerificationStart(Model model) {
var numberVerificationParams = new NumberVerificationParams();
numberVerificationParams.msisdn = "+990123456";
model.addAttribute(NUMBER_VERIFICATION_PARAMS_NAME, numberVerificationParams);
return NUMBER_VERIFICATION_TEMPLATE_NAME;
}

@PostMapping(NUMBER_VERIFICATION_URL)
public RedirectView buildVerificationUrl(@ModelAttribute NumberVerificationParams nvParams, Model model) {
var redirectUrl = getServerUrl().resolve(NUMBER_VERIFICATION_REDIRECT_ENDPOINT);
try {
nvParams.url = getNumberVerificationClient().initiateVerification(
nvParams.msisdn, redirectUrl, nvParams.msisdn
);
model.addAttribute(NUMBER_VERIFICATION_PARAMS_NAME, nvParams);
return new RedirectView(nvParams.url.toString());
}
catch (Exception ex) {
return new RedirectView(ERROR_TEMPLATE);
}
}

@GetMapping(NUMBER_VERIFICATION_REDIRECT_ENDPOINT)
public String inboundWebhook(@RequestParam String code, @RequestParam(required = false) String state, Model model) {
System.out.println("Received code '"+code+"' with state '"+state+"'.");
boolean result = getNumberVerificationClient().verifyNumber(code);
var nvParams = new NumberVerificationParams();
nvParams.code = code;
nvParams.msisdn = state;
nvParams.state = state;
nvParams.result = result ? "Number matches." : "Fraudulent!";
model.addAttribute(NUMBER_VERIFICATION_PARAMS_NAME, nvParams);
return NUMBER_VERIFICATION_TEMPLATE_NAME;
}

public static class NumberVerificationParams {
private String msisdn, code, state, result;
private URI url;

public String getMsisdn() {
return msisdn;
}

public void setMsisdn(String msisdn) {
this.msisdn = msisdn;
}

public URI getUrl() {
return url;
}

public void setUrl(URI url) {
this.url = url;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.vonage.sample.serversdk.springboot;

import com.vonage.client.camara.simswap.SimSwapClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.time.Instant;

@Controller
public final class SimSwapController extends VonageController {
private static final String
SIM_SWAP_TEMPLATE_NAME = "sim_swap",
SIM_SWAP_PARAMS_NAME = "simSwapParams",
SIM_SWAP_URL = "/simSwap";

private SimSwapClient getSimSwapClient() {
return getVonageClient().getSimSwapClient();
}

@GetMapping(SIM_SWAP_URL)
public String simSwapStart(Model model) {
var simSwapParams = new SimSwapParams();
simSwapParams.msisdn = "+990123456";
model.addAttribute(SIM_SWAP_PARAMS_NAME, simSwapParams);
return SIM_SWAP_TEMPLATE_NAME;
}

@PostMapping(SIM_SWAP_URL)
public String simSwapPost(@ModelAttribute SimSwapParams simSwapParams, Model model) {
if (simSwapParams.msisdn != null) {
simSwapParams.date = getSimSwapClient().retrieveSimSwapDate(simSwapParams.msisdn);
}
model.addAttribute(SIM_SWAP_PARAMS_NAME, simSwapParams);
return SIM_SWAP_TEMPLATE_NAME;
}

public static class SimSwapParams {
private String msisdn;
private Instant date;

public String getMsisdn() {
return msisdn;
}

public void setMsisdn(String msisdn) {
this.msisdn = msisdn;
}

public Instant getDate() {
return date;
}

public void setDate(Instant date) {
this.date = date;
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h2 class="text-2xl font-semibold mb-4">Demos:</h2>
<ul>
<li><h3><a href="/account">Account and Numbers</a></h3></li>
<li><h3><a href="/verify">Verify</a></h3></li>
<li><h3><a href="/numberVerification">Number Verification</a></h3></li>
<li><h3><a href="/simSwap">SIM Swap</a></h3></li>
<li><h3><a href="/messages">Messages</a></h3></li>
<li><h3><a href="/voice">Voice</a></h3></li>
Expand Down
47 changes: 47 additions & 0 deletions src/main/resources/templates/number_verification.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Verify phone number network</title>
<div th:include="~{fragments/head :: preamble}"></div>
<script type="text/javascript">
const updateNumberVerificationSnippet = function() {
updateSnippet(
`URI follow = client.getNumberVerificationClient().initiateVerification("${$('#msisdn').val()}", redirectUrl, null);`
);
};

$(document).ready(() => {
updateNumberVerificationSnippet();
$('#form').change(updateNumberVerificationSnippet);
});
</script>
</head>
<body>
<a href="/">Home</a><br>
<a href="https://developer.vonage.com/en/number-verification/overview">Number Verification API overview</a><br>
<hr><br>
<div class="flex">
<div class="w-1/3 p-4">
<form id="form" th:action="@{/numberVerification}" th:object="${numberVerificationParams}" method="post">
<div id="to-div" class="md:flex md:items-center mb-6">
<div class="md:w-1/3">
<label for="msisdn" class="block text-gray-500 font-bold md:text-right mb-1 md:mb-0 pr-4">
German or Spanish phone number:
</label>
</div>
<div class="md:w-2/3">
<input type="tel" minlength="5" maxlength="50" th:field="*{msisdn}" id="msisdn" placeholder="990111199" class="bg-gray-200 border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500"/>
</div>
</div>
<div id="submit-div" class="md:w-2/3 float-right">
<input type="submit" value="Submit" class="float-right mb-8 mr-1 shadow bg-purple-500 hover:bg-purple-400 focus:shadow-outline focus:outline-none text-white font-bold py-2 px-4 rounded">
</div>
</form>
</div>
<div th:include="~{fragments/head :: java-code-snippet}"></div>
</div>
<hr><br>
<h3 id="result" th:text="${numberVerificationParams.result}"></h3>
<hr>
</body>
</html>
6 changes: 3 additions & 3 deletions src/main/resources/templates/sim_swap.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Send multimedia message</title>
<title>Check for SIM Swap</title>
<div th:include="~{fragments/head :: preamble}"></div>
<script type="text/javascript">
const updateSimSwapSnippet = function() {
Expand All @@ -22,15 +22,15 @@
<hr><br>
<div class="flex">
<div class="w-1/3 p-4">
<form id="form" th:action="@{/camara/simSwap}" th:object="${simSwapParams}" method="post">
<form id="form" th:action="@{/simSwap}" th:object="${simSwapParams}" method="post">
<div id="to-div" class="md:flex md:items-center mb-6">
<div class="md:w-1/3">
<label for="msisdn" class="block text-gray-500 font-bold md:text-right mb-1 md:mb-0 pr-4">
German or Spanish phone number:
</label>
</div>
<div class="md:w-2/3">
<input type="tel" minlength="5" maxlength="50" th:field="*{msisdn}" id="msisdn" placeholder="447900000001" class="bg-gray-200 border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500"/>
<input type="tel" minlength="5" maxlength="50" th:field="*{msisdn}" id="msisdn" placeholder="990111199" class="bg-gray-200 border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500"/>
</div>
</div>
<div id="submit-div" class="md:w-2/3 float-right">
Expand Down

0 comments on commit 48a8fa8

Please sign in to comment.