This template shows you how to create an invoice in Stripe based on an SMS prompt (in this case DONATE <AMOUNT>
). Once the invoice is finalized, it will reply with a payment link.
It is divided up into two functions:
- create-invoice: handles the Twilio webhook event when an SMS is received and creates the invoice in Stripe.
- send-invoice-sms: handles the Stripe webhook event when the invoice is finalized and send the payment link SMS to the customer.
The best way to use the Function templates is through the Twilio CLI as described below. If you'd like to use the template without the Twilio CLI, check out our usage docs.
- Install the Twilio CLI
- Install the Twilio Serverless Toolkit
twilio plugins:install @twilio-labs/plugin-serverless
- Initiate a new project
twilio serverless:init twilio-stripe-payment-link --template=stripe-payment-link-sms && cd twilio-stripe-payment-link
This project requires some environment variables to be set. To keep your tokens and secrets secure, make sure to not commit the .env
file in git. When setting up the project with twilio serverless:init ...
the Twilio CLI will create a .gitignore
file that excludes .env
from the version history.
In your .env
file, set the following values:
Variable | Meaning | Required |
---|---|---|
ACCOUNT_SID |
Find in the console | Yes |
AUTH_TOKEN |
Find in the console | Yes |
STRIPE_SECRET_KEY |
Find in your Stripe Dashboard | Yes |
TWILIO_PHONE_NUMBER |
Needs to be configured | Maybe (depending on country) |
To run your function on Twilio, you need to:
- Enable
ACCOUNT_SID
andAUTH_TOKEN
in your functions configuration. - Add your
STRIPE_SECRET_KEY
to your environment variables. - Add
stripe
to your NPM package dependencies. You can find the lateststripe-node
version number on GitHub.
Deploy your function with the Twilio CLI. Note: you must run this from inside your project folder.
twilio serverless:deploy
After successful deployment the Twilio CLI will output two function URLs along the lines of:
- https://stripe-payment-link-sms-1234-dev.twil.io/create-invoice
- https://stripe-payment-link-sms-1234-dev.twil.io/send-invoice-sms
Set (1) as the messaging webhook for your phone number in the Twilio console:
Note: if you don't have an SMS enabled Twilio phone number yet, you will need to buy one in the console.
Set (2) as the webhook URL in your Stripe Dashboard (Note: you need to set up test and live mode webhooks separately):
Send DONATE 10
to your SMS enabled Twilio phone number
Create an invoice in the Stripe Dashbboard. Follow this guide or this video. Make sure to set a phone number when entering the customer details.