# Polar order refunded notifications on your phone

> Get a low-priority push notification when a Polar order is fully or partially refunded.

- Company: Polar (https://www.justpush.io/recipes/polar)
- Event: `order.refunded` (order.refunded)
- Tags: Payments & billing, Refunds
- Install: https://studio.justpush.io/recipes/polar/order-refunded
- Web page: https://www.justpush.io/recipes/polar/order-refunded

## 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 order.refunded event and save.
5. Tick **order.refunded**. It fires for full and partial refunds and carries the whole order, so the push can name the customer and product. (`refund.created` fires for every refund whatever its status, and without the customer's name, so this recipe doesn't use it.)
6. The amount is the total refunded on the order so far, including tax. A second partial refund on the same order shows the new running total.
7. Testing in the **sandbox**? Add `?sandbox=1` to the URL so the button opens the sandbox dashboard.
8. 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 → order.refunded
// Fires when an order is fully or partially refunded.
// Docs: https://polar.sh/docs/api-reference/current/order_refunded
//
// Polar keeps refunds as running totals on the order: refunded_amount (before tax) plus
// refunded_tax_amount. The push shows the total refunded so far.

// 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()}`
    }
}

// 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())
}

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

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

    const net = typeof order.refunded_amount === "number" ? order.refunded_amount : null
    const tax = typeof order.refunded_tax_amount === "number" ? order.refunded_tax_amount : 0
    const refunded = net === null ? null : money(net + tax, order.currency)
    const total = money(order.total_amount, order.currency)
    const partial = order.status === "partially_refunded"
    const who = text(order.customer?.name) ?? text(order.billing_name) ?? text(order.customer?.email)
    const product = text(order.product?.name) ?? text(order.description)
    // ?sandbox=1 points the button at the sandbox dashboard.
    const host = flag(request.query, "sandbox") ? "https://sandbox.polar.sh" : "https://polar.sh"

    return {
        title: partial ? "↩️ Partial refund" : "↩️ Order refunded",
        message:
            (refunded ? `${refunded} refunded` : "Refund issued") +
            (partial && total ? ` of ${total}` : "") +
            (who ? ` to ${who}` : "") +
            (product ? ` · ${product}` : ""),
        topic: "Polar",
        priority: -1, // Low: you (or a teammate) most likely issued it yourself
        buttons: link(`${host}/to/dashboard/sales/${encodeURIComponent(order.id)}`, "View order"),
    }
}
```

## Polar order.refunded webhook payload (sample)

```json
{
  "data": {
    "id": "7e3a5c1d-2b4f-4c8e-9a1b-0f2d3e4c5b6a",
    "paid": true,
    "items": [
      {
        "label": "Lifetime licence",
        "amount": 4900,
        "proration": false,
        "tax_amount": 1029
      }
    ],
    "status": "partially_refunded",
    "product": {
      "id": "p1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
      "name": "Lifetime licence",
      "description": null,
      "is_archived": false,
      "is_recurring": false,
      "organization_id": "o1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
      "recurring_interval": null
    },
    "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"
    },
    "metadata": [],
    "created_at": "2026-09-29T08:06:30.000000Z",
    "due_amount": 0,
    "net_amount": 4900,
    "product_id": "p1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
    "tax_amount": 1029,
    "checkout_id": "k1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
    "customer_id": "c1a2b3c4-d5e6-4f70-8a9b-0c1d2e3f4a5b",
    "description": "Lifetime licence",
    "discount_id": null,
    "modified_at": "2026-09-29T10:15:00.000000Z",
    "billing_name": "Jane Doe",
    "subscription": null,
    "total_amount": 5929,
    "billing_reason": "purchase",
    "invoice_number": "ACME-0042",
    "discount_amount": 0,
    "refunded_amount": 2000,
    "subscription_id": null,
    "subtotal_amount": 4900,
    "refunded_tax_amount": 420,
    "applied_balance_amount": 0
  },
  "type": "order.refunded",
  "timestamp": "2026-09-29T10:15:00.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.
