react-native-purchases

repository·main·Indexed 22 days ago

https://github.com/revenuecat/react-native-purchases

A client-side SDK for React Native that provides a unified wrapper around StoreKit, Google Play Billing, and RevenueCat Web Billing. It enables developers to implement in-app purchases and subscription tracking for iOS and Android while syncing with the RevenueCat backend. The package includes react-native-purchases-ui for pre-built paywall components and supports Expo via development builds.

Tokens
17.2K
Snippets
67
Records
99
Agent score
78%

What's inside react-native-purchases

  1. Overview of React Native Purchases

    main

    React Native Purchases is a client-side wrapper for the RevenueCat subscription and purchase tracking system. It provides a unified interface for:

    • StoreKit (iOS)
    • Google Play Billing (Android)
    • RevenueCat Web Billing
    • RevenueCat Backend

    It allows developers to fetch products, execute purchases, and track subscription status across platforms using a single React Native API.

  2. Features demonstrated in Magic Weather Sample

    main

    The Magic Weather sample provides implementation examples for the following core RevenueCat workflows:

    FeatureImplementation Location
    Configuring the Purchases SDKApp.tsx
    Building a basic paywallsrc/screens/PaywallScreen.tsx
    Checking subscription statussrc/screens/WeatherScreen.tsx
    Restoring transactionssrc/components/RestorePurchasesButton.tsx
    Identifying the usersrc/components/LoginForm.tsx
    Logging out the usersrc/components/LogoutButton.tsx
  3. Handle multiple offers with StoreProduct in v6

    main

    In v6, a StoreProduct can contain multiple free trials and introductory offers.

    • Automatic Selection: When calling purchase() with a Package or StoreProduct, the SDK automatically selects an option by filtering out offers with the rc-ignore-default-offer tag and choosing the one with the longest free trial or cheapest first phase. If none match, it falls back to the base plan.
    • Manual Selection: For granular control, you can inspect the subscriptionOptions array on the StoreProduct to find specific offers (e.g., base plans, free trials, or intro offers).
    // Example of inspecting subscription options on a StoreProduct
    const basePlan = storeProduct.subscriptionOptions?.filter((option) => { option.isBasePlan });
    const defaultOption = storeProduct.defaultOption;
    const freeOffer = storeProduct.subscriptionOptions?.filter((option) => { !!option.freePhase });
    const trialOffer = storeProduct.subscriptionOptions?.filter((option) => { !!option.introPhase });
  4. Understand the Maestro E2E Test App configuration

    main

    The E2E tests rely on a specific RevenueCat project configuration to pass assertions:

    • V2 Paywall: The tests assert that a "Paywall V2" is visible.
    • Entitlements: The tests check for a pro entitlement status after a successful purchase.
    • Environment: The tests use the Test Store environment for purchase confirmation.

    Dependency Resolution

    This app is part of the react-native-purchases Yarn workspace. It uses Babel module-resolver aliases and Metro configuration (watchFolders and exclusionList) to ensure that imports for react-native-purchases and react-native-purchases-ui point to the local SDK source directories on the current branch rather than a published npm version.

  5. Install React Native Purchases UI

    main

    Install react-native-purchases-ui to use pre-built UI components for presenting paywalls in your React Native application.

    Expo Compatibility:

    • Expo Go: Compatible via a Preview API mode. Note that because react-native-purchases-ui requires native modules not present in Expo Go, the APIs will be available but will have no effect.
    • Development Builds: To test the real behavior of the RevenueCat SDK and paywalls, you must create a development build.
  6. Configure the Galaxy Store add-on for react-native-purchases

    main

    To use the Galaxy Store with react-native-purchases, you must configure the SDK with a specific store value of "GALAXY" and provide a galaxyBillingMode. The billing mode is controlled via the GALAXY_BILLING_MODE enum exported from react-native-purchases-store-galaxy.

    import Purchases from "react-native-purchases";
    import { GALAXY_BILLING_MODE } from "react-native-purchases-store-galaxy";
    
    Purchases.configure({
      apiKey: "galx_XYZ",
      store: "GALAXY",
      galaxyBillingMode: GALAXY_BILLING_MODE.TEST,
    });
  7. Set up the Maestro E2E Test App

    main

    The MaestroTestApp is a minimal React Native application used to verify RevenueCat SDK integration via Maestro end-to-end tests.

    Prerequisites

    • Node.js & Yarn
    • Xcode (for iOS) or Android Studio (for Android)
    • Maestro CLI
    • CocoaPods (gem install cocoapods)

    Installation

    Run the following commands from the e2e-tests/MaestroTestApp directory:

    yarn install
    cd ios && pod install && cd ..
  8. Configure the RevenueCat API Key for local testing

    main

    The app uses the placeholder MAESTRO_TESTS_REVENUECAT_API_KEY by default. To run the app locally with a real RevenueCat project, you must provide a valid API key.

    Warning: Do not commit real API keys to version control.

    Choose one of the following methods:

    1. Manual Edit: Replace the placeholder string MAESTRO_TESTS_REVENUECAT_API_KEY directly in App.tsx with your valid API key.
    2. Environment Variable: Export the environment variable and use a sed command (similar to the one used in the Fastlane CI pipeline) to inject the key before building.
  9. Configure RevenueCat for the Magic Weather Sample

    main

    Before running the sample app, you must set up your RevenueCat dashboard and local constants to match your store configuration:

    RevenueCat Dashboard Setup

    1. Add Products: Add your App Store or Play Store product IDs (e.g., rc_3999_1y) to the RevenueCat dashboard.
    2. Create Entitlements: Attach these products to an entitlement (e.g., premium).
    3. Create Offerings: Attach the product to a package (e.g., Annual) inside an offering (e.g., sale or default).

    Local Configuration

    Edit src/constants/index.ts to provide the app with the necessary credentials:

    • API_KEY: Your RevenueCat project API keys.
    • entitlementID: The ID of the entitlement you created (e.g., premium).