-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathInAppPurchaseManager.js
218 lines (187 loc) · 7.42 KB
/
InAppPurchaseManager.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* A plugin to enable iOS In-App Purchases.
*
* Copyright (c) Matt Kane 2011
* Copyright (c) Guillaume Charhon 2012
*/
var InAppPurchaseManager = function() {
cordova.exec('InAppPurchaseManager.setup');
}
/**
* Makes an in-app purchase.
*
* @param {String} productId The product identifier. e.g. "com.example.MyApp.myproduct"
* @param {int} quantity
*/
InAppPurchaseManager.prototype.makePurchase = function(productId, quantity) {
var q = parseInt(quantity);
if(!q) {
q = 1;
}
return cordova.exec('InAppPurchaseManager.makePurchase', productId, q);
}
/**
* Asks the payment queue to restore previously completed purchases.
* The restored transactions are passed to the onRestored callback, so make sure you define a handler for that first.
*
*/
InAppPurchaseManager.prototype.restoreCompletedTransactions = function() {
return cordova.exec('InAppPurchaseManager.restoreCompletedTransactions');
}
/**
* Retrieves the localised product data, including price (as a localised string), name, description.
* You must call this before attempting to make a purchase.
*
* @param {String} productId The product identifier. e.g. "com.example.MyApp.myproduct"
* @param {Function} successCallback Called once for each returned product id. Signature is function(productId, title, description, price)
* @param {Function} failCallback Called once for each invalid product id. Signature is function(productId)
*/
InAppPurchaseManager.prototype.requestProductData = function(productId, successCallback, failCallback) {
var key = 'f' + this.callbackIdx++;
window.plugins.inAppPurchaseManager.callbackMap[key] = {
success: function(productId, title, description, price ) {
if (productId == '__DONE') {
delete window.plugins.inAppPurchaseManager.callbackMap[key]
return;
}
successCallback(productId, title, description, price);
},
fail: failCallback
}
var callback = 'window.plugins.inAppPurchaseManager.callbackMap.' + key;
cordova.exec('InAppPurchaseManager.requestProductData', productId, callback + '.success', callback + '.fail');
}
/**
* Retrieves localised product data, including price (as localised
* string), name, description of multiple products.
*
* @param {Array} productIds
* An array of product identifier strings.
*
* @param {Function} callback
* Called once with the result of the products request. Signature:
*
* function(validProducts, invalidProductIds)
*
* where validProducts receives an array of objects of the form
*
* {
* id: "<productId>",
* title: "<localised title>",
* description: "<localised escription>",
* price: "<localised price>"
* }
*
* and invalidProductIds receives an array of product identifier
* strings which were rejected by the app store.
*/
InAppPurchaseManager.prototype.requestProductsData = function(productIds, callback) {
var key = 'b' + this.callbackIdx++;
window.plugins.inAppPurchaseManager.callbackMap[key] = function(validProducts, invalidProductIds) {
delete window.plugins.inAppPurchaseManager.callbackMap[key];
callback(validProducts, invalidProductIds);
};
var callbackName = 'window.plugins.inAppPurchaseManager.callbackMap.' + key;
cordova.exec('InAppPurchaseManager.requestProductsData', callbackName, {productIds: productIds});
};
/* function(transactionIdentifier, productId, transactionReceipt) */
InAppPurchaseManager.prototype.onPurchased = null;
/* function(originalTransactionIdentifier, productId, originalTransactionReceipt) */
InAppPurchaseManager.prototype.onRestored = null;
/* function(errorCode, errorText) */
InAppPurchaseManager.prototype.onFailed = null;
/* function() */
InAppPurchaseManager.prototype.onRestoreCompletedTransactionsFinished = null;
/* function(errorCode) */
InAppPurchaseManager.prototype.onRestoreCompletedTransactionsFailed = null;
/* This is called from native.*/
InAppPurchaseManager.prototype.updatedTransactionCallback =
function(state, errorCode, errorText, transactionIdentifier,
productId, transactionReceipt) {
switch(state) {
case "PaymentTransactionStatePurchased":
if(window.plugins.inAppPurchaseManager.onPurchased) {
window.plugins.inAppPurchaseManager.onPurchased(transactionIdentifier,
productId, transactionReceipt);
} else {
this.eventQueue.push(arguments);
this.watchQueue();
}
return;
case "PaymentTransactionStateFailed":
if(window.plugins.inAppPurchaseManager.onFailed) {
window.plugins.inAppPurchaseManager.onFailed(errorCode,
errorText);
} else {
this.eventQueue.push(arguments);
this.watchQueue();
}
return;
case "PaymentTransactionStateRestored":
if(window.plugins.inAppPurchaseManager.onRestored) {
window.plugins.inAppPurchaseManager.onRestored(transactionIdentifier,
productId, transactionReceipt);
} else {
this.eventQueue.push(arguments);
this.watchQueue();
}
return;
}
};
InAppPurchaseManager.prototype.restoreCompletedTransactionsFinished = function() {
if (this.onRestoreCompletedTransactionsFinished) {
this.onRestoreCompletedTransactionsFinished();
}
};
InAppPurchaseManager.prototype.restoreCompletedTransactionsFailed = function(errorCode) {
if (this.onRestoreCompletedTransactionsFailed) {
this.onRestoreCompletedTransactionsFailed(errorCode);
}
};
/*
* This queue stuff is here because we may be sent events before listeners have been registered. This is because if we have
* incomplete transactions when we quit, the app will try to run these when we resume. If we don't register to receive these
* right away then they may be missed. As soon as a callback has been registered then it will be sent any events waiting
* in the queue.
*/
InAppPurchaseManager.prototype.runQueue = function() {
if(!this.eventQueue.length || (!this.onPurchased && !this.onFailed && !this.onRestored)) {
return;
}
var args;
/* We can't work directly on the queue, because we're pushing new elements onto it */
var queue = this.eventQueue.slice();
this.eventQueue = [];
while(args = queue.shift()) {
this.updatedTransactionCallback.apply(this, args);
}
if(!this.eventQueue.length) {
this.unWatchQueue();
}
}
InAppPurchaseManager.prototype.watchQueue = function() {
if(this.timer) {
return;
}
this.timer = setInterval("window.plugins.inAppPurchaseManager.runQueue()", 10000);
}
InAppPurchaseManager.prototype.unWatchQueue = function() {
if(this.timer) {
clearInterval(this.timer);
this.timer = null;
}
}
InAppPurchaseManager.prototype.callbackMap = {};
InAppPurchaseManager.prototype.callbackIdx = 0;
InAppPurchaseManager.prototype.eventQueue = [];
InAppPurchaseManager.prototype.timer = null;
cordova.addConstructor(function() {
// shim to work in 1.5 and 1.6
if (!window.Cordova) {
window.Cordova = cordova;
};
if(!window.plugins) {
window.plugins = {};
}
window.plugins.inAppPurchaseManager = InAppPurchaseManager.manager = new InAppPurchaseManager();
});