# Stripe subscription canceled notifications on your phone

> Get a push notification when a Stripe subscription ends, with the customer's cancellation feedback when they left any.

- Company: Stripe (https://www.justpush.io/recipes/stripe)
- Event: `customer.subscription.deleted` (customer.subscription.deleted)
- Tags: Finance, Subscriptions
- Install: https://studio.justpush.io/recipes/stripe/subscription-canceled
- Web page: https://www.justpush.io/recipes/stripe/subscription-canceled

## 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 customer.subscription.deleted 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 → customer.subscription.deleted
// Fires when a subscription ends (canceled immediately or at period end).
// Docs: https://docs.stripe.com/api/events/types
//
// Paste this into a JustPush Studio integration and add a Stripe webhook destination
// for the "customer.subscription.deleted" 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}`
}

// Describe the first subscription item, e.g. "Pro — £20.00/month".
// Stripe only sends the product ID, so the price's nickname is the best name available.
function plan(subscription) {
    const item = subscription.items?.data?.[0]
    const price = item?.price

    if (!price) return null

    const recurring = price.recurring
    const every = !recurring
        ? ""
        : recurring.interval_count > 1
          ? ` every ${recurring.interval_count} ${recurring.interval}s`
          : `/${recurring.interval}`
    const each = money(price.unit_amount * (item.quantity ?? 1), price.currency)
    const cost = each ? each + every : null

    return [price.nickname, cost].filter(Boolean).join(" — ") || null
}

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 !== "customer.subscription.deleted" || !event.data?.object) {
        console.log(`Ignoring Stripe event "${event?.type ?? "unknown"}"`)
        return null
    }

    const subscription = event.data.object
    const summary = plan(subscription) ?? "A subscription"
    const details = subscription.cancellation_details ?? {}
    // "feedback" is what the customer picked in the cancellation flow; "reason" is how it ended.
    const why = details.feedback ?? details.reason
    const comment = details.comment ? ` “${details.comment}”` : ""

    return {
        title: "👋 Subscription canceled",
        message: summary + (why ? ` — ${why.replace(/_/g, " ")}` : "") + comment,
        topic: "Stripe",
        priority: 0, // Normal — worth knowing, nothing to fix right now
        buttons: [{ cta: "View customer", url: dashboardUrl(event, `customers/${subscription.customer}`) }],
    }
}
```

## Stripe customer.subscription.deleted webhook payload (sample)

```json
{
  "id": "evt_test_subscription_canceled",
  "data": {
    "object": {
      "id": "sub_test_1Qa2Bc",
      "items": {
        "data": [
          {
            "id": "si_test_Q1a2",
            "price": {
              "id": "price_test_1Pro",
              "object": "price",
              "product": "prod_test_Pro",
              "currency": "eur",
              "nickname": "Pro",
              "recurring": {
                "interval": "month",
                "interval_count": 1
              },
              "unit_amount": 2000
            },
            "object": "subscription_item",
            "quantity": 1
          }
        ],
        "object": "list"
      },
      "object": "subscription",
      "status": "canceled",
      "customer": "cus_test_R2d2",
      "ended_at": 1790584100,
      "canceled_at": 1790584100,
      "cancellation_details": {
        "reason": "cancellation_requested",
        "comment": "Great product, just over budget right now.",
        "feedback": "too_expensive"
      }
    }
  },
  "type": "customer.subscription.deleted",
  "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.
