# Gumroad refund issued notifications on your phone

> Get a low-priority push notification when a Gumroad sale is fully or partially refunded.

- Company: Gumroad (https://www.justpush.io/recipes/gumroad)
- Event: `refund` (refund)
- Tags: Payments & billing, Refunds
- Install: https://studio.justpush.io/recipes/gumroad/refund
- Web page: https://www.justpush.io/recipes/gumroad/refund

## 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 Gumroad, open Settings > Advanced.
3. For sales, paste your webhook URL (shown in Studio after install) into Ping endpoint and save. For other events, subscribe it to the refund resource with the API call in the setup notes.
4. Gumroad doesn't sign pings, so keep the URL private.
5. Gumroad's **Ping endpoint** setting only covers sales. For `refund` pings, subscribe the URL through Gumroad's API:
6. In Gumroad, open **Settings → Advanced → Applications**, create an application (any name, and any https URL such as `https://example.com` as the redirect URI), then click **Generate access token**.
7. Run this, with your token in place of `YOUR_ACCESS_TOKEN`:
8. ```
9. curl https://api.gumroad.com/v2/resource_subscriptions \
10. -d "access_token=YOUR_ACCESS_TOKEN" \
11. -d "resource_name=refund" \
12. --data-urlencode "post_url=your webhook URL (shown in Studio after install)" \
13. -X PUT
14. ```
15. Refunds you issue from Gumroad, refunds made at the payment processor and partial refunds all ping. The ping carries the original sale price, not the refunded amount.
16. Pings only arrive while the application and its access token exist, so don't delete or revoke them. Gumroad sends them form-encoded, retries a failed delivery a few times and can deliver the same event twice.
17. Gumroad doesn't sign pings, 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
// Gumroad → refund (resource subscription)
// Fires when a sale is fully or partially refunded, by you, Gumroad or the payment processor.
// Docs: https://gumroad.com/api#resource-subscriptions
//
// Gumroad POSTs form-encoded fields such as product_name=…, price=4900, refunded=true.
// Depending on how the body was parsed, it can arrive as a nested object, a flat object
// with bracket keys, or only as the raw string. Every value is a string ("true", "4900").

const TOPIC = "Gumroad"

// Keys that must never become object properties.
const UNSAFE = new Set(["__proto__", "constructor", "prototype"])

// Put a value at a bracket path such as card[visual] or purchase_ids[].
function setPath(target, key, value) {
    const list = /\[\]$/.test(key)
    const parts = String(key).replace(/\[\]$/, "").replace(/\]/g, "").split("[")
    if (parts.some((p) => p === "" || UNSAFE.has(p))) return
    let node = target
    for (const part of parts.slice(0, -1)) {
        if (!Object.prototype.hasOwnProperty.call(node, part) || !node[part] || typeof node[part] !== "object" || Array.isArray(node[part])) node[part] = {}
        node = node[part]
    }
    const last = parts[parts.length - 1]
    if (!list) node[last] = value
    else if (Array.isArray(node[last]) && Object.prototype.hasOwnProperty.call(node, last)) node[last].push(value)
    else node[last] = [value]
}

// Decode one form-encoded piece; a bad escape shouldn't break the whole body.
function decode(value) {
    try {
        return decodeURIComponent(String(value).replace(/\+/g, " "))
    } catch {
        return String(value)
    }
}

// Turn whatever Studio gives us (object, JSON string or raw form string) into one object.
function readForm(request) {
    let body = request.body
    if (typeof body === "string" && body.trim().startsWith("{")) {
        try {
            body = JSON.parse(body)
        } catch {
            body = null
        }
    }
    const out = {}
    if (body && typeof body === "object" && !Array.isArray(body)) {
        for (const [key, value] of Object.entries(body)) {
            if (key.includes("[")) setPath(out, key, value)
            else if (!UNSAFE.has(key)) out[key] = value
        }
        if (Object.keys(out).length) return out
    }
    // Fall back to the raw form-encoded string.
    const raw = typeof body === "string" ? body : typeof request.raw === "string" ? request.raw : ""
    for (const pair of raw.split("&")) {
        if (!pair) continue
        const at = pair.indexOf("=")
        setPath(out, decode(at < 0 ? pair : pair.slice(0, at)), at < 0 ? "" : decode(pair.slice(at + 1)))
    }
    return out
}

// Read an own field only, so names like "toString" never resolve to something inherited.
function field(form, key) {
    return Object.prototype.hasOwnProperty.call(form, key) ? form[key] : undefined
}

// Form fields are strings; treat blanks as missing.
function text(value) {
    if (typeof value === "number" && Number.isFinite(value)) return String(value)
    return typeof value === "string" && value.trim() ? value.trim() : null
}

// Booleans arrive as "true"/"false" in form bodies and as true/false in JSON.
function flag(value) {
    return value === true || value === "true"
}

// Gumroad's price is always in USD cents, whatever the product's currency field says.
function usd(cents) {
    const n = typeof cents === "number" ? cents : /^-?\d+(\.\d+)?$/.test(text(cents) ?? "") ? Number(cents) : NaN
    if (!Number.isFinite(n)) return null
    try {
        return new Intl.NumberFormat("en-GB", { style: "currency", currency: "USD" }).format(n / 100)
    } catch {
        return `US$${(n / 100).toFixed(2)}`
    }
}

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

function handleRequest(request) {
    const form = readForm(request)
    const resource = text(field(form, "resource_name"))

    // Guard: only refund pings. They look like sale pings, so resource_name decides.
    if (resource !== "refund") {
        console.log(`Ignoring Gumroad "${resource ?? "unknown"}" ping`)
        return null
    }

    // refunded=true means fully refunded; a partial refund pings with refunded=false.
    const partial = field(form, "refunded") === false || field(form, "refunded") === "false"
    // The ping carries the original sale price (USD cents), not the refunded amount.
    const amount = usd(field(form, "price"))
    const who = text(field(form, "full_name")) ?? text(field(form, "email")) ?? "a customer"
    const product = text(field(form, "product_name")) ?? "a product"

    return {
        title: partial ? "↩️ Partial refund" : "↩️ Refund issued",
        message:
            (partial ? `Part of ${amount ? `a ${amount} sale` : "a sale"} refunded` : `${amount ?? "A sale"} refunded`) +
            ` to ${who} — ${product}` +
            (flag(field(form, "test")) ? " (test)" : ""),
        topic: TOPIC,
        priority: -1, // Low — you (or Gumroad support) most likely issued it
        buttons: link("https://app.gumroad.com/customers", "View sales"),
    }
}
```

## Gumroad refund webhook payload (sample)

```json
{
  "card": {
    "type": "visa",
    "visual": "**** **** **** 4242"
  },
  "email": "jane@example.com",
  "price": "4900",
  "sale_id": "yN3kP8sQ1wR5tU7vX9zA2g==",
  "currency": "usd",
  "disputed": "false",
  "quantity": "1",
  "referrer": "direct",
  "refunded": "true",
  "full_name": "Jane Doe",
  "permalink": "acme-course",
  "seller_id": "x7Q2kL0vT1aB7cD3eF5gHw==",
  "ip_country": "Netherlands",
  "product_id": "Qm9vc3RlclRlc3QxMjM0NQ==",
  "can_contact": "true",
  "dispute_won": "false",
  "gumroad_fee": "540",
  "order_number": "524980123",
  "product_name": "Acme Design Course",
  "purchaser_id": "1234567890123",
  "resource_name": "refund",
  "sale_timestamp": "2026-09-29T09:45:53Z",
  "short_product_id": "qzxbt",
  "product_permalink": "https://acme.gumroad.com/l/acme-course",
  "discover_fee_charged": "false",
  "is_gift_receiver_purchase": "false"
}
```

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