forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpurchase.js
More file actions
44 lines (39 loc) · 993 Bytes
/
purchase.js
File metadata and controls
44 lines (39 loc) · 993 Bytes
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
import helpers from "utils/helpers";
export default function purchaseListener(onpurchase, onerror) {
return [
(purchases) => {
const [purchase] = purchases;
if (purchase.purchaseState === iap.PURCHASE_STATE_PURCHASED) {
if (!purchase.isAcknowledged) {
iap.acknowledgePurchase(
purchase.purchaseToken,
() => {
onpurchase();
},
(error) => {
if (typeof onerror === "function") onerror(error);
},
);
return;
}
onpurchase();
return;
}
const message =
purchase.purchaseState === iap.PURCHASE_STATE_PENDING
? strings["purchase pending"]
: strings.failed;
helpers.error(message);
if (typeof onerror === "function") onerror(message);
},
(error) => {
if (error === iap.ITEM_ALREADY_OWNED) {
onpurchase();
return;
}
const message =
error === iap.USER_CANCELED ? strings.failed : strings.canceled;
if (typeof onerror === "function") onerror(message);
},
];
}