-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
119 lines (103 loc) 路 3.14 KB
/
index.js
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
import {
NativeModules,
NativeEventEmitter,
DeviceEventEmitter,
Platform,
} from "react-native";
const { RNInAppPurchase } = NativeModules;
export const InAppPurchaseErrorType = {
FETCH_PRODUCTS: "FETCH_PRODUCTS",
PURCHASE: "PURCHASE",
CONNECTION: "CONNECTION",
};
export const InAppPurchaseErrorCode = {
USER_CANCELED: Platform.select({
ios: "2",
android: "1",
}),
};
export const InAppPurchaseProductType = {
Subscription: "subs",
InApp: "inapp",
};
const addListener = (event, listener) =>
Platform.select({
ios: new NativeEventEmitter(RNInAppPurchase),
android: DeviceEventEmitter,
}).addListener(event, listener);
const removeAllListeners = (event) =>
Platform.select({
ios: new NativeEventEmitter(RNInAppPurchase),
android: DeviceEventEmitter,
}).removeAllListeners(event);
const onFetchProducts = (listener) =>
addListener("iap:onFetchProductsSuccess", listener);
const fetchReceipt = () => {
if (Platform.OS === "ios") {
return RNInAppPurchase.fetchReceipt();
} else {
return Promise.resolve(undefined);
}
};
const onPurchase = (listener) => addListener("iap:onPurchaseSuccess", listener);
const onAlternativeBillingFlow = (listener) => addListener("iap:onAlternativeBillingFlow", listener);
const onError = (listener) => {
if (Platform.OS === "android") {
addListener("iap:onConnectionFailure", (e) =>
listener(Object.assign(e, { type: InAppPurchaseErrorType.CONNECTION }))
);
}
addListener("iap:onFetchProductsFailure", (e) =>
listener(Object.assign(e, { type: InAppPurchaseErrorType.FETCH_PRODUCTS }))
);
addListener("iap:onPurchaseFailure", (e) =>
listener(Object.assign(e, { type: InAppPurchaseErrorType.PURCHASE }))
);
};
const clear = () => {
removeAllListeners("iap:onFetchProductsSuccess");
removeAllListeners("iap:onPurchaseSuccess");
removeAllListeners("iap:onAlternativeBillingFlow");
removeAllListeners("iap:onFetchProductsFailure");
removeAllListeners("iap:onPurchaseFailure");
if (Platform.OS === "android") {
removeAllListeners("iap:onConnectionFailure");
}
};
const configure = (options) => {
return Platform.OS === "android"
? RNInAppPurchase.configure(options)
: RNInAppPurchase.configure();
}
const purchase = (productId, { planId, offerId, userId, keyIdentifier, nonce, signature, timestamp, originalPurchaseToken, obfuscatedAccountId, obfuscatedProfileId }) => {
if (Platform.OS === "android") {
RNInAppPurchase.purchase(
productId,
planId || null,
offerId || null,
originalPurchaseToken || null,
obfuscatedAccountId || null,
obfuscatedProfileId || null
);
} else {
RNInAppPurchase.purchase(productId, offerId, userId, keyIdentifier, nonce, signature, timestamp);
}
};
const finalize = (purchase, isConsumable) => {
return Platform.OS === "android"
? RNInAppPurchase.finalize(purchase, isConsumable)
: RNInAppPurchase.finalize(purchase);
};
export default {
configure,
fetchProducts: RNInAppPurchase.fetchProducts,
flush: RNInAppPurchase.flush,
purchase,
finalize,
fetchReceipt,
onFetchProducts,
onPurchase,
onAlternativeBillingFlow,
onError,
clear,
};