VPNBuilder Developers vpnbuilder.org

In-app purchases with RevenueCat

Sell subscriptions in your apps with RevenueCat and let VPNBuilder activate the customer's plan automatically through a webhook. This guide covers the RevenueCat setup, how events map to plans, and how to grant plans sold elsewhere (e.g. Stripe) with the Reseller API.

How it fits together

app ──Purchases.logIn("vb_42")──▶ RevenueCat ◀──purchase── App Store / Play Store
                                      │
                                      └──webhook──▶ POST /v2/webhooks/revenuecat ──▶ account 42 active
app ──GET /v2/me──▶ subscription.active = true

VPNBuilder keeps one entitlement per account (user.subscription). The RevenueCat webhook and the Reseller API both write it; the app only reads it.

1. Identify the customer in RevenueCat

After every sign-in (login, register, reset, TV sign-in), log the customer in to RevenueCat with the revenuecat_app_user_id from the user object. It is always vb_<account id>:

// iOS (RevenueCat SDK)
let (customerInfo, _) = try await Purchases.shared.logIn(user.revenuecatAppUserId) // "vb_42"
// Android
Purchases.sharedInstance.logInWith(user.revenuecatAppUserId) { customerInfo, created -> /* ... */ }

Call Purchases.logOut() when the user signs out. Do not use anonymous RevenueCat ids for purchases: events whose ids are all $RCAnonymousID… cannot be matched to an account and are only recorded.

2. Configure the webhook

In the RevenueCat dashboard (Project → Integrations → Webhooks):

Field Value
Webhook URL https://mercury.vpnbuilder.org/v2/webhooks/revenuecat
Authorization header A long random secret, e.g. Bearer 9f2c7a1e5b8d4f6a0c3e7b2d9a1f4c8e

Send the same secret to the platform operator, who stores it as your brand's revenuecat.secret:

php artisan vb:brand:set acme revenuecat.secret 9f2c7a1e5b8d4f6a0c3e7b2d9a1f4c8e

The header may be Bearer <secret> or just <secret>; the secret identifies your brand, so the webhook needs no app key. A wrong or missing secret is 401 unauthenticated; a body without event.id or event.type is 422 validation_failed.

Optional brand settings (set by the operator):

Setting Default Effect
revenuecat.entitlement none Only events carrying this entitlement id (entitlement_id / entitlement_ids) change the plan; others are recorded only. Set it if your RevenueCat project has several entitlements.
revenuecat.allow_sandbox true When false, SANDBOX events are recorded only. Turn it off in production once testing is done.
manage_urls.<source> App Store / Play Store subscription pages The manage_url returned for that source.

3. What each event does

Event type Effect
INITIAL_PURCHASE, RENEWAL, UNCANCELLATION, PRODUCT_CHANGE, NON_RENEWING_PURCHASE Plan active: type from the product id, expires_at from expiration_at_ms (null = no expiry), source from store
EXPIRATION Plan inactive
anything else (CANCELLATION, BILLING_ISSUE, SUBSCRIPTION_PAUSED, TRANSFER, TEST, …) Recorded only. A cancelled subscription stays active until its EXPIRATION event.

The response body is {"status": "processed" | "recorded" | "duplicate"} (always 200 when the secret and body are valid):

  • processed: the plan was changed.
  • recorded: stored but no change (unknown account, ignored type, older than the last applied event, sandbox disabled, other entitlement).
  • duplicate: this event id was already received for your brand. Webhooks are idempotent, so RevenueCat retries are safe.

Ordering: events with an event_timestamp_ms older than the last applied event for that account are recorded only, so a late retry never undoes a newer renewal.

Even without an expiration event, a plan stops counting as active once its expires_at is in the past.

Product id → plan type

The plan type is derived from the product id (new_product_id for product changes):

Product id contains type
lifetime Lifetime Account
<n> + year/yr (e.g. acme_1_year, vpn.2yr) <n> Year
<n> + month/mo (e.g. monthly_3mo, acme.1month) <n> Month
<n> + week/wk <n×7> Day
<n> + day <n> Day
annual or year (no number) 1 Year
quarter 3 Month
month 1 Month
week 7 Day
anything else the product id itself

Store → source

RevenueCat store subscription.source
APP_STORE app_store
MAC_APP_STORE mac_app_store
PLAY_STORE play_store
STRIPE, RC_BILLING stripe
any other store manual
missing unchanged

Customers from older systems

Besides vb_<id>, the webhook also matches (within your brand) the legacy aliases mvpn_<id> and email addresses found in app_user_id, original_app_user_id and aliases. If such an alias belongs to a customer of your older system who has not signed in to the new app yet, their account is created (without a password; they set one with "forgot password" or sign in with their old password).

4. In the app after a purchase

The webhook usually arrives within seconds. After a successful purchase:

  1. Call GET /v2/me and check user.subscription.active.
  2. If it is still false, retry a few times with a short delay (e.g. 2s, 4s, 8s) before showing "Your purchase is being processed".
  3. Then fetch the VPN profile and connect.

Show a "Manage subscription" button that opens subscription.manage_url (Apple/Google subscription settings, or your own billing page for stripe and manual).

5. Billing outside the app stores (Stripe, etc.)

If customers pay on your website, set the plan from your backend with the Reseller API, e.g. in your Stripe webhook handler:

curl -X PUT https://mercury.vpnbuilder.org/reseller/v2/accounts/42/entitlement \
  -H 'Accept: application/json' -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer sk_acme_Q7vTn2LxP9sRk4Wb8yHc1Me6Ud3Fj5Ga0Zq7Np2Vt9Xs4Kr' \
  -d '{"active": true, "type": "1 Month", "expires_at": "2027-01-02T00:00:00Z", "source": "stripe"}'
  • active is required; type, expires_at and source are optional and keep their current value when omitted. source is one of app_store, play_store, mac_app_store, stripe, manual.
  • Send {"active": false} when the subscription ends.
  • Find the account id with GET /reseller/v2/accounts?email=…, or create the account with POST /reseller/v2/accounts.

RevenueCat and the Reseller API write the same entitlement: the latest write wins. If you sell through both, let one system own each customer.