https://www.useplunk.com/blog/sending-emails-on-stripe-events []Plunk LogoPlunk LogoPlunk PlaygroundIntegrationsBlogDocs Sign inCreate account Open main menu Published on Wed Jul 20 2022 How to send emails on Stripe events Customers come and some customers go. No founder likes churned users but every user that cancels their subscription is an amazing opportunity to learn and improve your business. In this post you will learn how to automatically follow up with users that cancel their plan in Stripe through Plunk's behavioural emails. How to send emails on Stripe eventsHow to send emails on Stripe events Getting the setup right In this setup we will make use of two services, Stripe and Plunk. This setup will work with any payment processor though! As long as your payment processor supports webhooks then you will be able to follow along in this tutorial. Initial Plunk setup Head over to the Plunk dashboard and create a new project. You can skip the onboarding for now, I'll personally guide you through it. Head over to your project settings and write down your API key, you will need it in the next steps. Creating and catching a webhook A webhook is a callback a service makes to another one on certain events. Stripe can also send events over webhooks, you just need to configure it! On the Stripe dashboard, you will find webhooks under the developer settings. An image showing the webhook creation screen on the Stripe dashboard An image showing the webhook creation screen on the Stripe dashboard As you can see, Stripe even shows us the code that we will need to add to set up a web application that can catch the events. Pretty neat! Configuring an endpoint Firstly, we need to let Stripe know where they should send the event. If you have an application running somewhere then you can add the URL to an endpoint of your application there. If you don't yet have an application running and will only be testing it out locally then I suggest you to enter a fictive endpoint and to use the Stripe CLI to catch the events and send them to localhost! Selecting events In our example, we want to send an email every time a user cancels their subscription. For this we will listen to the customer.subscription.deleted event. There are of course many other options, almost any action in Stripe can be turned into a webhook event! Adding the code to our application I am a big fan of JavaScript so I will be writing my API endpoint in express but you can use any language you like. As long as you have some way to send an API call to Plunk, you should be fine! Stripe's code fragment already let me know that I will need to install Express and Stripe, but we will be adding an extra package to it! To send events to Plunk, we will make use of the Plunk Node.js wrapper. You can install all packages with the following command npm i express stripe @plunk/node If you are following along and using Node.js then you can copy this application to test it out! All you need to add is your Stripe API key, your Plunk API key and your Stripe Webhook secret. 1const express = require('express'); 2const Stripe = require('stripe'); 3const Plunk = require('@plunk/node'); 4 5const app = express(); 6 7// Stripe 8const stripe = new Stripe.default('STRIPE_API_KEY'); 9const endpointSecret = "STRIPE_WEBHOOK_SECRET"; 10 11// Plunk 12const plunk = new Plunk.default('PLUNK_API_KEY'); 13 14app.post('/webhook', express.raw({ type: 'application/json' }), async (request, response) => { 15 const sig = request.headers['stripe-signature']; 16 17 let event; 18 19 try { 20 event = Stripe.webhooks.constructEvent(request.body, sig, endpointSecret); 21 } catch (err) { 22 response.status(400).send(`Webhook Error: ${err.message}`); 23 return; 24 } 25 26 switch (event.type) { 27 case 'customer.subscription.deleted': 28 const subscription = event.data.object; 29 30 // Fetch the complete customer from Stripe 31 const customer = await stripe.customers.retrieve(subscription.customer); 32 33 // Send the event to Plunk 34 await plunk.events.publish({ 35 event: 'subscription-cancelled', 36 email: customer.email 37 }); 38 39 break; 40 } 41 42 response.send(); 43}); 44 45app.listen(4242, () => console.log('Running on port 4242')); Completing Plunk setup Trigger the customer.subscription.deleted event through the Stripe CLI and if everything went alright then you should see that the event now shows up in your Plunk dashboard. Congratulations, the most difficult part is now complete! All that is left to do now is to create an email design and setting up a Plunk action. Once we have done that, users will automatically start to receive emails when they cancel their subscription. Creating a template Head over to the templates section of the Plunk dashboard and design a new template. Give it a great subject and save it. An image showing an email designed in Plunk's drag-and-drop editorAn image showing an email designed in Plunk's drag-and-drop editor Creating an action We now have all the components we need to create an action. Select our Stripe event and the template we have just created. Optionally, configure a delay and select if this action should run once or multiple times. Other use cases with Stripe While sending emails when a user cancels their plan is a great start, we can imagine some other use cases as well! * Sending a thank you email when they initially start a subscription, customer.subscription.created * Sending an email to let them know a discount has been attached to their account, customer.discount.created Conclusion If you have reached this part of the tutorial, then you have succesfully added Plunk's behavioural email marketing to an application and linked it up to your Stripe account. This is already a very big achievement but the possibilities with Plunk are endless. You can catch and trigger Plunk events from nearly anywhere, making it an incredibely powerful tool to add to your SaaS stack. I can not wait to see what other creative solutions you can imagine! --------------------------------------------------------------------- Want to share this article with the world? Share on Twitter Share on LinkedIn