A Capacitor plugin for handling in-app purchases (consumable products only) on iOS and Android.
npm install capacitor-plugin-purchase
npx cap sync
Add the following to your app's Info.plist
:
<key>SKStoreProductIdentifier</key>
<string>YOUR_APP_ID</string>
Add the following to your app's build.gradle
:
dependencies {
// ... other dependencies
implementation 'com.android.billingclient:billing:5.0.0'
}
import { InAppPurchase } from 'capacitor-plugin-purchase';
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');
}
};
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);
}
};
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);
}
};
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.
Check if the user/device can make purchases.
Returns: Promise<{ allowed: boolean }>
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)
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)
-
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 ofPAYMENT_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.
For iOS, you can use the Sandbox environment to test purchases without actual charges. Make sure to:
- Create a Sandbox tester account in App Store Connect
- Sign out of your regular Apple ID on the test device
- Sign in with the Sandbox tester account when prompted during testing
For Android, you can use test accounts and test cards:
- Upload your app to the internal testing track in Google Play Console
- Add test accounts in the Google Play Console
- Use the test accounts to make purchases
- 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
- 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
MIT