React native in app billing SDK for React native.
npm i iab-myket-reactnative
To activate Myket reactNative Billing SDK, open the android directory located in your root project and add this piece of code to app level build.gradle file:
def marketApplicationId = "ir.mservices.market"
def marketBindAddress = "ir.mservices.market.InAppBillingService.BIND"
manifestPlaceholders = [marketApplicationId: "${marketApplicationId}",
marketBindAddress : "${marketBindAddress}",
marketPermission : "${marketApplicationId}.BILLING"]
if your project doesn't have and android directory, make one using the command below:
expo eject
Note that application must have the same application Id as you definded in the Myket panel.
import myket from 'myket-iab-reactnative';
function MyComponent() {
const myketInstance = useMyket(myInAppBillingKey);
// ...
const someHandler = async () => {
const inventory = await myket.queryPurchaseProduct(true, productIds);
console.log(JSON.JSON.stringify(inventory));
};
// ...
}
import myket from 'myket-iab-reactnative';
class App extends Component
componentDidMount() {
this.didMount = true;
myket.connect(base64RSA).catch((error) => {
/**
* myket is not installed or RSA public key is null or empty
*/
this.log(error)
}).then(this.retrieverProducts);
}
componentWillUnmount() {
myket.disconnect().catch(() => this.log);
}
}
For better understanding see the Trivial drive app in here which provided a complete react-native implementation.
- Connect and initialize myket billing , take RSA public key as a parameter, find more information about public key here.
- disconnect will release the resources occupied by the SDK and mhelper.
- Enable Debugging in Android logcat.
-
Launches an Activity and shows the myket billing dialog.
-
developerPayload : Generate a string and pass it to the function and compare to the response of purchase for security reasons.
-
sku : The SKU of the item that user intended to buy.
- Consume a Purchase, the result of purchasing the product must be consumed by the application.
- query inventory for details of sku's and previously bought items that hasn't consumed. for some reasons , user may bought a SKU but hasn't consumed by the application , make sure you call consumed purchases immediately after calling queryPurchaseProduct.
- moreSkus : list of array to define wich skuItem's details need to get fetched.
- querySkuDetails : pass true if you want to get the sku details,pass false if you want to avoid calling another service.
export type Inventory = {
allProducts: SkuDetails[];
allPurchases: Purchase[];
};
export type SkuDetails = {
itemType: string;
sku: string;
type: string;
price: string;
title: string;
description: string;
json: string;
};
export type Purchase = {
itemType: string;
orderId: string;
packageName: string;
sku: string;
purchaseTime: string;
purchaseState: number;
developerPayload: string;
token: string;
originalJson: string;
signature: string;
};
the following is the error schema returned from almost every API of the SDK; in case you want to handle errors for more customized scenario :
{
"message":"String"
"responseCode":"Int"
}
MIT