-
-
Notifications
You must be signed in to change notification settings - Fork 626
/
platform.ts
135 lines (113 loc) · 3.12 KB
/
platform.ts
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
import {NativeModules, Platform} from 'react-native';
import {ErrorCode} from '../purchaseError';
const {RNIapIos, RNIapIosSk2, RNIapModule, RNIapAmazonModule} = NativeModules;
export const isIos = Platform.OS === 'ios';
export const isAndroid = Platform.OS === 'android';
export const isAmazon = isAndroid && !!RNIapAmazonModule;
export const isPlay = isAndroid && !!RNIapModule;
// Android
let androidNativeModule = RNIapModule;
export const setAndroidNativeModule = (
nativeModule: typeof RNIapModule,
): void => {
androidNativeModule = nativeModule;
};
export const checkNativeAndroidAvailable = (): void => {
if (!RNIapModule && !RNIapAmazonModule) {
throw new Error(ErrorCode.E_IAP_NOT_AVAILABLE);
}
};
/**
* If changing the typings of `getAndroidModule` to accommodate extra modules,
* make sure to update `getAndroidModuleType`.
*/
export const getAndroidModule = ():
| typeof RNIapModule
| typeof RNIapAmazonModule => {
checkNativeAndroidAvailable();
return androidNativeModule
? androidNativeModule
: RNIapModule
? RNIapModule
: RNIapAmazonModule;
};
/**
* Returns whether the Android in-app-purchase code is using the Android,
* Amazon, or another store.
*/
export const getAndroidModuleType = (): 'android' | 'amazon' | null => {
const module = getAndroidModule();
switch (module) {
case RNIapModule:
return 'android';
case RNIapAmazonModule:
return 'amazon';
default:
return null;
}
};
export const getNativeModule = ():
| typeof RNIapModule
| typeof RNIapAmazonModule
| typeof RNIapIos
| typeof RNIapIosSk2 => {
return isAndroid ? getAndroidModule() : getIosModule();
};
// iOS
let iosNativeModule: typeof RNIapIos | typeof RNIapIosSk2 = RNIapIos;
export const isStorekit2Avaiable = (): boolean =>
isIos && RNIapIosSk2?.isAvailable() === 1;
export const isIosStorekit2 = () =>
isIos &&
!!iosNativeModule &&
iosNativeModule === RNIapIosSk2 &&
isStorekit2Avaiable();
export const setIosNativeModule = (
nativeModule: typeof RNIapIos | typeof RNIapIosSk2,
): void => {
iosNativeModule = nativeModule;
};
export const storekit2Mode = () => {
iosNativeModule = RNIapIosSk2;
if (isStorekit2Avaiable()) {
RNIapIos.disable();
return true;
}
if (isIos) {
console.warn('Storekit 2 is not available on this device');
return false;
}
return true;
};
export const storekit1Mode = () => {
iosNativeModule = RNIapIos;
if (isStorekit2Avaiable()) {
RNIapIosSk2.disable();
return true;
}
return false;
};
export const storekitHybridMode = () => {
if (isStorekit2Avaiable()) {
iosNativeModule = RNIapIosSk2;
console.info('Using Storekit 2');
return true;
} else {
iosNativeModule = RNIapIos;
console.info('Using Storekit 1');
return true;
}
};
const checkNativeIOSAvailable = (): void => {
if (!RNIapIos && !isStorekit2Avaiable()) {
throw new Error(ErrorCode.E_IAP_NOT_AVAILABLE);
}
};
export const getIosModule = (): typeof RNIapIos | typeof RNIapIosSk2 => {
checkNativeIOSAvailable();
return iosNativeModule
? iosNativeModule
: RNIapIosSk2
? RNIapIosSk2
: RNIapIos;
};