Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'react-native-iap' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
if (Platform.OS === 'ios') {
validation = await validateAppleIAPJSON(this.props.auth.email, this.props.auth.accessToken, this.state.receipt.transactionReceipt);
}
if (Platform.OS === 'android') {
//ToDo: check verify Play store purchases
validation = await validateGoogleIAPJSON(this.props.auth.email, this.props.auth.accessToken, {receipt: this.state.receipt.dataAndroid, signature: this.state.receipt.signatureAndroid});
// console.log(validation);
// validation = {tx: "remove this"};
}
console.log('validation:');
console.log(validation);
// consume products
const purchases = await RNIap.getAvailablePurchases();
console.log(purchases);
purchases.forEach(async purchase => {
console.log(purchase);
await RNIap.consumePurchase(purchase.purchaseToken);
});
if (validation.tx) {
this.props.addAlert('success', 'Thank you for your purchase!', 'Your coins will appear in your wallet soon');
} else {
this.props.addAlert('error', '', validation.error ? validation.error : 'Something went wrong');
}
} catch (err) {
console.log(err); // standardized err.code and err.message available
this.props.addAlert('error', '', err.message);
}
};
async componentDidMount() {
try {
const products = await RNIap.getProducts(itemSkus);
products.sort(this.compare);
this.setState({products});
console.log(products);
} catch (err) {
console.log(err); // standardized err.code and err.message available
}
}
async componentDidMount() {
if (!GITHUB_VERSION) {
try {
const products = await RNIap.getProducts(itemSkus);
// Do this just to ensure that all IAPs are available
await RNIap.consumeAllItems();
products.sort((a, b) => a.price.localeCompare(b.price));
this.setState({products});
} catch(err) {
console.warn(err); // standardized err.code and err.message available
}
}
}
async buyProduct(product) {
if (this.props.profile.profileAddress) {
try {
// Will return a purchase object with a receipt which can be used to validate on your server.
const purchase = await RNIap.buyProduct(product.productId);
console.log(purchase);
this.setState({
receipt: purchase, // save the receipt if you need it, whether locally, or to your server.
});
await this.consumePurchases();
} catch (err) {
// standardized err.code and err.message available
console.log(err.code, err.message);
this.props.addAlert('error', '', 'Failed to buy product');
const subscription = RNIap.addAdditionalSuccessPurchaseListenerIOS(async (purchase) => {
this.setState({receipt: purchase}, async () => await this.consumePurchases());
subscription.remove();
});
}
} else {
async supportWoWsInfo(item) {
try {
// Will return a purchase object with a receipt which can be used to validate on your server.
const purchase = await RNIap.buyProduct(item.productId);
// Consume it right away to buy multiple times
await RNIap.consumePurchase(purchase.purchaseToken);
this.setState({
receipt: purchase.transactionReceipt, // save the receipt if you need it, whether locally, or to your server.
});
} catch(err) {
// standardized err.code and err.message available
console.error(err.code, err.message);
const subscription = RNIap.addAdditionalSuccessPurchaseListenerIOS(async (purchase) => {
this.setState({ receipt: purchase.transactionReceipt }, () => this.goToNext());
subscription.remove();
});
}
}
}
async buyProduct(product) {
if (this.props.profile.profileAddress) {
try {
// Will return a purchase object with a receipt which can be used to validate on your server.
const purchase = await RNIap.buyProduct(product.productId);
console.log(purchase);
this.setState({
receipt: purchase, // save the receipt if you need it, whether locally, or to your server.
});
await this.consumePurchases();
} catch (err) {
// standardized err.code and err.message available
console.log(err.code, err.message);
this.props.addAlert('error', '', 'Failed to buy product');
const subscription = RNIap.addAdditionalSuccessPurchaseListenerIOS(async (purchase) => {
this.setState({receipt: purchase}, async () => await this.consumePurchases());
subscription.remove();
});
}
} else {
this.props.addAlert('error', '', 'No wallet address available');
}
}
async supportWoWsInfo(item) {
try {
// Will return a purchase object with a receipt which can be used to validate on your server.
const purchase = await RNIap.buyProduct(item.productId);
// Consume it right away to buy multiple times
await RNIap.consumePurchase(purchase.purchaseToken);
this.setState({
receipt: purchase.transactionReceipt, // save the receipt if you need it, whether locally, or to your server.
});
} catch(err) {
// standardized err.code and err.message available
console.error(err.code, err.message);
const subscription = RNIap.addAdditionalSuccessPurchaseListenerIOS(async (purchase) => {
this.setState({ receipt: purchase.transactionReceipt }, () => this.goToNext());
subscription.remove();
});
}
}
}
async supportWoWsInfo(item) {
try {
// Will return a purchase object with a receipt which can be used to validate on your server.
const purchase = await RNIap.buyProduct(item.productId);
// Consume it right away to buy multiple times
await RNIap.consumePurchase(purchase.purchaseToken);
this.setState({
receipt: purchase.transactionReceipt, // save the receipt if you need it, whether locally, or to your server.
});
} catch(err) {
// standardized err.code and err.message available
console.error(err.code, err.message);
const subscription = RNIap.addAdditionalSuccessPurchaseListenerIOS(async (purchase) => {
this.setState({ receipt: purchase.transactionReceipt }, () => this.goToNext());
subscription.remove();
});
}
}
}
const maybeAcknowledgePurchaseAndroid = async (purchase: ProductPurchase) => {
if (requiresAcknowledging(purchase)) {
try {
// TODO check the response!
return !!(await acknowledgePurchaseAndroid(
purchase.purchaseToken || '', // if it's not set then set to empty string which will fail
))
} catch (ackErr) {
console.warn('ackErr', ackErr)
return false
}
}
return true
}
async componentDidMount() {
if (!GITHUB_VERSION) {
try {
const products = await RNIap.getProducts(itemSkus);
// Do this just to ensure that all IAPs are available
await RNIap.consumeAllItems();
products.sort((a, b) => a.price.localeCompare(b.price));
this.setState({products});
} catch(err) {
console.warn(err); // standardized err.code and err.message available
}
}
}