# Polar subscription past due notifications on your phone

> Get a High-priority push notification when a Polar subscription renewal payment fails and the subscription goes past due.

- Company: Polar (https://www.justpush.io/recipes/polar)
- Event: `subscription.past_due` (subscription.past_due)
- Tags: Payments & billing, Failed payments & disputes, Subscriptions
- Install: https://studio.justpush.io/recipes/polar/subscription-past-due
- Web page: https://www.justpush.io/recipes/polar/subscription-past-due

## 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 Polar dashboard, open Settings > Webhooks and click Add Endpoint.
3. Paste your webhook URL (shown in Studio after install) as the URL and leave the format on Raw.
4. Generate a secret (JustPush doesn't need it), tick the subscription.past_due event and save.
5. Tick **subscription.past_due**. It fires when a renewal payment fails. That's recoverable: Polar retries, and the customer can update their payment method in the customer portal. If the retries run out, Polar sends `subscription.revoked` with status `unpaid` (see the *Subscription ended* recipe).
6. Testing in the **sandbox**? Add `?sandbox=1` to the URL so the button opens the sandbox dashboard.
7. Polar signs every delivery (`webhook-id`, `webhook-timestamp`, `webhook-signature`; Standard Webhooks for secrets made on or after 8 September 2026), but JustPush doesn't check that signature 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
// Polar → subscription.past_due
// Fires when a subscription renewal payment fails and the subscription goes past due. Polar
// retries the payment and the customer can update their payment method; if the retries run out,
// subscription.revoked follows.
// Docs: https://polar.sh/docs/api-reference/current/subscription_past_due

// Polar amounts are in the currency's smallest unit (cents), except zero-decimal currencies.
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) {
    if (typeof amount !== "number" || !Number.isFinite(amount) || typeof currency !== "string" || !currency) return null

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

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

// "€19.00/month", or "€49.00 every 3 months".
const INTERVALS = { day: "day", week: "week", month: "month", year: "year" }

function price(sub) {
    const amount = money(sub.amount, sub.currency)
    const unit = pick(INTERVALS, sub.recurring_interval, null)

    if (!amount) return null
    if (!unit) return amount

    const count = Number.isInteger(sub.recurring_interval_count) ? sub.recurring_interval_count : 1
    return count > 1 ? `${amount} every ${count} ${unit}s` : `${amount}/${unit}`
}

// "29 Oct 2026" from an ISO timestamp, or null.
function day(value) {
    const date = typeof value === "string" ? new Date(value) : null
    return date && !Number.isNaN(date.getTime())
        ? date.toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric", timeZone: "UTC" })
        : null
}

// Table lookup that ignores inherited names such as "toString".
function pick(table, key, fallback) {
    return typeof key === "string" && Object.prototype.hasOwnProperty.call(table, key) ? table[key] : fallback
}

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

// A non-empty string, or null.
function text(value) {
    return typeof value === "string" && value.trim() ? value.trim() : null
}

function flag(query, name) {
    return ["1", "true", "yes"].includes(String(query?.[name] ?? "").toLowerCase())
}

// "Jane Doe's Pro plan (€19.00/month)"
function describe(sub) {
    const who = text(sub.customer?.name) ?? text(sub.customer?.billing_name) ?? text(sub.customer?.email)
    const product = text(sub.product?.name) ?? "subscription"
    const cost = price(sub)

    return (who ? `${who}'s ${product}` : `A ${product}`) + (cost ? ` (${cost})` : "")
}

function dashboard(request, sub) {
    // ?sandbox=1 points the button at the sandbox dashboard.
    const host = flag(request.query, "sandbox") ? "https://sandbox.polar.sh" : "https://polar.sh"
    return link(`${host}/to/dashboard/sales/subscriptions/${encodeURIComponent(sub.id)}`, "View subscription")
}

function handleRequest(request) {
    const body = request.body && typeof request.body === "object" ? request.body : {}
    const sub = body.data

    // Guard: only subscription.past_due, so other Polar events on this integration stay quiet.
    if (body.type !== "subscription.past_due" || !sub || typeof sub !== "object" || !sub.id) {
        console.log(`Ignoring Polar "${typeof body.type === "string" ? body.type : "unknown"}" event`)
        return null
    }

    return {
        title: "⚠️ Subscription payment failed",
        message: `${describe(sub)} couldn't be renewed and is past due. Polar will retry the payment.`,
        topic: "Polar",
        priority: 1, // High: revenue at risk, and you may want to reach out
        buttons: dashboard(request, sub),
    }
}
```

## Polar subscription.past_due webhook payload (sample)

```json
{
  "data": {
    "id": "s1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
    "amount": 1900,
    "status": "past_due",
    "ends_at": null,
    "product": {
      "id": "p2a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
      "name": "Pro plan",
      "description": null,
      "is_archived": false,
      "is_recurring": true,
      "organization_id": "o1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
      "recurring_interval": "month",
      "recurring_interval_count": 1
    },
    "currency": "eur",
    "customer": {
      "id": "c1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
      "name": "Jane Doe",
      "type": "individual",
      "email": "jane@example.com",
      "billing_name": "Jane Doe",
      "email_verified": true,
      "organization_id": "o1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b"
    },
    "discount": null,
    "ended_at": null,
    "metadata": [],
    "trial_end": null,
    "created_at": "2026-06-29T08:00:00.000000Z",
    "product_id": "p2a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
    "started_at": "2026-06-29T08:00:00.000000Z",
    "canceled_at": null,
    "checkout_id": null,
    "customer_id": "c1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
    "discount_id": null,
    "modified_at": "2026-09-29T08:06:40.000000Z",
    "past_due_at": "2026-09-29T08:06:40.000000Z",
    "trial_start": null,
    "current_period_end": "2026-10-29T08:00:00.000000Z",
    "recurring_interval": "month",
    "cancel_at_period_end": false,
    "current_period_start": "2026-09-29T08:00:00.000000Z",
    "recurring_interval_count": 1,
    "customer_cancellation_reason": null,
    "customer_cancellation_comment": null
  },
  "type": "subscription.past_due",
  "timestamp": "2026-09-29T08:06:40.000000Z"
}
```

## 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 Polar 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.
