# RevenueCat refund notifications on your phone

> Get a push notification when an App Store, Google Play or other purchase tracked by RevenueCat is refunded.

- Company: RevenueCat (https://www.justpush.io/recipes/revenuecat)
- Event: `CANCELLATION` (CANCELLATION)
- Tags: Finance, Payments
- Install: https://studio.justpush.io/recipes/revenuecat/refund
- Web page: https://www.justpush.io/recipes/revenuecat/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 the RevenueCat dashboard, open your project's Integrations > Webhooks and add a new webhook.
3. Paste your webhook URL (shown in Studio after install) as the webhook URL.
4. Send all events, or only CANCELLATION; the recipe ignores every other event type.
5. Save, then click Send test event to confirm it arrives.

## 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
// RevenueCat → CANCELLATION (refunds)
// Fires when a purchase is refunded (RevenueCat sends these as CANCELLATION with cancel_reason CUSTOMER_SUPPORT).
// Docs: https://www.revenuecat.com/docs/integrations/webhooks/event-types-and-fields
//
// Paste this into a JustPush Studio integration and add a RevenueCat webhook pointing
// at the integration's endpoint URL.

// Human-readable store names.
const STORES = {
    APP_STORE: "App Store",
    MAC_APP_STORE: "Mac App Store",
    PLAY_STORE: "Google Play",
    AMAZON: "Amazon",
    STRIPE: "Stripe",
    PADDLE: "Paddle",
    RC_BILLING: "Web Billing",
    ROKU: "Roku",
    PROMOTIONAL: "a promotional grant",
    TEST_STORE: "Test Store",
}

// RevenueCat prices are decimals ("9.99"), not cents. Returns null when there's no price.
function money(amount, currency) {
    const value = Number(amount)

    if (amount == null || !Number.isFinite(value) || !currency) return null

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

// Turn "NL" into 🇳🇱.
function flag(countryCode) {
    return /^[A-Z]{2}$/.test(countryCode ?? "")
        ? String.fromCodePoint(...[...countryCode].map((c) => 127397 + c.charCodeAt(0)))
        : ""
}

// "pro_monthly on App Store from jane@example.com 🇳🇱 (sandbox)"
function where(event) {
    const attributes = event.subscriber_attributes ?? {}
    const who = attributes.$email?.value ?? attributes.$displayName?.value
    const store = STORES[event.store] ?? event.store

    return (
        (event.product_id ?? "a product") +
        (store ? ` on ${store}` : "") +
        (who ? ` from ${who}` : "") +
        (flag(event.country_code) ? ` ${flag(event.country_code)}` : "") +
        (event.environment === "SANDBOX" ? " (sandbox)" : "")
    )
}

function handleRequest(request) {
    const event = (request.body || {}).event

    // "Send test event" in RevenueCat; confirm the connection with a quiet push.
    if (event?.type === "TEST") {
        return { title: "🔔 RevenueCat connected", message: "Test event received", topic: "RevenueCat", priority: -1 }
    }

    // Guard: only handle this event type, so other RevenueCat events on this integration stay quiet.
    if (event?.type !== "CANCELLATION") {
        console.log(`Ignoring RevenueCat event "${event?.type ?? "unknown"}"`)
        return null
    }

    // Only refunds; regular cancellations are handled by the cancellation recipe.
    if (event.cancel_reason !== "CUSTOMER_SUPPORT") {
        console.log(`Skipping "${event.cancel_reason ?? "unknown"}" cancellation — not a refund`)
        return null
    }

    // Refund prices are negative; show the refunded amount as a positive figure.
    const amount = event.price_in_purchased_currency
    const refunded = amount == null ? null : money(Math.abs(Number(amount)), event.currency)

    return {
        title: "↩️ Refund",
        message: (refunded ? `${refunded} refunded for ` : "Refund for ") + where(event),
        topic: "RevenueCat",
        priority: 0, // Normal — app store refunds happen without you, so it's worth knowing
    }
}
```

## RevenueCat CANCELLATION webhook payload (sample)

```json
{
  "event": {
    "id": "CD489E0E-REFUND",
    "type": "CANCELLATION",
    "price": -10.87,
    "store": "APP_STORE",
    "app_id": "app1a2b3c4d5",
    "aliases": [
      "user_8Hq2"
    ],
    "currency": "EUR",
    "product_id": "pro_monthly",
    "app_user_id": "user_8Hq2",
    "environment": "PRODUCTION",
    "period_type": "NORMAL",
    "country_code": "NL",
    "cancel_reason": "CUSTOMER_SUPPORT",
    "transaction_id": "2000000712345678",
    "entitlement_ids": [
      "pro"
    ],
    "purchased_at_ms": 1790584200000,
    "expiration_at_ms": 1793176200000,
    "event_timestamp_ms": 1790584200000,
    "subscriber_attributes": {
      "$email": {
        "value": "jane@example.com",
        "updated_at_ms": 1790584100000
      }
    },
    "original_transaction_id": "2000000612345678",
    "price_in_purchased_currency": -9.99
  },
  "api_version": "1.0"
}
```

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