-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCPurchaseController.tsx
277 lines (241 loc) · 8.62 KB
/
RCPurchaseController.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { Platform } from 'react-native';
import Superwall, {
PurchaseController,
PurchaseResult,
RestorationResult,
SubscriptionStatus,
PurchaseResultCancelled,
PurchaseResultFailed,
PurchaseResultPending,
PurchaseResultPurchased,
PurchaseResultRestored,
} from '@superwall/react-native-superwall';
import Purchases, {
type CustomerInfo,
PRODUCT_CATEGORY,
type PurchasesStoreProduct,
type SubscriptionOption,
PURCHASES_ERROR_CODE,
type MakePurchaseResult,
} from 'react-native-purchases';
export class RCPurchaseController extends PurchaseController {
constructor() {
super();
Purchases.setLogLevel(Purchases.LOG_LEVEL.DEBUG);
const apiKey =
Platform.OS === 'ios'
? 'appl_XmYQBWbTAFiwLeWrBJOeeJJtTql'
: 'goog_DCSOujJzRNnPmxdgjOwdOOjwilC';
Purchases.configure({ apiKey });
}
syncSubscriptionStatus() {
// Listen for changes
Purchases.addCustomerInfoUpdateListener((customerInfo) => {
const hasActiveEntitlementOrSubscription =
this.hasActiveEntitlementOrSubscription(customerInfo);
Superwall.shared.setSubscriptionStatus(
hasActiveEntitlementOrSubscription
? SubscriptionStatus.ACTIVE
: SubscriptionStatus.INACTIVE
);
});
}
async purchaseFromAppStore(productId: string): Promise<PurchaseResult> {
const products = await Promise.all([
Purchases.getProducts([productId], PRODUCT_CATEGORY.SUBSCRIPTION),
Purchases.getProducts([productId], PRODUCT_CATEGORY.NON_SUBSCRIPTION),
]).then((results) => results.flat());
// Assuming an equivalent for Dart's firstOrNull is not directly available in TypeScript,
// so using a simple conditional check
const storeProduct = products.length > 0 ? products[0] : null;
if (!storeProduct) {
return new PurchaseResultFailed(
'Failed to find store product for $productId'
);
}
return await this._purchaseStoreProduct(storeProduct);
}
async purchaseFromGooglePlay(
productId: string,
basePlanId?: string,
offerId?: string
): Promise<PurchaseResult> {
// Find products matching productId from RevenueCat
const products = await Promise.all([
Purchases.getProducts([productId], PRODUCT_CATEGORY.SUBSCRIPTION),
Purchases.getProducts([productId], PRODUCT_CATEGORY.NON_SUBSCRIPTION),
]).then((results) => results.flat());
// Choose the product which matches the given base plan.
// If no base plan set, select first product or fail.
const storeProductId = `${productId}:${basePlanId}`;
// Initialize matchingProduct as null explicitly
let matchingProduct: PurchasesStoreProduct | null = null;
// Loop through each product in the products array
for (const product of products) {
// Check if the current product's identifier matches the given storeProductId
if (product.identifier === storeProductId) {
// If a match is found, assign this product to matchingProduct
matchingProduct = product;
// Break the loop as we found our matching product
break;
}
}
let storeProduct: PurchasesStoreProduct | null =
matchingProduct ??
(products.length > 0 && typeof products[0] !== 'undefined'
? products[0]
: null);
// If no product is found (either matching or the first one), return a failed purchase result.
if (storeProduct === null) {
return new PurchaseResultFailed('Product not found');
}
switch (storeProduct.productCategory) {
case PRODUCT_CATEGORY.SUBSCRIPTION:
const subscriptionOption =
await this._fetchGooglePlaySubscriptionOption(
storeProduct,
basePlanId,
offerId
);
if (subscriptionOption === null) {
return new PurchaseResultFailed(
'Valid subscription option not found for product.'
);
}
return await this._purchaseSubscriptionOption(subscriptionOption);
case PRODUCT_CATEGORY.NON_SUBSCRIPTION:
return await this._purchaseStoreProduct(storeProduct);
default:
return new PurchaseResultFailed('Unable to determine product category');
}
}
private async _purchaseStoreProduct(
storeProduct: PurchasesStoreProduct
): Promise<PurchaseResult> {
const performPurchase = async (): Promise<MakePurchaseResult> => {
// Attempt to purchase product
const makePurchaseResult = await Purchases.purchaseStoreProduct(
storeProduct
);
return makePurchaseResult;
};
return await this.handleSharedPurchase(performPurchase);
}
private async _fetchGooglePlaySubscriptionOption(
storeProduct: PurchasesStoreProduct,
basePlanId?: string,
offerId?: string
): Promise<SubscriptionOption | null> {
const subscriptionOptions = storeProduct.subscriptionOptions;
if (subscriptionOptions && subscriptionOptions.length > 0) {
// Concatenate base + offer ID
const subscriptionOptionId = this.buildSubscriptionOptionId(
basePlanId,
offerId
);
// Find first subscription option that matches the subscription option ID or use the default offer
let subscriptionOption: SubscriptionOption | null = null;
// Search for the subscription option with the matching ID
for (const option of subscriptionOptions) {
if (option.id === subscriptionOptionId) {
subscriptionOption = option;
break;
}
}
// If no matching subscription option is found, use the default option
subscriptionOption = subscriptionOption ?? storeProduct.defaultOption;
// Return the subscription option
return subscriptionOption;
}
return null;
}
private buildSubscriptionOptionId(
basePlanId?: string,
offerId?: string
): string {
let result = '';
if (basePlanId !== null) {
result += basePlanId;
}
if (offerId !== null) {
if (basePlanId !== null) {
result += ':';
}
result += offerId;
}
return result;
}
private async _purchaseSubscriptionOption(
subscriptionOption: SubscriptionOption
): Promise<PurchaseResult> {
// Define the async perform purchase function
const performPurchase = async (): Promise<MakePurchaseResult> => {
// Attempt to purchase product
const purchaseResult = await Purchases.purchaseSubscriptionOption(
subscriptionOption
);
return purchaseResult;
};
const purchaseResult: PurchaseResult = await this.handleSharedPurchase(
performPurchase
);
return purchaseResult;
}
private async handleSharedPurchase(
performPurchase: () => Promise<MakePurchaseResult>
): Promise<PurchaseResult> {
try {
// Store the current purchase date to later determine if this is a new purchase or restore
const purchaseDate = new Date();
// Perform the purchase using the function provided
const makePurchaseResult = await performPurchase();
// Handle the results
if (
this.hasActiveEntitlementOrSubscription(makePurchaseResult.customerInfo)
) {
const latestTransactionPurchaseDate = new Date(
makePurchaseResult.transaction.purchaseDate
);
// If no latest transaction date is found, consider it as a new purchase.
const isNewPurchase = latestTransactionPurchaseDate === null;
// If the current date (`purchaseDate`) is after the latestTransactionPurchaseDate,
const purchaseHappenedInThePast = latestTransactionPurchaseDate
? purchaseDate > latestTransactionPurchaseDate
: false;
if (!isNewPurchase && purchaseHappenedInThePast) {
return new PurchaseResultRestored();
} else {
return new PurchaseResultPurchased();
}
} else {
return new PurchaseResultFailed('No active subscriptions found.');
}
} catch (e: any) {
// Catch block to handle exceptions, adjusted for TypeScript
if (e.userCancelled) {
return new PurchaseResultCancelled();
}
if (e.code === PURCHASES_ERROR_CODE.PAYMENT_PENDING_ERROR) {
return new PurchaseResultPending();
} else {
return new PurchaseResultFailed(e.message);
}
}
}
async restorePurchases(): Promise<RestorationResult> {
try {
await Purchases.restorePurchases();
return RestorationResult.restored();
} catch (e: any) {
return RestorationResult.failed(e.message);
}
}
private hasActiveEntitlementOrSubscription(
customerInfo: CustomerInfo
): Boolean {
return (
customerInfo.activeSubscriptions.length > 0 &&
Object.keys(customerInfo.entitlements.active).length > 0
);
}
}