-
Notifications
You must be signed in to change notification settings - Fork 7
/
InAppPurchaseManager.m
278 lines (223 loc) · 9.38 KB
/
InAppPurchaseManager.m
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// InAppPurchaseManager.m
// beetight
//
// Created by Matt Kane on 20/02/2011.
// Copyright 2011 Matt Kane. All rights reserved.
//
#import "InAppPurchaseManager.h"
// Help create NSNull objects for nil items (since neither NSArray nor NSDictionary can store nil values).
#define NILABLE(obj) ((obj) != nil ? (NSObject *)(obj) : (NSObject *)[NSNull null])
// To avoid compilation warning, declare JSONKit and SBJson's
// category methods without including their header files.
@interface NSArray (StubsForSerializers)
- (NSString *)JSONString;
- (NSString *)JSONRepresentation;
@end
// Helper category method to choose which JSON serializer to use.
@interface NSArray (JSONSerialize)
- (NSString *)JSONSerialize;
@end
@implementation NSArray (JSONSerialize)
- (NSString *)JSONSerialize {
return [self respondsToSelector:@selector(JSONString)] ? [self JSONString] : [self JSONRepresentation];
}
@end
@implementation InAppPurchaseManager
-(void) setup:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
- (void) requestProductData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
if([arguments count] < 3) {
return;
}
NSLog(@"Getting product data");
NSSet *productIdentifiers = [NSSet setWithObject:[arguments objectAtIndex:0]];
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
ProductsRequestDelegate* delegate = [[[ProductsRequestDelegate alloc] init] retain];
delegate.command = self;
delegate.successCallback = [arguments objectAtIndex:1];
delegate.failCallback = [arguments objectAtIndex:2];
productsRequest.delegate = delegate;
[productsRequest start];
}
/**
* Request product data for the productIds given in the option with
* key "productIds". See js for further documentation.
*/
- (void) requestProductsData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
if([arguments count] < 1) {
return;
}
NSSet *productIdentifiers = [NSSet setWithArray:[options objectForKey:@"productIds"]];
NSLog(@"Getting products data");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
BatchProductsRequestDelegate* delegate = [[[BatchProductsRequestDelegate alloc] init] retain];
delegate.command = self;
delegate.callback = [arguments objectAtIndex:0];
productsRequest.delegate = delegate;
[productsRequest start];
}
- (void) makePurchase:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSLog(@"About to do IAP");
if([arguments count] < 1) {
return;
}
if ([SKPaymentQueue canMakePayments]){
SKMutablePayment *payment = [SKMutablePayment paymentWithProductIdentifier:[arguments objectAtIndex:0]];
if([arguments count] > 1) {
id quantity = [arguments objectAtIndex:1];
if ([quantity respondsToSelector:@selector(integerValue)]) {
payment.quantity = [quantity integerValue];
}
}
[[SKPaymentQueue defaultQueue] addPayment:payment];
}else{
NSArray *callbackArgs = [NSArray arrayWithObjects:
@"PaymentTransactionStateFailed",
[NSNumber numberWithInt:-99999],
@"Payments are disabled.",
nil,
nil,
nil,
nil];
NSString *js = [NSString stringWithFormat:@"plugins.inAppPurchaseManager.updatedTransactionCallback.apply(plugins.inAppPurchaseManager, %@)", [callbackArgs JSONSerialize]];
[self writeJavascript: js];
}
}
- (void) restoreCompletedTransactions:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
// SKPaymentTransactionObserver methods
// called when the transaction status is updated
//
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSString *state, *error, *transactionIdentifier, *transactionReceipt, *productId;
NSInteger errorCode;
for (SKPaymentTransaction *transaction in transactions)
{
error = state = transactionIdentifier = transactionReceipt = productId = @"";
errorCode = 0;
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing:
continue;
case SKPaymentTransactionStatePurchased:
state = @"PaymentTransactionStatePurchased";
transactionIdentifier = transaction.transactionIdentifier;
transactionReceipt = [[transaction transactionReceipt] base64EncodedString];
productId = transaction.payment.productIdentifier;
break;
case SKPaymentTransactionStateFailed:
state = @"PaymentTransactionStateFailed";
error = transaction.error.localizedDescription;
errorCode = transaction.error.code;
NSLog(@"error %d %@", errorCode, error);
break;
case SKPaymentTransactionStateRestored:
state = @"PaymentTransactionStateRestored";
transactionIdentifier = transaction.originalTransaction.transactionIdentifier;
transactionReceipt = [[transaction transactionReceipt] base64EncodedString];
productId = transaction.originalTransaction.payment.productIdentifier;
break;
default:
NSLog(@"Invalid state");
continue;
}
NSLog(@"state: %@", state);
NSArray *callbackArgs = [NSArray arrayWithObjects:
NILABLE(state),
[NSNumber numberWithInt:errorCode],
NILABLE(error),
NILABLE(transactionIdentifier),
NILABLE(productId),
NILABLE(transactionReceipt),
nil];
NSString *js = [NSString stringWithFormat:@"plugins.inAppPurchaseManager.updatedTransactionCallback.apply(plugins.inAppPurchaseManager, %@)", [callbackArgs JSONSerialize]];
NSLog(@"js: %@", js);
[self writeJavascript: js];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
NSString *js = [NSString stringWithFormat:@"plugins.inAppPurchaseManager.onRestoreCompletedTransactionsFailed(%d)", error.code];
[self writeJavascript: js];
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSString *js = @"plugins.inAppPurchaseManager.onRestoreCompletedTransactionsFinished()";
[self writeJavascript: js];
}
@end
@implementation ProductsRequestDelegate
@synthesize successCallback, failCallback, command;
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"got iap product response");
for (SKProduct *product in response.products) {
NSLog(@"sending js for %@", product.productIdentifier);
NSArray *callbackArgs = [NSArray arrayWithObjects:
NILABLE(product.productIdentifier),
NILABLE(product.localizedTitle),
NILABLE(product.localizedDescription),
NILABLE(product.localizedPrice),
nil];
NSString *js = [NSString stringWithFormat:@"%@.apply(plugins.inAppPurchaseManager, %@)", successCallback, [callbackArgs JSONSerialize]];
NSLog(@"js: %@", js);
[command writeJavascript: js];
}
for (NSString *invalidProductId in response.invalidProductIdentifiers) {
NSLog(@"sending fail (%@) js for %@", failCallback, invalidProductId);
[command writeJavascript: [NSString stringWithFormat:@"%@('%@')", failCallback, invalidProductId]];
}
NSLog(@"done iap");
[command writeJavascript: [NSString stringWithFormat:@"%@('__DONE')", successCallback]];
[request release];
[self release];
}
- (void) dealloc
{
[successCallback release];
[failCallback release];
[command release];
[super dealloc];
}
@end
/**
* Receives product data for multiple productIds and passes arrays of
* js objects containing these data to a single callback method.
*/
@implementation BatchProductsRequestDelegate
@synthesize callback, command;
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSMutableArray *validProducts = [NSMutableArray array];
for (SKProduct *product in response.products) {
[validProducts addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
NILABLE(product.productIdentifier), @"id",
NILABLE(product.localizedTitle), @"title",
NILABLE(product.localizedDescription), @"description",
NILABLE(product.localizedPrice), @"price",
nil]];
}
NSArray *callbackArgs = [NSArray arrayWithObjects:
NILABLE(validProducts),
NILABLE(response.invalidProductIdentifiers),
nil];
NSString *js = [NSString stringWithFormat:@"%@.apply(plugins.inAppPurchaseManager, %@);", callback, [callbackArgs JSONSerialize]];
[command writeJavascript: js];
[request release];
[self release];
}
- (void) dealloc {
[callback release];
[command release];
[super dealloc];
}
@end