Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

TwiML™ Voice: <Pay>


(warning)

Warning

This TwiML verb is only supported in the default US1 Twilio Region Ireland (IE1) and Australia (AU1) are not yet supported.

For more information on supported features in Twilio Regions, go to the Global Infrastructure docs.

You can use TwiML's <Pay> verb to capture and process both ACH and credit card data during a call.

<Pay> will prompt your customer to enter their payment information and will handle retries if a piece of information is invalid.

In addition to managing necessary interactions when there is timeout or invalid input, <Pay> will send webhooks to your statusCallback URL to keep you informed of the progress of <Pay>.

Once all required credit card or ACH data is captured, <Pay> will send the payment information to your payment processor or gateway via a Pay Connector installed and configured on your account.

Once <Pay> finishes collecting a customer's valid payment information, information about the transaction result will be sent via webhook to your statusCallback URL and/or your action URL.

(information)

Info

<Pay> will terminate when DTMF * keypress is received at any point.

When <Pay> finishes collecting a customer's valid payment information or is terminated with a * keypress, Twilio will immediately send a webhook (an HTTP POST request) to the action URL.

Twilio will continue the current call using the TwiML returned in the response from the action URL.

<Pay> can create two types of transactions, charge and tokenize.

  • A charge transaction means you want to immediately capture funds from the customer's supplied payment method (i.e. credit card) in return for the goods or services you offer.

    • To create a charge transaction, set the chargeAmount attribute in your <Pay> verb to a decimal value greater than 0.
  • A tokenize transaction means you want to obtain a token based on the user's supplied credit card information from the payment processor instead of posting any charge. Tokens are typically stored so that you can charge the user in the future without having to ask for the credit card information again. Note that tokens are provided by your payment gateway or processor.

    • To create a tokenize transaction, you must set the chargeAmount to "0" or omit the chargeAmount attribute from your <Pay> verb.

<Pay> Attributes

pay-attributes page anchor

The <Pay> verb supports the following attributes to modify its default behavior:

Attribute Name: input

Allowed Values:

"dtmf"

Default Values:

"dtmf"

Attribute Name: action

Allowed Values:

relative or absolute URL

Default Values:

Current document URL. Must use https. Only POST is supported.

Attribute Name: statusCallback

Allowed Values:

relative or absolute URL

Default Values:

none

Attribute Name: paymentMethod

Allowed Values:

"credit-card" "ach-debit"

Default Values:

"credit-card"

Attribute Name: bankAccountType

Allowed Values:

"consumer-checking" "consumer-savings" "commercial-checking"

Default Values:

"consumer-checking"

Attribute Name: timeout

Allowed Values:

positive integer

Default Values:

5

Attribute Name: maxAttempts

Allowed Values:

1 2 3

Default Values:

none

Attribute Name: securityCode

Allowed Values:

"true" "false"

Default Values:

"true"

Attribute Name: postalCode

Allowed Values:

"true" "false" String

Default Values:

"true"

Attribute Name: minPostalCodeLength

Allowed Values:

Positive integer

Default Values:

none

Attribute Name: paymentConnector

Allowed Values:

String

Default Values:

"Default"

Attribute Name: tokenType

Allowed Values:

"one-time", "reusable", "payment-method" (Stripe Pay Connector only)

Default Values:

"reusable"

Attribute Name: chargeAmount

Allowed Values:

decimal (min: 0, max: 1,000,000)

Default Values:

none

Attribute Name: currency

Allowed Values:

String

Default Values:

"usd"

Attribute Name: language

Allowed Values:

For credit card payments, possible values are: English:

  • en-AU
  • en-CA
  • en-GB
  • en-IN
  • en-US

Spanish:

  • es-ES
  • es-MX

French:

  • fr-CA
  • fr-FR

German:

  • de-DE

Italian:

  • it-IT

For ACH, possible values are: English:

  • en-AU
  • en-CA
  • en-GB
  • en-IN
  • en-US

Default Values:

"en-US"

Attribute Name: description

Allowed Values:

String

Default Values:

none

Attribute Name: validCardTypes

Allowed Values:

String containing one or more of the following values separated by a space: visa mastercard amex maestro discover optima jcb diners-club enroute

Default Values:

"visa mastercard amex"

input

input page anchor

A list of inputs that Twilio should accept for <Pay>; only dtmf is supported.

All digits captured by <Pay> are redacted from the logs.

Back to attributes list

The URL where Twilio will send a POST request for the next set of TwiML instructions after a successful <Pay> transaction.

The action attribute takes an absolute or relative URL as value.

When the <Pay> verb has successfully tokenized or created a charge, Twilio will make a POST request to the action URL. The body of the request includes the standard request parameters and the additional parameters described in the Twilio's POST request to your action URL section below.

If no action is provided, Twilio will by default make a POST request to the current document URL.

The attribute only accepts https protocol for the URL.

If you made a tokenize transaction, Twilio's request to your application will include the PaymentToken and/or ProfileId parameter. These fields contain the tokenized information received from the Payment Gateway.

If you made a charge transaction, Twilio's request to your application will include a PaymentConfirmationCode parameter with the confirmation code received from the payment processor/gateway.

(warning)

Warning

If you started or updated a call with a twiml parameter, the action URLs for <Record>, <Gather>, and <Pay> must be absolute.

The Call Resource API Docs have language-specific examples of creating and updating Calls with TwiML:

  • See "Create a Call resource with TwiML" under Create a Call Resource to see examples of creating a call with a twiml parameter.
  • See "Update a Call resource with TwiML" under Update a Call Resource to see examples of updating a call with a twiml parameter.

Twilio's POST request to your action URL

twilios-post-request-to-your-action-url page anchor

After a successful <Pay> transaction, Twilio will send a POST request to your action URL. The body of this request will contain the standard request parameters, along with the additional parameters listed in the table below.

Parameter: Result

Description:

The result of the <Pay> transaction. Possible values are:

  • "success" <Pay> successfully captured the payment data and either tokenized or processed the payment
  • "too-many-failed-attempts" Max attempts reached when capturing the payment information
  • "payment-connector-error" <Pay> experienced an error communicating with the payment processor or gateway
  • "caller-interrupted-with-star" Caller pressed * (star) key to interrupt <Pay>
  • "caller-hung-up" Caller hung up the call
  • "validation-error" Invalid <Pay> verb attribute (e.g. paymentAmount="-0.5")
  • "internal-error"

Parameter: ProfileId

Description:

The identifier of the customer object to which the payment is associated. Can be used as a token depending on which Pay Connector and/or payment processor used.

  • ACI: no value
  • Base Commerce: no value
  • Braintree: ID of the customer object (can't be used as a token)
  • CardConnect: profile
  • Chase: customer reference number
  • Stripe: ID of the customer resource

Parameter: PaymentToken

Description:

The tokenized value of the credit card or ACH payment data. Each payment processor handles this differently:

Parameter: PaymentConfirmationCode

Description:

If <Pay> was used to process the payment instead of tokenizing, this is the confirmation code from the payment gateway.

Parameter: PaymentMethod

Description:

The payment method provided by the developer in the <Pay> verb's paymentMethod attribute. Example value: "ach-debit"

Parameter: PaymentCardNumber

Description:

If credit-card paymentMethod was used, this is the card number provided by the caller/consumer with only last 4 digits visible. Example value: "xxxx-xxxxxx-x4001"

Parameter: PaymentCardType

Description:

If credit-card paymentMethod was used, this is the type of card provided by the caller/consumer. The value will be one of the validCardTypes:

  • visa
  • mastercard
  • amex
  • maestro
  • discover
  • optima
  • jcb
  • diners-club
  • enroute

Parameter: ExpirationDate

Description:

If credit-card paymentMethod was used, this is the expiration date provided by the caller/consumer in MMDD (two-digit month, two-digit date) format. Example value: 0522

Parameter: SecurityCode

Description:

If credit-card paymentMethod was used, this is the security code provided by the caller/consumer. This value will be redacted. Example value: ***

Parameter: PaymentCardPostalCode

Description:

If credit-card paymentMethod was used, this is the postal code provided by the calller/consumer. Example value: 94109

Parameter: BankAccountNumber

Description:

If ach-debit paymentMethod was used, this is the bank account number provided by the caller/consumer. The value will be redacted except for the last 2 digits. Example: A customer enters 508862392. The BankAccountNumber value will be *******92

Parameter: BankRoutingNumber

Description:

If ach-debit paymentMethod was used, this is the bank routing number provided by the caller/consumer. The value will be the full routing number provided by the caller/consumer. Example value: 121181976

Parameter: BankAccountType

Description:

If ach-debit paymentMethod was used, this is the type of bank account provided by the caller/consumer. Possible values are either "personal" or "business".

Parameter: PaymentError

Description:

Validation error for incorrect <Pay> verb attribute.

  • Example value: "paymentAmount='-0.59' (not a number between 0.00 - 1,000,000.00)"

Payment error for failures.

  • Example value: "card is declined"

Payment error for input timeout.

  • Example value: "invalid-date (if user enters an invalid date or incorrect number of digits and times out)"
  • Example value: "invalid-security-code (if user enters an invalid CVV and times out)"
  • Example value: "invalid-postal-code (if user enters incorrect number of digits for the postal/zip code and times out)"

Parameter: PayErrorCode

Description:

A numerical error code that gives more details about the error. To learn more about the error, please visit [/docs/api/errors) and search for the error code.

Parameter: ConnectorError

Description:

This parameter contains the actual error code/message received from the underlying payment platform.

Back to attributes list

The statusCallback attribute takes an absolute or relative URL as value. Whenever a status change happens in <Pay>, Twilio will make a POST request to this URL with the following parameters.

Twilio's POST request to your statusCallback URL

twilios-post-request-to-your-statuscallback-url page anchor

Twilio will send the following parameters in the body of the POST request to your statusCallback URL:

Parameter: AccountSid

Description:

The unique identifier of the Twilio Account responsible for this Pay session

Parameter: CallSid

Description:

A unique identifier for the Call Resource associated with the Pay sessions. CallSid will always refer to the parent leg of a two-leg call.

Parameter: For

Description:

The current stage of <Pay>. The possible values are:

  • "payment-card-number" the customer is asked for credit or debit card information
  • "expiration-date" the customer is asked for the expiration date for their payment card
  • "security-code" the customer is asked for the security code for their payment card
  • "postal-code" the customer is asked for the postal code associated with the payment card
  • "bank-routing-number" the customer is asked for their bank's routing number
  • "bank-account-number" the customer is asked for their bank account number
  • "payment-processing" the payment is processing

Parameter: ErrorType

Description:

The type of error that occurred (if applicable). The possible error types are:

  • "input-timeout" <Pay> experienced a timeout at one of the stages of the Pay session. See PaymentError for more details on what field did the caller timed out on.
  • "invalid-card-number" The card entered did not pass validation. This could include incorrect number of digits for the credit card number, expiration date, security code, or postal code.
  • "invalid-card-type" The card number didn't match the accepted card types as specified by the validCardTypes attribute
  • "invalid-date" The date entered was the incorrect number of digits, was in the past, or was otherwise not a valid date
  • "invalid-security-code" <Pay> received an invalid security code
  • "invalid-postal-code" <Pay> didn't receive the correct number of digits for the postal/zip code
  • "invalid-bank-routing-number" <Pay> either didn't receive the correct number of digits for the routing number or the routing number provided failed validation
  • "invalid-bank-account-number" <Pay> didn't receive the minimum number of digits required for the bank account number
  • "invalid-bank-account-type" <Pay> didn't receive the accepted values for BankAccountType field
  • "invalid-card-number-security-code-capture-sequence" CVV can only be validated against a credit card. If CVV was captured in incorrect sequence i.e. before the credit card number, this error is thrown.
  • "input-matching-failed" Caller's inputs didn't match when using <Prompt> with requireMatchingInputs .
  • "session-in-progress " If <Pay> receives a request to start a new Pay session, while the existing Pay session is not complete or cancelled, this error is thrown.
  • "internal-error"

Parameter: Attempt

Description:

The current attempt count. Possible values are "1", "2", or "3".

Parameter: PaymentCardNumber

Description:

If paymentMethod="credit-card", this is the card number provided to <Pay> with only the last 4 digits visible. e.g. "xxxxxxxxxxx4001"

Parameter: PaymentCardType

Description:

If paymentMethod="credit-card", this is the type of card provided to <Pay>, e.g. "amex" The value provided here will be one of the values provided in the validCardTypes attribute.

Parameter: ExpirationDate

Description:

If paymentMethod="credit-card", this is the expiration date provided to <Pay> in MMDD (two-digit month, two-digit date) format, e.g. "0522" Note: Expiration date is not PCI data, so it can be clearly visible.

Parameter: SecurityCode

Description:

If paymentMethod="credit-card", this is the security code provided to <Pay> with all digits redacted, e.g. "xxxx"

Parameter: PaymentCardPostalCode

Description:

If paymentMethod="credit-card", this is the postal code provided to <Pay>, e.g. "94109" Note: Postal Code is not PCI data, so it can be clearly visible.

Parameter: BankAccountNumber

Description:

If paymentMethod="ach-debit", this is the bank account number provided by the caller/consumer. Only the last two digits will be unredacted, e.g. "*******92".

Parameter: BankRoutingNumber

Description:

If paymentMethod="ach-debit", this is the bank routing number provided by the caller/consumer. <Pay> will return the full routing number provided by the caller/consumer, e.g. "121181976"

Parameter: BankAccountType

Description:

If paymentMethod="ach-debit", this is the bank account type provided by the caller/consumer. Possible values are:

  • "consumer-checking"
  • "consumer-savings"
  • "commercial-checking"

Back to attributes list

The paymentMethod attribute specifies whether to capture credit card or ACH payment information.

<Pay> by default captures credit card information (credit card number, expiration date, security code, and postal code).

To capture bank account information, set the value of the paymentMethod attribute to "ach-debit" as shown below.


_10
<Response>
_10
<Pay paymentConnector=”Your_Connector_Name” paymentMethod=”ach-debit” />
_10
</Response>

Once <Pay> successfully captures the information, it will securely send that information to the appropriate payment platform using the Pay Connector you've configured. Twilio returns the results from the payment platform via a webhook to the action URL specified in the <Pay> verb.

Back to attributes list

This attribute indicates the type of bank account information is being provided by the caller/consumer when capturing ACH payments. The bankAccountType attribute accepts either "consumer-checking","consumer-savings", "commercial-checking".

The speed at which the ACH transactions are processed depends on the bankAccountType and the underlying payment platform used.

Use <Gather> to capture the type of bank account and pass one of the allowed values to your bankAccountType attribute when using <Pay> to capture ACH payments.

Back to attributes list

The timeout attribute sets the limit in seconds that <Pay> will wait for the caller to press another digit before moving on to validate the digits captured.

For example, if timeout is 3, <Pay> will wait three seconds for the caller to press a key when capturing either credit card number, expiration date, security code or zip code. When accepting ACH payments, <Pay> will wait three seconds for the caller to press a key when capturing either bank account or routing numbers.

Back to attributes list

The maxAttempts attribute specifies number of times <Pay> should retry when collecting information.

The default is 1 which means <Pay> will retry once when a timeout or invalid value is received. For example, if a timeout is received when prompted for credit card number, <Pay> will reprompt one more time to enter credit card number before terminating. When <Pay> hits the maxAttempts value, <Pay> will terminate and TwiML execution will start with next verb after <Pay>.

Back to attributes list

The securityCode attribute takes true or false to let <Pay> know whether to prompt for security code.

When paymentMethod is credit-card, <Pay> by default will collect credit card number, expiration date, security code and zip code. Use <Pay securityCode=”false” /> to disable prompting for security code.

Back to attributes list

The postalCode attribute takes true or false to let <Pay> know whether to prompt for postal code (i.e zip code).

When paymentMethod is credit-card, <Pay> by default will collect credit card number, expiration date, security code and postal code (Zip code in US). Use <Pay postalCode=”false” /> to disable prompting for postal code. In addition, if you already have access to customer code instead of asking the payee for that information provide the postal code as value to the attribute. For example, if the billing postal code is 95105 then use <Pay postalCode=”94105” /> and <Pay> will pass 94105 to the Payment Gateway when processing the payment.

Back to attributes list

The minPostalCodeLength attribute takes a positive integer to let <Pay> validate the length of the postalCode attribute. Users are expected to enter at least these many digits.

Back to attributes list

The chargeAmount attribute takes an amount to charge against the credit card or bank account captured by <Pay>. The attribute takes a decimal value with no currency prefix and defaults to USD.

(warning)

Warning

If the chargeAmount attribute has a value greater than 0, the transaction will be a charge transaction.

If the chargeAmount attribute has a value of 0 or is omitted from the <Pay> verb, the transaction will be a tokenize transaction.

For example, use chargeAmount="20.45" to process payment in the amount of $20.45.

The default currency can be overriden with currency attribute.

Back to attributes list

The currency attribute is used to provide the currency of the amount attribute. The default value for currency is usd (US Dollars) and accepts all values accepted by the selected Pay Connector

Back to attributes list

The language attribute is used to provide the language that a customer hears when interacting with <Pay>.

For credit card payments, possible values are:

  • English:

    • en-AU
    • en-CA
    • en-GB
    • en-IN
    • en-US
  • Spanish:

    • es-ES
    • es-MX
  • French:

    • fr-CA
    • fr-FR
  • German:

    • de-DE
  • Italian:

    • it-IT

For ACH, possible values are:

  • English:

    • en-AU
    • en-CA
    • en-GB
    • en-IN
    • en-US

You can also further customize what the customer hears using the <Prompt> noun.

Back to attributes list

The paymentConnector attribute must contain the unique name corresponding to the Pay Connector installed in your Twilio Add-ons in the Twilio Console. Learn more on the Pay Connectors page.

For example, to process the transaction using Stripe use paymentConnector=Stripe_1, where Stripe_1 is the unique name specified when configuring the Pay Connector Add-on in the Marketplace.

If no paymentConnector is specified, <Pay> will use the Pay Connector on your account with the unique name "Default".

If you are using a Generic Pay Connector, you can use the <Parameter> noun with <Pay> to pass custom parameters to your payment processor.

(warning)

Warning

You must have an installed Pay Connector named "Default" or you must include the paymentConnector attribute.

Back to attributes list

The tokenType attribute takes either one-time or reusable as value.

If you're using a Stripe Pay Connector, payment-method is also a possible value.

(warning)

Warning

To tokenize a payment method, set chargeAmount = 0 or omit the chargeAmount attribute.

If <Pay> should generate a one-time token, use tokenType="one-time" and to generate a token for recurring payments use, tokenType="reusable".

Back to attributes list

The description attribute takes a value that describes more details regarding the payment.

This information is submitted along with the payment details to the Payment Gateway which are then posted on the transactions. For example, you can provide "Payment of $20.52 submitted from CallSid CAxxxxxx and Phone Number (xxx)-xxx-xxxx" to create a record to show which call created the payment.

Back to attributes list

The validCardTypes attributes takes credit card types separated by space that <Pay> should accept.

If the payee enters a card number that is outside of valid card types, <Pay> will generate an "invalid-card-type" error. For example, if validCardTypes=visa mastercard and payee enters an American Express card number, then <Pay> will generate an "invalid-card-type" error.

The default value of validCardTypes is “visa mastercard amex”.

A customer could provide any of the following card types separated by space:

Card TypeDescription
visaValid length: 13, 15, 19 digits. First digit must be a 4.
mastercardValid length: 16 digits. First digit must be 5 and second digit must be in the range 1 through 5 inclusive. The range is 510000 through 559999. First digit must be 2 and second digit must be in the range 2 through 7 inclusive. The range is 222100 through 272099.
amexValid length: 15 digits. First digit must be a 3 and second digit must be a 4 or 7.
maestroValid length: 12-19 digits. First digit must be either 5 or 6. If the first digit starts with 5, then second digit must be either 0, 6, 7 or 8.
discoverValid length: 16-19 digits. Must start with either 64, 65 or 6011
jcbValid length: 16 to 19 digits. First 4 digits must be in the range 3528 through 3589.
diners-clubDiners Club for USA & Canada Valid length: 16-19 digits. * The digits must begin with 300, 301, 302, 303, 304, 3095, 36, 38, or 39. * Note: If first two digits are 36, valid length is 14-19 digits.
enrouteValid length: 15 digits. First four digits must be 2014 or 2149.

Back to attributes list


Collect payment data during a voice call and charge a specific amount.

Collect payment for a specific amount

collect-payment-for-a-specific-amount page anchor

Charge a specific amount and collect payment.

Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
_10
const response = new VoiceResponse();
_10
response.say('Calling Twilio Pay');
_10
response.pay({
_10
chargeAmount: '20.45'
_10
});
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Say>Calling Twilio Pay</Say>
_10
<Pay chargeAmount="20.45"/>
_10
</Response>

Collect payment of a specific amount and specify a callback handler

Receive a callback on payment actions

receive-a-callback-on-payment-actions page anchor

Capture payment details, charge a specific amount, and set a callback URL.

Node.js
Python
C#
Java
PHP
Ruby

_11
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_11
_11
_11
const response = new VoiceResponse();
_11
response.say('Calling Twilio Pay');
_11
response.pay({
_11
chargeAmount: '20.45',
_11
action: 'https://enter-your-callback-function-url.twil.io/pay'
_11
});
_11
_11
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Say>Calling Twilio Pay</Say>
_10
<Pay chargeAmount="20.45"
_10
action="https://enter-your-callback-function-url.twil.io/pay"/>
_10
</Response>

Ensure when tokenizing a payment to set the chargeAmount to 0

Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
const response = new VoiceResponse();
_10
response.pay({tokenType: 'one-time', chargeAmount: '0'});
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Pay tokenType="one-time" chargeAmount="0" />
_10
</Response>


Rate this page: