# RevenueCat, Stripe, App Store and Google Play

> Send purchases, renewals and refunds from RevenueCat, Stripe, the App Store or Google Play to Herald as events, and use a revenue event as an agent goal.

:::note Built and tested against each vendor's documented API; tell us if anything differs in your account.:::

Revenue events reach Herald from RevenueCat, Stripe, the App Store or Google Play as ordinary events, so an agent can learn from them. Every revenue event carries `revenue` (in major units: 9.99, not 999) and `currency`, so a goal tier can weight by value.

## What you'll need

- An admin of the account. Integrations are set up in **Settings → Integrations**.
- **RevenueCat:** access to the project's webhooks.
- **Stripe:** access to Developers → Webhooks, to add an endpoint and copy its signing secret.
- **App Store:** access to the app's App Store Server Notifications in App Store Connect.
- **Google Play:** a Google Cloud project for Pub/Sub, and a service account with **View financial data** on the app in Play Console.
- Purchases tagged with your own user id, as described under each store.

## Use a revenue event as a goal

Pick `subscription_started` (or `purchase` for a one-off sale) as an agent's goal. The agent then learns which messages lead to paying. The goal picker lists the event names seen for the app, so a name appears once the first webhook arrives.

## RevenueCat

1. In **Settings → Integrations**, connect RevenueCat and copy the URL and secret.
2. In RevenueCat, open **Integrations → Webhooks** and add a webhook. Paste the URL.
3. Set the **Authorization header value** to the secret. It must equal the secret, with or without a `Bearer ` prefix.
4. Set RevenueCat's **App User ID** to the same user id your app sends Herald.

Option: **Include sandbox purchases** (off by default).

| RevenueCat `type` | Event | `subscription_status` attribute |
|---|---|---|
| `INITIAL_PURCHASE` with `period_type` `TRIAL` | `trial_started` | `active` |
| `INITIAL_PURCHASE` | `subscription_started` | `active` |
| `RENEWAL` with `is_trial_conversion: true` | `trial_converted` | `active` |
| `RENEWAL` | `subscription_renewed` | `active` |
| `NON_RENEWING_PURCHASE` | `purchase` | unchanged |
| `CANCELLATION` | `subscription_cancelled` | `cancelled` |
| `UNCANCELLATION` | `subscription_uncancelled` | `active` |
| `EXPIRATION` | `subscription_expired` | `expired` |
| `BILLING_ISSUE` | `billing_issue` | `billing_issue` |
| `PRODUCT_CHANGE` | `subscription_changed` | `active` |
| `TEST` | Nothing ingested; answers `200`, counted in `skipped` | |
| Any other type | Skipped | |

- Properties: `revenue` is RevenueCat's `price`, `currency` is `USD` (its `price` is always USD), plus `product_id`, `store`, `environment` and `period_type`. `price` can be 0 (trials) or negative (refunds).
- Anonymous ids (`$RCAnonymousID:…`) are skipped. Sandbox events are skipped unless the option is on.
- The event id is `revenuecat:<event.id>`, so RevenueCat's retries count as duplicates.
- `subscription_status` is set on the user with the event, so segments can target it.

### Common errors

| Message | Cause | Fix |
|---|---|---|
| `Unauthorized` | `401`: the **Authorization header value** isn't the secret, or the integration is paused | Paste the secret again, with or without `Bearer ` |
| `Expected a JSON object` | `400`: the body isn't a JSON object | Use RevenueCat's webhook, not a custom body |

## Stripe

1. In **Settings → Integrations**, connect Stripe to get the URL.
2. In Stripe, open **Developers → Webhooks** and add an endpoint with that URL. Choose `checkout.session.completed`, `invoice.paid`, `invoice.payment_failed`, `customer.subscription.deleted` and `charge.refunded`.
3. Paste the endpoint's signing secret (`whsec_…`) into the console. Stripe makes this secret. Until it is set, deliveries answer `401`.
4. Put your user id in `metadata.herald_user_id` on checkout sessions or subscriptions, or in `client_reference_id` on checkout.

Options: **Include test mode events** (off by default). After rolling the secret in Stripe, paste the new one with **Rotate secret**.

| Stripe event | Event | `revenue` from |
|---|---|---|
| `checkout.session.completed` | `purchase` | `amount_total` |
| `invoice.paid` with `billing_reason: subscription_create` | `subscription_started` | `amount_paid` |
| `invoice.paid`, other reasons | `subscription_renewed` | `amount_paid` |
| `invoice.payment_failed` | `billing_issue` | `amount_due` |
| `customer.subscription.deleted` | `subscription_cancelled` | none |
| `charge.refunded` | `refund` | `amount_refunded` |
| Any other type | Skipped: answers `200`, counted in `skipped` | |

- `revenue` is the minor-unit amount divided by the currency's exponent: 100 for most currencies, no division for zero-decimal currencies such as JPY and KRW, 1000 for BHD, JOD, KWD, OMR and TND. `currency` is lower case, as Stripe sends it. There is no currency conversion.
- The signature is `Stripe-Signature`, checked against the signing secret, and its timestamp must be within 5 minutes of now.
- An event with no user id is skipped and counted. Herald never calls Stripe's API.
- The event id is `stripe:<event.id>`.

### Common errors

| Message | Cause | Fix |
|---|---|---|
| `Unauthorized` | `401`: no signing secret is set, it doesn't match, or the `Stripe-Signature` timestamp is more than 5 minutes old | Paste the endpoint's `whsec_…` secret; after rolling it in Stripe, use **Rotate secret** |

## App Store and Google Play

For apps that don't use RevenueCat: Apple and Google tell Herald about purchases directly. The events and the `subscription_status` attribute are the same as RevenueCat's, so goals work unchanged: pick `subscription_started` (or `trial_started`, `purchase`) as the agent's goal.

**Identity (required).** A store notification doesn't say who the person is unless the app put it on the purchase. Herald never creates people from store notifications: an unmatched purchase answers `200` and is counted in `skipped`.

- **Apple:** set `appAccountToken` when the app starts a purchase. If the user id your app sends Herald is a UUID, use it. Otherwise create a UUID, store it with the user, and send it to Herald as the `app_account_token` attribute. Herald matches the person whose user id equals the token (case-insensitive), then the person whose `app_account_token` attribute equals it.
- **Google:** set `obfuscatedAccountId` (`BillingFlowParams.setObfuscatedAccountId`) to the user id your app sends Herald. Herald matches it exactly.

### App Store

1. In **Settings → Integrations**, connect App Store with the app's **Bundle ID**. Option: **Include sandbox purchases** (off by default).
2. In App Store Connect, open your app → **App Information → App Store Server Notifications**. Paste the URL as the **Production Server URL** (and as the Sandbox URL if sandbox is on). Choose **Version 2**.

There is no secret: Apple's signature is the authentication. A notification with a missing or invalid signature answers `401` and nothing is ingested. **Test** can't sign anything for Apple: request a test notification with the App Store Server API and watch the event count (a `TEST` notification counts in `skipped`). A notification for another bundle id answers `200` (skipped) and shows as the last error. `environment: Sandbox` is skipped unless sandbox is on.

| Apple `notificationType` / `subtype` | Event | `subscription_status` attribute |
|---|---|---|
| `SUBSCRIBED` / `INITIAL_BUY`, transaction `offerDiscountType: FREE_TRIAL` | `trial_started` | `active` |
| `SUBSCRIBED` / `INITIAL_BUY` or `RESUBSCRIBE` | `subscription_started` | `active` |
| `DID_RENEW` | `subscription_renewed` | `active` |
| `DID_CHANGE_RENEWAL_STATUS` / `AUTO_RENEW_DISABLED` | `subscription_cancelled` | `cancelled` |
| `EXPIRED` | `subscription_expired` | `expired` |
| `DID_FAIL_TO_RENEW` | `billing_issue` | `billing_issue` |
| `REFUND` | `refund` | unchanged |
| `ONE_TIME_CHARGE` (consumables, non-consumables, non-renewing) | `purchase` | unchanged |
| `TEST`, any other type | Skipped: answers `200`, counted in `skipped` | |

- `revenue` is the transaction's `price` / 1000 (Apple's price is in milliunits), plus `currency`, `product_id`, `store: "app_store"` and `environment`.
- The event id is `app_store:<notificationUUID>` (Apple's retries reuse it) and the event time is `signedDate`.

#### Common errors

| Message | Cause | Fix |
|---|---|---|
| `Unauthorized` | `401`: the notification's Apple signature is missing or invalid | Use the URL as a Version 2 notification URL |
| `Notification for bundle id <id>, not this integration's` | Shown as the last error; the notification is skipped | Set the integration's **Bundle ID** to the app's |

### Google Play

1. In Google Cloud, create a Pub/Sub topic and give `google-play-developer-notifications@system.gserviceaccount.com` the **Publisher** role.
2. Add a **push** subscription with the URL from the console, turn on **authentication** and pick a service account. Enter that account's email in the console (`pushServiceAccount`).
3. In Play Console → **Monetize → Monetization setup**, enter the topic.
4. Create a service account with access to your app in Play Console (**Users and permissions → View financial data**) and paste its JSON key as the secret.

Config: `packageName` (required) and the option **Include test purchases** (off by default). **Test** signs in with the key and reads the app's subscriptions to prove access.

The push request must carry a Google-signed token for your push service account and the exact webhook URL as its audience; otherwise it answers `401`. A notification only says something changed, so Herald reads the purchase from the Play Developer API before anything counts. Play answering 404 or 410 answers `200` (skipped); any other Play failure answers `503` so Pub/Sub retries, and shows as the last error. A notification for another package answers `200` (skipped) and shows as the last error.

| Google Play notification | Event | `subscription_status` attribute |
|---|---|---|
| subscription `PURCHASED`, line item with a free trial | `trial_started` | `active` |
| subscription `PURCHASED` | `subscription_started` | `active` |
| subscription `RENEWED`, `RECOVERED` | `subscription_renewed` | `active` |
| subscription `CANCELED` | `subscription_cancelled` | `cancelled` |
| subscription `EXPIRED`, `REVOKED` | `subscription_expired` | `expired` |
| subscription `IN_GRACE_PERIOD`, `ON_HOLD` | `billing_issue` | `billing_issue` |
| one-time `ONE_TIME_PRODUCT_PURCHASED` | `purchase` | unchanged |
| `voidedPurchaseNotification` | `refund` | unchanged |
| `testNotification`, any other type | Skipped: answers `200`, counted in `skipped` | |

- Test purchases are skipped unless the option is on.
- `revenue` and `currency` come from the line item's recurring price when present; there is none for a free trial or a one-time product (the API gives no price). Also `product_id` and `store: "google_play"`.
- The event id is `google_play:<message.messageId>` (Pub/Sub redeliveries reuse it) and the event time is `eventTimeMillis`.

#### Common errors

| Message | Cause | Fix |
|---|---|---|
| `Unauthorized` | `401`: the push request has no Google-signed token for `pushServiceAccount`, or its audience isn't the exact URL | Turn on authentication on the push subscription and enter that account's email |
| `Expected a Pub/Sub push message` | `400`: the body isn't a Pub/Sub push | Use a push subscription on the Play topic |
| `Notification for package <got>, not <package>` | Shown as the last error; the notification is skipped | Set `packageName` to the app's |
| `Google's keys are unavailable; try again` or `Google Play is unavailable; try again` | `503`: Google couldn't confirm the notification now | None: Pub/Sub retries |
| `Google Play answered HTTP <status>` | Shown as the last error: Google Play refused the purchase lookup | Check the service account's access in Play Console |
| `Google Play doesn't know the app <package>` | **Test**: the service account can't see the app | Grant it access to the app, or fix `packageName` |

## Troubleshooting

- **No goal to pick.** Event names appear in the picker after the first webhook arrives. Use **Send test** in the vendor, or make a sandbox purchase with the sandbox option on.
- **Events are skipped.** Check that the vendor's user id matches the id your app sends Herald. For the App Store and Google Play, check that the purchase carries `appAccountToken` or `obfuscatedAccountId` as described above.
