Notify the team and offer a coupon when a payment fails

Tell the team when a card payment fails, wait an hour, re-check the shopper did not pay, then email a personal coupon. A validated payment.failed@1 definition.

When a payment on a checkout fails, tell the team at once, wait an hour, check the shopper did not retry and pay, then give them a personal coupon by email. Trigger: payment.failed@1. Steps: notify_me@1, wait, condition on a live value, create_customer_coupon@1, send_customer_email@1.

Model background: Automations.

The definition

{
  "schemaVersion": 1,
  "trigger": {
    "type": "payment.failed@1",
    "origins": [
      "USER",
      "API",
      "MCP",
      "ASSISTANT",
      "BULK",
      "IMPORT",
      "DUPLICATE",
      "SYSTEM",
      "AUTOMATION"
    ],
    "filter": {
      "op": "all",
      "rules": [
        {
          "path": "event.facts.payment.provider",
          "op": "eq",
          "value": "STRIPE"
        },
        {
          "path": "event.facts.cart.subtotal",
          "op": "gte",
          "value": 50
        }
      ]
    }
  },
  "steps": [
    {
      "id": "tell",
      "type": "action",
      "contract": "notify_me@1",
      "input": {
        "title": "A payment failed on a cart of {{cart.subtotal}} {{cart.currency}}",
        "message": "A payment on cart {{cart.id}} ({{cart.itemCount}} items) did not go through."
      }
    },
    {
      "id": "pause",
      "type": "wait",
      "duration": {
        "amount": 1,
        "unit": "hours"
      }
    },
    {
      "id": "not_paid",
      "type": "condition",
      "when": {
        "op": "all",
        "rules": [
          {
            "path": "current.cart.status",
            "op": "neq",
            "value": "CONVERTED"
          }
        ]
      },
      "then": [
        {
          "id": "coupon",
          "type": "action",
          "contract": "create_customer_coupon@1",
          "input": {
            "customerId": {
              "$ref": "event.facts.customer.id"
            },
            "type": "percent",
            "value": 10,
            "expiresInDays": 7
          }
        },
        {
          "id": "mail",
          "type": "action",
          "contract": "send_customer_email@1",
          "input": {
            "customerId": {
              "$ref": "event.facts.customer.id"
            },
            "templateId": "TEMPLATE_ID",
            "kind": "marketing",
            "frequencyDays": 7
          }
        }
      ]
    }
  ]
}

Replace before saving:

  • STRIPE: the upper-case slug of the payment app (STRIPE, PAYPAL, GROW, and so on). Remove the rule to react to every app that reports failures.
  • The number 50: the smallest cart subtotal worth the effort, in the cart's own currency.
  • TEMPLATE_ID: the id of a template in the store's campaign library that prints the coupon with {{coupon.code}}. The email always sends the template's latest version.
  • percent, 10 and expiresInDays (1 to 365): the coupon. It is single-use and only that customer can redeem it.

How it works

  • payment.failed@1 fires once per failed payment attempt on a checkout. Its entity is the cart the checkout was started on, the same family as checkout.abandoned@1, so a customer step acts on the cart's customer and the test runs on a Cart.
  • Its facts: event.facts.cart.id, .createdAt, .itemCount, .lineCount, .subtotal, .currency, .productIds (at most 250, .listsTruncated says when it was cut), .hasCoupon, .salesChannelId; event.facts.payment.provider (upper case, such as STRIPE); event.facts.payment.reason (declined, authentication_failed, cancelled, error or unknown); and event.facts.customer (null for a guest). The provider's own error message is never a fact. Most payment apps send only free text, so many failures read unknown: do not build a rule that needs declined.
  • A text may use {{cart.id}}, {{cart.itemCount}}, {{cart.subtotal}}, {{cart.currency}}, {{customer.id}}, {{customer.ordersCount}}, {{payment.provider}} and {{payment.reason}}.
  • A shopper may fail and then pay within minutes. After a wait the facts are the old ones, so the condition tests the live value current.cart.status neq CONVERTED: a cart that became an order reads CONVERTED, and the coupon and the email are skipped for that shopper. The condition has no else, so a false result ends the run SUCCEEDED with the outcome CONDITION_NOT_MET. A cart deleted meanwhile ends the run SKIPPED with CART_NOT_FOUND.
  • A guest who only typed an email at checkout has no customer. The team notice still goes out, but the coupon and the email are skipped for them, and a customer rule on the trigger is not met.
  • The customer id is { "$ref": "event.facts.customer.id" }.

Which failures start it

Only payment apps that report a failure send it: Stripe, PayPal, Grow, Cardcom, iCredit, MAX, Takbull and Tranzila. Nedarim Plus (declines are ignored on purpose), Invoice4U and Morning report only successes. Sola has no webhook, so a decline reaches the automation only when the storefront's confirm step gets it. Do not tell a merchant that every failed payment triggers it.

What it does not do

  • It does not fire for a failed attempt on a payment link, for a failed payment on an order that already exists as pending, for an amount mismatch or a "payment cannot be verified" check (those are integrity failures, not a decline), for a late failure report on a checkout that is already paid, or for a payment the provider is still processing.
  • It does not send the shopper a "your payment failed" message of its own. send_customer_email@1 is a marketing email: it goes only to a customer who agreed to marketing emails, and never when they are suppressed (SUPPRESSED), have no address (NO_CUSTOMER_EMAIL), or already got one inside frequencyDays (FREQUENCY_CAP). It needs a business postal address on the store (NO_POSTAL_ADDRESS otherwise). A coupon minted for a shopper who never agreed to marketing is minted all the same, but nobody emails it.
  • It does not conflict with the store's own abandoned-checkout recovery emails: the RECOVERY_EMAIL_ACTIVE refusal applies to checkout.abandoned@1 only. A shopper can still get both a recovery email and this one.
  • It does not read the provider's error text, a card number, a name or an address. None of them is in the facts.
  • It is the automation trigger. The outbound payment.failed webhook event is a different thing and is not changed by it.

Create it

With the Admin MCP:

  1. get_automation_guide with topic: "triggers", then "live-values" and "messages".
  2. validate_automation_definition. Fix every finding.
  3. test_automation with entity: { "type": "Cart", "id": "<a recent cart id>" }. The cart is tested as if its payment had just been declined by a sample provider, the wait is shown and never waited, and the condition is judged on today's value. Testing on a cart needs permission to view every order.
  4. create_automation_draft with name: "Payment failed follow-up".
  5. Tell the merchant to approve it in the dashboard.

With the SDK:

const check = await admin.validateAutomationDefinition(storeId, { definition });
if (!check.ok) throw new Error(JSON.stringify(check.errors));

const draft = await admin.createAutomation(storeId, {
  name: 'Payment failed follow-up',
  definition,
});

What the merchant sees

A draft in Settings, then Automations. Once on, each failed attempt that matches the filter starts a run: the notice arrives at once, the run shows WAITING for an hour, then ends SUCCEEDED (coupon created and email sent, or the shopper paid meanwhile) or SKIPPED with the reason code. A shopper who fails three times in a row starts three runs, one per attempt. The list's "Last 7 days" column counts them.