UNPKG

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