To enable PSD2 mode on your account, contact Twilio Support.
PSD2 is the short name for Payment Service Directive 2, a set of regulations introduced by the European Banking Authority aimed at combating the rising costs of fraud. PSD2 requires Strong Customer Authentication (SCA) for online transactions involving more than 30 Euros. To learn more about PSD2, SCA, and dynamic linking check out this post.
Twilio Verify already allows you to quickly verify phone number ownership with one-time passwords (OTP) over SMS. In a few steps, you can extend these capabilities to help comply with PSD2 by verifying transactions using dynamic linking and Strong Customer Authentication (SCA).
First, you must contact Twilio Support to enable PSD2 mode on your account.
Next, create a new Service with PSD2 mode enabled, as shown in the code sample below.
Once enabled, requests to start and/or complete verifications require the Payee
and Amount
parameters.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createService() {11const service = await client.verify.v2.services.create({12friendlyName: "My PSD2 Service",13psd2Enabled: true,14});1516console.log(service.psd2Enabled);17}1819createService();
1{2"sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"friendly_name": "My PSD2 Service",5"code_length": 4,6"lookup_enabled": false,7"psd2_enabled": true,8"skip_sms_to_landlines": false,9"dtmf_input_required": false,10"tts_name": "name",11"mailer_sid": "MDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",12"do_not_share_warning_enabled": false,13"custom_code_enabled": true,14"push": {15"include_date": false,16"apn_credential_sid": "CRaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"fcm_credential_sid": null18},19"totp": {20"issuer": "test-issuer",21"time_step": 30,22"code_length": 3,23"skew": 224},25"whatsapp": {26"msg_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",27"from": "whatsapp:+1234567890"28},29"default_template_sid": "HJaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",30"verify_event_subscription_enabled": false,31"date_created": "2015-07-30T20:00:00Z",32"date_updated": "2015-07-30T20:00:00Z",33"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",34"links": {35"verification_checks": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/VerificationCheck",36"verifications": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications",37"rate_limits": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/RateLimits",38"messaging_configurations": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/MessagingConfigurations",39"entities": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Entities",40"webhooks": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Webhooks",41"access_tokens": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/AccessTokens"42}43}
To start a transaction verification, send an HTTP POST
request to your PSD2-enabled Service's Verifications resource. This request must contain the Amount
, Payee
, To
, and Channel
parameters.
This HTTP request causes Twilio to send a verification code to the user. Each verification code is dynamically-linked to the Amount
and Payee
of each transaction. The code is unique to the To
(e.g., the recipient's phone number), Amount
, and Payee
combination. This ensures that verification fails in the event of code interception or transaction mutations.
Each verification code is valid for 10 minutes. Within that ten-minute time frame, any subsequent HTTP POST
requests to the Verifications resource for the transaction cause Twilio send the same verification code.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createVerification() {11const verification = await client.verify.v212.services("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.verifications.create({14amount: "€39.99",15channel: "sms",16payee: "Acme Inc.",17to: "+15017122661",18});1920console.log(verification.sid);21}2223createVerification();
1{2"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",5"to": "+15017122661",6"channel": "sms",7"status": "pending",8"valid": false,9"date_created": "2015-07-30T20:00:00Z",10"date_updated": "2015-07-30T20:00:00Z",11"lookup": {},12"amount": "€39.99",13"payee": "Acme Inc.",14"send_code_attempts": [15{16"time": "2015-07-30T20:00:00Z",17"channel": "SMS",18"attempt_sid": "VLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"19}20],21"sna": null,22"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications/VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"23}
For some regions, Twilio is unable to return carrier and cellphone data by default. To enable these regions, contact Twilio Support.
More information can be found in the Help Center.
To check if a verification code is correct, send an HTTP POST
request to your PSD2-enabled Service's Verification Check resource. This request must contain the Code
, To
(e.g., the user's phone number), Amount
, and Payee
parameters. A sample request is shown in the example below.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createVerificationCheck() {11const verificationCheck = await client.verify.v212.services("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.verificationChecks.create({14amount: "€39.99",15code: "1234",16payee: "Acme Inc.",17to: "+15017122661",18});1920console.log(verificationCheck.status);21}2223createVerificationCheck();
1{2"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",5"to": "+15017122661",6"channel": "sms",7"status": "approved",8"valid": true,9"amount": "€39.99",10"payee": "Acme Inc.",11"sna_attempts_error_codes": [],12"date_created": "2015-07-30T20:00:00Z",13"date_updated": "2015-07-30T20:00:00Z"14}
In some instances, the details of a transaction may change before it can be completed. When that occurs, you can cancel an in-progress transaction verification by updating the Status
of the Verification resource. An example of this request is shown below.
This prevents a user from verifying an out-of-date transaction.
That transactions that have been successfully verified cannot be canceled.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function updateVerification() {11const verification = await client.verify.v212.services("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.verifications("VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")14.update({ status: "canceled" });1516console.log(verification.sid);17}1819updateVerification();
1{2"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",5"to": "+15017122661",6"channel": "sms",7"status": "canceled",8"valid": false,9"date_created": "2015-07-30T20:00:00Z",10"date_updated": "2015-07-30T20:00:00Z",11"lookup": {},12"amount": null,13"payee": null,14"send_code_attempts": [15{16"time": "2015-07-30T20:00:00Z",17"channel": "SMS",18"attempt_sid": "VLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"19}20],21"sna": null,22"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications/VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"23}