Blog

Trigger an AI Agent When a Stripe Payment Fails

Tutorial

ยท

Quick answer: To trigger an AI agent when a Stripe payment fails, add the Stripe Trigger to a BuildShip workflow, add your Stripe secret key as an Integration Key, and subscribe to invoice.payment_failed (and customer.subscription.past_due if you also want late renewals). Click Connect. BuildShip creates the webhook in your Stripe account, so there is no endpoint to paste by hand. When a charge fails, the agent gets the event payload, looks up usage and account health, writes a recovery recommendation to the customer record and can send a tailored dunning email. Every run has step-level logs, and you can edit the JavaScript or TypeScript on any node.

Trigger an AI agent when a Stripe payment fails: a failed charge hits the Stripe trigger, then a BuildShip AI agent writes a recovery plan

A failed payment is a small event with a short window. The card declines at 09:14. If your process is a daily export into a spreadsheet, the customer has already had a day of silent access, or a day of a broken account, before anyone notices. ChatGPT can draft a polite dunning email if you paste in the invoice, but it cannot receive the Stripe webhook, read your usage data, write a note on the customer record or send the email from your domain.

That is the job of an AI agent with a backend. The Stripe event is the trigger. The agent does the judgement. BuildShip keeps a log of both.

Why a payment-failed agent beats a spreadsheet

Stripe already tells you, in real time, that a payment failed. The Stripe Trigger is how that event starts a BuildShip workflow, instead of sitting in the Dashboard until someone opens it.

A general chat agent cannot do this on its own:

  • It has no public URL that Stripe can call.

  • It cannot hold your Stripe secret, query usage in your database or write a recovery plan back to a customer record.

  • It leaves no step-level log of what it saw or what it sent.

The agent is useful because the next step is not a fixed template. A first failure on a new card is different from the third failure on an account that used the product heavily last week. The agent can look at usage, plan, country and history, then choose a tone and a next step: a gentle retry reminder, a request to update the card, or a flag for a human if the account looks at risk of churn.

This is not lead scoring, and it is not a support inbox. It is dunning with context.

What the Stripe Trigger actually does

The Stripe Trigger connects your BuildShip workflow to your Stripe account and starts a run when the webhook events you subscribe to arrive. It is built for activities on charges, customers, invoices and subscriptions, and it sends you the event payload Stripe would send to any webhook endpoint.

You need a Stripe secret key from the Developers section of the Stripe Dashboard. In BuildShip, click the Integration Key input and add that key to the project. Once it is saved, the same key can be used by Stripe nodes and triggers across the project. Use a test-mode key while you build, then switch to a live key when you are ready.

The trigger has two inputs:

  • Integration Key. The Stripe secret key you just added.

  • Stripe Events. A multi-select list of events to subscribe to. The default is all events. You can also add a custom event name if you need one that is not in the list. Stripeโ€™s own catalogue of event types is in the Events API.

For this agent, do not leave it on all events. Subscribe to the ones you will act on:

  • invoice.payment_failed, when an invoice charge fails.

  • customer.subscription.past_due, when a subscription has missed its payment and moved into the past-due state.

That second event catches renewals that have slipped, even if you also handle the invoice event. You can add invoice.paid later if you want a sibling workflow that clears the recovery flag once the customer pays.

When you click Connect, BuildShip creates the webhook in your Stripe project. Open Stripe Webhooks and you should see an endpoint that matches the URL in the post-connection preview, with the same event subscriptions.

The Stripe Trigger is event-based, so there is no response body to map back to Stripe. You do map the incoming payload to your workflow inputs, because Stripe sends a different shape for each event type.

A concrete case: recover a failed renewal

Imagine a B2B analytics product billed monthly on Stripe. A renewal fails on Friday night because a company card expired. The agent should not wait until Monday, and it should not send the same email it would send to a trial user whose first payment never went through.

Here is the path I would build:

  1. Stripe sends invoice.payment_failed.

  2. The Stripe Trigger starts the workflow with the event payload.

  3. A Branch or script node confirms the event type and drops anything you did not mean to handle, just in case a test event sneaks through.

  4. The workflow loads the customer and recent usage from your database, using the Stripe customer id on the event.

  5. An AI step writes a short recovery recommendation: what happened, how engaged the account is, and what to say.

  6. That recommendation is written to the customer record, with a status such as recovery_needed.

  7. A tailored email goes out from your domain, or, if the account is high-value and already on a second failure, a Slack message goes to revenue operations instead of an automatic email.

The model does the judgement. The writes, the email and the logs are what a chat window cannot do.

A failed Stripe payment starts a BuildShip Stripe Trigger, an AI agent looks up usage and writes a recovery plan, then a tailored dunning email is sent

The agent only runs when Stripe reports a failure. It uses your usage data, writes a plan to the customer record and can email the customer or notify your team.

Set up the trigger in five steps

  1. Create a workflow in BuildShip, or open an existing one. You can build the logic first and connect the trigger when you are ready.

  2. Open the Connect tab, search for Stripe Trigger, and add it.

  3. Add your Stripe secret key as an Integration Key. Start with a test-mode key from the Stripe Dashboard.

  4. In Stripe Events, clear all events and add invoice.payment_failed. Add customer.subscription.past_due if you want late subscriptions as well.

  5. Click Connect. Check the Stripe Webhooks page to confirm the endpoint and the subscribed events are there.

Then map the payload to workflow inputs. The easiest way is Get Data. Trigger a failed payment in Stripe test mode (Stripe documents test cards for declined charges in its testing guide), then click Get Data on the trigger preview. BuildShip fetches the latest request, shows the payload as a tree, and lets you add the fields you need to the workflow input schema.

You will usually want:

  • type, so you can branch on the event.

  • data.object.customer, the Stripe customer id.

  • data.object.id, the invoice or subscription id.

  • Amount, currency, attempt count and the failure message, which live on the invoice or charge object depending on the event.

Use the context menu (x) and dot notation to reach those fields from Trigger Data.

Subscribe the Stripe Trigger to invoice.payment_failed and customer.subscription.past_due rather than all events, so the agent only runs when a payment actually needs attention

Leave all events for exploration. In production, subscribe only to the payment-failure events your agent is built to handle.

Build the recovery agent

Once the trigger is connected, the workflow is an ordinary BuildShip canvas. A useful first version looks like this.

Filter first. Add a Branch node that continues only if type is invoice.payment_failed or customer.subscription.past_due. Events you are not ready to handle should stop here, cheaply, with a log line.

Load context. Use the Stripe customer id to fetch the customer from your own database, and, if you store it, recent usage: seats, API calls, last active date, plan name, previous failures. If the customer is not in your database yet, fetch them from Stripe with a Stripe node and write a minimal record. The agent needs facts, not just the invoice.

Write the recommendation. Add an AI node (keyless nodes run on your BuildShip credits, or you can bring your own provider key). Give it a tight instruction, such as:

> You are helping revenue operations recover a failed subscription payment. Using the invoice event and the account context, write a short recovery plan with: a one-line diagnosis, how engaged this account looks, a recommended next step (retry reminder, card update request, or human follow-up), and a draft email in a calm, specific tone. Do not invent usage numbers that are not in the context.

Pass the event fields and the database record as the prompt. Ask for structured output if you want to store the diagnosis and the email separately.

Write it back. Save the plan and a recovery_needed status on the customer record, in your database or as Stripe metadata if that is where you keep operational flags. This is the step a chat agent cannot take.

Notify. Send the draft email through your email node, or post the plan to Slack if the attempt count is already high or the account is on a plan you always handle by hand. A Branch node can make that split with a simple rule, so the model is not deciding whether to bother a person.

If you would rather not wire the canvas by hand, click Build with AI to open Compass and describe the flow in plain language. It builds on your actual canvas, so you can still test and edit every step.

For failures that need a longer back-and-forth, a workflow can hand off to an AI agent built with Agent Max, which can call your other workflows as tools. The trigger is the same. The judgement can be a single AI node or a fuller agent, depending on how much context you want it to gather.

A general chat agent can draft a dunning email only if you paste the invoice in. A BuildShip agent receives the Stripe webhook, reads usage, writes the customer record and sends the email, with a log of every step

The model is not the missing piece. The webhook, the database writes and the logs are.

Test it, ship it and watch a failure all the way through

  1. In Stripe test mode, trigger a payment failure. Use Stripeโ€™s test cards for declined charges, or the Stripe Dashboard and CLI to send a test webhook for invoice.payment_failed.

  2. Click Get Data in BuildShip if you still need to map fields, then run a test from the test panel with that payload.

  3. Click Ship to make the workflow active.

  4. Trigger one more test failure and watch the run appear.

Open the logging panel at the bottom of the workspace to see what happened. You can switch from the Workflow view to the Node view to read the logs for a single step, and test runs show live logs as they happen. Production logs usually show up within about 45 seconds, and the refresh icon loads them straight away. To add your own messages, use the Log Message to Console node, or call logging.log inside any nodeโ€™s code.

If a node fails, BuildShip can retry it. See the node failure docs for how retries and error handling work, and the logging docs for how to read a run after the fact. Use version history before you change a live dunning flow, so you can roll back if a prompt change gets too chatty or too sharp.

When you move to live mode, switch the Integration Key to your live Stripe secret and confirm the live webhook in the Stripe Dashboard. Keep test and live as separate, obvious setups. You do not want a test agent emailing real customers.

Built for teams that run this in production

Failed-payment workflows tend to stay in production, so a few things matter beyond the first test:

  • Full code access. Open any node to read or change its JavaScript or TypeScript. Nothing is a black box.

  • Team access control. Role-based access lets you decide who can view a workflow, who can edit it and who can ship it.

  • Your own Google Cloud. Workflows run on Google Cloud, and you can run them in your own Google Cloud project as an enterprise option.

  • Logs you can hand to finance. When someone asks why a customer received an email, you can open the run and see the event, the recommendation and the message that went out.

If you already store Stripe subscriptions in your own database, you can reuse that data as context for this agent. We have an older walkthrough on storing Stripe subscription data in Supabase. This post is the other direction: Stripe tells you something happened, and the agent acts.

For the broader case of waking an agent from a payment, a row change or an email, rather than from a timer, see Why AI Agents Need Listeners, Not Cron Jobs.

FAQ

Can I trigger an AI agent from a Stripe webhook?

Yes. Add the Stripe Trigger to a BuildShip workflow, connect your Stripe secret key, subscribe to the events you care about and click Connect. BuildShip creates the webhook in Stripe. An AI node or an Agent Max agent in the same workflow then runs on each matching event, with the payload as input.

Which Stripe events should I subscribe to for failed payments?

Start with invoice.payment_failed. Add customer.subscription.past_due if you also want to catch subscriptions that have moved into the past-due state. Avoid all events in production, so the agent only runs when a payment actually needs attention.

Does BuildShip create the Stripe webhook for me?

Yes. After you click Connect, a webhook should appear in the Stripe Webhooks tab with the same endpoint as the post-connection preview and the same event subscriptions. You can confirm it there.

How do I test a failed payment without charging a real card?

Use Stripe test mode and a test secret key in the Integration Key. Stripeโ€™s testing guide lists test cards that decline, and you can also send a test webhook from the Dashboard or the Stripe CLI. Use Get Data in BuildShip to map the test payload to your workflow inputs.

Can the agent update Stripe or my own database?

Yes. Use Stripe nodes to write metadata or retrieve objects, and use your database nodes to update the customer record. The trigger itself does not write anything back to Stripe. Your workflow does that in later steps, which is what shows up in the logs.

Is this the same as qualifying leads from Stripe signups?

No. That is a different workflow, with a different event and a different outcome. This guide is about payment failures: looking up usage, writing a recovery plan and following up. If you want signup automation, we have a separate post on qualifying leads from Stripe signups into Google Sheets.

How much does it cost to try?

BuildShip plans start with a 7-day trial, and a card is required to start it. See the pricing page for plans and credits.

Start building

The quickest way to learn this is to build it. Start your 7-day trial (a card is required), create a workflow, add the Stripe Trigger, subscribe to invoice.payment_failed and send a test failure. If you are building payment recovery for a larger team, talk to us about enterprise.

You might also like

All it takes is an idea.

Imagine. Build. Ship.

Subscribe to our newsletter

Stay in the loop with product updates and news.

ยฉ 2026 BuildShip, Rowy Inc.

All it takes is an idea.

Imagine. Build. Ship.

Subscribe to our newsletter

Stay in the loop with product updates and news.

ยฉ 2026 BuildShip, Rowy Inc.

All it takes is an idea.

Imagine. Build. Ship.

Subscribe to our newsletter

Stay in the loop with product updates and news.

ยฉ 2026 BuildShip, Rowy Inc.