Email customers about failed payments and log them in Sheets

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.

Deterministic Code
StripeAgentMailGoogle SheetsFinanceOperationsEmail AutomationNotifications & AlertsData Sync
PromptCreate

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.

What does this prompt do?

  • Watches Stripe for failed payments and reacts the moment one happens.
  • Sends the customer a clear retry email written for the specific failure reason, like a declined card versus an expired card.
  • Appends a row to your Failed Payments sheet with the timestamp, customer, amount, failure code, and the email message id so finance has a full audit trail.
  • Runs deterministically so every failure gets the same treatment and nothing slips through the cracks.

What do I need to use this?

  • A Stripe account with permission to manage webhooks.
  • An AgentMail inbox you can send from, such as billing@yourcompany.
  • A Google account with edit access to a Failed Payments spreadsheet.

How can I customize it?

  • Swap in your own email copy for each failure reason, like card declined, insufficient funds, or expired card.
  • Change which sender address the email goes from, or point the customer to a different retry link.
  • Pick a different sheet, tab, or column order for the audit log, or add columns like invoice id or plan name.

FAQs

Does this handle subscription payment failures too?
Yes. You can listen for both one-off charge failures and recurring invoice payment failures, so trials, subscriptions, and one-time charges all flow through the same retry email and audit log.
Can I personalize the retry email per customer?
The email picks copy based on the failure reason, like declined card versus expired card, and fills in the customer name, amount, and retry link. For heavier personalization, like tier-based offers or sales follow-ups, an agent variant of this workflow is a better fit.
Will this send a duplicate email if Stripe retries the same charge?
Stripe sends one event per failure, so each failure produces one email and one row. If you want to suppress repeat emails to the same customer within a window, that is an easy tweak in the prompt.
Do I need to write any code to set this up?
No. You connect Stripe, AgentMail, and Google Sheets once, pick the inbox and the sheet, and the workflow takes care of the rest.
What does finance actually see in the sheet?
Each row captures the time the payment failed, who the customer is, how much was attempted, the failure reason from Stripe, and the id of the email you sent them so it is easy to reconcile later.

Related templates

Call overdue Xero customers with an AI collections agent

Every weekday at 10am, an AI voice agent phones your most overdue Xero accounts, logs what each customer promised, and reports back to finance in Slack.

LiveKit
Xero
Deepgram
+2
Agentic Task
Local listing health board for every location you manage

See every Google Maps listing you manage on one screen, ranked worst first, with an audit button that writes the fix list for you.

Local Business Data
Google Sheets
App
Let support send one-off Loops emails without an engineer

Your team picks a template, finds the customer, checks they are safe to email, then sends it, with every send logged where the whole team can see it.

Loops.so
Stripe
Google Sheets
App
Stop cold emails to anyone with a live deal in Pipedrive

Every night at 2am, pull the contacts on your open and won deals and block them from your cold outreach before the next send goes out.

Mailshake
Pipedrive
Google Sheets
Deterministic Code
Customer lifecycle inspector for Loops, Stripe and PostHog

Open one screen each morning to see a paying customer's billing, product activity and email history, then send the right next email without leaving it.

Loops.so
Stripe
PostHog
App
iMessage campaign console with pre-flight checks and delivery board

Build every text campaign in one screen: check who is actually reachable, see a realistic send plan, then watch delivery land row by row.

LoopMessage
Google Sheets
HubSpot
App

Stop chasing failed payments by hand.

Connect Stripe, AgentMail, and Google Sheets once, and every failed charge gets a tailored retry email and a clean audit row.