# Lemon Squeezy payment failed notifications on your phone

> Get a High-priority push notification when a Lemon Squeezy subscription renewal payment fails.

- Company: Lemon Squeezy (https://www.justpush.io/recipes/lemonsqueezy)
- Event: `subscription_payment_failed` (subscription_payment_failed)
- Tags: Finance, Subscriptions
- Install: https://studio.justpush.io/recipes/lemonsqueezy/payment-failed
- Web page: https://www.justpush.io/recipes/lemonsqueezy/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 Lemon Squeezy, go to Settings > Webhooks and add a webhook.
3. Paste your webhook URL (shown in Studio after install) as the callback URL, enter a signing secret, and tick subscription_payment_failed.
4. Save. Test mode and live mode have separate webhooks.
5. Add a webhook under **Settings → Webhooks** with the URL above and tick `subscription_payment_failed`. Lemon Squeezy retries failed payments automatically.
6. Lemon Squeezy signs deliveries with `X-Signature` (HMAC-SHA256 of your signing secret), but JustPush doesn't check it yet, so keep your endpoint URL private.

## 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
// Lemon Squeezy → subscription payment failed
// Fires when a renewal payment fails. Lemon Squeezy retries it automatically.
// Docs: https://docs.lemonsqueezy.com/help/webhooks/event-types

// The event name comes in the X-Event-Name header and in meta.event_name.
// Header names can arrive in any case.
function header(request, name) {
    const headers = request.headers || {}
    const key = Object.keys(headers).find((k) => k.toLowerCase() === name)
    const value = key ? headers[key] : null
    return Array.isArray(value) ? value[0] : value
}

// Lemon Squeezy puts test_mode in meta on real deliveries and in the attributes in its examples.
function testMode(body) {
    return (body.meta?.test_mode ?? body.data?.attributes?.test_mode) === true ? " (test mode)" : ""
}

// Only link to real http(s) URLs.
function link(url, cta) {
    return typeof url === "string" && /^https?:\/\//.test(url) ? [{ cta, url }] : []
}

function handleRequest(request) {
    const body = request.body || {}
    const event = header(request, "x-event-name") ?? body.meta?.event_name
    const order = body.data?.attributes ?? {}

    // Guard: only subscription_payment_failed events.
    if (event !== "subscription_payment_failed") {
        console.log(`Ignoring Lemon Squeezy "${event ?? "unknown"}" event`)
        return null
    }

    // Payment events carry a subscription invoice, not the subscription itself.
    const invoice = order
    const card = invoice.card_last_four ? ` (${invoice.card_brand ?? "card"} ••${invoice.card_last_four})` : ""

    return {
        title: "⚠️ Subscription payment failed",
        message:
            (invoice.total_formatted ?? "A renewal payment") +
            (invoice.user_name ? ` from ${invoice.user_name}` : "") +
            card +
            testMode(body),
        topic: "Lemon Squeezy",
        priority: 1, // High — a paying customer is at risk of churning
        buttons: link(invoice.urls?.invoice_url, "View invoice"),
    }
}
```

## Lemon Squeezy subscription_payment_failed webhook payload (sample)

```json
{
  "data": {
    "id": "88990",
    "type": "subscription-invoices",
    "attributes": {
      "urls": {
        "invoice_url": "https://app.lemonsqueezy.com/my-orders/invoice/88990?signature=abc"
      },
      "total": 1500,
      "status": "failed",
      "currency": "EUR",
      "store_id": 12345,
      "test_mode": false,
      "user_name": "Jane Doe",
      "card_brand": "visa",
      "user_email": "jane@example.com",
      "billing_reason": "renewal",
      "card_last_four": "4242",
      "subscription_id": 33445,
      "total_formatted": "€15.00",
      "status_formatted": "Failed"
    }
  },
  "meta": {
    "test_mode": false,
    "event_name": "subscription_payment_failed",
    "webhook_id": "a1b2c3d4-e5f6-7a8b-9c0d-1e2f3a4b5c6d"
  }
}
```

## 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 Lemon Squeezy 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.
