Skip to content

Commit ae1d2d3

Browse files
committed
Merge remote-tracking branch 'davidshimjs/master'
2 parents 82b512f + a25a823 commit ae1d2d3

File tree

4 files changed

+152
-129
lines changed

4 files changed

+152
-129
lines changed

iPhone/InAppPurchaseManager/InAppPurchaseManager.h

+24-34
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,42 @@
99
#import <Foundation/Foundation.h>
1010
#import <StoreKit/StoreKit.h>
1111

12-
#ifdef PHONEGAP_FRAMEWORK
13-
#import <PhoneGap/PGPlugin.h>
14-
#else
15-
#import "PGPlugin.h"
16-
#endif
17-
18-
#ifdef PHONEGAP_FRAMEWORK
19-
#import <PhoneGap/NSData+Base64.h>
20-
#else
21-
#import "NSData+Base64.h"
22-
#endif
12+
#import <Cordova/CDVPlugin.h>
13+
#import <Cordova/NSData+Base64.h>
2314

2415
#import "SKProduct+LocalizedPrice.h"
2516

26-
@interface InAppPurchaseManager : PGPlugin <SKPaymentTransactionObserver> {
27-
17+
@interface InAppPurchaseManager : CDVPlugin <SKPaymentTransactionObserver> {
18+
NSMutableDictionary* list;
2819
}
29-
- (void) setup:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
30-
- (void) makePurchase:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
31-
- (void) requestProductData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
32-
- (void) requestProductsData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
20+
@property (nonatomic, retain) NSMutableDictionary* list;
21+
- (void) setup:(CDVInvokedUrlCommand*)command;
22+
- (void) makePurchase:(CDVInvokedUrlCommand*)command;
23+
- (void) requestProductData:(CDVInvokedUrlCommand*)command;
24+
- (void) requestProductsData:(CDVInvokedUrlCommand*)command;
25+
- (void) restoreCompletedTransactions:(CDVInvokedUrlCommand*)command;
26+
//- (void) setup:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
27+
//- (void) makePurchase:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
28+
//- (void) requestProductData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
29+
//- (void) requestProductsData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
3330
- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
3431
- (void) paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error;
3532
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue;
36-
3733
@end
3834

39-
@interface ProductsRequestDelegate : NSObject <SKProductsRequestDelegate>{
40-
NSString* successCallback;
41-
NSString* failCallback;
42-
43-
InAppPurchaseManager* command;
35+
@interface ProductsRequestDelegate : NSObject <SKProductsRequestDelegate>
36+
{
37+
InAppPurchaseManager* plugin;
38+
CDVInvokedUrlCommand* command;
4439
}
45-
46-
@property (nonatomic, copy) NSString* successCallback;
47-
@property (nonatomic, copy) NSString* failCallback;
48-
@property (nonatomic, retain) InAppPurchaseManager* command;
49-
40+
@property (nonatomic, retain) InAppPurchaseManager* plugin;
41+
@property (nonatomic, retain) CDVInvokedUrlCommand* command;
5042
@end;
5143

5244
@interface BatchProductsRequestDelegate : NSObject <SKProductsRequestDelegate> {
53-
NSString* callback;
54-
InAppPurchaseManager* command;
45+
InAppPurchaseManager* plugin;
46+
CDVInvokedUrlCommand* command;
5547
}
56-
57-
@property (nonatomic, copy) NSString* callback;
58-
@property (nonatomic, retain) InAppPurchaseManager* command;
59-
48+
@property (nonatomic, retain) InAppPurchaseManager* plugin;
49+
@property (nonatomic, retain) CDVInvokedUrlCommand* command;
6050
@end;

iPhone/InAppPurchaseManager/InAppPurchaseManager.js

+45-32
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*/
77

88
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", []);
1012
}
1113

1214
/**
@@ -21,7 +23,9 @@ InAppPurchaseManager.prototype.makePurchase = function(productId, quantity) {
2123
if(!q) {
2224
q = 1;
2325
}
24-
return PhoneGap.exec('InAppPurchaseManager.makePurchase', productId, q);
26+
27+
// return
28+
cordova.exec(null, null, "InAppPurchaseManager", "makePurchase", [productId, q]);
2529
}
2630

2731
/**
@@ -31,7 +35,8 @@ InAppPurchaseManager.prototype.makePurchase = function(productId, quantity) {
3135
*/
3236

3337
InAppPurchaseManager.prototype.restoreCompletedTransactions = function() {
34-
return PhoneGap.exec('InAppPurchaseManager.restoreCompletedTransactions');
38+
// return
39+
cordova.exec(null, null, "InAppPurchaseManager", "restoreCompletedTransactions", []);
3540
}
3641

3742

@@ -45,19 +50,7 @@ InAppPurchaseManager.prototype.restoreCompletedTransactions = function() {
4550
*/
4651

4752
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]);
6154
}
6255

6356
/**
@@ -85,13 +78,7 @@ InAppPurchaseManager.prototype.requestProductData = function(productId, successC
8578
* strings which were rejected by the app store.
8679
*/
8780
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]);
9582
};
9683

9784
/* function(transactionIdentifier, productId, transactionReceipt) */
@@ -104,15 +91,16 @@ InAppPurchaseManager.prototype.onRestored = null;
10491
InAppPurchaseManager.prototype.onFailed = null;
10592

10693
/* function() */
107-
InAppPurchaseManager.prototype.onRestoreCompletedTransactionsFinished = null;
94+
InAppPurchaseManager.prototype.onRestoreCompletedTransactionsFinished = function () {
95+
console.log("restored transaction");
96+
};
10897

10998
/* function(errorCode) */
11099
InAppPurchaseManager.prototype.onRestoreCompletedTransactionsFailed = null;
111100

112101
/* This is called from native.*/
113102

114103
InAppPurchaseManager.prototype.updatedTransactionCallback = function(state, errorCode, errorText, transactionIdentifier, productId, transactionReceipt) {
115-
alert(state);
116104
switch(state) {
117105
case "PaymentTransactionStatePurchased":
118106
if(window.plugins.inAppPurchaseManager.onPurchased)
@@ -156,6 +144,7 @@ InAppPurchaseManager.prototype.runQueue = function() {
156144
if(!this.eventQueue.length || (!this.onPurchased && !this.onFailed && !this.onRestored)) {
157145
return;
158146
}
147+
159148
var args;
160149
/* We can't work directly on the queue, because we're pushing new elements onto it */
161150
var queue = this.eventQueue.slice();
@@ -182,15 +171,39 @@ InAppPurchaseManager.prototype.unWatchQueue = function() {
182171
}
183172
}
184173

185-
186174
InAppPurchaseManager.prototype.callbackMap = {};
187175
InAppPurchaseManager.prototype.callbackIdx = 0;
188176
InAppPurchaseManager.prototype.eventQueue = [];
189177
InAppPurchaseManager.prototype.timer = null;
190178

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

Comments
 (0)