UNPKG

react-native-iap

Version:

React Native In-App Purchases module for iOS and Android using Nitro

369 lines 13.3 kB
import 'react-native-nitro-modules'; import type { NitroPurchaseResult } from './specs/RnIap.nitro'; import type { Product, ProductRequest, Purchase, PurchaseOptions, PurchaseParams, PurchaseError, SubscriptionStatusIOS } from './types'; export type { RnIap, NitroProduct, NitroPurchase, NitroPurchaseResult, } from './specs/RnIap.nitro'; export * from './types'; export * from './utils/error'; export interface EventSubscription { remove(): void; } export type FinishTransactionParams = { purchase: Purchase; isConsumable?: boolean; }; export { useIAP } from './hooks/useIAP'; export declare const getPromotedProductIOS: () => Promise<Product | null>; export declare const requestPurchaseOnPromotedProductIOS: () => Promise<void>; export declare const restorePurchases: (options?: PurchaseOptions) => Promise<Purchase[]>; /** * Initialize connection to the store */ export declare const initConnection: () => Promise<boolean>; /** * End connection to the store */ export declare const endConnection: () => Promise<boolean>; /** * Fetch products from the store * @param params - Product request configuration * @param params.skus - Array of product SKUs to fetch * @param params.type - Optional filter: 'inapp' (default) for products, 'subs' for subscriptions, or 'all' for both. * @returns Promise<Product[]> - Array of products from the store * * @example * ```typescript * // Regular products * const products = await fetchProducts({ skus: ['product1', 'product2'] }); * * // Subscriptions * const subscriptions = await fetchProducts({ skus: ['sub1', 'sub2'], type: 'subs' }); * ``` */ export declare const fetchProducts: ({ skus, type, }: ProductRequest) => Promise<Product[]>; /** * Request a purchase for products or subscriptions * @param params - Purchase request configuration * @param params.request - Platform-specific purchase parameters * @param params.type - Type of purchase: 'inapp' for products (default) or 'subs' for subscriptions * * @example * ```typescript * // Product purchase * await requestPurchase({ * request: { * ios: { sku: productId }, * android: { skus: [productId] } * }, * type: 'inapp' * }); * * // Subscription purchase * await requestPurchase({ * request: { * ios: { sku: subscriptionId }, * android: { * skus: [subscriptionId], * subscriptionOffers: [{ sku: subscriptionId, offerToken: 'token' }] * } * }, * type: 'subs' * }); * ``` */ /** * Request a purchase for products or subscriptions * ⚠️ Important: This is an event-based operation, not promise-based. * Listen for events through purchaseUpdatedListener or purchaseErrorListener. * @param params - Purchase request configuration * @param params.requestPurchase - Platform-specific purchase parameters (in-app) * @param params.requestSubscription - Platform-specific subscription parameters (subs) * @param params.type - Type of purchase (defaults to in-app) */ export declare const requestPurchase: (params: PurchaseParams) => Promise<void>; /** * Get available purchases (purchased items not yet consumed/finished) * @param params - Options for getting available purchases * @param params.alsoPublishToEventListener - Whether to also publish to event listener * @param params.onlyIncludeActiveItems - Whether to only include active items * * @example * ```typescript * const purchases = await getAvailablePurchases({ * onlyIncludeActiveItemsIOS: true * }); * ``` */ export declare const getAvailablePurchases: ({ alsoPublishToEventListenerIOS, onlyIncludeActiveItemsIOS, }?: PurchaseOptions) => Promise<Purchase[]>; /** * Finish a transaction (consume or acknowledge) * @param params - Transaction finish parameters * @param params.purchase - The purchase to finish * @param params.isConsumable - Whether this is a consumable product (Android only) * * @example * ```typescript * await finishTransaction({ * purchase: myPurchase, * isConsumable: true * }); * ``` */ export declare const finishTransaction: ({ purchase, isConsumable, }: FinishTransactionParams) => Promise<NitroPurchaseResult | boolean>; /** * Acknowledge a purchase (Android only) * @param purchaseToken - The purchase token to acknowledge * * @example * ```typescript * await acknowledgePurchaseAndroid('purchase_token_here'); * ``` */ export declare const acknowledgePurchaseAndroid: (purchaseToken: string) => Promise<NitroPurchaseResult>; /** * Consume a purchase (Android only) * @param purchaseToken - The purchase token to consume * * @example * ```typescript * await consumePurchaseAndroid('purchase_token_here'); * ``` */ export declare const consumePurchaseAndroid: (purchaseToken: string) => Promise<NitroPurchaseResult>; /** * Purchase updated event listener * Fired when a purchase is successful or when a pending purchase is completed. * * @param listener - Function to call when a purchase is updated * @returns EventSubscription object with remove method * * @example * ```typescript * const subscription = purchaseUpdatedListener((purchase) => { * console.log('Purchase successful:', purchase); * // 1. Validate receipt with backend * // 2. Deliver content to user * // 3. Call finishTransaction to acknowledge * }); * * // Later, clean up * subscription.remove(); * ``` */ export declare const purchaseUpdatedListener: (listener: (purchase: Purchase) => void) => EventSubscription; /** * Purchase error event listener * Fired when a purchase fails or is cancelled by the user. * * @param listener - Function to call when a purchase error occurs * @returns EventSubscription object with remove method * * @example * ```typescript * const subscription = purchaseErrorListener((error) => { * switch (error.code) { * case 'E_USER_CANCELLED': * // User cancelled - no action needed * break; * case 'E_ITEM_UNAVAILABLE': * // Product not available * break; * case 'E_NETWORK_ERROR': * // Retry with backoff * break; * } * }); * * // Later, clean up * subscription.remove(); * ``` */ export declare const purchaseErrorListener: (listener: (error: PurchaseError) => void) => EventSubscription; /** * iOS-only listener for App Store promoted product events. * Fired when a user clicks on a promoted in-app purchase in the App Store. * * @param listener - Callback function that receives the promoted product * @returns EventSubscription object with remove method * * @example * ```typescript * const subscription = promotedProductListenerIOS((product) => { * console.log('Promoted product:', product); * // Trigger purchase flow for the promoted product * }); * * // Later, clean up * subscription.remove(); * ``` * * @platform iOS */ export declare const promotedProductListenerIOS: (listener: (product: Product) => void) => EventSubscription; /** * Validate receipt on both iOS and Android platforms * @param sku - Product SKU * @param androidOptions - Android-specific validation options (required for Android) * @returns Promise<ReceiptValidationResultIOS | ReceiptValidationResultAndroid> - Platform-specific receipt validation result */ export declare const validateReceipt: (sku: string, androidOptions?: { packageName: string; productToken: string; accessToken: string; isSub?: boolean; }) => Promise<import("./types").ReceiptValidationResult>; /** * Sync iOS purchases with App Store (iOS only) * @returns Promise<boolean> * @platform iOS */ export declare const syncIOS: () => Promise<boolean>; /** * Request the promoted product from the App Store (iOS only) * @returns Promise<Product | null> - The promoted product or null if none available * @platform iOS */ export declare const requestPromotedProductIOS: () => Promise<Product | null>; /** * Present the code redemption sheet for offer codes (iOS only) * @returns Promise<boolean> - True if the sheet was presented successfully * @platform iOS */ export declare const presentCodeRedemptionSheetIOS: () => Promise<boolean>; /** * Buy promoted product on iOS * @returns Promise<void> * @platform iOS */ export declare const buyPromotedProductIOS: () => Promise<void>; /** * Clear unfinished transactions on iOS * @returns Promise<void> * @platform iOS */ export declare const clearTransactionIOS: () => Promise<void>; /** * Begin a refund request for a product on iOS 15+ * @param sku - The product SKU to refund * @returns Promise<string | null> - The refund status or null if not available * @platform iOS */ export declare const beginRefundRequestIOS: (sku: string) => Promise<string | null>; /** * Get subscription status for a product (iOS only) * @param sku - The product SKU * @returns Promise<SubscriptionStatusIOS[]> - Array of subscription status objects * @throws Error when called on non-iOS platforms or when IAP is not initialized * @platform iOS */ export declare const subscriptionStatusIOS: (sku: string) => Promise<SubscriptionStatusIOS[]>; /** * Get current entitlement for a product (iOS only) * @param sku - The product SKU * @returns Promise<Purchase | null> - Current entitlement or null * @platform iOS */ export declare const currentEntitlementIOS: (sku: string) => Promise<Purchase | null>; /** * Get latest transaction for a product (iOS only) * @param sku - The product SKU * @returns Promise<Purchase | null> - Latest transaction or null * @platform iOS */ export declare const latestTransactionIOS: (sku: string) => Promise<Purchase | null>; /** * Get pending transactions (iOS only) * @returns Promise<Purchase[]> - Array of pending transactions * @platform iOS */ export declare const getPendingTransactionsIOS: () => Promise<Purchase[]>; /** * Show manage subscriptions screen (iOS only) * @returns Promise<Purchase[]> - Subscriptions where auto-renewal status changed * @platform iOS */ export declare const showManageSubscriptionsIOS: () => Promise<Purchase[]>; /** * Check if user is eligible for intro offer (iOS only) * @param groupID - The subscription group ID * @returns Promise<boolean> - Eligibility status * @platform iOS */ export declare const isEligibleForIntroOfferIOS: (groupID: string) => Promise<boolean>; /** * Get receipt data (iOS only) * @returns Promise<string> - Base64 encoded receipt data * @platform iOS */ export declare const getReceiptDataIOS: () => Promise<string>; /** * Check if transaction is verified (iOS only) * @param sku - The product SKU * @returns Promise<boolean> - Verification status * @platform iOS */ export declare const isTransactionVerifiedIOS: (sku: string) => Promise<boolean>; /** * Get transaction JWS representation (iOS only) * @param sku - The product SKU * @returns Promise<string | null> - JWS representation or null * @platform iOS */ export declare const getTransactionJwsIOS: (sku: string) => Promise<string | null>; /** * Get the storefront identifier for the user's App Store account (iOS only) * @returns Promise<string> - The storefront identifier (e.g., 'USA' for United States) * @platform iOS * * @example * ```typescript * const storefront = await getStorefrontIOS(); * console.log('User storefront:', storefront); // e.g., 'USA', 'GBR', 'KOR' * ``` */ export declare const getStorefrontIOS: () => Promise<string>; /** * Gets the storefront country code from the underlying native store. * Returns a two-letter country code such as 'US', 'KR', or empty string on failure. * * Cross-platform alias aligning with expo-iap. */ export declare const getStorefront: () => Promise<string>; /** * Deeplinks to native interface that allows users to manage their subscriptions * Cross-platform alias aligning with expo-iap */ export declare const deepLinkToSubscriptions: (options?: { skuAndroid?: string; packageNameAndroid?: string; }) => Promise<void>; /** * iOS only - Gets the original app transaction ID if the app was purchased from the App Store * @platform iOS * @description * This function retrieves the original app transaction information if the app was purchased * from the App Store. Returns null if the app was not purchased (e.g., free app or TestFlight). * * @returns {Promise<string | null>} The original app transaction ID or null * * @example * ```typescript * const appTransaction = await getAppTransactionIOS(); * if (appTransaction) { * console.log('App was purchased, transaction ID:', appTransaction); * } else { * console.log('App was not purchased from App Store'); * } * ``` */ export declare const getAppTransactionIOS: () => Promise<string | null>; export { getActiveSubscriptions, hasActiveSubscriptions, } from './helpers/subscription'; export { convertNitroProductToProduct, convertNitroPurchaseToPurchase, convertProductToProductSubscription, validateNitroProduct, validateNitroPurchase, checkTypeSynchronization, } from './utils/type-bridge'; /** * @deprecated Use acknowledgePurchaseAndroid instead */ export declare const acknowledgePurchase: (purchaseToken: string) => Promise<NitroPurchaseResult>; /** * @deprecated Use consumePurchaseAndroid instead */ export declare const consumePurchase: (purchaseToken: string) => Promise<NitroPurchaseResult>; //# sourceMappingURL=index.d.ts.map