-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSCPStoreKitManager.m
More file actions
executable file
·250 lines (199 loc) · 8.94 KB
/
SCPStoreKitManager.m
File metadata and controls
executable file
·250 lines (199 loc) · 8.94 KB
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
//
// SCPStoreKitManager.m
// SCPStoreKitManager
//
// Created by Ste Prescott on 22/11/2013.
// Copyright (c) 2013 Ste Prescott. All rights reserved.
//
#import "SCPStoreKitManager.h"
@interface SCPStoreKitManager()
@property (nonatomic, strong) NSNumberFormatter *numberFormatter;
@property (nonatomic, copy) ProductsReturnedSuccessfully productsReturnedSuccessfullyBlock;
@property (nonatomic, copy) InvalidProducts invalidProductsBlock;
@property (nonatomic, copy) Failure failureBlock;
@property (nonatomic, copy) PaymentTransactionStatePurchasing paymentTransactionStatePurchasingBlock;
@property (nonatomic, copy) PaymentTransactionStatePurchased paymentTransactionStatePurchasedBlock;
@property (nonatomic, copy) PaymentTransactionStateFailed paymentTransactionStateFailedBlock;
@property (nonatomic, copy) PaymentTransactionStateRestored paymentTransactionStateRestoredBlock;
@property (nonatomic, strong, readwrite) NSArray *products;
@end
@implementation SCPStoreKitManager
+ (id)sharedInstance
{
static SCPStoreKitManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (id)init
{
self = [super init];
if(self)
{
self.numberFormatter = [[NSNumberFormatter alloc] init];
[_numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
}
return self;
}
- (void)requestProductsWithIdentifiers:(NSSet *)productsSet productsReturnedSuccessfully:(ProductsReturnedSuccessfully)productsReturnedSuccessfullyBlock invalidProducts:(InvalidProducts)invalidProductsBlock failure:(Failure)failureBlock
{
self.productsReturnedSuccessfullyBlock = productsReturnedSuccessfullyBlock;
self.invalidProductsBlock = invalidProductsBlock;
self.failureBlock = failureBlock;
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productsSet];
[productsRequest setDelegate:self];
[productsRequest start];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
if(_productsReturnedSuccessfullyBlock)
{
self.products = response.products;
_productsReturnedSuccessfullyBlock(response.products);
}
if([[response invalidProductIdentifiers] count] > 0 && _invalidProductsBlock)
{
_invalidProductsBlock([response invalidProductIdentifiers]);
}
}
- (void)configurePaymentTransactionStatePurchasing:(PaymentTransactionStatePurchasing)paymentTransactionStatePurchasingBlock paymentTransactionStatePurchased:(PaymentTransactionStatePurchased)paymentTransactionStatePurchasedBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock paymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock failure:(Failure)failureBlock {
self.paymentTransactionStatePurchasingBlock = paymentTransactionStatePurchasingBlock;
self.paymentTransactionStatePurchasedBlock = paymentTransactionStatePurchasedBlock;
self.paymentTransactionStateFailedBlock = paymentTransactionStateFailedBlock;
self.paymentTransactionStateRestoredBlock = paymentTransactionStateRestoredBlock;
self.failureBlock = failureBlock;
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
- (void)requestPaymentForProduct:(SKProduct *)product {
SKPayment *payment = [SKPayment paymentWithProduct:product];
if([SKPaymentQueue canMakePayments])
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else
{
if(self.failureBlock)
{
self.failureBlock([NSError errorWithDomain:SCPStoreKitDomain code:SCPErrorCodePaymentQueueCanNotMakePayments errorDescription:@"SKPaymentQueue can not make payments" errorFailureReason:@"Has the SKPaymentQueue got any uncompleted purchases?" errorRecoverySuggestion:@"Finish all transactions"]);
}
}
}
- (void)requestPaymentForProduct:(SKProduct *)product paymentTransactionStatePurchasing:(PaymentTransactionStatePurchasing)paymentTransactionStatePurchasingBlock paymentTransactionStatePurchased:(PaymentTransactionStatePurchased)paymentTransactionStatePurchasedBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock paymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock failure:(Failure)failureBlock
{
self.paymentTransactionStatePurchasingBlock = paymentTransactionStatePurchasingBlock;
self.paymentTransactionStatePurchasedBlock = paymentTransactionStatePurchasedBlock;
self.paymentTransactionStateFailedBlock = paymentTransactionStateFailedBlock;
self.paymentTransactionStateRestoredBlock = paymentTransactionStateRestoredBlock;
self.failureBlock = failureBlock;
SKPayment *payment = [SKPayment paymentWithProduct:product];
if([SKPaymentQueue canMakePayments])
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else
{
if(failureBlock)
{
failureBlock([NSError errorWithDomain:SCPStoreKitDomain code:SCPErrorCodePaymentQueueCanNotMakePayments errorDescription:@"SKPaymentQueue can not make payments" errorFailureReason:@"Has the SKPaymentQueue got any uncompleted purchases?" errorRecoverySuggestion:@"Finish all transactions"]);
}
}
}
- (void)restorePurchasesPaymentTransactionStateRestored:(PaymentTransactionStateRestored)paymentTransactionStateRestoredBlock paymentTransactionStateFailed:(PaymentTransactionStateFailed)paymentTransactionStateFailedBlock failure:(Failure)failureBlock
{
self.paymentTransactionStateFailedBlock = paymentTransactionStateFailedBlock;
self.paymentTransactionStateRestoredBlock = paymentTransactionStateRestoredBlock;
self.failureBlock = failureBlock;
if([SKPaymentQueue canMakePayments])
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
else
{
if(failureBlock)
{
failureBlock([NSError errorWithDomain:SCPStoreKitDomain code:SCPErrorCodePaymentQueueCanNotMakePayments errorDescription:@"SKPaymentQueue can not make payments" errorFailureReason:@"Has the SKPaymentQueue got any uncompleted purchases?" errorRecoverySuggestion:@"Finish all transactions"]);
}
}
}
#pragma mark - SKPaymentTransactionObserver methods
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
if (self.paymentTransactionStateFailedBlock)
self.paymentTransactionStateFailedBlock(error);
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
if (self.paymentTransactionStateRestoredBlock)
self.paymentTransactionStateRestoredBlock(queue.transactions);
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
if([transactions count] > 0)
{
[transactions enumerateObjectsUsingBlock:^(SKPaymentTransaction *transaction, NSUInteger idx, BOOL *stop) {
switch([transaction transactionState])
{
case SKPaymentTransactionStatePurchased:
case SKPaymentTransactionStateFailed:
case SKPaymentTransactionStateRestored:
{
[queue finishTransaction:transaction];
break;
}
default:
{
break;
}
}
}];
}
if(_paymentTransactionStatePurchasingBlock)
{
NSArray *purchasingTransactions = [transactions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"transactionState == %i", SKPaymentTransactionStatePurchasing]];
if([purchasingTransactions count] > 0)
{
_paymentTransactionStatePurchasingBlock(purchasingTransactions);
}
}
if(_paymentTransactionStatePurchasedBlock)
{
NSArray *purchasedTransactions = [transactions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"transactionState == %i", SKPaymentTransactionStatePurchased]];
if([purchasedTransactions count] > 0)
{
_paymentTransactionStatePurchasedBlock(purchasedTransactions);
}
}
if(_paymentTransactionStateFailedBlock)
{
NSArray *failedTransactions = [transactions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"transactionState == %i", SKPaymentTransactionStateFailed]];
if([failedTransactions count] > 0)
{
_paymentTransactionStateFailedBlock(failedTransactions);
}
}
if(_paymentTransactionStateRestoredBlock)
{
NSArray *restoredTransactions = [transactions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"transactionState == %i", SKPaymentTransactionStateRestored]];
if([restoredTransactions count] > 0)
{
_paymentTransactionStateRestoredBlock(restoredTransactions);
}
}
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
if(_failureBlock)
{
_failureBlock(error);
}
}
- (NSString *)localizedPriceForProduct:(SKProduct *)product
{
[_numberFormatter setLocale:product.priceLocale];
NSString *formattedPrice = [_numberFormatter stringFromNumber:product.price];
return formattedPrice;
}
@end