-
Notifications
You must be signed in to change notification settings - Fork 7
/
MGStoreKit.m
169 lines (148 loc) · 5.22 KB
/
MGStoreKit.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
//
// MGStoreKit.m
//
// Created by Matt Greenfield on 7/08/11.
// Copyright Big Paua 2012. All rights reserved.
//
#import "MGStoreKit.h"
@interface MGStoreKit ()
@property (nonatomic, retain) SKProductsRequest *productsRequest;
@property (nonatomic, copy) ProductsCallback productsCallback;
@property (nonatomic, copy) PurchaseCallback purchaseCallback;
@property (nonatomic, copy) ProductsCallback restoreCallback;
@property (nonatomic, copy) PurchaseCallback failedCallback;
@end
@implementation MGStoreKit
static MGStoreKit *singleton;
@synthesize cachedProducts, productsRequest;
@synthesize productsCallback, purchaseCallback, failedCallback, restoreCallback;
+ (void)initialize {
singleton = [[MGStoreKit alloc] init];
}
- (id)init {
self = [super init];
self.cachedProducts = [NSMutableDictionary dictionary];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
return self;
}
+ (MGStoreKit *)store {
return singleton;
}
- (void)requestProductsData:(NSSet *)keys
callback:(ProductsCallback)callback {
BOOL haveAllCached = YES;
NSMutableArray *have = [NSMutableArray arrayWithCapacity:[keys count]];
for (NSString *key in keys) {
SKProduct *product = [self.cachedProducts objectForKey:key];
if (![self.cachedProducts objectForKey:key]) {
haveAllCached = NO;
break;
}
[have addObject:product];
}
if (haveAllCached && callback) {
callback(have);
return;
}
self.productsCallback = callback;
self.productsRequest =
[[SKProductsRequest alloc] initWithProductIdentifiers:keys];
self.productsRequest.delegate = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.productsRequest start];
});
}
- (BOOL)canMakePayments {
return [SKPaymentQueue canMakePayments];
}
- (void)purchaseProduct:(NSString *)productId
success:(PurchaseCallback)callback
failed:(PurchaseCallback)failed {
self.purchaseCallback = callback;
self.failedCallback = failed;
SKProduct *product = [self.cachedProducts objectForKey:productId];
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request
didReceiveResponse:(SKProductsResponse *)response {
if ([response.invalidProductIdentifiers count]) {
for (NSString *invalidProductId in response.invalidProductIdentifiers) {
NSLog(@"invalid product key:%@", invalidProductId);
}
if (self.productsCallback) {
dispatch_async(dispatch_get_main_queue(), ^{
self.productsCallback(nil);
});
}
return;
}
if (![response.products count]) {
NSLog(@"no products in response");
if (self.productsCallback) {
dispatch_async(dispatch_get_main_queue(), ^{
self.productsCallback(nil);
});
}
return;
}
for (SKProduct *product in response.products) {
[self.cachedProducts
setObject:product forKey:product.productIdentifier];
}
if (self.productsCallback) {
dispatch_async(dispatch_get_main_queue(), ^{
self.productsCallback(response.products);
});
}
}
- (void)restore:(ProductsCallback)callback {
self.restoreCallback = callback;
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)
paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
NSMutableArray *restored = [NSMutableArray array];
for (SKPaymentTransaction *transaction in queue.transactions) {
if (transaction.transactionState == SKPaymentTransactionStateRestored) {
[restored
addObject:transaction.originalTransaction.payment.productIdentifier];
}
}
self.restoreCallback(restored);
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
NSLog(@"%@", error);
}
#pragma mark - SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue
updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
if (self.purchaseCallback) {
self.purchaseCallback(transaction.payment.productIdentifier);
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
if (self.purchaseCallback) {
self.purchaseCallback(
transaction.originalTransaction.payment.productIdentifier);
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(@"%@", transaction.error);
if (self.failedCallback) {
self.failedCallback(transaction.payment.productIdentifier);
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
default:
break;
}
}
}
@end