/
DEStoreKitManager.h
226 lines (163 loc) · 9.84 KB
/
DEStoreKitManager.h
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
//
// DEStoreKitManager.h
// DEStoreKitManager
//
// Created by Jeremy Flores on 11/19/12.
//
// Copyright (c) 2012 Dream Engine Interactive, Inc. ( http://dreamengineinteractive.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
typedef void (^DEStoreKitProductsFetchSuccessBlock)(NSArray *products, NSArray *invalidIdentifiers);
typedef void (^DEStoreKitErrorBlock)(NSError *error);
typedef void (^DEStoreKitTransactionBlock)(SKPaymentTransaction *transaction);
typedef void (^DEStoreKitTransactionsRestorationCompletedBlock)(NSArray *restoredTransactions, NSArray *invalidTransactions);
typedef void (^DEStoreKitTransactionsVerifyingBlock)(NSArray *transactions);
@interface SKProduct (DEStoreKitManager)
-(NSString *) localizedPrice;
@end
@protocol DEStoreKitManagerDelegate <NSObject>
@optional
-(void) productsFetched: (NSArray *)products
invalidIdentifiers: (NSArray *)invalidIdentifiers;
-(void) productsFetchFailed: (NSError *)error;
-(void) transactionSucceeded: (SKPaymentTransaction *)transaction;
// If a transaction should be restored but the delegate does not implement transactionRestored:,
// then DEStoreKitManager will call transactionSucceeded: instead. This can be used when a
// restored transaction can be treated identically to a successful transaction.
-(void) transactionRestored: (SKPaymentTransaction *)transaction;
-(void) transactionFailed: (SKPaymentTransaction *)transaction;
// If a transaction was canceled but the delegate does not implement transactionCanceled:,
// then DEStoreKitManager will instead call transactionFailed:.
-(void) transactionCanceled: (SKPaymentTransaction *)transaction;
/*
Provides a delegate an opportunity to verify the receipt of a completed or restored transaction. If
this method is not implemented in the delegate, then the transaction will be completed and either
the delegate's transactionSucceeded: or transactionRestored: will be called. This method must be used
in conjunction with the Store Kit Manager's transaction:didVerify: method.
Note that, if this method is implemented in the delegate, the Store Kit Manager will still call
transactionSucceeded: or transactionRestored: if the receipt was verified. It is therefore recommended
that your delegate keep the 'unlock feature' code within transactionSucceeded:/transactionRestored:
and not bundle it with your custom verification procedures to practice separation of concerns.
If this method is implemented in the delegate, the Store Kit Manager's transaction:didVerify: MUST
be called to complete the transaction once your delegate has determined the validity of the purchase,
otherwise transactionSucceeded:/transactionRestored:/transactionFailed: will not be called and the
transaction will stay in the SKPaymentQueue and never be finalized.
*/
-(void) transactionNeedsVerification:(SKPaymentTransaction *)transaction;
-(void) restorationSucceeded:(NSArray *)restoredTransactions invalidTransactions:(NSArray *)invalidTransactions;
-(void) restorationFailed:(NSError *)error;
/*
Provides a delegate an opportunity to verify the receipts for restored transactions.
As with transactionNeedsVerification:, the delegate is responsible for calling -transaction:didVerify: in
DEStoreKitManager once validity has been determined for each transaction to continue the restoration procedure.
*/
-(void) restorationNeedsVerification:(NSArray *)transactions;
@end
@interface DEStoreKitManager : NSObject
@property (nonatomic, readonly) NSArray *cachedProducts;
+(id)sharedManager;
-(BOOL) canMakePurchases;
// returns nil if product not found
-(SKProduct *)cachedProductWithIdentifier:(NSString *)productIdentifier;
-(void) removeProductsFromCache:(NSArray *)products;
-(void) removeAllProductsFromCache;
// This will cache the products in DEStoreKitManager for later use in memory. If you do not want
// the products to be cached, then use fetchProductsWithIdentifiers:delegate:cacheResult: and pass NO
// for the last parameter.
-(void) fetchProductsWithIdentifiers: (NSArray *)productIdentifiers
delegate: (id<DEStoreKitManagerDelegate>) delegate;
-(void) fetchProductsWithIdentifiers: (NSArray *)productIdentifiers
delegate: (id<DEStoreKitManagerDelegate>) delegate
cacheResult: (BOOL)shouldCache;
/*
Use these if you'd prefer not to use the delegation pattern
*/
-(void) fetchProductsWithIdentifiers: (NSArray *)productIdentifiers
onSuccess: (DEStoreKitProductsFetchSuccessBlock)success
onFailure: (DEStoreKitErrorBlock)failure;
-(void) fetchProductsWithIdentifiers: (NSArray *)productIdentifiers
onSuccess: (DEStoreKitProductsFetchSuccessBlock)success
onFailure: (DEStoreKitErrorBlock)failure
cacheResult: (BOOL)shouldCache;
/*
Attempts to purchase a product with matching productIdentifier.
This method should only be used if the productIdentifier's SKProduct has already been fetched and
cached by the StoreKitManager. If your app does not utilize the StoreKitManager's caching system,
then you should use purchaseProduct:delegate: instead.
Returns YES if an SKProduct was found in the StoreKitManager's cache matching the provided
productIdentifier, meaning that the purchase will be attempted. Otherwise, returns NO, indicating
that there was not a matching SKProduct and therefore no purchase will be attempted.
*/
-(BOOL) purchaseProductWithIdentifier: (NSString *)productIdentifier
delegate: (id<DEStoreKitManagerDelegate>) delegate;
-(void) purchaseProduct: (SKProduct *)product
delegate: (id<DEStoreKitManagerDelegate>) delegate;
/*
Use these if you'd prefer not to use the delegation pattern.
*/
-(BOOL) purchaseProductWithIdentifier: (NSString *)productIdentifier
onSuccess: (DEStoreKitTransactionBlock)success
onRestore: (DEStoreKitTransactionBlock)restore
onFailure: (DEStoreKitTransactionBlock)failure
onCancel: (DEStoreKitTransactionBlock)cancel
onVerify: (DEStoreKitTransactionBlock)verify;
-(void) purchaseProduct: (SKProduct *)product
onSuccess: (DEStoreKitTransactionBlock)success
onRestore: (DEStoreKitTransactionBlock)restore
onFailure: (DEStoreKitTransactionBlock)failure
onCancel: (DEStoreKitTransactionBlock)cancel
onVerify: (DEStoreKitTransactionBlock)verify;
/*
Returns YES if there are not other restoration processes currently occuring, meaning that the
restoration process will begin. Returns NO otherwise, meaning that the current restoration request
will be ignored.
Due to the nature of the SKPaymentTransactionObserver/SKPaymentQueue implementation, DEStoreKitManager
can only ever have one restoration occurring at any time.
If successful, calls restorationSucceeded:invalidTransactions:. Otherwise, calls restorationFailed:.
If the delegate does not implement restorationNeedsVerification:, then restorationSucceeded...
will be called instead if available.
*/
-(BOOL) restorePreviousPurchasesWithDelegate: (id<DEStoreKitManagerDelegate>)delegate;
/*
Returns YES if there are not other restoration processes currently occuring, meaning that the
restoration process will begin. Returns NO otherwise, meaning that the current restoration request
will be ignored.
Due to the nature of the SKPaymentTransactionObserver/SKPaymentQueue implementation, DEStoreKitManager
can only ever have one restoration occurring at any time.
If nil is passed in for the verify block, verification will not occur and the success block
will instead be called (if provided).
The success block receives to sets: restoredTransactions, which is a set of valid (verified) transactions, and
invalidTransactions, which is a set of transactions whose receipts did not pass validation.
*/
-(BOOL) restorePreviousPurchasesOnSuccess: (DEStoreKitTransactionsRestorationCompletedBlock)success
onFailure: (DEStoreKitErrorBlock)failure
onVerify: (DEStoreKitTransactionsVerifyingBlock)verify;
/*
Notifies the Store Kit Manager that a transaction's receipt has either been verified as valid or
rejected by Apple's servers as invalid. This method should only be used in conjunction with
the DEStoreKitManagerDelegate's transactionNeedsVerification: method and called only when the
delegate's custom verification code has determined the validiy of the transaction.
Note that, if the delegate does not implement transactionNeedsVerification:, then this method should
never be called.
*/
-(void) transaction: (SKPaymentTransaction *)transaction
didVerify: (BOOL)isValid;
@end