Chargebee payment failed notifications on your phone.

Get a High-priority push notification when a Chargebee payment fails, with the gateway's reason, the card and the next retry date.

How it works

Three steps to your first push

STEP 01

Install the recipe

One click in Studio gives you a personal webhook URL.

STEP 02

Paste your URL into Chargebee

Add it as a webhook for payment_failed.

STEP 03

Get a push on your phone

With the text, sound and buttons from the recipe.

Setup

How to set up the Chargebee payment_failed webhook

  1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
  2. In Chargebee, go to Settings > Configure Chargebee > API Keys and Webhooks, open the Webhooks tab and click Add Webhook.
  3. Paste your webhook URL as the Webhook URL (add ?site=your-site for dashboard buttons).https://••••••••/•••••••• your personal URL, shown after install
  4. Under Events to Send, pick only payment_failed and save the webhook.
  5. Under **Events to Send**, pick only `payment_failed` (Chargebee selects All Events by default). It fires on each failed attempt, so a card that keeps failing during dunning can push once per retry.
  6. **Dashboard button:** Chargebee events don't include your site's name, so add it to the webhook URL to get a button that opens the invoice in Chargebee. Use the part before `.chargebee.com` in your dashboard address (test sites usually end in `-test`):
  7. ```
  8. your webhook URL?site=your-site
  9. ```
  10. Without `?site=` the push arrives without a button.
  11. Built for API version **v2** events. Chargebee's **Test Webhook** button sends a sample of the event you pick, which should give a normal push.
  12. Chargebee doesn't sign webhooks. It can send a username and password (basic authentication), but JustPush doesn't check them yet, so you can leave that off. Keep your endpoint URL private.
The code

What runs when the webhook arrives

Studio calls handleRequest(request) with the incoming webhook and sends the message it returns. It's yours after install; change anything.

transform.js
1// Chargebee → payment_failed
2// Fires when an attempt to charge a customer fails, such as a declined card on a renewal.
3// Docs: https://apidocs.chargebee.com/docs/api/events/webhook/payment_failed
4//
5// Paste this into a JustPush Studio integration and add a Chargebee webhook for the
6// "payment_failed" event pointing at the integration's endpoint URL.
7// Add ?site=your-site to the URL to get a button that opens the invoice in Chargebee.
8
9// Chargebee amounts are whole numbers in the currency's smallest unit (2900 = €29.00),
10// except for zero-decimal currencies such as JPY, where 500 means ¥500. This is Chargebee's
11// own list: https://www.chargebee.com/docs/billing/2.0/site-configuration/supported-currencies
12const ZERO_DECIMAL = new Set(["clp", "isk", "jpy", "krw", "mga", "pyg", "vnd", "xaf", "xof", "xpf"])
13
14function money(amount, currency) {
15 // Don't invent a figure when the event doesn't carry one.
16 if (typeof amount !== "number" || !Number.isFinite(amount) || typeof currency !== "string" || !currency) return null
17
18 const code = currency.toLowerCase()
19 const value = ZERO_DECIMAL.has(code) ? amount : amount / 100
20
21 try {
22 return new Intl.NumberFormat("en-GB", { style: "currency", currency: code }).format(value)
23 } catch {
24 return `${value} ${code.toUpperCase()}`
25 }
26}
27
28// A trimmed string, or null for anything else.
29function text(value) {
30 return typeof value === "string" && value.trim() ? value.trim() : null
31}
32
33// Only link to real http(s) URLs.
34function link(url, cta) {
35 return typeof url === "string" && /^https?:\/\//.test(url) ? [{ cta, url }] : []
36}
37
38// Chargebee events don't carry your site's name, so the dashboard link needs ?site=acme
39// (or ?site=acme-test for a test site) on the webhook URL.
40function adminUrl(request, path, id) {
41 const raw = Array.isArray(request.query?.site) ? request.query.site[0] : request.query?.site
42 const site = (typeof raw === "string" ? raw : "")
43 .trim()
44 .toLowerCase()
45 .replace(/^https?:\/\//, "")
46 .replace(/\.chargebee\.com.*$/, "")
47
48 if (!/^[a-z0-9][a-z0-9-]*$/.test(site) || !text(id)) return null
49 return `https://${site}.chargebee.com/d/${path}/${encodeURIComponent(id)}`
50}
51
52// "Jane Doe", or the company or email when there's no name.
53function customerName(customer) {
54 const name = [text(customer?.first_name), text(customer?.last_name)].filter(Boolean).join(" ")
55 return name || text(customer?.company) || text(customer?.email)
56}
57
58// The plan's item price ID (Product Catalog 2.0) or plan ID (Product Catalog 1.0).
59function planName(subscription) {
60 if (!subscription || typeof subscription !== "object") return null
61 const items = Array.isArray(subscription.subscription_items) ? subscription.subscription_items : []
62 const item = items.find((entry) => entry?.item_type === "plan") ?? items[0]
63 return text(item?.item_price_id) ?? text(subscription.plan_id)
64}
65
66// Chargebee timestamps are Unix seconds.
67function day(seconds) {
68 if (typeof seconds !== "number" || !Number.isFinite(seconds)) return null
69 return new Date(seconds * 1000).toLocaleDateString("en-GB", {
70 day: "numeric",
71 month: "short",
72 timeZone: "UTC",
73 })
74}
75
76// The gateway's error message (or code), kept short enough for a notification.
77function failure(transaction) {
78 const raw = text(transaction.error_text) ?? text(transaction.error_code)?.replace(/_/g, " ")
79 if (!raw) return null
80 const clean = raw.replace(/\s+/g, " ").replace(/\.$/, "")
81 return clean.length > 120 ? `${clean.slice(0, 119)}…` : clean
82}
83
84function handleRequest(request) {
85 const event = request.body && typeof request.body === "object" ? request.body : {}
86
87 // Guard: only this event type, so other Chargebee events on this integration stay quiet.
88 // Chargebee sends no event header, so the payload's event_type decides.
89 if (
90 event.event_type !== "payment_failed" ||
91 (event.object !== undefined && event.object !== "event") ||
92 !event.content?.transaction ||
93 typeof event.content.transaction !== "object"
94 ) {
95 console.log(`Ignoring Chargebee event "${text(event.event_type) ?? "unknown"}"`)
96 return null
97 }
98
99 const { transaction, invoice, customer, subscription } = event.content
100 const amount = money(transaction.amount, transaction.currency_code) ?? "A payment"
101 const who = customerName(customer)
102 const plan = planName(subscription)
103 const reason = failure(transaction)
104 const last4 = text(transaction.masked_card_number)?.match(/(\d{4})$/)?.[1]
105 const card = last4 ? ` (card ••${last4})` : ""
106 const retry = day(invoice?.next_retry_at)
107 const invoiceUrl = adminUrl(request, "invoices", invoice?.id)
108
109 return {
110 title: "🚫 Payment failed",
111 message:
112 amount +
113 (who ? ` from ${who}` : "") +
114 (plan ? ` for ${plan}` : "") +
115 (reason ? ` — ${reason}${card}` : card) +
116 (retry ? `. Retrying ${retry}.` : ""),
117 topic: "Chargebee",
118 priority: 1, // High — a customer's payment didn't go through and revenue is at risk
119 buttons: invoiceUrl
120 ? link(invoiceUrl, "View invoice")
121 : link(adminUrl(request, "customers", transaction.customer_id ?? customer?.id), "View customer"),
122 }
123}
Payload

The Chargebee payment_failed webhook

This is what Chargebee sends to your URL for payment_failed. It is a sample, trimmed to the fields recipes use.

payment_failed · sample.json
1{
2 "id": "ev_test_payment_failed",
3 "object": "event",
4 "source": "scheduled_job",
5 "content": {
6 "invoice": {
7 "id": "1043",
8 "total": 2900,
9 "object": "invoice",
10 "status": "payment_due",
11 "recurring": true,
12 "amount_due": 2900,
13 "amount_paid": 0,
14 "customer_id": "cus_test_16BjpLU3",
15 "currency_code": "EUR",
16 "next_retry_at": 1790843400,
17 "dunning_status": "in_progress",
18 "subscription_id": "sub_test_16BjpLU3"
19 },
20 "customer": {
21 "id": "cus_test_16BjpLU3",
22 "email": "[email protected]",
23 "object": "customer",
24 "last_name": "Doe",
25 "first_name": "Jane"
26 },
27 "transaction": {
28 "id": "txn_test_16BjpLU4",
29 "date": 1790584200,
30 "type": "payment",
31 "amount": 2900,
32 "object": "transaction",
33 "status": "failure",
34 "gateway": "stripe",
35 "error_code": "card_declined",
36 "error_text": "Your card has insufficient funds.",
37 "customer_id": "cus_test_16BjpLU3",
38 "currency_code": "EUR",
39 "payment_method": "card",
40 "subscription_id": "sub_test_16BjpLU3",
41 "masked_card_number": "************4242"
42 },
43 "subscription": {
44 "id": "sub_test_16BjpLU3",
45 "object": "subscription",
46 "status": "active",
47 "currency_code": "EUR",
48 "billing_period": 1,
49 "subscription_items": [
50 {
51 "amount": 2900,
52 "quantity": 1,
53 "item_type": "plan",
54 "unit_price": 2900,
55 "item_price_id": "Pro-EUR-Monthly"
56 }
57 ],
58 "billing_period_unit": "month"
59 }
60 },
61 "event_type": "payment_failed",
62 "api_version": "v2",
63 "occurred_at": 1790584200,
64 "webhook_status": "scheduled"
65}
FAQ

Chargebee payment failed notifications: questions

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 Chargebee 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.

Ready when you are

Chargebee on your phone in two minutes.

Install the recipe, paste one URL, done. Free for 30 days, no credit card required.