Introduction
Last updated
Last updated
Welcome to the documentation for the cordova-plugin-purchase
plugin (version 13+). This plugin provides a unified JavaScript API for integrating In-App Purchases (IAP) across multiple platforms like Apple App Store, Google Play, and payment gateways like Braintree, directly from your Cordova, Capacitor, or Ionic application.
Abstracts Platform Differences: Provides a single API (CdvPurchase.store
) to manage products, purchases, and subscriptions, reducing the need for platform-specific code in your application logic.
Handles Purchase Flows: Initiates purchase requests (offer.order()
, store.requestPayment()
), processes platform responses, and delivers transaction information to your app via event listeners (store.when()
).
Supports Various Product Types: Consumables, Non-Consumables, Auto-Renewing Subscriptions (PAID_SUBSCRIPTION
), Non-Renewing Subscriptions.
Integrates with Payment Gateways: Supports custom payment amounts via Braintree (requires the extension).
Facilitates Receipt Validation: Provides receipt data necessary for server-side validation, which is crucial for security and managing entitlements, especially subscriptions. Integrates easily with services like .
Test Platform: Includes a built-in Platform.TEST
adapter for local development and testing without real store interaction.
This documentation is for developers building hybrid mobile applications using frameworks like Cordova, Capacitor, or Ionic who need to implement In-App Purchases for digital goods or services.
Understanding these core concepts is essential for using the plugin effectively:
CdvPurchase
Namespace: The global namespace containing all plugin classes, enums, and the main store
object. Access members like CdvPurchase.store
, CdvPurchase.ProductType
, CdvPurchase.Platform
.
CdvPurchase.store
: The main object for interacting with the plugin. Used for initialization, registration, event handling, and accessing products/receipts.
Platform
: An enum representing the target store or payment provider (Platform.APPLE_APPSTORE
, Platform.GOOGLE_PLAY
, Platform.BRAINTREE
, Platform.TEST
).
ProductType
: An enum defining the type of item (ProductType.CONSUMABLE
, ProductType.NON_CONSUMABLE
, ProductType.PAID_SUBSCRIPTION
, ProductType.NON_RENEWING_SUBSCRIPTION
).
Product
: Represents an item available for purchase (e.g., "Unlock Premium Features", "100 Gold Coins", "Monthly Subscription"). You define these in the platform's developer console (App Store Connect, Google Play Console) and register them in the app using store.register()
. A Product
contains metadata like title, description, and one or more Offer
s.
Offer
: Represents a specific way to purchase a Product
. It defines the price and terms. Subscriptions often have multiple offers (e.g., monthly vs. annual). An Offer
contains one or more PricingPhase
s. Accessed via product.getOffer()
.
PricingPhase
: Details a specific period within an Offer
's price, like a free trial, an introductory price period, or the regular recurring price. Contains price
, priceMicros
, currency
, billingPeriod
, paymentMode
, etc. Accessed via offer.pricingPhases
.
Receipt
: A record of purchases associated with the user's platform account (App Store, Google Play) as reported locally by the device's SDK. Contains one or more Transaction
objects. Accessed via store.localReceipts
. Local receipts should not be solely trusted for granting entitlements.
Transaction
: Represents a single purchase attempt within a Receipt
. Transactions progress through states (TransactionState.INITIATED
, TransactionState.APPROVED
, TransactionState.FINISHED
, etc.). Contains transactionId
, purchaseDate
, etc.
Receipt Validation: The critical process of verifying the authenticity and details of a purchase with the platform's servers (Apple/Google) via a secure backend. This must always be done server-side. .
store.validator
: The configuration property where you provide the URL or function for your receipt validation service.
VerifiedReceipt
/ VerifiedPurchase
: The result returned by your store.validator
after successful validation. Contains authoritative purchase information (like subscription expiry dates, renewal status). Accessed via the .verified()
event handler or store.verifiedReceipts
/ store.verifiedPurchases
. This is the trusted source for managing entitlements.
Entitlement: The user's right to access a feature or content based on a valid, verified purchase.
transaction.finish()
/ receipt.finish()
: Acknowledging a purchase to the platform. This signals that you have processed the transaction (and delivered the goods/service). It's mandatory to complete the transaction cycle, prevent repeated notifications, and (for consumables) allow repurchase.
Wait for deviceready
.
Register Products: Tell the plugin about your products using store.register([...])
.
Configure Validator: Set store.validator
to your validation service endpoint (highly recommended).
Setup Listeners: Use store.when()
to listen for events like productUpdated
, approved
, verified
, finished
.
Initialize: Call store.initialize([...platforms])
to activate the necessary platforms and load initial data.
Display Products: Use data from store.products
(updated via productUpdated
) to show available items and prices in your UI.
Handle Purchase Request: When a user wants to buy, call offer.order()
or store.requestPayment()
.
Process Approval: In the .approved()
listener, call transaction.verify()
.
Process Verification: In the .verified()
listener, check the VerifiedReceipt
, grant entitlement to the user, and call receipt.finish()
.
Handle Finished: The .finished()
listener confirms the transaction is complete. Update UI if necessary.
While optional, these Cordova plugins can enhance the purchase plugin's reliability and functionality:
cordova-plugin-network-information
:
Why? Allows the purchase plugin to detect when the device comes back online after being offline. This enables quicker retries for failed network operations, such as initialization attempts or receipt validation requests that failed due to network issues. Without it, the plugin relies on less frequent periodic retries.
Installation:
cordova-plugin-advanced-http
:
Why? Provides a native HTTP client for making network requests (like receipt validation). This bypasses potential browser limitations like CORS (Cross-Origin Resource Sharing) errors that can occur with standard web fetch
or XMLHttpRequest
when calling your validator. It can also offer more control over headers and potentially support features like certificate pinning for enhanced security.
Installation:
Discover: Read the conceptual pages to understand IAP basics, the plugin's architecture, and the importance of receipt validation.
Use Cases: Find the guide that matches your specific need (e.g., "Subscription on AppStore") and follow the setup and implementation steps. These provide practical, step-by-step examples.
Ready? Let's get started with setting up your chosen platform or exploring a specific use case!
API Reference: Consult the detailed for specific methods and properties of classes like Store
, Product
, Offer
, Transaction
, Receipt
, etc.
Troubleshooting: Check the for common problems and solutions.
Migration: If upgrading from v11 or earlier, review the .