This function shows you how to programmatically send a payment receipt URL to your customer via SMS using a Stripe webhook event as trigger.
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-sample --template=stripe-sms-receipt && cd twilio-stripe-sample
- Install the dependencies for local testing
npm install
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 Dahsboard | Yes |
TWILIO_PHONE_NUMBER |
Needs to be configured | Maybe (depending on country) |
Using the Stripe CLI we can execute predefined fixtures to run a sequence of requests (in this case create a customer and then create a payment), and also we can forward Stripe webhook events to our local server running the Twilio function.
- Install the Stripe CLI & login with your Stripe account.
- Replace
+12025551212
with your phone number for testing in the assets/stripe_fixtures/create_customer_and_payment.private.json file. Be sure to follow the E.164 format. - Start the server with the Twilio CLI:
twilio serverless:start
- In a separate terminal window, start the webhook event forwarding:
stripe listen --forward-to http://localhost:3000/send-sms-receipt
- In a third terminal window, trigger the Stripe fixtures:
stripe fixtures assets/stripe_fixtures/create_customer_and_payment.private.json
- Monitor the Twilio CLI logs for errors, otherwise monitor your phone for incoming messages 🎉
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 deploying, copy the deployment URL with the webhook path (something along the lines of: https://stripe-sms-receipt-1234-dev.twil.io/send-sms-receipt) and create a live webhook endpoint in your Stripe dashboard.