-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
Subscriptions.tsx
160 lines (149 loc) · 4.57 KB
/
Subscriptions.tsx
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
import React, {useEffect, useState} from 'react';
import {ScrollView, StyleSheet, Text, View} from 'react-native';
import {
isIosStorekit2,
PurchaseError,
requestSubscription,
useIAP,
} from 'react-native-iap';
import {Box, Button, Heading, Row, State} from '../components';
import {
constants,
contentContainerStyle,
errorLog,
isAmazon,
isIos,
isPlay,
} from '../utils';
export const Subscriptions = () => {
const {
connected,
subscriptions,
getSubscriptions,
currentPurchase,
finishTransaction,
} = useIAP();
const [ownedSubscriptions, setOwnedSubscriptions] = useState<string[]>([]);
const handleGetSubscriptions = async () => {
try {
await getSubscriptions({skus: constants.subscriptionSkus});
} catch (error) {
errorLog({message: 'handleGetSubscriptions', error});
}
};
const handleBuySubscription = async (
productId: string,
offerToken?: string,
) => {
if (isPlay && !offerToken) {
console.warn(
`There are no subscription Offers for selected product (Only requiered for Google Play purchases): ${productId}`,
);
}
try {
await requestSubscription({
sku: productId,
...(offerToken && {
subscriptionOffers: [{sku: productId, offerToken}],
}),
});
} catch (error) {
if (error instanceof PurchaseError) {
errorLog({message: `[${error.code}]: ${error.message}`, error});
} else {
errorLog({message: 'handleBuySubscription', error});
}
}
};
useEffect(() => {
const checkCurrentPurchase = async () => {
try {
if (currentPurchase?.productId) {
await finishTransaction({
purchase: currentPurchase,
isConsumable: true,
});
setOwnedSubscriptions(prev => [...prev, currentPurchase?.productId]);
}
} catch (error) {
if (error instanceof PurchaseError) {
errorLog({message: `[${error.code}]: ${error.message}`, error});
} else {
errorLog({message: 'handleBuyProduct', error});
}
}
};
checkCurrentPurchase();
}, [currentPurchase, finishTransaction]);
return (
<ScrollView contentContainerStyle={contentContainerStyle}>
<State connected={connected} storekit2={isIosStorekit2()} />
<Box>
<View style={styles.container}>
<Heading copy="Subscriptions" />
{subscriptions.map((subscription, index) => {
const owned = ownedSubscriptions.find(pId => {
return isAmazon
? pId === constants.amazonBaseSku
: pId === subscription.productId;
});
return (
<Row
key={subscription.productId}
fields={[
{
label: 'Subscription Id',
value: subscription.productId,
},
{
label: 'type',
value:
'type' in subscription
? subscription.type
: subscription.productType,
},
]}
isLast={subscriptions.length - 1 === index}>
{owned && <Text>Subscribed</Text>}
{!owned &&
isPlay &&
// On Google Play Billing V5 you might have multiple offers for a single sku
'subscriptionOfferDetails' in subscription &&
subscription?.subscriptionOfferDetails?.map(offer => (
<Button
title={`Subscribe ${offer.pricingPhases.pricingPhaseList
.map(ppl => ppl.billingPeriod)
.join(',')}`}
onPress={() => {
handleBuySubscription(
subscription.productId,
offer.offerToken,
);
}}
/>
))}
{!owned && (isIos || isAmazon) && (
<Button
title="Subscribe"
onPress={() => {
handleBuySubscription(subscription.productId);
}}
/>
)}
</Row>
);
})}
</View>
<Button
title="Get the subscriptions"
onPress={handleGetSubscriptions}
/>
</Box>
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
marginBottom: 20,
},
});