Send Stripe failed payment and receipt emails
Stripe tells you when a payment succeeds or fails. Here are three ways to turn those events into emails, when each one fits, and how to set up the Loops version.
Stripe tells you when a payment succeeds or fails. You can let Stripe send the email, send it from a Loops workflow, or send a transactional email from your own server. The right choice depends on whether every customer must get it.
Stripe can send receipts and failed payment notices itself, from your Stripe Dashboard settings. It is the fastest start. It sends a notice after each failed payment and uses your Stripe branding, but you can’t change the copy or add your own follow-ups.
Connect Stripe to Loops, then send branded emails from workflows triggered by Stripe events such as invoice.payment_failed and invoice.paid. No code is needed. Workflow emails respect unsubscribes, so a customer who unsubscribed from your marketing email will not get them.
Your server receives the Stripe event and sends a transactional email through Loops. Transactional email reaches unsubscribed contacts too, so use this route for receipts and payment notices that every customer must get. You write and host the webhook handler.
In Loops, open Settings, then Stripe, and click Connect to Stripe.
Follow Stripe’s install steps. You return to Loops when it is done.
Turn on invoice.payment_failed, plus invoice.paid if you want receipts, then click Save.
Loops matches each Stripe customer to a contact by email address and creates the contact if it is new.
Create a workflow from the Stripe - Payment Failed template.
Set the trigger to Event received, then Stripe, then invoice.payment_failed.
Write the email: which payment failed, where the customer updates their card, and when you will try again.
Send a test event with the Stripe CLI. Add an email address, because Loops ignores Stripe events without one.
stripe trigger invoice.payment_failed --add customer:email=YOUR_EMAILThen open the Events page in Loops to confirm the event arrived and the workflow ran.
A single failed payment email is easy to miss. Send a short sequence over your retry period instead: one when the payment fails, a reminder a few days later, and a final notice before the subscription ends. Store the payment state in a contact property such as subscriptionStatus, set it to Failed through the API when a payment fails and back to Paying on invoice.paid, and trigger a workflow when it changes to Failed. Add an audience filter set to All following nodes so customers who pay leave the sequence. The lifecycle email recipe shows this setup.
import Stripe from "stripe";
import { LoopsClient, APIError, RateLimitExceededError } from "loops";
// Your own record of handled Stripe event IDs, such as a database table.
import { wasProcessed, markProcessed } from "./processed-events";
function env(name: string): string {
const value = process.env[name];
if (!value) throw new Error(`${name} is not set`);
return value;
}
const stripe = new Stripe(env("STRIPE_SECRET_KEY"));
const loops = new LoopsClient(env("LOOPS_API_KEY"));
const webhookSecret = env("STRIPE_WEBHOOK_SECRET");
const templates = {
"invoice.payment_failed": env("LOOPS_PAYMENT_FAILED_ID"),
"invoice.paid": env("LOOPS_RECEIPT_ID"),
};
// Call this from the route that receives Stripe's raw request body.
export async function handleStripeEvent(rawBody: string | Buffer, signature: string) {
const event = stripe.webhooks.constructEvent(rawBody, signature, webhookSecret);
if (event.type !== "invoice.payment_failed" && event.type !== "invoice.paid") return;
if (await wasProcessed(event.id)) return;
const invoice = event.data.object;
if (invoice.customer_email) {
const cents = event.type === "invoice.paid" ? invoice.amount_paid : invoice.amount_due;
await send({
transactionalId: templates[event.type],
email: invoice.customer_email,
dataVariables: {
amount: (cents / 100).toFixed(2),
currency: invoice.currency,
invoiceUrl: invoice.hosted_invoice_url ?? "",
},
headers: { "Idempotency-Key": event.id },
});
}
await markProcessed(event.id);
}
type SendParams = Parameters<LoopsClient["sendTransactionalEmail"]>[0];
async function send(params: SendParams, attempt = 0): Promise<void> {
try {
await loops.sendTransactionalEmail(params);
} catch (err) {
if (err instanceof APIError && err.statusCode === 409) return; // already sent
if (err instanceof RateLimitExceededError && attempt < 3) {
await new Promise((r) => setTimeout(r, 500 * 2 ** attempt));
return send(params, attempt + 1);
}
throw err;
}
}Stripe can deliver the same event more than once, and it retries a failed delivery for up to three days. The event ID is the idempotency key, so a 409 means the email already went out. Loops only remembers an Idempotency-Key for 24 hours, though, so a retry after that would send the email again. Store each Stripe event ID you handle and skip the ones you have already seen. A 429 throws RateLimitExceededError, not APIError, so the handler waits and retries with the same key. amount_due and amount_paid are in the smallest currency unit, so dividing by 100 only works for two-decimal currencies.
Use one route per event. If the Loops Stripe app also has invoice.payment_failed or invoice.paid turned on and a workflow sends from it, the customer gets two emails: one from the workflow and one from this handler. Sending your own payment events to Loops while the app is connected can also trigger a workflow twice, for example when the app’s Trigger an additional custom event setting uses the same event name as your code.
A failed payment email should give the amount, which payment method failed, a link to update it, and when you will try again. Keep the tone plain and helpful. A receipt should give the amount, the date, the plan or items, and a link to the invoice.
Can Loops send Stripe receipts?
Yes. Turn on invoice.paid (or checkout.session.completed for Stripe Checkout) in the Loops Stripe settings and send the email from a workflow, or send a transactional email from your own webhook handler.
Do failed payment emails reach customers who unsubscribed?
Not if they come from a workflow, because workflow emails respect unsubscribes. Send them as transactional email from your server if every customer must get them.
Which Stripe events can trigger emails in Loops?
Payment events such as invoice.payment_failed, invoice.paid, and checkout.session.completed, plus customer, subscription, dispute, and quote events. The Stripe integration docs have the full list.
What's next
More on sending transactional email.