
Twilio Pay is a game-changer for businesses of all sizes, allowing them to send and receive payments with ease. It's a robust payment solution that integrates seamlessly with Twilio's communication platform.
With Twilio Pay, businesses can send and receive payments in over 130 currencies, making it a global payment solution. This is particularly useful for businesses with international customers or partners.
Twilio Pay also offers a range of payment methods, including credit and debit cards, bank transfers, and even cryptocurrencies like Bitcoin. This flexibility makes it easy for businesses to accommodate different customer preferences.
One of the most significant benefits of Twilio Pay is its low transaction fees, which can save businesses a significant amount of money compared to traditional payment gateways.
Connector and Token
To process transactions, you need to specify a paymentConnector attribute, which corresponds to the unique name of your Pay Connector in the Twilio Console. This name is specified when configuring the Pay Connector Add-on in the Marketplace.
For example, if you're using Stripe, you would use paymentConnector=Stripe_1. If you don't specify a paymentConnector, Twilio will use the Pay Connector with the unique name "Default".
You must have an installed Pay Connector named "Default" or include the paymentConnector attribute.
Readers also liked: Use Twilio with Lambda
Connector

You can create multiple instances of a Pay Connector, which is useful for different environments like production, staging, and development. This allows you to have a separate instance for each environment.
You can create three instances for Stripe, for example, one for production, one for staging, and one for development.
Here's an example of how you might set up these instances:
You can then use the paymentConnector attribute to specify which instance to use. For example, if you want to process a transaction using Stripe, you would use paymentConnector=Stripe_1. If you don't specify a paymentConnector, the default instance will be used.
Here's an interesting read: How to Access Apple Pay on Phone
TokenType
The tokenType attribute is a crucial part of the payment process. It can take either one-time or reusable as a value.
If you're using a Stripe Pay Connector, payment-method is also a possible value. This is a game-changer for businesses that want to integrate payment functionality into their voice calls.
See what others are reading: Twilio - Sms/mms-svr

To tokenize a payment method, set chargeAmount=0 or omit the chargeAmount attribute altogether. This ensures that the payment method is processed correctly.
You can generate a one-time token by using tokenType="one-time". This is perfect for businesses that want to charge customers a specific amount during a voice call.
For recurring payments, use tokenType="reusable". This allows customers to save their payment information for future transactions.
Tokenize Transaction Response
When a payment processor handles a tokenize transaction, it returns an object to Twilio with an ID number for the transaction, any applicable error code/message, and custom parameters if necessary.
This response object is expected to contain a "token_id" property with a value of some identification number associated with the transaction.
Twilio supports two transaction types: charge and tokenize, which is useful to know when working with payment processors.
The payment processor will take the request object from above and return an object to Twilio with the necessary information.
The "token_id" property is a crucial part of the response object, as it identifies the transaction itself or the token.
For another approach, see: Twilio Virtual Phone Number
Handling Results
Handling Results is a crucial step in the Twilio Pay process.
You'll receive a response from the payment processor, which Twilio will then use to determine the outcome of the payment. This response will contain information such as the charge ID or token ID, along with any applicable error codes or messages.
In the case of a successful payment, the response will contain a "charge_id" property with a value associated with the charge transaction. This is the ID number that Twilio will use to track the payment.
If the payment fails, Twilio will expect to receive an error response with the "charge_id" field for a charge error or the "token_id" field for a tokenize error. The "parameters" field is optional in this case.
Here's a breakdown of the expected error response structure from the payment processor:
Twilio will use this information to update the payment status and notify the user accordingly.
Error Handling and Retry
Twilio Pay's retry logic is designed to handle failed payment processor responses. If the payment processor fails to respond within 10 seconds, Twilio will retry the request up to 5 times, passing the same transaction_id.
It's essential to note that the transaction_id behaves as an idempotency token to avoid duplicate charges. This means if a response is dropped in transit, the payment processor should either return an error stating it was a duplicate transaction or the original charge_id/token_id from the first attempted response.
Twilio expects a specific error response structure from payment processors. For charge errors, the payment processor should include the charge_id field in the response, while for tokenize errors, the token_id field is expected. The parameters field is optional in both cases.
For more insights, see: 30003 Twilio Error Code
Error Handling and Retry
Twilio will retry a payment request up to 5 times if the payment processor fails to respond within 10 seconds.
The transaction_id is used as an idempotency token to avoid duplicate charges in case a response is dropped in transit.
If the transaction is still in progress after more than 10 seconds from the initial request, Twilio expects the successful response to be returned to the retry request.
The payment processor should handle the retried request properly, either returning an error stating it was a duplicate transaction or the original charge_id/token_id.
In case of an error, Twilio expects to receive the charge_id field in the response for a charge error and the token_id field in the response for a tokenize error.
The parameters field is optional in the error response structure from the payment processor.
Timeout
Timeout is a crucial setting in error handling and retry, as it determines how long a system will wait for user input before moving on to the next step.
The timeout attribute sets a specific limit in seconds for waiting, which is essential for preventing infinite loops and ensuring a smooth user experience.
For example, if the timeout is set to 3 seconds, the system will wait exactly 3 seconds for the user to press a key when capturing sensitive information, such as credit card numbers or bank account details.
This timeout period is critical in ACH payments, where it applies to both bank account and routing numbers capture.
If this caught your attention, see: Twilio Test Numbers
Customization and Parameters
Twilio allows you to send custom parameters in the request to the payment processor.
You can also receive custom parameters from the payment processor, which will be added as properties on the POST request body to your action URL.
These custom parameters will be prepended with "PayConnector_" and can be used to pass additional information between the payment processor and your action URL.
For example, given a sample response from the payment processor, the body of the Twilio POST request to your action URL could include properties such as "PayConnector_custom_param1" and "PayConnector_custom_param2".
You might like: How to Increase Facebook Post Reach without Paying
Custom Parameters
Custom Parameters are a powerful tool that allows you to send custom parameters in the request to the payment processor.
Twilio enables you to receive custom parameters from the payment processor, which can be added as properties on the POST request body to your action URL.
After your payment processor handles your transaction, the response body it sends back can contain a parameters property, which will be added to the POST request body.
For example, given a sample response from the payment processor, the body of the Twilio POST request to your action URL could include properties like PayConnector_transactionId and PayConnector_amount.
You can use these custom parameters to tailor your payment flow and provide more detailed information to your users.
Use Cases

Twilio Pay can be used for a variety of purposes, including enabling donations and payments to be taken on outbound calls.
One of the biggest requirements of implementing Twilio Pay is to provide a seamless payment process, as seen in the example with Oxfam UK.
This involves collecting information within instant messengers (IMs) before automatically providing a link for the customer to click through to complete their transaction in a PCI compliant way.
Using Twilio Pay to kick off the payment process in instant messengers (IMs) forms a seamless process, instead of having to ask for information to be repeated.
This approach provides an elegant way of progressing customer communications to the point of payment on channels that do not have native payment options.
Featured Images: pexels.com


