Command palette

@yasinsensoy/native

React Native · APIs and utilities

In-app purchases

Purchase API.

10 exports

Import

import { Purchases, useProducts, useActivePurchases } from "@yasinsensoy/native";

Purchases

module

Purchase API. Flow: get products → purchase → deliver the content (preferably verify the token on your server) → finish with finish. An unfinished transaction is refunded after 3 days on Android, and comes back on every launch on iOS.

NameTypeDescription
canMakePayments() => booleanWhether the device allows purchases (parental controls etc.).
getProducts(ids: readonly string[]) => Promise<Product[]>Store products (by App Store Connect / Play Console ids). Ones not found are not in the list.
purchase(productId: string, options?: PurchaseOptions) => Promise<Purchase>Purchases. On success deliver the content and call Purchases.finish.
finish(purchase: Purchase, { consume }?: { consume?: boolean; }) => Promise<void>Finishes a transaction. consume: true makes a consumable product (coins, lives…) purchasable again; do not pass it for subscriptions and permanent products.
getActive() => Promise<Purchase[]>Owned products and active subscriptions (including unfinished transactions).
restore() => Promise<Purchase[]>Restores purchases (syncs with the store on iOS; for a "Restore purchases" button).
manageSubscriptions(productId?: string) => Promise<void>Subscription management screen (a sheet on iOS, Play Store on Android).
addUpdateListener(listener: (purchase: Purchase) => void) => () => voidTransactions completed outside the app: payments awaiting approval, renewals, refunds, purchases from another device. Listen as soon as the app opens.

useProducts

function

Loads products.

useProducts(ids: readonly string[]): {
    products: Product[]

Owned products; updated with update events and refresh. onPurchase gives a newly arrived transaction (completed outside the app): deliver the content and finish it.

useActivePurchases(onPurchase?: (purchase: Purchase) => void): {
    purchases: Purchase[]

Product type. iOS knows the type; on Android one-time products are oneTime (you tell whether it is consumable with consume when finishing).

type ProductType = "consumable" | "nonConsumable" | "subscription" | "nonRenewingSubscription" | "oneTime" | "unknown"

ProductPeriod

interface

Period.

NameTypeDescription
unit"day" | "week" | "month" | "year"Unit.
valuenumberNumber of units.

A subscription offer.

NameTypeDescription
idstring | nulliOS promotional id / Android offer or base plan id; null for an introductory offer.
token?stringAndroid: offer token to pass when purchasing.
basePlanId?stringAndroid: base plan.
tags?string[]Android: offer tags.
price?stringiOS: formatted price.
priceAmount?numberiOS: price as a number.
period?ProductPeriodiOS: period and count, payment mode.
periodCount?numberiOS: number of periods.
paymentMode?"freeTrial" | "payAsYouGo" | "payUpFront"iOS: payment mode.
phases?{ price: string; priceAmount: number; currency: string; period: ProductPeriod; cycles: number; recurring: boolean; }[]Android: pricing phases (trial → discount → regular).

Product

interface

A store product.

NameTypeDescription
idstringProduct id in the store.
typeProductTypeProduct type.
titlestringName in the store.
descriptionstringDescription in the store.
pricestringFormatted price ("$4.99").
priceAmountnumberPrice as a number.
currencystringCurrency code (ISO 4217).
subscription?{ period: ProductPeriod | null; /** iOS subscription group. */ groupId?: string; /** iOS introductory offer (trial). */ introductoryOffer?: SubscriptionOffer | null; offers: SubscriptionOffer[]; }Subscription details; only for subscriptions.

Purchase

interface

A purchase / transaction.

NameTypeDescription
idstringiOS transaction id / Android order id.
productIdstringPurchased product id.
datenumberPurchase time (ms, epoch).
state"purchased" | "pending""pending" while payment is awaited (e.g. parental approval).
tokenstringFor server verification: iOS JWS (jwsRepresentation), Android purchaseToken. On Android it is also used when finishing.
quantitynumberQuantity.
originalId?stringiOS: the original transaction (the same across renewals).
expirationDate?number | nulliOS subscription expiry (ms).
revoked?booleaniOS: refunded.
environment?stringiOS: Sandbox, Production, Xcode.
appAccountToken?string | nulliOS: account token given at purchase.
productIds?string[]Android.
acknowledged?booleanAndroid: whether the purchase is acknowledged.
autoRenewing?booleanAndroid: whether the subscription renews automatically.
signature?stringAndroid: purchase signature (for server verification).
originalJson?stringAndroid: raw purchase JSON (for server verification).

Options of Purchases.purchase.

NameTypeDescription
offerToken?stringAndroid subscription offer (product.subscription.offers[i].token); the base plan if omitted.
appAccountToken?stringiOS: matching with the user account (UUID).
accountId?stringAndroid: obfuscated account id (for comparison).
profileId?stringAndroid: obfuscated profile id.
quantity?numberiOS: quantity.

Error codes: E_CANCELLED (the user cancelled), E_PENDING (awaiting approval/payment; the result arrives with the update event), E_ALREADY_OWNED, E_NOT_FOUND, E_UNAVAILABLE (no store, or Google Play Billing is unavailable), E_NETWORK, E_UNVERIFIED, E_PURCHASE.

type PurchaseErrorCode = "E_CANCELLED" | "E_PENDING" | "E_ALREADY_OWNED" | "E_NOT_FOUND" | "E_UNAVAILABLE" | "E_NETWORK" | "E_UNVERIFIED" | "E_PURCHASE"