6
6
*/
7
7
8
8
var InAppPurchaseManager = function ( ) {
9
- PhoneGap . exec ( 'InAppPurchaseManager.setup' ) ;
9
+ cordova . exec ( null , function ( ) {
10
+ // It occurs when user can't purchase anything
11
+ } , "InAppPurchaseManager" , "setup" , [ ] ) ;
10
12
}
11
13
12
14
/**
@@ -21,7 +23,9 @@ InAppPurchaseManager.prototype.makePurchase = function(productId, quantity) {
21
23
if ( ! q ) {
22
24
q = 1 ;
23
25
}
24
- return PhoneGap . exec ( 'InAppPurchaseManager.makePurchase' , productId , q ) ;
26
+
27
+ // return
28
+ cordova . exec ( null , null , "InAppPurchaseManager" , "makePurchase" , [ productId , q ] ) ;
25
29
}
26
30
27
31
/**
@@ -31,7 +35,8 @@ InAppPurchaseManager.prototype.makePurchase = function(productId, quantity) {
31
35
*/
32
36
33
37
InAppPurchaseManager . prototype . restoreCompletedTransactions = function ( ) {
34
- return PhoneGap . exec ( 'InAppPurchaseManager.restoreCompletedTransactions' ) ;
38
+ // return
39
+ cordova . exec ( null , null , "InAppPurchaseManager" , "restoreCompletedTransactions" , [ ] ) ;
35
40
}
36
41
37
42
@@ -45,19 +50,7 @@ InAppPurchaseManager.prototype.restoreCompletedTransactions = function() {
45
50
*/
46
51
47
52
InAppPurchaseManager . prototype . requestProductData = function ( productId , successCallback , failCallback ) {
48
- var key = 'f' + this . callbackIdx ++ ;
49
- window . plugins . inAppPurchaseManager . callbackMap [ key ] = {
50
- success : function ( productId , title , description , price ) {
51
- if ( productId == '__DONE' ) {
52
- delete window . plugins . inAppPurchaseManager . callbackMap [ key ]
53
- return ;
54
- }
55
- successCallback ( productId , title , description , price ) ;
56
- } ,
57
- fail : failCallback
58
- }
59
- var callback = 'window.plugins.inAppPurchaseManager.callbackMap.' + key ;
60
- PhoneGap . exec ( 'InAppPurchaseManager.requestProductData' , productId , callback + '.success' , callback + '.fail' ) ;
53
+ cordova . exec ( successCallback , failCallback , "InAppPurchaseManager" , "requestProductData" , [ productId ] ) ;
61
54
}
62
55
63
56
/**
@@ -85,13 +78,7 @@ InAppPurchaseManager.prototype.requestProductData = function(productId, successC
85
78
* strings which were rejected by the app store.
86
79
*/
87
80
InAppPurchaseManager . prototype . requestProductsData = function ( productIds , callback ) {
88
- var key = 'b' + this . callbackIdx ++ ;
89
- window . plugins . inAppPurchaseManager . callbackMap [ key ] = function ( validProducts , invalidProductIds ) {
90
- delete window . plugins . inAppPurchaseManager . callbackMap [ key ] ;
91
- callback ( validProducts , invalidProductIds ) ;
92
- } ;
93
- var callbackName = 'window.plugins.inAppPurchaseManager.callbackMap.' + key ;
94
- PhoneGap . exec ( 'InAppPurchaseManager.requestProductsData' , callbackName , { productIds : productIds } ) ;
81
+ cordova . exec ( callback , null , "InAppPurchaseManager" , "requestProductsData" , [ productIds ] ) ;
95
82
} ;
96
83
97
84
/* function(transactionIdentifier, productId, transactionReceipt) */
@@ -104,15 +91,16 @@ InAppPurchaseManager.prototype.onRestored = null;
104
91
InAppPurchaseManager . prototype . onFailed = null ;
105
92
106
93
/* function() */
107
- InAppPurchaseManager . prototype . onRestoreCompletedTransactionsFinished = null ;
94
+ InAppPurchaseManager . prototype . onRestoreCompletedTransactionsFinished = function ( ) {
95
+ console . log ( "restored transaction" ) ;
96
+ } ;
108
97
109
98
/* function(errorCode) */
110
99
InAppPurchaseManager . prototype . onRestoreCompletedTransactionsFailed = null ;
111
100
112
101
/* This is called from native.*/
113
102
114
103
InAppPurchaseManager . prototype . updatedTransactionCallback = function ( state , errorCode , errorText , transactionIdentifier , productId , transactionReceipt ) {
115
- alert ( state ) ;
116
104
switch ( state ) {
117
105
case "PaymentTransactionStatePurchased" :
118
106
if ( window . plugins . inAppPurchaseManager . onPurchased )
@@ -156,6 +144,7 @@ InAppPurchaseManager.prototype.runQueue = function() {
156
144
if ( ! this . eventQueue . length || ( ! this . onPurchased && ! this . onFailed && ! this . onRestored ) ) {
157
145
return ;
158
146
}
147
+
159
148
var args ;
160
149
/* We can't work directly on the queue, because we're pushing new elements onto it */
161
150
var queue = this . eventQueue . slice ( ) ;
@@ -182,15 +171,39 @@ InAppPurchaseManager.prototype.unWatchQueue = function() {
182
171
}
183
172
}
184
173
185
-
186
174
InAppPurchaseManager . prototype . callbackMap = { } ;
187
175
InAppPurchaseManager . prototype . callbackIdx = 0 ;
188
176
InAppPurchaseManager . prototype . eventQueue = [ ] ;
189
177
InAppPurchaseManager . prototype . timer = null ;
190
178
191
- PhoneGap . addConstructor ( function ( ) {
192
- if ( ! window . plugins ) {
193
- window . plugins = { } ;
194
- }
195
- window . plugins . inAppPurchaseManager = InAppPurchaseManager . manager = new InAppPurchaseManager ( ) ;
196
- } ) ;
179
+ document . addEventListener ( "deviceready" , function ( ) {
180
+ window . plugins = window . plugins || { } ;
181
+ window . plugins . inAppPurchaseManager = InAppPurchaseManager . manager = new InAppPurchaseManager ( ) ;
182
+
183
+ window . plugins . inAppPurchaseManager . onPurchased = function ( transactionIdentifier , productId , transactionReceipt ) {
184
+ console . log ( 'purchased: ' + productId ) ;
185
+
186
+ // If failed a receipt validation on server, you can restore transaction this payment.
187
+ // window.plugins.inAppPurchaseManager.restoreCompletedTransactions();
188
+ }
189
+
190
+ // Perhaps It did rollback a transaction
191
+ window . plugins . inAppPurchaseManager . onRestored = function ( transactionIdentifier , productId , transactionReceipt ) {
192
+ console . log ( 'restored: ' + productId ) ;
193
+ /* See the developer guide for details of what to do with this */
194
+ }
195
+
196
+ // Failed to purchase an item
197
+ window . plugins . inAppPurchaseManager . onFailed = function ( errno , errtext ) {
198
+ alert ( errtext ) ;
199
+ }
200
+
201
+ // requestProductData -> { id, title, description, price }
202
+ // requestProductsData -> [[{ id, title, description, price }, ... ], [invaildID]]
203
+ // plugins.inAppPurchaseManager.requestProductData("1", function(result) {
204
+ // window.plugins.inAppPurchaseManager.makePurchase(result.id, 1 /* quantity */);
205
+ // }, function(id) {
206
+ // console.log("[In JS] Invalid product id: " + id);
207
+ // }
208
+ // );
209
+ } ) ;
0 commit comments