Skip to main content
Registers a listener invoked when Encore’s offer flow completes and a purchase is needed. The listener receives the product ID and placement ID and should trigger the purchase via your billing library.
You must call completePurchaseRequest() in every code path of your handler — both success and failure. Failing to do so will block the SDK from presenting future offers until the app restarts.

Signature

onPurchaseRequest(handler: (event: PurchaseRequestEvent) => void): () => void

Parameters

NameTypeDescription
handler(event: PurchaseRequestEvent) => voidCallback receiving the purchase request event

PurchaseRequestEvent

FieldTypeDescription
productIdstringThe App Store / Play Store product ID to purchase
placementIdstring?Which placement triggered this request

Returns

An unsubscribe function. Call it to remove the listener.

When It Fires

ScenarioFires?
User accepts an offerYes
User dismisses without purchasingNo — see onPassthrough()
No offer available for this userNo — see onPassthrough()

Usage

const unsubscribe = Encore.onPurchaseRequest(async ({ productId }) => {
  try {
    const { customerInfo } = await Purchases.purchaseProduct(productId);
    await Encore.completePurchaseRequest(true);
  } catch (error) {
    await Encore.completePurchaseRequest(false);
  }
});
Every code path must call completePurchaseRequest(). If your handler throws without calling it, the SDK will remain locked until the app restarts. Always use try/catch.

With onPassthrough

useEffect(() => {
  const unsub1 = Encore.onPurchaseRequest(async ({ productId }) => {
    try {
      await billingService.purchase(productId);
      await Encore.completePurchaseRequest(true);
    } catch (error) {
      await Encore.completePurchaseRequest(false);
    }
  });

  const unsub2 = Encore.onPassthrough(({ placementId }) => {
    proceedWithCancellation();
  });

  return () => {
    unsub1();
    unsub2();
  };
}, []);
Clean up listeners when your component unmounts by calling the unsubscribe function in a useEffect cleanup.