import { connectAsync, disconnectAsync, finishTransactionAsync, getBillingResponseCodeAsync, getProductsAsync, getPurchaseHistoryAsync, IAPResponseCode, purchaseItemAsync, setPurchaseListener, IAPItemDetails, InAppPurchase, } from 'expo-in-app-purchases'; import React from 'react'; import { Button, Platform, SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, View, } from 'react-native'; export default class InAppPurchases extends React.Component { static navigationOptions = { title: 'In-App Purchases', }; state = { items: [], history: [], responseCode: 0, }; componentDidMount() { this.prepareAsync(); } async prepareAsync() { // This method must be called first to initialize listeners and billing client 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'], default: [], }); // Get product details const { responseCode, results } = await getProductsAsync(items); if (responseCode === IAPResponseCode.OK) { this.setState({ items: 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) { // `gas` is the only consumable product, the rest are subscriptions. finishTransactionAsync(purchase, purchase.productId === 'gas'); } } } 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 componentWillUnmount() { await disconnectAsync(); } async queryPurchaseHistory() { const { responseCode, results } = await getPurchaseHistoryAsync({ useGooglePlayCache: false }); if (responseCode === IAPResponseCode.OK) { this.setState({ history: results }); } } renderItem(item: IAPItemDetails) { return ( {item.title} Description: {item.description} Price: {item.price} Currency Code: {item.priceCurrencyCode} Price Amount Micros: {item.priceAmountMicros} Product ID: {item.productId} Type: {item.type} Subscription Period: {item.subscriptionPeriod}