/
ILSimSKPaymentQueue.m
278 lines (214 loc) · 7.78 KB
/
ILSimSKPaymentQueue.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
//
// ILSimSKPaymentQueue.m
// SimStoreKit
//
// Created by ∞ on 02/02/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "ILSimStoreKit.h"
#if kILSimAllowSimulatedStoreKit
#import "ILSimTransactionSimulator.h"
#import "ILSimSKPaymentQueue.h"
#import "ILSimSKPaymentTransaction_Private.h"
#import "ILSimSKProductsRequest.h"
#import "ILSimSKProduct_Private.h"
NSString* const kILSimSKErrorDomain = @"net.infinite-labs.SimulatedStoreKit";
@interface ILSimSKPaymentQueue ()
@property(nonatomic, retain) ILSimSKPaymentTransaction* currentTransaction;
- (void) ask;
- (void) succeed;
- (void) fail:(NSError *)e;
@end
@implementation ILSimSKPaymentQueue
@synthesize transactionSimulator;
+ (BOOL) canMakePayments;
{
id o = [[[NSProcessInfo processInfo] environment] objectForKey:kILSimSKCanMakePaymentsEnvironmentVariable];
return !o || [o boolValue];
}
- (id) init
{
self = [super init];
if (self != nil) {
observers = [NSMutableSet new];
transactions = [NSMutableArray new];
transactionSimulator = ILSimDefaultTransactionSimulator();
}
return self;
}
@synthesize currentTransaction;
- (void) dealloc
{
[observers release];
[transactions release];
[currentTransaction release];
[super dealloc];
}
+ (ILSimSKPaymentQueue*) defaultQueue;
{
static id me = nil; if (!me)
me = [self new];
return me;
}
- (void) addPayment:(ILSimSKPayment*) p;
{
NSAssert([ILSimSKPaymentQueue canMakePayments], @"Payments must be available to add payments onto the queue.");
ILSimSKPaymentTransaction* t = [[ILSimSKPaymentTransaction new] autorelease];
t.transactionState = kILSimSKPaymentTransactionStatePurchasing;
t.payment = p;
t.transactionDate = [NSDate date];
[transactions addObject:t];
if ([observers count] > 0)
[self performSelector:@selector(processNextTransaction) withObject:nil afterDelay:2.0];
}
- (void) processNextTransaction;
{
if (self.currentTransaction)
return;
if ([transactions count] == 0)
return;
self.currentTransaction = [transactions objectAtIndex:0];
self.currentTransaction.transactionState = kILSimSKPaymentTransactionStatePurchasing;
for (id <ILSimSKPaymentTransactionObserver> o in observers)
[o paymentQueue:self updatedTransactions:[NSArray arrayWithObject:self.currentTransaction]];
srandomdev();
long l = random();
self.currentTransaction.transactionIdentifier = [NSString stringWithFormat:@"%xl", l];
NSString* action = [[[NSProcessInfo processInfo] environment] objectForKey:@"ILSimSKTransactionResult"];
if ([action isEqual:@"AlwaysSucceed"])
[self succeed];
else if ([action isEqual:@"AlwaysFail"])
[self fail:[NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorPaymentNotAllowed userInfo:nil]];
else
[self ask];
}
- (void) ask;
{
ILSimSKProduct* p = [ILSimSKProductsRequest simulatedProductForIdentifier:self.currentTransaction.payment.productIdentifier];
if (!p)
[self fail:[NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorPaymentInvalid userInfo:nil]];
else {
[self.transactionSimulator simulateTransaction:self.currentTransaction product:p forQueue:self completionHandler:^(NSError* e) {
if (!e)
[self succeed];
else
[self fail:e];
}];
}
}
- (void) succeed;
{
ILSimSKProduct* p = [ILSimSKProductsRequest simulatedProductForIdentifier:self.currentTransaction.payment.productIdentifier];
NSDictionary* r = [NSDictionary dictionaryWithObjectsAndKeys:
p.productIdentifier, @"ProductID",
[NSNumber numberWithInteger:self.currentTransaction.payment.quantity], @"Quantity",
self.currentTransaction.transactionIdentifier, @"TransactionID",
[NSNumber numberWithInteger:p.simulatedProductType], @"ProductType",
self.currentTransaction.transactionDate, @"Date",
nil];
NSString* err = nil;
NSData* d = [NSPropertyListSerialization dataFromPropertyList:r format:NSPropertyListBinaryFormat_v1_0 errorDescription:&err];
if (d)
self.currentTransaction.transactionReceipt = d;
else {
if (err) {
NSLog(@"%@", err);
[err release];
}
[self fail:[NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]];
return;
}
self.currentTransaction.transactionState = kILSimSKPaymentTransactionStatePurchased;
NSMutableArray* a = [[[[NSUserDefaults standardUserDefaults] arrayForKey:@"ILSimSKTransactions"] mutableCopy] autorelease];
if (!a)
a = [NSMutableArray array];
BOOL shouldAdd = YES;
if (p.simulatedProductType == kILSimSimulatedProductTypeNonConsumable) {
for (NSDictionary* receipt in a) {
if ([[receipt objectForKey:@"ProductID"] isEqual:p.productIdentifier]) {
shouldAdd = NO;
break;
}
}
}
if (shouldAdd) {
[a addObject:r];
[[NSUserDefaults standardUserDefaults] setObject:a forKey:@"ILSimSKTransactions"];
}
[self performSelector:@selector(signalFinished) withObject:nil afterDelay:2.0];
}
- (void) signalFinished;
{
for (id <ILSimSKPaymentTransactionObserver> o in observers)
[o paymentQueue:self updatedTransactions:[NSArray arrayWithObject:self.currentTransaction]];
}
- (void) fail:(NSError*) e;
{
self.currentTransaction.error = e;
self.currentTransaction.transactionState = kILSimSKPaymentTransactionStateFailed;
NSArray* t = [NSArray arrayWithObject:self.currentTransaction];
for (id <ILSimSKPaymentTransactionObserver> o in observers)
[o paymentQueue:self updatedTransactions:t];
}
- (void) finishTransaction:(ILSimSKPaymentTransaction*) t;
{
if ([transactions count] > 0 && self.currentTransaction == [transactions objectAtIndex:0])
[transactions removeObjectAtIndex:0];
else
return;
for (id <ILSimSKPaymentTransactionObserver> o in observers) {
if ([o respondsToSelector:@selector(paymentQueue:removedTransactions:)])
[o paymentQueue:self removedTransactions:[NSArray arrayWithObject:self.currentTransaction]];
}
self.currentTransaction = nil;
if ([observers count] > 0)
[self performSelector:@selector(processNextTransaction) withObject:nil afterDelay:2.0];
}
- (void) restoreCompletedTransactions;
{
srandomdev();
NSMutableArray* a = [NSMutableArray array];
for (id d in [[NSUserDefaults standardUserDefaults] arrayForKey:@"ILSimSKTransactions"]) {
if (![d isKindOfClass:[NSDictionary class]])
continue;
id kind = [d objectForKey:@"ProductType"];
if (!kind || ![kind isKindOfClass:[NSNumber class]] || [kind integerValue] != kILSimSimulatedProductTypeNonConsumable)
continue;
ILSimSKPaymentTransaction* t = [[ILSimSKPaymentTransaction new] autorelease];
t.transactionDate = [NSDate date];
t.transactionState = kILSimSKPaymentTransactionStateRestored;
t.transactionIdentifier = [NSString stringWithFormat:@"%lx", random()];
ILSimSKPaymentTransaction* original = [[ILSimSKPaymentTransaction new] autorelease];
original.transactionDate = [d objectForKey:@"TransactionDate"];
original.transactionIdentifier = [d objectForKey:@"TransactionID"];
ILSimSKMutablePayment* p = [ILSimSKMutablePayment paymentWithProductIdentifier:[d objectForKey:@"ProductID"]];
p.quantity = [[d objectForKey:@"Quantity"] integerValue];
original.payment = p;
t.originalTransaction = original;
[a addObject:t];
}
if ([a count] > 0) {
for (id <ILSimSKPaymentTransactionObserver> o in observers)
[o paymentQueue:self updatedTransactions:a];
}
for (id <ILSimSKPaymentTransactionObserver> o in observers) {
if ([o respondsToSelector:@selector(paymentQueueRestoreCompletedTransactionsFinished:)])
[o paymentQueueRestoreCompletedTransactionsFinished:self];
}
}
- (void) addTransactionObserver:(id <ILSimSKPaymentTransactionObserver>) o;
{
[observers addObject:o];
[self processNextTransaction];
}
- (void) removeTransactionObserver:(id <ILSimSKPaymentTransactionObserver>) o;
{
[observers removeObject:o];
// TODO stop processing
}
- (NSArray*) transactions;
{
return transactions;
}
@end
#endif // #if kILSimAllowSimulatedStoreKit