# In-app purchases on iOS and Android

> Sell in an Expo, React Native or Swift app through the owner's own RevenueCat account, and gate on the server with vibiz.payments.hasActive.

In-app purchases go through **the owner's own RevenueCat account**; there is no thelastapi fee on them. Never ask for the owner's RevenueCat key in the chat.

1. **Connected?** `vibiz_payments { action: "app_store_status" }`. If not, `vibiz_payments { action: "connect_app_store" }` and give the owner the `url`: they paste a V2 secret key from their RevenueCat dashboard ▸ Project settings ▸ API keys there. You can't complete it.
2. **Catalog.** Plans are the project's products and prices with a `lookup_key` (`vibiz_payments create_product`, in each environment you use).
3. **Set up** (one RevenueCat project per app: a project there has one current offering). Read the bundle id from `app.json` (`expo.ios.bundleIdentifier`, `expo.android.package`) or the Xcode target, then `vibiz_payments { action: "app_store_setup", project_id, env: "development", ios_bundle_id: "com.acme.bakery" }` (and again with `env: "production"`). Keep `ios.public_key` and repeat `next` to the owner: the App Store product ids to create in App Store Connect, the store credentials to upload in their RevenueCat dashboard, and the webhook if it wasn't registered.
4. **The app.** The public key is safe to ship. Use the user id the app already uses for web checkout.

Expo / React Native (`npx expo install react-native-purchases`; needs a development build, not Expo Go):

```ts
import Purchases from 'react-native-purchases';

// app.config: extra or EXPO_PUBLIC_REVENUECAT_IOS_KEY = the VIBIZ_REVENUECAT_IOS_KEY value
Purchases.configure({ apiKey: process.env.EXPO_PUBLIC_REVENUECAT_IOS_KEY!, appUserID: user.id });
// …or after sign-in:
await Purchases.logIn(user.id);

const offerings = await Purchases.getOfferings();
const monthly = offerings.current?.monthly; // the `$rc_monthly` package
if (monthly) await Purchases.purchasePackage(monthly);
```

Swift (the RevenueCat SDK, Swift Package Manager `purchases-ios-spm`):

```swift
import RevenueCat

Purchases.configure(withAPIKey: "<VIBIZ_REVENUECAT_IOS_KEY>", appUserID: user.id)
let offerings = try await Purchases.shared.offerings()
if let pkg = offerings.current?.monthly { _ = try await Purchases.shared.purchase(package: pkg) }
```

5. **Gate on the server**, never only in the app:

```ts
import { vibiz } from '@vibiz/app';
if (!(await vibiz.payments.hasActive(userId, 'pro'))) return new Response('Upgrade', { status: 402 });
```

`hasActive(user, key)` takes the entitlement key (`pro`; prefer it), a lookup key (`pro-monthly`), a price id or a store product id, in that order of precedence (a string that is an entitlement key matches only as one), and answers for web checkout and the stores alike. `vibiz.payments.entitlements(user)` lists what the user has, each with its `source`.

6. **Test** with an App Store sandbox tester (or a Google Play license tester): sandbox purchases land in `development`; `vibiz_payments { action: "subscriptions", env: "development", user }` shows them with `source: "app_store"`.

Facts to repeat: Apple or Google pay the owner (after their commission), thelastapi takes no fee on store purchases; the user cancels a store subscription in the store, not through thelastapi.
