# Stripe invoice payment failed notifications on your phone

> Get a High-priority push notification when a Stripe invoice (such as a subscription renewal) fails to charge, with when Stripe will retry.

- Company: Stripe (https://www.justpush.io/recipes/stripe)
- Event: `invoice.payment_failed` (invoice.payment_failed)
- Tags: Finance, Subscriptions
- Install: https://studio.justpush.io/recipes/stripe/invoice-payment-failed
- Web page: https://www.justpush.io/recipes/stripe/invoice-payment-failed

## Setup

1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
2. In the Stripe Dashboard, open Workbench > Webhooks and click Add destination.
3. Select the invoice.payment_failed event and choose Webhook endpoint.
4. Paste your webhook URL (shown in Studio after install) as the endpoint URL and save.
5. Reveal the signing secret (whsec_…) and copy it into JustPush.

## Code

Studio calls `handleRequest(request)` with the incoming webhook (`{ method, headers, body, query, raw }`) and sends the message object it returns. Returning `null` sends nothing.

```js
// Stripe → invoice.payment_failed
// Fires when an invoice payment fails (e.g. a subscription renewal).
// Docs: https://docs.stripe.com/api/events/types
//
// Paste this into a JustPush Studio integration and add a Stripe webhook destination
// for the "invoice.payment_failed" event pointing at the integration's endpoint URL.

// Stripe amounts are in the currency's smallest unit (cents), except for
// zero-decimal currencies such as JPY, where 500 means ¥500.
const ZERO_DECIMAL = new Set([
    "bif", "clp", "djf", "gnf", "jpy", "kmf", "krw", "mga",
    "pyg", "rwf", "ugx", "vnd", "vuv", "xaf", "xof", "xpf",
])

function money(amount, currency) {
    // Don't invent a figure when the event doesn't carry one.
    if (typeof amount !== "number" || !Number.isFinite(amount) || !currency) return null

    const code = String(currency).toLowerCase()
    const value = ZERO_DECIMAL.has(code) ? amount : amount / 100

    try {
        return new Intl.NumberFormat("en-GB", { style: "currency", currency: code }).format(value)
    } catch {
        return `${value} ${code.toUpperCase()}`
    }
}

// Test-mode objects live under /test in the Dashboard.
function dashboardUrl(event, path) {
    return `https://dashboard.stripe.com/${event.livemode ? "" : "test/"}${path}`
}

// Stripe timestamps are seconds since epoch.
function day(seconds) {
    return new Date(seconds * 1000).toLocaleDateString("en-GB", {
        day: "numeric",
        month: "short",
        timeZone: "UTC",
    })
}

function handleRequest(request) {
    const event = request.body

    // Guard: only handle this event type, so other Stripe events on this integration stay quiet.
    if (event?.object !== "event" || event.type !== "invoice.payment_failed" || !event.data?.object) {
        console.log(`Ignoring Stripe event "${event?.type ?? "unknown"}"`)
        return null
    }

    const invoice = event.data.object
    const amount = money(invoice.amount_due, invoice.currency) ?? "An invoice"
    const who = invoice.customer_name ?? invoice.customer_email
    const retry = invoice.next_payment_attempt
        ? ` Stripe retries on ${day(invoice.next_payment_attempt)}.`
        : " No more retries scheduled."

    return {
        title: "⚠️ Invoice payment failed",
        message:
            `${amount}` +
            (who ? ` from ${who}` : "") +
            (invoice.attempt_count ? ` (attempt ${invoice.attempt_count}).` : ".") +
            retry,
        topic: "Stripe",
        priority: 1, // High — a paying customer is at risk of churning
        buttons: [{ cta: "View invoice", url: dashboardUrl(event, `invoices/${invoice.id}`) }],
    }
}
```

## Stripe invoice.payment_failed webhook payload (sample)

```json
{
  "id": "evt_test_invoice_payment_failed",
  "data": {
    "object": {
      "id": "in_test_1Qb3Cd",
      "object": "invoice",
      "status": "open",
      "currency": "eur",
      "customer": "cus_test_R2d2",
      "amount_due": 2000,
      "attempt_count": 2,
      "customer_name": "Jane Doe",
      "billing_reason": "subscription_cycle",
      "customer_email": "jane@example.com",
      "next_payment_attempt": 1790843400
    }
  },
  "type": "invoice.payment_failed",
  "object": "event",
  "created": 1790584200,
  "livemode": false,
  "api_version": "2025-09-30.clover"
}
```

## Signature check

Stripe signs every webhook with a timestamped HMAC-SHA256 signature in the Stripe-Signature header, made with your endpoint's signing secret. JustPush checks it before the code runs.

## FAQ

### Does this work on iPhone and Android?

Yes. Install the JustPush app from the App Store or Google Play and sign in. Every phone signed in to your account gets the notification.

### Do I need to write code?

No. Install the recipe in Studio, paste your webhook URL into Stripe and you are done. The code is there if you want to change the text, the sound or the buttons.

### Can I change what the notification says?

Yes. After install the recipe's code is yours. Edit it in Studio and test it against the sample payload before you save.

### What does it cost?

JustPush is free for 30 days. After that it's $19.99 a year, or $39.99 once. There is no extra charge for recipes.
