-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathnativeModule.ts
More file actions
369 lines (363 loc) · 14.3 KB
/
Copy pathnativeModule.ts
File metadata and controls
369 lines (363 loc) · 14.3 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import {
INTRO_ELIGIBILITY_STATUS,
MakePurchaseResult,
} from '@revenuecat/purchases-typescript-internal';
import { PurchasesCommon } from '@revenuecat/purchases-js-hybrid-mappings';
import { validateAndTransform, isCustomerInfo, isPurchasesOfferings, isPurchasesOffering, isLogInResult, isMakePurchaseResult, isPurchasesVirtualCurrencies } from './typeGuards';
import { isExpoGo, isRorkSandbox } from '../utils/environment';
import { ensurePurchasesConfigured, methodNotSupportedOnWeb } from './utils';
import { purchaseSimulatedPackage } from './simulatedstore/purchaseSimulatedPackageHelper';
const packageVersion = '9.1.0';
/**
* Browser implementation of the native module. This will be used in the browser and Expo Go.
*/
export const browserNativeModuleRNPurchases = {
setupPurchases: (
apiKey: string,
appUserID: string | null,
_purchasesAreCompletedBy: string | null,
_userDefaultsSuiteName: string | null,
_storeKitVersion: string | null,
_useAmazon: boolean,
_store: string | null,
_galaxyBillingMode: string | null,
_shouldShowInAppMessagesAutomatically: boolean,
_entitlementVerificationMode: string | null,
_pendingTransactionsForPrepaidPlansEnabled: boolean,
_diagnosticsEnabled: boolean,
_automaticDeviceIdentifierCollectionEnabled: boolean,
_preferredUILocaleOverride: string | null
) => {
try {
// Make sure that when running in Expo Go or Rork sandbox a web-compatible API key is used, because the underlying purchases-js error message isn't super clear when a non-compatible API key type is used in this case
const webPlatformCompatibleApiKeyPrefixList = ['test_', 'rcb_'];
const isWebPlatformCompatibleApiKey = webPlatformCompatibleApiKeyPrefixList.some(prefix => apiKey.startsWith(prefix));
if (!isWebPlatformCompatibleApiKey && (isExpoGo() || isRorkSandbox())) {
const platform = isRorkSandbox() ? 'Rork sandbox' : 'Expo Go';
throw new Error(
`Invalid API key. The native store is not available when running inside ${platform}, please use your Test Store API Key or create a development build in order to use native features. See https://rev.cat/sdk-test-store on how to use the Test Store.`
);
}
PurchasesCommon.configure({
apiKey,
appUserId: appUserID || undefined,
flavor: 'react-native',
flavorVersion: packageVersion,
});
} catch (error) {
console.error('Error configuring Purchases:', error);
throw error;
}
},
setAllowSharingStoreAccount: async (_allowSharing: boolean) => {
methodNotSupportedOnWeb('setAllowSharingStoreAccount');
},
setSimulatesAskToBuyInSandbox: async (_simulatesAskToBuyInSandbox: boolean) => {
methodNotSupportedOnWeb('setSimulatesAskToBuyInSandbox');
},
getOfferings: async () => {
ensurePurchasesConfigured();
const offerings = await PurchasesCommon.getInstance().getOfferings();
return validateAndTransform(offerings, isPurchasesOfferings, 'PurchasesOfferings');
},
getCurrentOfferingForPlacement: async (placementIdentifier: string) => {
ensurePurchasesConfigured();
const offering = await PurchasesCommon.getInstance().getCurrentOfferingForPlacement(placementIdentifier);
return offering ? validateAndTransform(offering, isPurchasesOffering, 'PurchasesOffering') : null;
},
syncAttributesAndOfferingsIfNeeded: async () => {
ensurePurchasesConfigured();
const offerings = await PurchasesCommon.getInstance().getOfferings();
return validateAndTransform(offerings, isPurchasesOfferings, 'PurchasesOfferings');
},
setAppstackAttributionParams: async (_data: Record<string, any>) => {
methodNotSupportedOnWeb('setAppstackAttributionParams');
},
getProductInfo: async (_productIdentifiers: string[], _type: string) => {
methodNotSupportedOnWeb('getProductInfo');
},
restorePurchases: async () => {
ensurePurchasesConfigured();
// For web, restoring purchases just returns current customer info
const customerInfo = await PurchasesCommon.getInstance().getCustomerInfo();
return validateAndTransform(customerInfo, isCustomerInfo, 'CustomerInfo');
},
getAppUserID: async () => {
ensurePurchasesConfigured();
return PurchasesCommon.getInstance().getAppUserId();
},
getStorefront: async () => {
methodNotSupportedOnWeb('getStorefront');
},
setDebugLogsEnabled: async (_enabled: boolean) => {
methodNotSupportedOnWeb('setDebugLogsEnabled');
},
setLogLevel: async (level: string) => {
PurchasesCommon.setLogLevel(level);
},
setLogHandler: async (handler: (level: string, message: string) => void) => {
PurchasesCommon.setLogHandler(handler);
},
trackCustomPaywallImpression: async (_data: any) => {
methodNotSupportedOnWeb('trackCustomPaywallImpression');
},
getCustomerInfo: async () => {
ensurePurchasesConfigured();
const customerInfo = await PurchasesCommon.getInstance().getCustomerInfo();
return validateAndTransform(customerInfo, isCustomerInfo, 'CustomerInfo');
},
logIn: async (appUserID: string) => {
ensurePurchasesConfigured();
const result = await PurchasesCommon.getInstance().logIn(appUserID);
return validateAndTransform(result, isLogInResult, 'LogInResult');
},
logOut: async () => {
ensurePurchasesConfigured();
const customerInfo = await PurchasesCommon.getInstance().logOut();
return validateAndTransform(customerInfo, isCustomerInfo, 'CustomerInfo');
},
syncPurchases: async () => {
methodNotSupportedOnWeb('syncPurchases');
},
syncPurchasesForResult: async () => {
methodNotSupportedOnWeb('syncPurchasesForResult');
},
syncAmazonPurchase: async (
_productID: string,
_receiptID: string,
_amazonUserID: string,
_isoCurrencyCode: string | null,
_price: number | null
) => {
methodNotSupportedOnWeb('syncAmazonPurchase');
},
syncObserverModeAmazonPurchase: async (
_productID: string,
_receiptID: string,
_amazonUserID: string,
_isoCurrencyCode: string | null,
_price: number | null
) => {
methodNotSupportedOnWeb('syncObserverModeAmazonPurchase');
},
recordPurchaseForProductID: async (_productID: string) => {
methodNotSupportedOnWeb('recordPurchaseForProductID');
},
enableAdServicesAttributionTokenCollection: async () => {
methodNotSupportedOnWeb('enableAdServicesAttributionTokenCollection');
},
purchaseProduct: async (
_productIdentifier: string,
_googleProductChangeInfo: any,
_type: string,
_discountTimestamp: string | null,
_googleInfo: any,
_presentedOfferingContext: any
) => {
methodNotSupportedOnWeb('purchaseProduct');
},
purchasePackage: async (
packageIdentifier: string,
presentedOfferingContext: any,
_googleProductChangeInfo: any,
_discountTimestamp: string | null,
_googleInfo: any
): Promise<MakePurchaseResult> => {
ensurePurchasesConfigured();
if (isExpoGo() || isRorkSandbox()) {
return await purchaseSimulatedPackage(packageIdentifier, presentedOfferingContext);
}
const purchaseResult = await PurchasesCommon.getInstance().purchasePackage(
{
packageIdentifier: packageIdentifier,
presentedOfferingContext: presentedOfferingContext,
}
);
return validateAndTransform(purchaseResult, isMakePurchaseResult, 'MakePurchaseResult');
},
purchaseSubscriptionOption: async (
_productIdentifier: string,
_optionIdentifier: string,
_upgradeInfo: any,
_discountTimestamp: string | null,
_googleInfo: any,
_presentedOfferingContext: any
) => {
methodNotSupportedOnWeb('purchaseSubscriptionOption');
},
isAnonymous: async () => {
ensurePurchasesConfigured();
return PurchasesCommon.getInstance().isAnonymous();
},
makeDeferredPurchase: async (_callbackID: number) => {
methodNotSupportedOnWeb('makeDeferredPurchase');
},
checkTrialOrIntroductoryPriceEligibility: async (productIDs: string[]) => {
const result: { [productId: string]: any } = {};
productIDs.forEach(productId => {
result[productId] = INTRO_ELIGIBILITY_STATUS.INTRO_ELIGIBILITY_STATUS_UNKNOWN;
});
return result;
},
getPromotionalOffer: async (_productIdentifier: string, _discount: any) => {
methodNotSupportedOnWeb('getPromotionalOffer');
},
eligibleWinBackOffersForProductIdentifier: async (_productID: string) => {
methodNotSupportedOnWeb('eligibleWinBackOffersForProductIdentifier');
},
purchaseProductWithWinBackOffer: async (_productID: string, _winBackOfferID: string) => {
methodNotSupportedOnWeb('purchaseProductWithWinBackOffer');
},
purchasePackageWithWinBackOffer: async (_packageID: string, _winBackOfferID: string) => {
methodNotSupportedOnWeb('purchasePackageWithWinBackOffer');
},
invalidateCustomerInfoCache: async () => {
methodNotSupportedOnWeb('invalidateCustomerInfoCache');
},
presentCodeRedemptionSheet: async () => {
methodNotSupportedOnWeb('presentCodeRedemptionSheet');
},
setAttributes: async (attributes: { [key: string]: string | null }) => {
await PurchasesCommon.getInstance().setAttributes(attributes);
},
setEmail: async (email: string) => {
await PurchasesCommon.getInstance().setEmail(email);
},
setPhoneNumber: async (phoneNumber: string) => {
await PurchasesCommon.getInstance().setPhoneNumber(phoneNumber);
},
setDisplayName: async (displayName: string) => {
await PurchasesCommon.getInstance().setDisplayName(displayName);
},
setPushToken: async (_pushToken: string) => {
methodNotSupportedOnWeb('setPushToken');
},
setProxyURLString: async (proxyURLString: string) => {
PurchasesCommon.setProxyUrl(proxyURLString);
},
collectDeviceIdentifiers: async () => {
methodNotSupportedOnWeb('collectDeviceIdentifiers');
},
setAdjustID: async (_adjustID: string) => {
methodNotSupportedOnWeb('setAdjustID');
},
setAppsflyerID: async (_appsflyerID: string) => {
methodNotSupportedOnWeb('setAppsflyerID');
},
setFBAnonymousID: async (_fbAnonymousID: string) => {
methodNotSupportedOnWeb('setFBAnonymousID');
},
setMparticleID: async (_mparticleID: string) => {
methodNotSupportedOnWeb('setMparticleID');
},
setCleverTapID: async (_cleverTapID: string) => {
methodNotSupportedOnWeb('setCleverTapID');
},
setMixpanelDistinctID: async (_mixpanelDistinctID: string) => {
methodNotSupportedOnWeb('setMixpanelDistinctID');
},
setFirebaseAppInstanceID: async (_firebaseAppInstanceID: string) => {
methodNotSupportedOnWeb('setFirebaseAppInstanceID');
},
setTenjinAnalyticsInstallationID: async (_tenjinAnalyticsInstallationID: string) => {
methodNotSupportedOnWeb('setTenjinAnalyticsInstallationID');
},
setKochavaDeviceID: async (_kochavaDeviceID: string) => {
methodNotSupportedOnWeb('setKochavaDeviceID');
},
setOnesignalID: async (_onesignalID: string) => {
methodNotSupportedOnWeb('setOnesignalID');
},
setAirshipChannelID: async (_airshipChannelID: string) => {
methodNotSupportedOnWeb('setAirshipChannelID');
},
setMediaSource: async (_mediaSource: string) => {
methodNotSupportedOnWeb('setMediaSource');
},
setMediaCampaign: async () => {
methodNotSupportedOnWeb('setMediaCampaign');
},
setCampaign: async (_campaign: string) => {
methodNotSupportedOnWeb('setCampaign');
},
setAdGroup: async (_adGroup: string) => {
methodNotSupportedOnWeb('setAdGroup');
},
setAd: async (_ad: string) => {
methodNotSupportedOnWeb('setAd');
},
setKeyword: async (_keyword: string) => {
methodNotSupportedOnWeb('setKeyword');
},
setCreative: async (_creative: string) => {
methodNotSupportedOnWeb('setCreative');
},
setAppsFlyerConversionData: async (_data: Record<string, any> | null) => {
methodNotSupportedOnWeb('setAppsFlyerConversionData');
},
overridePreferredLocale: async (_locale: string | null) => {
methodNotSupportedOnWeb('overridePreferredLocale');
},
canMakePayments: async (_features: any[]) => {
return true;
},
beginRefundRequestForActiveEntitlement: async () => {
methodNotSupportedOnWeb('beginRefundRequestForActiveEntitlement');
},
beginRefundRequestForEntitlementId: async (_entitlementIdentifier: string) => {
methodNotSupportedOnWeb('beginRefundRequestForEntitlementId');
},
beginRefundRequestForProductId: async (_productIdentifier: string) => {
methodNotSupportedOnWeb('beginRefundRequestForProductId');
},
showManageSubscriptions: async () => {
methodNotSupportedOnWeb('showManageSubscriptions');
},
showInAppMessages: async (_messageTypes: any[]) => {
methodNotSupportedOnWeb('showInAppMessages');
},
isWebPurchaseRedemptionURL: async (_urlString: string) => {
methodNotSupportedOnWeb('isWebPurchaseRedemptionURL');
},
isConfigured: async () => {
return PurchasesCommon.isConfigured();
},
redeemWebPurchase: async (_urlString: string) => {
methodNotSupportedOnWeb('redeemWebPurchase');
},
getVirtualCurrencies: async () => {
ensurePurchasesConfigured();
const virtualCurrencies = await PurchasesCommon.getInstance().getVirtualCurrencies();
return validateAndTransform(virtualCurrencies, isPurchasesVirtualCurrencies, 'PurchasesVirtualCurrencies');
},
invalidateVirtualCurrenciesCache: async () => {
ensurePurchasesConfigured();
PurchasesCommon.getInstance().invalidateVirtualCurrenciesCache();
},
getCachedVirtualCurrencies: async () => {
ensurePurchasesConfigured();
const cachedVirtualCurrencies = PurchasesCommon.getInstance().getCachedVirtualCurrencies();
return cachedVirtualCurrencies ? validateAndTransform(cachedVirtualCurrencies, isPurchasesVirtualCurrencies, 'PurchasesVirtualCurrencies') : null;
},
trackAdDisplayed: async (_data: any) => {
methodNotSupportedOnWeb('trackAdDisplayed');
},
trackAdOpened: async (_data: any) => {
methodNotSupportedOnWeb('trackAdOpened');
},
trackAdLoaded: async (_data: any) => {
methodNotSupportedOnWeb('trackAdLoaded');
},
trackAdRevenue: async (_data: any) => {
methodNotSupportedOnWeb('trackAdRevenue');
},
trackAdFailedToLoad: async (_data: any) => {
methodNotSupportedOnWeb('trackAdFailedToLoad');
},
generateRewardVerificationToken: async (_impressionId: string) => {
methodNotSupportedOnWeb('generateRewardVerificationToken');
},
pollRewardVerification: async (_clientTransactionId: string) => {
methodNotSupportedOnWeb('pollRewardVerification');
},
};