-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathApp.js
More file actions
184 lines (168 loc) · 5.49 KB
/
Copy pathApp.js
File metadata and controls
184 lines (168 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {Component} from 'react';
import {Platform, LogBox, I18nManager} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import 'react-native-gesture-handler';
import {createStackNavigator} from '@react-navigation/stack';
import HomeScreen from './components/HomeScreen.js';
import Cart from './components/Cart.js';
import Item from './components/Item.js';
import {
initConnection,
finishTransaction,
purchaseErrorListener,
purchaseUpdatedListener,
finishInAppTransaction,
flushFailedPurchasesCachedAsPendingAndroid,
withIAPContext,
getProducts,
getSubscriptions,
endConnection,
} from 'react-native-iap';
import {AppsFlyerPurchaseConnector} from 'react-native-appsflyer';
const Stack = createStackNavigator();
try {
// Disable RTL alignments for this app
I18nManager.allowRTL(false);
} catch (e) {
console.log('Failed to disable RTL', e);
}
// Ignore certain warnings regarding navigation state
LogBox.ignoreLogs([
'Non-serializable values were found in the navigation state',
]);
// Test items (hardcoded)
/*
const items = Platform.select({
ios: ['com.appsflyer.inapppurchase.non.cons', 'com.appsflyer.inapppurchase.cons', 'com.appsflyer.inapppurchase.two'],
android: ['noa_coin1', 'paz_test', 'btc'],
});
const subscriptions = Platform.select({
ios: ['com.appsflyer.inapppurchase.non.renew', 'com.appsflyer.inapppurchase.auto.renew'],
android: ['cheap', 'intro'],
});
*/
class App extends Component {
/*
purchaseUpdateSubscription = null;
purchaseErrorSubscription = null;
componentDidMount() {
this.setupIAP();
}
setupIAP = async () => {
try {
await initConnection().then(() => {});
if (Platform.OS == 'android') {
await flushFailedPurchasesCachedAsPendingAndroid().catch(() => {
console.warn(
"there are pending purchases that are still pending (we can't consume a pending purchase)",
);
});
}
console.log('[RNPurchaseConnector] Items: >>', items);
console.log('[RNPurchaseConnector] Subscriptions: >>', subscriptions);
await getProducts({skus: items})
.then(res => {
console.log('[Products] >> ', res);
})
.catch(err => {
console.log('[Error finding products] >> ', err);
});
await getSubscriptions({skus: subscriptions})
.then(res => {
console.log('[Subscriptions] >> ', res);
})
.catch(err => {
console.log('[Error finding Subscriptions] >> ', err);
});
this.purchaseUpdateSubscription = purchaseUpdatedListener(
async purchase => {
try {
console.log('purchaseUpdatedListener', purchase);
const receipt = purchase.transactionReceipt;
if (receipt) {
// Check if the purchased product is a subscription or a consumable item
const isSubscription = subscriptions.includes(purchase.productId);
const isConsumable = items.includes(purchase.productId);
console.log('[Receipt] >> ', receipt);
if (isSubscription || isConsumable) {
await finishTransaction({
purchase: purchase,
isConsumable: isConsumable, // true for consumables, false for subscriptions
}).catch(error => {
console.warn('Error finishing transaction:', error);
});
} else {
// Handle the case where the purchase is non-consumable and not a subscription.
await finishTransaction({
purchase: purchase,
isConsumable: false,
}).catch(error => {
console.warn('Error finishing transaction:', error);
});
}
}
} catch (error) {
console.warn('Error in purchaseUpdatedListener', error);
}
},
);
this.purchaseErrorSubscription = purchaseErrorListener(error => {
console.warn('purchaseErrorListener', error);
});
} catch (err) {
console.warn('Failed to init IAP connection:', err);
}
};
componentWillUnmount() {
if (this.purchaseUpdateSubscription) {
this.purchaseUpdateSubscription.remove();
this.purchaseUpdateSubscription = null;
}
if (this.purchaseErrorSubscription) {
this.purchaseErrorSubscription.remove();
this.purchaseErrorSubscription = null;
}
endConnection();
}
*/
render() {
return (
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerStyle: {
backgroundColor: '#52c41a',
},
headerTintColor: '#FFFFFF',
headerTitleStyle: {
fontWeight: 'bold',
},
headerTitleAlign: 'center',
}}>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{
title: 'The AppsFlyer Shop!',
}}
/>
{/* <Stack.Screen name="Cart" component={withIAPContext(Cart)} /> */}
<Stack.Screen name="Cart" component={Cart} />
<Stack.Screen
name="Item"
component={Item}
options={({route}) => ({title: route.params.product.name})}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
}
export default withIAPContext(App);