Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Latest commit

 

History

History
 
 

stripe-sms-receipt

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Stripe SMS Receipt Webhook Handler

This function shows you how to programmatically send a payment receipt URL to your customer via SMS using a Stripe webhook event as trigger.

Stripe SMS receipt demo gif

How to use the template

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.

Create a new project with this template

  1. Install the Twilio CLI
  2. Install the Twilio Serverless Toolkit
twilio plugins:install @twilio-labs/plugin-serverless
  1. Initiate a new project
twilio serverless:init twilio-stripe-sample --template=stripe-sms-receipt && cd twilio-stripe-sample
  1. Install the dependencies for local testing
npm install

Testing locally

Environment variables

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

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.

  1. Install the Stripe CLI & login with your Stripe account.
  2. 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.
  3. Start the server with the Twilio CLI:
twilio serverless:start
  1. In a separate terminal window, start the webhook event forwarding:
stripe listen --forward-to http://localhost:3000/send-sms-receipt
  1. In a third terminal window, trigger the Stripe fixtures:
stripe fixtures assets/stripe_fixtures/create_customer_and_payment.private.json
  1. Monitor the Twilio CLI logs for errors, otherwise monitor your phone for incoming messages 🎉

Deploying

To run your function on Twilio, you need to:

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.