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

How to capture payment during a voice call (Generic Pay Connector)


In this tutorial, you will create an interactive voice response (IVR) that collects payment details from a customer and passes the information in a PCI-compliant manner to your payment processor of choice. This tutorial uses a TwiML Bin with the <Pay> verb and a Generic Pay Connector.

In this tutorial, you will learn how to:

  1. Enable PCI Mode on your account
  2. Install and configure a Generic Pay Connector
  3. Create a TwiML Bin with <Pay>
  4. Create a TwiML Bin to be executed after <Pay>
  5. Purchase a Twilio phone number
  6. Configure a Twilio number to use a TwiML Bin for incoming calls
  7. Create a mock payment processor using Twilio Serverless Functions
  8. Test capturing credit card details
(error)

Danger

Generic Pay Connectors only handle sending payment details to a payment processor. It is up to the payment processor of your choosing to process a transaction and return a response to Twilio in the required format.

In this tutorial, we will create a mock payment processor that is not PCI compliant and not production-ready.

Depending on your real-life use case, you may choose to create your own PCI-compliant payment processor application or use another, existing payment processor. If you choose an existing payment processor, they will need to write custom code to interface with Twilio's Generic Pay Connectors.

Twilio also offers branded Pay Connectors that are already designed to integrate with the following payment processors/gateways: Stripe, CardConnect, Base Commerce, Chase Paymentech, Braintree, and Adyen.


Prerequisites

prerequisites page anchor

Before you continue with this tutorial, you should familiarize yourself with the <Pay> and Generic Pay Connector docs.


In order to use any of Twilio's Pay Connectors, you must enable PCI mode on your Twilio account. This ensures that Twilio will capture the payment details from your customers in a PCI-compliant manner and redact any sensitive PCI information from any call logs.

(warning)

Warning

PCI Mode will redact sensitive information from ALL of your account's logs. Turning on PCI Mode cannot be undone.

If you want to avoid redacting information from all logs on an account, consider creating another Twilio account, enable PCI Mode on that account, and use that account when collecting payments with <Pay> or Agent Assist/Payment API.

To enable PCI Mode, complete the following steps:

  1. Navigate to the Twilio Voice Settings(link takes you to an external page) in the Twilio Console. (In the left navigation pane, click on Voice > Settings > General .)
  2. Click on the Enable PCI Mode button.

    Enable PCI Mode.HTTPS POST requests from Twilio. We will create a Twilio Function endpoint to handle these POST requests later in this tutorial.

    Generic Pay Connector Tutorial Configuration.

    _10
    <?xml version="1.0" encoding="UTF-8"?>
    _10
    <Response>
    _10
    <Pay paymentConnector="My_Pay_Connector" action="" chargeAmount="10.00" />
    _10
    </Response>

    Notice how the <Pay> verb has three attributes: paymentConnector, action, and chargeAmount.
    The paymentConnector attribute's value is the Pay Connector you want to use with this <Pay> verb. In this case, it's the Generic Pay Connector you just created called "My_Pay_Connector".
    The action attribute is left blank for now. This attribute will be a URL. Upon the completion of a <Pay> transaction, Twilio will send a webhook to your action URL to get a new set of TwiML instructions. We'll create another TwiML Bin with a new set of TwiML instructions for Twilio to execute after a completed <Pay> transaction and put that new TwiML Bin's URL in this action attribute.
    The chargeAmount attribute is set to 10.00, representing a charge of $10.00. (If you wanted to create a tokenize transaction, you would set this attribute value to 0 or omit the attribute altogether.)

  3. Click the Save button at the bottom of the page.

4. Create a TwiML Bin to be executed after <Pay>

4-create-a-twiml-bin-to-be-executed-after-pay page anchor
  1. Navigate to the TwiML Bin page in your Twilio Console by clicking My TwiML Bins at the top of the page of the TwiML Bin you created in the previous section. (You can also search for "TwiML Bins" at the top of the Twilio Console.)
  2. Click on the blue + button to create a new TwiML Bin.
  3. Give this TwiML Bin a FRIENDLY NAME of " Generic Pay Tutorial Action"
  4. In the TWIML input box, paste the follwing TwiML:


    _10
    <?xml version="1.0" encoding="UTF-8"?>
    _10
    <Response>
    _10
    <Say>Thank you for entering your payment information.</Say>
    _10
    {{#PaymentError}}
    _10
    <Say>Sorry. There was an error: {{PaymentError}}</Say>
    _10
    {{/PaymentError}}
    _10
    <Hangup />
    _10
    </Response>

    When Twilio executes this TwiML Bin, a caller will hear "Thank you for entering your payment information." and the call will end.

    If Twilio's webhook request to this TwiML Bin contains a PaymentError property (meaning that an error occurred), the caller will also hear "Sorry. There was an error." along with a description of the error before the call ends. (This TwiML Bin uses a conditional content template. Read more about TwiML Templates on the "How to use templates with TwiML Bins" support page(link takes you to an external page).)

  5. Click on the blue Save button at the bottom of the screen. (You'll be asked to confirm due to "invalid" TwiML syntax. It is valid, so ignore this bug.)
  6. Find the URL field at the top of the page for this TwiML Bin and copy the URL by clicking on the Copy button (on the right side of the URL field). You will set this URL as the action attribute for the other TwiML Bin you created, so that Twilio will execute this set of TwiML instructions after the <Pay> verb has completed in your other TwiML Bin.
  7. Click on My TwiML bins at the top of the page.
  8. Click on the Generic Pay Connector Tutorial <Pay> TwiML Bin.
  9. Paste the copied URL between the action attribute's quotation marks. Your TwiML Bin should look something like this:


    _10
    <?xml version="1.0" encoding="UTF-8"?>
    _10
    <Response>
    _10
    <Pay paymentConnector="My_Pay_Connector" action="https://handler.twilio.com/twiml/EHXXXXXXXXXXXXXXXXXXXXXXXXX" chargeAmount="10.00" />
    _10
    </Response>

  10. Click the blue Save button at the bottom of the page.

5. Purchase a Twilio phone number

5-purchase-a-twilio-phone-number page anchor

If you don't already own a Twilio Voice-enabled phone number, complete the following steps:

  1. In the Twilio Console, navigate to the Buy a Number(link takes you to an external page) page.
  2. Make sure the "Voice" checkbox is checked and click the Search button.

    Search for a Number - Click on the 'Search' button.Rate this page:

    Need some help?

    Terms of service

    Copyright © 2024 Twilio Inc.