capacitor-plugin-purchase
TypeScript icon, indicating that this package has built-in type declarations

0.0.17 • Public • Published

Capacitor In-App Purchase Plugin

A Capacitor plugin for handling in-app purchases (consumable products only) on iOS and Android.

Installation

npm install capacitor-plugin-purchase
npx cap sync

Configuration

iOS Configuration

Add the following to your app's Info.plist:

<key>SKStoreProductIdentifier</key>
<string>YOUR_APP_ID</string>

Android Configuration

Add the following to your app's build.gradle:

dependencies {
    // ... other dependencies
    implementation 'com.android.billingclient:billing:5.0.0'
}

Usage

Import the plugin

import { InAppPurchase } from 'capacitor-plugin-purchase';

Check if purchases are allowed

const checkPurchases = async () => {
  const { allowed } = await InAppPurchase.canMakePurchases();
  if (allowed) {
    console.log('In-app purchases are allowed');
  } else {
    console.log('In-app purchases are not allowed');
  }
};

Get available products

const getProducts = async () => {
  try {
    const result = await InAppPurchase.getProducts({
      productIds: ['product_id_1', 'product_id_2']
    });
    
    console.log('Products:', result.products);
    // Products will contain details like title, description, price, etc.
  } catch (error) {
    console.error('Error fetching products:', error);
  }
};

Purchase a product

const purchaseProduct = async (productId: string) => {
  try {
    const result = await InAppPurchase.purchaseProduct({
      productId: productId
    });
    
    if (result.status === 'PURCHASED') {
      console.log('Purchase successful!');
      console.log('Transaction details:', result.transaction);
      
      // Handle the successful purchase
      // For consumable products, the purchase is automatically consumed
      // You can update your app state or server here
    } else {
      console.log('Purchase failed with status:', result.status);
    }
  } catch (error) {
    console.error('Error during purchase:', error);
  }
};

Web Implementation

The web implementation provides mock responses as in-app purchases are not supported in web environments. This allows you to develop and test your app in a web browser without errors, but actual purchases will only work on iOS and Android devices.

API

canMakePurchases()

Check if the user/device can make purchases.

Returns: Promise<{ allowed: boolean }>

getProducts(options: { productIds: string[] })

Get product information from the app store.

Parameters:

  • options.productIds: Array of product identifiers

Returns: Promise<{ products: Product[] }>

Where Product has the following properties:

  • productId: string
  • title: string
  • description: string
  • price: string (formatted price with currency symbol)
  • priceAsDecimal: number (price as a decimal number)
  • currency: string (currency code)

purchaseProduct(options: { productId: string })

Initiate a purchase for a specific product.

Parameters:

  • options.productId: Product identifier to purchase

Returns: Promise

Where PurchaseResult has the following properties:

  • status: One of the following strings:
    • 'PURCHASED': Purchase was successful
    • 'PAYMENT_CANCELLED': User cancelled the payment
    • 'PAYMENT_INVALID': Payment was invalid
    • 'PAYMENT_NOT_ALLOWED': Payment not allowed on this device
    • 'UNKNOWN': Unknown error occurred
    • 'ERR_INVALID_PRODUCT_ID': Invalid product ID
    • 'ERR_PRODUCT_NOT_AVAILABLE': Product not available
  • transaction: (Only present if status is 'PURCHASED')
    • id: string (transaction identifier)
    • receipt: string (purchase receipt)
    • productId: string (purchased product identifier)
    • date: string (ISO 8601 formatted date)

Notes for Web Usage

When using this plugin in a web application, all methods will return mock responses:

  • canMakePurchases() will return { allowed: false }
  • getProducts() will return an empty array of products
  • purchaseProduct() will return a status of PAYMENT_NOT_ALLOWED

This allows you to develop and test your app in a web browser without errors, but actual purchases will only work on iOS and Android devices.

Testing In-App Purchases

iOS Testing

For iOS, you can use the Sandbox environment to test purchases without actual charges. Make sure to:

  1. Create a Sandbox tester account in App Store Connect
  2. Sign out of your regular Apple ID on the test device
  3. Sign in with the Sandbox tester account when prompted during testing

Android Testing

For Android, you can use test accounts and test cards:

  1. Upload your app to the internal testing track in Google Play Console
  2. Add test accounts in the Google Play Console
  3. Use the test accounts to make purchases

Troubleshooting

iOS Issues

  • Make sure your app has the proper capabilities enabled in Xcode
  • Verify that your product IDs are correctly configured in App Store Connect
  • Check that your app is properly signed with a valid provisioning profile

Android Issues

  • Ensure your app is properly signed with the same key used in Google Play Console
  • Verify that your product IDs are correctly configured in Google Play Console
  • Make sure the Google Play Billing Library is properly implemented

License

MIT

Dependents (0)

Package Sidebar

Install

npm i capacitor-plugin-purchase

Weekly Downloads

128

Version

0.0.17

License

MIT

Unpacked Size

85.2 kB

Total Files

26

Last publish

Collaborators

  • scgscorp