Handle flow & paywall events - React Native

This guide covers event handling for purchases, restorations, product selection, and flow rendering. You can also set up button handling (closing the flow, opening links, custom actions, etc.). See our guide on handling button actions for details.

Flows and paywalls built with the Flow Builder don’t need extra code to make and restore purchases. However, they generate some events that your app can respond to. Those events include button presses (close buttons, URLs, product selections, and so on) as well as notifications on purchase-related actions taken on the flow. Learn how to respond to these events below.

To control or monitor processes occurring on the flow screen within your mobile app, implement event handlers:

Event examples (Click to expand)
// onCloseButtonPress
{
  //Record the event
}

// onAndroidSystemBack
{
  //Record the event
}

// onUrlPress
{
  "url": "https://example.com/terms"
}

// onCustomAction
{
  "actionId": "login"
}

// onProductSelected
{
  "productId": "premium_monthly"
}

// onPurchaseStarted
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onPurchaseCompleted - Success
{
  "purchaseResult": {
    "type": "success",
    "profile": {
      "accessLevels": {
        "premium": {
          "id": "premium",
          "isActive": true,
          "expiresAt": "2024-02-15T10:30:00Z"
        }
      }
    }
  },
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onPurchaseCompleted - Cancelled
{
  "purchaseResult": {
    "type": "user_cancelled"
  },
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onPurchaseFailed
{
  "error": {
    "code": "purchase_failed",
    "message": "Purchase failed due to insufficient funds",
    "details": {
      "underlyingError": "Insufficient funds in account"
    }
  },
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onRestoreCompleted
{
  "profile": {
    "accessLevels": {
      "premium": {
        "id": "premium",
        "isActive": true,
        "expiresAt": "2024-02-15T10:30:00Z"
      }
    },
    "subscriptions": [
      {
        "vendorProductId": "premium_monthly",
        "isActive": true,
        "expiresAt": "2024-02-15T10:30:00Z"
      }
    ]
  }
}

// onRestoreFailed
{
  "error": {
    "code": "restore_failed",
    "message": "Purchase restoration failed",
    "details": {
      "underlyingError": "No previous purchases found"
    }
  }
}

// onError
{
  "error": {
    "code": "rendering_failed",
    "message": "Failed to render flow interface",
    "details": {
      "underlyingError": "Invalid flow configuration"
    }
  }
}

// onLoadingProductsFailed
{
  "error": {
    "code": "products_loading_failed",
    "message": "Failed to load products from the server",
    "details": {
      "underlyingError": "Network timeout"
    }
  }
}

// onAppeared
{
  //Record the event
}

// onDisappeared
{
  //Record the event
}

// onWebPaymentNavigationFinished
{
  //Record the event
}

You can register event handlers you need, and miss those you do not need. In this case, unused event listeners would not be created. There are no required event handlers.

Event handlers return a boolean. If true is returned, the displaying process is considered complete, thus the flow screen closes and event listeners for this view are removed.

Some event handlers have a default behavior that you can override if needed:

  • onCloseButtonPress: closes the flow when close button pressed.
  • onUrlPress: opens the tapped URL and keeps the flow open.
  • onAndroidSystemBack (only for modal presentation): keeps the flow open when the Back button is pressed. Return true to close it.
  • onRestoreCompleted: keeps the flow open after a successful restore. Return true to close it.
  • onPurchaseCompleted: keeps the flow open after a purchase completes. Return true to close it.
  • onError: closes the flow if its rendering fails.

Event handlers

Event handlerDescription
onCustomActionInvoked when a user performs a custom action, e.g., clicks a custom button.
onUrlPressInvoked when a user clicks a URL in your flow.
onAndroidSystemBackModal presentation only: Invoked when a user taps the system Android Back button.
onCloseButtonPressInvoked when the close button is visible and a user taps it. It is recommended to dismiss the flow screen in this handler.
onPurchaseCompletedInvoked when the purchase completes, whether successful, cancelled by user, or pending approval. In case of a successful purchase, it provides an updated AdaptyProfile. User cancellations and pending payments (e.g., parental approval required) trigger this event, not onPurchaseFailed.
onPurchaseStartedInvoked when a user taps the “Purchase” action button to start the purchase process.
onPurchaseFailedInvoked when a purchase fails due to errors (e.g., payment restrictions, invalid products, network failures, transaction verification failures). Not invoked for user cancellations or pending payments, which trigger onPurchaseCompleted instead.
onRestoreStartedInvoked when a user starts a purchase restoration process.
onRestoreCompletedInvoked when purchase restoration succeeds and provides an updated AdaptyProfile. It is recommended to dismiss the screen if the user has the required accessLevel. Refer to the Subscription status topic to learn how to check it.
onRestoreFailedInvoked when the restore process fails and provides AdaptyError.
onProductSelectedInvoked when any product in the flow view is selected, allowing you to monitor what the user selects before the purchase.
onErrorInvoked when an error occurs during view rendering and provides AdaptyError. Such errors should not occur, so if you come across one, please let us know.
onLoadingProductsFailedInvoked when product loading fails and provides AdaptyError. If you haven’t set prefetchProducts: true in view creation, AdaptyUI will retrieve the necessary objects from the server by itself.
onAppearedInvoked when the flow is displayed to the user. On iOS, also invoked when a user taps the web paywall button inside a flow, and a web paywall opens in an in-app browser.
onDisappearedModal presentation only: Invoked when the flow is closed by the user. On iOS, also invoked when a web paywall opened from a flow in an in-app browser disappears from the screen.
onWebPaymentNavigationFinishedInvoked after attempting to open a web paywall for purchase, whether successful or failed.
onAnalyticsReserved for custom analytic events from a flow. Flows don’t emit these to your code yet, so you don’t need to implement it.
onRequestAppReviewReserved for app-review requests from a flow. Flows don’t trigger app-review requests yet, so you don’t need to implement it.
onRequestPermissionReserved for system-permission requests (such as push notifications or camera access) from a flow. Flows don’t trigger permission requests yet, so you don’t need to implement it.
onObserverPurchaseInitiatedObserver mode only: Invoked when a user taps the purchase button in a flow. Adapty does not make the purchase — perform it with your own purchase code, then report the transaction to Adapty. See Handle purchases in observer mode below.
onObserverRestoreInitiatedObserver mode only: Invoked when a user taps the restore button in a flow. Adapty does not restore — perform it yourself, then report any restored transactions. See Handle purchases in observer mode below.

Handle purchases in observer mode

If you activated the SDK in Observer mode (observerMode: true) and present an Adapty-rendered flow, the SDK does not make purchases for you. When a user taps the purchase or restore button, the SDK invokes onObserverPurchaseInitiated or onObserverRestoreInitiated instead. Perform the purchase or restore with your own code, drive the flow’s loading indicator with the provided callbacks, and report the transaction to Adapty afterwards.

const unsubscribe = view.setEventHandlers({
  onObserverPurchaseInitiated(product, onStartPurchase, onFinishPurchase) {
    onStartPurchase(); // show the flow's loading indicator
    myPurchaseApi(product.vendorProductId)
      .then((transactionId) => adapty.reportTransaction(transactionId))
      .finally(() => onFinishPurchase()); // hide the loading indicator
    return false; // keep the flow open; dismiss it yourself after success
  },
  onObserverRestoreInitiated(onStartRestore, onFinishRestore) {
    onStartRestore();
    myRestoreApi()
      .finally(() => onFinishRestore());
    return false;
  },
});

This guide covers event handling for purchases, restorations, product selection, and paywall rendering. You must also implement button handling (closing paywall, opening links, etc.). See our guide on handling button actions for details.

Paywalls configured with the Paywall Builder don’t need extra code to make and restore purchases. However, they generate some events that your app can respond to. Those events include button presses (close buttons, URLs, product selections, and so on) as well as notifications on purchase-related actions taken on the paywall. Learn how to respond to these events below.

This guide is for new Paywall Builder paywalls only which require Adapty SDK v3.0 or later.

To control or monitor processes occurring on the paywall screen within your mobile app, implement event handlers:

Event examples (Click to expand)
// onCloseButtonPress
{
  //Record the event
}

// onAndroidSystemBack
{
  //Record the event
}

// onUrlPress
{
  "url": "https://example.com/terms"
}

// onCustomAction
{
  "actionId": "login"
}

// onProductSelected
{
  "productId": "premium_monthly"
}

// onPurchaseStarted
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onPurchaseCompleted - Success
{
  "purchaseResult": {
    "type": "success",
    "profile": {
      "accessLevels": {
        "premium": {
          "id": "premium",
          "isActive": true,
          "expiresAt": "2024-02-15T10:30:00Z"
        }
      }
    }
  },
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onPurchaseCompleted - Cancelled
{
  "purchaseResult": {
    "type": "user_cancelled"
  },
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onPurchaseFailed
{
  "error": {
    "code": "purchase_failed",
    "message": "Purchase failed due to insufficient funds",
    "details": {
      "underlyingError": "Insufficient funds in account"
    }
  },
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "price": {
      "amount": 9.99,
      "currencyCode": "USD",
      "currencySymbol": "$",
      "localizedString": "$9.99"
    }
  }
}

// onRestoreCompleted
{
  "profile": {
    "accessLevels": {
      "premium": {
        "id": "premium",
        "isActive": true,
        "expiresAt": "2024-02-15T10:30:00Z"
      }
    },
    "subscriptions": [
      {
        "vendorProductId": "premium_monthly",
        "isActive": true,
        "expiresAt": "2024-02-15T10:30:00Z"
      }
    ]
  }
}

// onRestoreFailed
{
  "error": {
    "code": "restore_failed",
    "message": "Purchase restoration failed",
    "details": {
      "underlyingError": "No previous purchases found"
    }
  }
}

// onRenderingFailed
{
  "error": {
    "code": "rendering_failed",
    "message": "Failed to render paywall interface",
    "details": {
      "underlyingError": "Invalid paywall configuration"
    }
  }
}

// onLoadingProductsFailed
{
  "error": {
    "code": "products_loading_failed",
    "message": "Failed to load products from the server",
    "details": {
      "underlyingError": "Network timeout"
    }
  }
}

// onPaywallShown
{
  //Record the event
}

// onPaywallClosed
{
  //Record the event
}

// onWebPaymentNavigationFinished
{
  //Record the event
}

You can register event handlers you need, and miss those you do not need. In this case, unused event listeners would not be created. There are no required event handlers.

Event handlers return a boolean. If true is returned, the displaying process is considered complete, thus the paywall screen closes and event listeners for this view are removed.

Some event handlers have a default behavior that you can override if needed:

  • onCloseButtonPress: closes paywall when close button pressed.
  • onUrlPress: opens the tapped URL and keeps the paywall open.
  • onAndroidSystemBack (only for modal presentation): closes paywall when the Back button pressed.
  • onRestoreCompleted: closes paywall after successful restore.
  • onPurchaseCompleted: closes paywall unless user cancelled.
  • onRenderingFailed: closes paywall if its rendering fails.

Event handlers

Event handlerDescription
onCustomActionInvoked when a user performs a custom action, e.g., clicks a custom button.
onUrlPressInvoked when a user clicks a URL in your paywall.
onAndroidSystemBackModal presentation only: Invoked when a user taps the system Android Back button.
onCloseButtonPressInvoked when the close button is visible and a user taps it. It is recommended to dismiss the paywall screen in this handler.
onPurchaseCompletedInvoked when the purchase completes, whether successful, cancelled by user, or pending approval. In case of a successful purchase, it provides an updated AdaptyProfile. User cancellations and pending payments (e.g., parental approval required) trigger this event, not onPurchaseFailed.
onPurchaseStartedInvoked when a user taps the “Purchase” action button to start the purchase process.
onPurchaseFailedInvoked when a purchase fails due to errors (e.g., payment restrictions, invalid products, network failures, transaction verification failures). Not invoked for user cancellations or pending payments, which trigger onPurchaseCompleted instead.
onRestoreStartedInvoked when a user starts a purchase restoration process.
onRestoreCompletedInvoked when purchase restoration succeeds and provides an updated AdaptyProfile. It is recommended to dismiss the screen if the user has the required accessLevel. Refer to the Subscription status topic to learn how to check it.
onRestoreFailedInvoked when the restore process fails and provides AdaptyError.
onProductSelectedInvoked when any product in the paywall view is selected, allowing you to monitor what the user selects before the purchase.
onRenderingFailedInvoked when an error occurs during view rendering and provides AdaptyError. Such errors should not occur, so if you come across one, please let us know.
onLoadingProductsFailedInvoked when product loading fails and provides AdaptyError. If you haven’t set prefetchProducts: true in view creation, AdaptyUI will retrieve the necessary objects from the server by itself.
onPaywallShownInvoked when the paywall is displayed to the user. On iOS, also invoked when a user taps the web paywall button inside a paywall, and a web paywall opens in an in-app browser.
onPaywallClosedModal presentation only: Invoked when the paywall is closed by the user. On iOS, also invoked when a web paywall opened from a paywall in an in-app browser disappears from the screen.
onWebPaymentNavigationFinishedInvoked after attempting to open a web paywall for purchase, whether successful or failed.