# RevenueCat subscription expired notifications on your phone

> Get a low-priority push notification when a subscription expires and the customer loses access, with the reason.

- Company: RevenueCat (https://www.justpush.io/recipes/revenuecat)
- Event: `EXPIRATION` (EXPIRATION)
- Tags: Finance, Subscriptions
- Install: https://studio.justpush.io/recipes/revenuecat/expiration
- Web page: https://www.justpush.io/recipes/revenuecat/expiration

## 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 EXPIRATION; 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 → EXPIRATION
// Fires when a subscription expires and the customer loses access.
// 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)" : "")
    )
}

// Why a subscription ended, in words.
const REASONS = {
    UNSUBSCRIBE: "turned off auto-renew",
    BILLING_ERROR: "payment failed",
    DEVELOPER_INITIATED: "canceled by you",
    PRICE_INCREASE: "didn't accept a price increase",
    CUSTOMER_SUPPORT: "refunded",
    SUBSCRIPTION_PAUSED: "paused",
}

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 !== "EXPIRATION") {
        console.log(`Ignoring RevenueCat event "${event?.type ?? "unknown"}"`)
        return null
    }

    const reason = REASONS[event.expiration_reason]

    return {
        title: "⌛ Subscription expired",
        message: where(event) + (reason ? ` — ${reason}` : ""),
        topic: "RevenueCat",
        priority: -1, // Low — you already heard about the cancellation or billing issue
    }
}
```

## RevenueCat EXPIRATION webhook payload (sample)

```json
{
  "event": {
    "id": "CD489E0E-EXPIRATI",
    "type": "EXPIRATION",
    "price": 0,
    "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",
    "transaction_id": "2000000712345678",
    "entitlement_ids": [
      "pro"
    ],
    "purchased_at_ms": 1790584200000,
    "expiration_at_ms": 1793176200000,
    "expiration_reason": "BILLING_ERROR",
    "event_timestamp_ms": 1790584200000,
    "subscriber_attributes": {
      "$email": {
        "value": "jane@example.com",
        "updated_at_ms": 1790584100000
      }
    },
    "original_transaction_id": "2000000612345678",
    "price_in_purchased_currency": 0
  },
  "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.
