UNPKG

2.32 kBJavaScriptView Raw
1var platform = require("./platform");
2
3if (platform.isEjecta()) {
4 var iap = new window.Ejecta.IAPManager();
5
6 module.exports = {
7 "get": function(sku, callback) {
8 iap.getProducts([sku], function(err, products) {
9 if (err) {
10 callback(err);
11 return;
12 }
13 callback(undefined, products[0]);
14 });
15 },
16 "buy": function(product, quantity, callback) {
17 product.purchase(quantity, callback);
18 },
19 "restore": function(callback) {
20 iap.restoreTransactions(function(err, transactions) {
21 if (err) {
22 callback(err);
23 return;
24 }
25 callback(undefined, transactions.map(function(transaction) {
26 return transaction.productId;
27 }));
28 });
29 }
30 };
31} else if (platform.isChromeApp()) {
32 // FIXME: needs google's buy.js included
33 // https://developer.chrome.com/webstore/payments-iap
34 module.exports = {
35 "get": function(sku, callback) {
36 window.google.payments.inapp.getSkuDetails({
37 "parameters": {
38 "env": "prod"
39 },
40 "sku": sku,
41 "success": function(response) {
42 callback(undefined, response.response.details.inAppProducts[0]);
43 },
44 "failure": function(response) {
45 callback(response);
46 }
47 });
48 },
49 "buy": function(product, quantity, callback) {
50 window.google.payments.inapp.buy({
51 "parameters": {
52 "env": "prod"
53 },
54 "sku": product.sku,
55 "success": function(response) {
56 callback(undefined, response);
57 },
58 "failure": function(response) {
59 callback(response);
60 }
61 });
62 },
63 "restore": function(callback) {
64 window.google.payments.inapp.getPurchases({
65 "success": function(response) {
66 callback(undefined, response.response.details.map(function(detail) {
67 return detail.sku;
68 }));
69 },
70 "failure": function(response) {
71 callback(response);
72 }
73 });
74 }
75 };
76} else {
77 module.exports = {
78 "get": function(sku, callback) {
79 callback(undefined, undefined);
80 },
81 "buy": function(product, quantity, callback) {
82 callback(undefined);
83 },
84 "restore": function(callback) {
85 callback(undefined, []);
86 }
87 };
88}