Email customers about failed payments and log them in Sheets

By General Input

When a Stripe charge fails, send the customer a retry email matched to the reason their card was declined and log the row for finance.

Integrations

  • Stripe
  • AgentMail
  • Google Sheets

Type

Deterministic Code

Categories

  • Finance
  • Operations

Build me a code workflow that runs failed-payment dunning end to end whenever Stripe reports a payment failure.

Trigger: a Stripe webhook on the charge.failed event. Also accept invoice.payment_failed for subscription invoices so both one-off charges and recurring invoices flow through the same path.

When the webhook fires, pull these fields out of the Stripe event payload: customer email, customer name, failed amount and currency, failure code (for example card_declined, insufficient_funds, expired_card), the human-readable failure message, and the Stripe customer portal or hosted invoice URL so the customer has a place to retry.

Step 1: call AgentMail Send Email from a billing@ inbox to the customer. Pick subject and body from a small lookup table keyed off the Stripe failure code. card_declined gets one short template (your bank declined the charge, here is the retry link), insufficient_funds gets another (the charge could not go through due to insufficient funds), expired_card gets another (your card on file appears to have expired, update it here), and any unrecognized code falls back to a generic retry template. Every template includes the customer's first name, the amount and currency formatted for humans, and a clear retry CTA pointing at the Stripe link from the event. Keep the templates short and avoid heavy personalization, this is intentionally a code workflow not an agent.

Step 2: call Google Sheets Append Values to append one row to a Failed Payments sheet (let me pick the spreadsheet and tab during setup). Columns in order: timestamp (ISO 8601 from the Stripe event created field), customer email, customer name, amount, currency, failure code, failure message, retry link, and the AgentMail message_id returned by Send Email so finance can reconcile which email was sent for which failure.

Every step is deterministic: one webhook in, one templated email out, one row appended. No judgement calls, no LLM in the loop, no extra branches. If the AgentMail send fails, still append the audit row but leave the message_id column blank and put the AgentMail error in the failure_message column so finance can see what happened.

Related prompts

Explore more prompts
Call overdue Xero customers with an AI collections agentLocal listing health board for every location you manageLet support send one-off Loops emails without an engineerStop cold emails to anyone with a live deal in PipedriveCustomer lifecycle inspector for Loops, Stripe and PostHogiMessage campaign console with pre-flight checks and delivery boardLinkedIn Ads budget pacing dashboard for every client accountFront desk appointment confirmation board for the next 3 daysGive your team Looker numbers without buying more seatsBuild audience segments from product usage and push to Loops