In App Purchase
A lightweight Cordova plugin for in app purchases on iOS/Android.
Repo: https://github.com/AlexDisler/cordova-plugin-inapppurchase
Installation
- Install the Cordova and Ionic Native plugins:
$ ionic cordova plugin add cordova-plugin-inapppurchase $ npm install --save @ionic-native/in-app-purchase@4
- Add this plugin to your app's module
Supported platforms
- Android
- iOS
Usage
import { InAppPurchase } from '@ionic-native/in-app-purchase';
constructor(private iap: InAppPurchase) { }
...
this.iap
.getProducts(['prod1', 'prod2', ...])
.then((products) => {
console.log(products);
// [{ productId: 'com.yourapp.prod1', 'title': '...', description: '...', price: '...' }, ...]
})
.catch((err) => {
console.log(err);
});
this.iap
.buy('prod1')
.then((data)=> {
console.log(data);
// {
// transactionId: ...
// receipt: ...
// signature: ...
// }
})
.catch((err)=> {
console.log(err);
});
Instance Members
getProducts(productId)
Retrieves a list of full product data from Apple/Google. This method must be called before making purchases.
Param | Type | Details |
---|---|---|
productId |
array<string>
|
an array of product ids. |
Returns: Promise<any>
Returns a Promise that resolves with an array of objects.
buy(productId)
Buy a product that matches the productId.
Param | Type | Details |
---|---|---|
productId |
string
|
A string that matches the product you want to buy. |
Returns: Promise<{transactionId: string, receipt: string, signature: string, productType: string}>
Returns a Promise that resolves with the transaction details.
subscribe(productId)
Same as buy, but for subscription based products.
Param | Type | Details |
---|---|---|
productId |
string
|
A string that matches the product you want to subscribe to. |
Returns: Promise<{transactionId: string, receipt: string, signature: string, productType: string}>
Returns a Promise that resolves with the transaction details.
consume(productType, receipt, signature)
Call this function after purchasing a “consumable” product to mark it as consumed. On Android, you must consume products that you want to let the user purchase multiple times. If you will not consume the product after a purchase, the next time you will attempt to purchase it you will get the error message:
Param | Type | Details |
---|---|---|
productType |
string
|
|
receipt |
string
|
|
signature |
string
|
Returns: Promise<any>
restorePurchases()
Restore all purchases from the store
Returns: Promise<any>
Returns a promise with an array of purchases.
getReceipt()
Platforms:iOS
Get the receipt.
Returns: Promise<string>
Returns a promise that contains the string for the receipt
Advanced
// fist buy the product...
this.iap
.buy('consumable_prod1')
.then(data => this.iap.consume(data.productType, data.receipt, data.signature))
.then(() => console.log('product was successfully consumed!'))
.catch( err=> console.log(err))