|
| 1 | +import { |
| 2 | + endConnection, |
| 3 | + getProducts, |
| 4 | + getSubscriptions, |
| 5 | + initConnection, |
| 6 | + isProductAndroid, |
| 7 | + isProductIos, |
| 8 | + isSubscriptionProductAndroid, |
| 9 | + isSubscriptionProductIos, |
| 10 | + purchaseErrorListener, |
| 11 | + purchaseUpdatedListener, |
| 12 | + requestPurchase, |
| 13 | + requestSubscription, |
| 14 | +} from 'expo-iap'; |
| 15 | +import type { |
| 16 | + Product, |
| 17 | + ProductPurchase, |
| 18 | + PurchaseError, |
| 19 | + SubscriptionProduct, |
| 20 | +} from 'expo-iap/build/ExpoIap.types'; |
| 21 | +import type {RequestSubscriptionAndroidProps} from 'expo-iap/build/types/ExpoIapAndroid.types'; |
| 22 | +import {Stack} from 'expo-router'; |
| 23 | +import {useEffect, useState} from 'react'; |
| 24 | +import { |
| 25 | + Alert, |
| 26 | + Button, |
| 27 | + InteractionManager, |
| 28 | + Pressable, |
| 29 | + SafeAreaView, |
| 30 | + ScrollView, |
| 31 | + StyleSheet, |
| 32 | + Text, |
| 33 | + View, |
| 34 | +} from 'react-native'; |
| 35 | +import {t} from '../../../src/STRINGS'; |
| 36 | + |
| 37 | +const productSkus = [ |
| 38 | + 'cpk.points.200', |
| 39 | + 'cpk.points.500', |
| 40 | + 'cpk.points.1000', |
| 41 | + 'cpk.points.5000', |
| 42 | + 'cpk.points.10000', |
| 43 | + 'cpk.points.30000', |
| 44 | +]; |
| 45 | + |
| 46 | +const subscriptionSkus = [ |
| 47 | + 'cpk.membership.monthly.bronze', |
| 48 | + 'cpk.membership.monthly.silver', |
| 49 | +]; |
| 50 | + |
| 51 | +const operations = [ |
| 52 | + 'initConnection', |
| 53 | + 'getProducts', |
| 54 | + 'getSubscriptions', |
| 55 | + 'endConnection', |
| 56 | +]; |
| 57 | +type Operation = (typeof operations)[number]; |
| 58 | + |
| 59 | +export default function App() { |
| 60 | + const [isConnected, setIsConnected] = useState(false); |
| 61 | + const [products, setProducts] = useState<Product[]>([]); |
| 62 | + const [subscriptions, setSubscriptions] = useState<SubscriptionProduct[]>([]); |
| 63 | + |
| 64 | + const handleOperation = async (operation: Operation) => { |
| 65 | + switch (operation) { |
| 66 | + case 'initConnection': |
| 67 | + if (await initConnection()) setIsConnected(true); |
| 68 | + return; |
| 69 | + |
| 70 | + case 'endConnection': |
| 71 | + if (await endConnection()) { |
| 72 | + setProducts([]); |
| 73 | + setIsConnected(false); |
| 74 | + } |
| 75 | + break; |
| 76 | + |
| 77 | + case 'getProducts': |
| 78 | + try { |
| 79 | + const products = await getProducts(productSkus); |
| 80 | + setProducts(products); |
| 81 | + } catch (error) { |
| 82 | + console.error(error); |
| 83 | + } |
| 84 | + break; |
| 85 | + |
| 86 | + case 'getSubscriptions': |
| 87 | + try { |
| 88 | + const subscriptions = await getSubscriptions(subscriptionSkus); |
| 89 | + setSubscriptions(subscriptions); |
| 90 | + } catch (error) { |
| 91 | + console.error(error); |
| 92 | + } |
| 93 | + break; |
| 94 | + |
| 95 | + default: |
| 96 | + console.log('Unknown operation'); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + useEffect(() => { |
| 101 | + const purchaseUpdatedSubs = purchaseUpdatedListener( |
| 102 | + (purchase: ProductPurchase) => { |
| 103 | + InteractionManager.runAfterInteractions(() => { |
| 104 | + Alert.alert('Purchase updated', JSON.stringify(purchase)); |
| 105 | + }); |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + const purchaseErrorSubs = purchaseErrorListener((error: PurchaseError) => { |
| 110 | + InteractionManager.runAfterInteractions(() => { |
| 111 | + Alert.alert('Purchase error', JSON.stringify(error)); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + return () => { |
| 116 | + purchaseUpdatedSubs.remove(); |
| 117 | + purchaseErrorSubs.remove(); |
| 118 | + endConnection(); |
| 119 | + }; |
| 120 | + }, []); |
| 121 | + |
| 122 | + return ( |
| 123 | + <SafeAreaView style={styles.container}> |
| 124 | + <Stack.Screen options={{title: t('settings.sponsors')}} /> |
| 125 | + <Text style={styles.title}>Expo IAP Example</Text> |
| 126 | + <View style={styles.buttons}> |
| 127 | + <ScrollView contentContainerStyle={styles.buttonsWrapper} horizontal> |
| 128 | + {operations.map((operation) => ( |
| 129 | + <Pressable |
| 130 | + key={operation} |
| 131 | + onPress={() => handleOperation(operation)} |
| 132 | + > |
| 133 | + <View style={styles.buttonView}> |
| 134 | + <Text>{operation}</Text> |
| 135 | + </View> |
| 136 | + </Pressable> |
| 137 | + ))} |
| 138 | + </ScrollView> |
| 139 | + </View> |
| 140 | + <View style={styles.content}> |
| 141 | + {!isConnected ? ( |
| 142 | + <Text>Not connected</Text> |
| 143 | + ) : ( |
| 144 | + <View style={{gap: 12}}> |
| 145 | + <Text style={{fontSize: 20}}>Products</Text> |
| 146 | + {products.map((item) => { |
| 147 | + if (isProductAndroid(item)) { |
| 148 | + return ( |
| 149 | + <View key={item.title} style={{gap: 12}}> |
| 150 | + <Text> |
| 151 | + {item.title} -{' '} |
| 152 | + {item.oneTimePurchaseOfferDetails?.formattedPrice} |
| 153 | + </Text> |
| 154 | + <Button |
| 155 | + title="Buy" |
| 156 | + onPress={() => { |
| 157 | + requestPurchase({ |
| 158 | + skus: [item.productId], |
| 159 | + }); |
| 160 | + }} |
| 161 | + /> |
| 162 | + </View> |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + if (isProductIos(item)) { |
| 167 | + return ( |
| 168 | + <View key={item.id} style={{gap: 12}}> |
| 169 | + <Text> |
| 170 | + {item.displayName} - {item.displayPrice} |
| 171 | + </Text> |
| 172 | + <Button |
| 173 | + title="Buy" |
| 174 | + onPress={() => { |
| 175 | + requestPurchase({ |
| 176 | + sku: item.id, |
| 177 | + }); |
| 178 | + }} |
| 179 | + /> |
| 180 | + </View> |
| 181 | + ); |
| 182 | + } |
| 183 | + })} |
| 184 | + |
| 185 | + <Text style={{fontSize: 20}}>Subscriptions</Text> |
| 186 | + {subscriptions.map((item) => { |
| 187 | + if (isSubscriptionProductAndroid(item)) { |
| 188 | + return item.subscriptionOfferDetails?.map((offer) => ( |
| 189 | + <View key={offer.offerId} style={{gap: 12}}> |
| 190 | + <Text> |
| 191 | + {item.title} -{' '} |
| 192 | + {offer.pricingPhases.pricingPhaseList |
| 193 | + .map((ppl) => ppl.billingPeriod) |
| 194 | + .join(',')} |
| 195 | + </Text> |
| 196 | + <Button |
| 197 | + title="Subscribe" |
| 198 | + onPress={() => { |
| 199 | + requestSubscription({ |
| 200 | + skus: [item.productId], |
| 201 | + ...(offer.offerToken && { |
| 202 | + subscriptionOffers: [ |
| 203 | + { |
| 204 | + sku: item.productId, |
| 205 | + offerToken: offer.offerToken, |
| 206 | + }, |
| 207 | + ], |
| 208 | + }), |
| 209 | + } as RequestSubscriptionAndroidProps); |
| 210 | + }} |
| 211 | + /> |
| 212 | + </View> |
| 213 | + )); |
| 214 | + } |
| 215 | + |
| 216 | + if (isSubscriptionProductIos(item)) { |
| 217 | + return ( |
| 218 | + <View key={item.id} style={{gap: 12}}> |
| 219 | + <Text> |
| 220 | + {item.displayName} - {item.displayPrice} |
| 221 | + </Text> |
| 222 | + <Button |
| 223 | + title="Subscribe" |
| 224 | + onPress={() => { |
| 225 | + requestSubscription({sku: item.id}); |
| 226 | + }} |
| 227 | + /> |
| 228 | + </View> |
| 229 | + ); |
| 230 | + } |
| 231 | + })} |
| 232 | + </View> |
| 233 | + )} |
| 234 | + </View> |
| 235 | + </SafeAreaView> |
| 236 | + ); |
| 237 | +} |
| 238 | + |
| 239 | +const styles = StyleSheet.create({ |
| 240 | + container: { |
| 241 | + flex: 1, |
| 242 | + backgroundColor: '#fff', |
| 243 | + alignItems: 'center', |
| 244 | + }, |
| 245 | + title: { |
| 246 | + marginTop: 24, |
| 247 | + fontSize: 20, |
| 248 | + fontWeight: 'bold', |
| 249 | + }, |
| 250 | + buttons: { |
| 251 | + height: 90, |
| 252 | + }, |
| 253 | + buttonsWrapper: { |
| 254 | + padding: 24, |
| 255 | + |
| 256 | + gap: 8, |
| 257 | + }, |
| 258 | + buttonView: { |
| 259 | + borderRadius: 8, |
| 260 | + borderWidth: 1, |
| 261 | + borderColor: '#000', |
| 262 | + padding: 8, |
| 263 | + }, |
| 264 | + content: { |
| 265 | + flex: 1, |
| 266 | + alignSelf: 'stretch', |
| 267 | + padding: 24, |
| 268 | + gap: 12, |
| 269 | + }, |
| 270 | +}); |
0 commit comments