Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'expo-in-app-purchases' 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.
/*
These item entries are created in App Store Connect and Google Play Console respectively.
If you want to add more or edit their attributes you can do so there.
*/
const items = Platform.select({
ios: [
'dev.expo.products.premium',
'dev.expo.payments.updates',
'dev.expo.payments.adfree',
'dev.expo.payments.gold',
],
android: ['gas', 'premium', 'gold_yearly', 'gold_monthly'],
});
// Get product details
const { responseCode, results } = await getProductsAsync(items);
if (responseCode === IAPResponseCode.OK) {
this.setState({ items: results, history: history.results });
}
// Set purchase listener
setPurchaseListener(({ responseCode, results, errorCode }) => {
if (responseCode === IAPResponseCode.OK) {
for (const purchase of results) {
console.log(`Successfully purchased ${purchase.productId}`);
if (!purchase.acknowledged) {
finishTransactionAsync(purchase, true);
}
}
} else if (responseCode === IAPResponseCode.USER_CANCELED) {
console.log('User canceled');
} else {
'dev.expo.products.premium',
'dev.expo.payments.updates',
'dev.expo.payments.adfree',
'dev.expo.payments.gold',
],
android: ['gas', 'premium', 'gold_yearly', 'gold_monthly'],
});
// Get product details
const { responseCode, results } = await getProductsAsync(items);
if (responseCode === IAPResponseCode.OK) {
this.setState({ items: results, history: history.results });
}
// Set purchase listener
setPurchaseListener(({ responseCode, results, errorCode }) => {
if (responseCode === IAPResponseCode.OK) {
for (const purchase of results) {
console.log(`Successfully purchased ${purchase.productId}`);
if (!purchase.acknowledged) {
finishTransactionAsync(purchase, true);
}
}
} else if (responseCode === IAPResponseCode.USER_CANCELED) {
console.log('User canceled');
} else {
console.warn(
`Something went wrong with the purchase. Received response code ${responseCode} and errorCode ${errorCode}`
);
}
});
}
async componentDidMount() {
// This method must be called first to initialize listeners and billing client
const history = await connectAsync();
/*
These item entries are created in App Store Connect and Google Play Console respectively.
If you want to add more or edit their attributes you can do so there.
*/
const items = Platform.select({
ios: [
'dev.expo.products.premium',
'dev.expo.payments.updates',
'dev.expo.payments.adfree',
'dev.expo.payments.gold',
],
android: ['gas', 'premium', 'gold_yearly', 'gold_monthly'],
});
// Get product details
setPurchaseListener(({ responseCode, results, errorCode }) => {
if (responseCode === IAPResponseCode.OK) {
for (const purchase of results) {
console.log(`Successfully purchased ${purchase.productId}`);
if (!purchase.acknowledged) {
finishTransactionAsync(purchase, true);
}
}
} else if (responseCode === IAPResponseCode.USER_CANCELED) {
console.log('User canceled');
} else {
console.warn(
`Something went wrong with the purchase. Received response code ${responseCode} and errorCode ${errorCode}`
);
}
});
}
setPurchaseListener(({ responseCode, results, errorCode }) => {
if (responseCode === IAPResponseCode.OK) {
for (const purchase of results) {
console.log(`Successfully purchased ${purchase.productId}`);
if (!purchase.acknowledged) {
finishTransactionAsync(purchase, true);
}
}
} else if (responseCode === IAPResponseCode.USER_CANCELED) {
console.log('User canceled');
} else {
console.warn(
`Something went wrong with the purchase. Received response code ${responseCode} and errorCode ${errorCode}`
);
}
});
}
setPurchaseListener(({ responseCode, results, errorCode }) => {
if (responseCode === IAPResponseCode.OK) {
for (const purchase of results) {
console.log(`Successfully purchased ${purchase.productId}`);
if (!purchase.acknowledged) {
finishTransactionAsync(purchase, true);
}
}
} else if (responseCode === IAPResponseCode.USER_CANCELED) {
console.log('User canceled');
} else {
console.warn(
`Something went wrong with the purchase. Received response code ${responseCode} and errorCode ${errorCode}`
);
}
});
}
async queryPurchaseHistory() {
const { responseCode, results } = await getPurchaseHistoryAsync(true);
if (responseCode === IAPResponseCode.OK) {
this.setState({ history: results });
}
}
async queryPurchaseHistory() {
const { responseCode, results } = await getPurchaseHistoryAsync(true);
if (responseCode === IAPResponseCode.OK) {
this.setState({ history: results });
}
}
async componentWillUnmount() {
await disconnectAsync();
}
async getBillingResult() {
const responseCode = await getBillingResponseCodeAsync();
this.setState({ responseCode });
}