forked from Simbul/baker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurchasesManager.m
349 lines (280 loc) · 13.1 KB
/
PurchasesManager.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//
// PurchasesManager.m
// Baker
//
// ==========================================================================================
//
// Copyright (c) 2010-2012, Davide Casali, Marco Colombo, Alessandro Morandi
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// Neither the name of the Baker Framework nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written
// permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "PurchasesManager.h"
#import "BakerAPI.h"
#import "NSData+Base64.h"
#import "NSMutableURLRequest+WebServiceClient.h"
#import "Utils.h"
#import "NSURL+Extensions.h"
#ifdef BAKER_NEWSSTAND
@implementation PurchasesManager
@synthesize products;
@synthesize subscribed;
-(id)init {
self = [super init];
if (self) {
self.products = [[NSMutableDictionary alloc] init];
self.subscribed = NO;
_purchases = [[NSMutableDictionary alloc] init];
_numberFormatter = [[NSNumberFormatter alloc] init];
[_numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
_enableProductRequestFailureNotifications = YES;
}
return self;
}
#pragma mark - Singleton
+ (PurchasesManager *)sharedInstance {
static dispatch_once_t once;
static PurchasesManager *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
#pragma mark - Purchased flag
- (BOOL)isMarkedAsPurchased:(NSString *)productID {
return [[NSUserDefaults standardUserDefaults] boolForKey:productID];
}
- (void)markAsPurchased:(NSString *)productID {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productID];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#pragma mark - Prices
- (void)retrievePricesFor:(NSSet *)productIDs {
[self retrievePricesFor:productIDs andEnableFailureNotifications:YES];
}
- (void)retrievePricesFor:(NSSet *)productIDs andEnableFailureNotifications:(BOOL)enable {
if ([productIDs count] > 0) {
_enableProductRequestFailureNotifications = enable;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIDs];
productsRequest.delegate = self;
[productsRequest start];
});
}
}
- (void)retrievePriceFor:(NSString *)productID {
[self retrievePriceFor:productID andEnableFailureNotification:YES];
}
- (void)retrievePriceFor:(NSString *)productID andEnableFailureNotification:(BOOL)enable {
NSSet *productIDs = [NSSet setWithObject:productID];
[self retrievePricesFor:productIDs andEnableFailureNotifications:enable];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSLog(@"############ REQUEST RECEIVED RESPONSE %@", response.products);
for (NSString *productID in response.invalidProductIdentifiers) {
NSLog(@"Invalid product identifier: %@", productID);
}
NSMutableSet *ids = [NSMutableSet setWithCapacity:response.products.count];
for (SKProduct *skProduct in response.products) {
[self.products setObject:skProduct forKey:skProduct.productIdentifier];
[ids addObject:skProduct.productIdentifier];
}
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:ids forKey:@"ids"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_products_retrieved" object:self userInfo:userInfo];
[request release];
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
NSLog(@"App Store request failure: %@", error);
if (_enableProductRequestFailureNotifications) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:error forKey:@"error"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_products_request_failed" object:self userInfo:userInfo];
}
[request release];
}
- (NSString *)priceFor:(NSString *)productID {
SKProduct *product = [products objectForKey:productID];
if (product) {
[_numberFormatter setLocale:product.priceLocale];
return [_numberFormatter stringFromNumber:product.price];
}
return nil;
}
#pragma mark - Purchases
- (BOOL)purchase:(NSString *)productID {
SKProduct *product = [self productFor:productID];
if (product) {
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
return YES;
} else {
NSLog(@"Trying to buy unavailable product %@", productID);
return NO;
}
}
- (BOOL)finishTransaction:(SKPaymentTransaction *)transaction {
if ([self recordTransaction:transaction]) {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
return YES;
} else {
return NO;
}
}
- (BOOL)recordTransaction:(SKPaymentTransaction *)transaction {
[[NSUserDefaults standardUserDefaults] setObject:transaction.transactionIdentifier forKey:@"receipt"];
BakerAPI *api = [BakerAPI sharedInstance];
if ([api canPostPurchaseReceipt]) {
NSString *receipt = [transaction.transactionReceipt base64EncodedString];
NSString *type = [self transactionType:transaction];
return [api postPurchaseReceipt:receipt ofType:type];
}
return YES;
}
- (NSString *)transactionType:(SKPaymentTransaction *)transaction {
NSString *productID = transaction.payment.productIdentifier;
if ([productID isEqualToString:FREE_SUBSCRIPTION_PRODUCT_ID]) {
return @"free-subscription";
} else if ([AUTO_RENEWABLE_SUBSCRIPTION_PRODUCT_IDS containsObject:productID]) {
return @"auto-renewable-subscription";
} else {
return @"issue";
}
}
- (void)retrievePurchasesFor:(NSSet *)productIDs {
BakerAPI *api = [BakerAPI sharedInstance];
if ([api canGetPurchasesJSON]) {
NSData* jsonResponse = [api getPurchasesJSON];
if (jsonResponse) {
NSError* error = nil;
NSDictionary *purchasesResponse = [NSJSONSerialization JSONObjectWithData:jsonResponse
options:0
error:&error];
// TODO: handle error
if (purchasesResponse) {
NSArray *purchasedIssues = [purchasesResponse objectForKey:@"issues"];
self.subscribed = [[purchasesResponse objectForKey:@"subscribed"] boolValue];
[productIDs enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
[_purchases setObject:[NSNumber numberWithBool:[purchasedIssues containsObject:obj]] forKey:obj];
}];
} else {
NSLog(@"ERROR: Could not parse response from purchases API call. Received: %@", jsonResponse);
}
}
}
}
- (BOOL)isPurchased:(NSString *)productID {
id purchased = [_purchases objectForKey:productID];
if (purchased) {
return [purchased boolValue];
} else {
return [self isMarkedAsPurchased:productID];
}
}
#pragma mark - Payment queue
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
NSLog(@"############ UPDATED TRANSACTIONS %@", transactions);
BOOL isRestoring = NO;
for(SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// Nothing to do at the moment
break;
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
isRestoring = YES;
[self restoreTransaction:transaction];
break;
default:
break;
}
}
if (isRestoring) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_multiple_restores" object:self userInfo:nil];
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:transaction forKey:@"transaction"];
NSString *productId = transaction.payment.productIdentifier;
if ([productId isEqualToString:FREE_SUBSCRIPTION_PRODUCT_ID] || [AUTO_RENEWABLE_SUBSCRIPTION_PRODUCT_IDS containsObject:productId]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_subscription_purchased" object:self userInfo:userInfo];
} else if ([self productFor:productId]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_issue_purchased" object:self userInfo:userInfo];
} else {
NSLog(@"ERROR: Completed transaction for %@, which is not a Product ID this app recognises", productId);
}
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:transaction forKey:@"transaction"];
NSString *productId = transaction.payment.productIdentifier;
if ([productId isEqualToString:FREE_SUBSCRIPTION_PRODUCT_ID] || [AUTO_RENEWABLE_SUBSCRIPTION_PRODUCT_IDS containsObject:productId]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_subscription_restored" object:self userInfo:userInfo];
} else if ([self productFor:productId]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_issue_restored" object:self userInfo:userInfo];
} else {
NSLog(@"ERROR: Trying to restore %@, which is not a Product ID this app recognises", productId);
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_restored_issue_not_recognised" object:self userInfo:userInfo];
}
}
-(void)failedTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"Payment transaction failure: %@", transaction.error);
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:transaction forKey:@"transaction"];
NSString *productId = transaction.payment.productIdentifier;
if ([productId isEqualToString:FREE_SUBSCRIPTION_PRODUCT_ID] || [AUTO_RENEWABLE_SUBSCRIPTION_PRODUCT_IDS containsObject:productId]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_subscription_failed" object:self userInfo:userInfo];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_issue_purchase_failed" object:self userInfo:userInfo];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restore {
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_restore_finished" object:self userInfo:nil];
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
NSLog(@"Transaction restore failure: %@", error);
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:error forKey:@"error"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_restore_failed" object:self userInfo:userInfo];
}
#pragma mark - Products
- (SKProduct *)productFor:(NSString *)productID {
return [self.products objectForKey:productID];
}
#pragma mark - Subscriptions
- (BOOL)hasSubscriptions {
return [FREE_SUBSCRIPTION_PRODUCT_ID length] > 0;
}
#pragma mark - Memory management
-(void)dealloc {
[products release];
[_numberFormatter release];
[_purchases release];
[super dealloc];
}
@end
#endif