forked from flutter-team-archive/plugins
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInAppPurchasePlugin.m
More file actions
348 lines (313 loc) · 14.8 KB
/
Copy pathInAppPurchasePlugin.m
File metadata and controls
348 lines (313 loc) · 14.8 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
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "InAppPurchasePlugin.h"
#import <StoreKit/StoreKit.h>
#import "FIAObjectTranslator.h"
#import "FIAPReceiptManager.h"
#import "FIAPRequestHandler.h"
#import "FIAPaymentQueueHandler.h"
@interface InAppPurchasePlugin ()
// Holding strong references to FIAPRequestHandlers. Remove the handlers from the set after
// the request is finished.
@property(strong, nonatomic) NSMutableSet *requestHandlers;
// After querying the product, the available products will be saved in the map to be used
// for purchase.
@property(copy, nonatomic) NSMutableDictionary *productsCache;
// Call back channel to dart used for when a listener function is triggered.
@property(strong, nonatomic) FlutterMethodChannel *callbackChannel;
@property(strong, nonatomic) NSObject<FlutterTextureRegistry> *registry;
@property(strong, nonatomic) NSObject<FlutterBinaryMessenger> *messenger;
@property(strong, nonatomic) NSObject<FlutterPluginRegistrar> *registrar;
@property(strong, nonatomic) FIAPReceiptManager *receiptManager;
@end
@implementation InAppPurchasePlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel *channel =
[FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/in_app_purchase"
binaryMessenger:[registrar messenger]];
InAppPurchasePlugin *instance = [[InAppPurchasePlugin alloc] initWithRegistrar:registrar];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (instancetype)initWithReceiptManager:(FIAPReceiptManager *)receiptManager {
self = [self init];
self.receiptManager = receiptManager;
return self;
}
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
self = [self initWithReceiptManager:[FIAPReceiptManager new]];
self.registrar = registrar;
self.registry = [registrar textures];
self.messenger = [registrar messenger];
__weak typeof(self) weakSelf = self;
self.paymentQueueHandler =
[[FIAPaymentQueueHandler alloc] initWithQueue:[SKPaymentQueue defaultQueue]
transactionsUpdated:^(NSArray<SKPaymentTransaction *> *_Nonnull transactions) {
[weakSelf handleTransactionsUpdated:transactions];
}
transactionRemoved:^(NSArray<SKPaymentTransaction *> *_Nonnull transactions) {
[weakSelf handleTransactionsRemoved:transactions];
}
restoreTransactionFailed:^(NSError *_Nonnull error) {
[weakSelf handleTransactionRestoreFailed:error];
}
restoreCompletedTransactionsFinished:^{
[weakSelf restoreCompletedTransactionsFinished];
}
shouldAddStorePayment:^BOOL(SKPayment *payment, SKProduct *product) {
return [weakSelf shouldAddStorePayment:payment product:product];
}
updatedDownloads:^void(NSArray<SKDownload *> *_Nonnull downloads) {
[weakSelf updatedDownloads:downloads];
}];
self.callbackChannel =
[FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/in_app_purchase_callback"
binaryMessenger:[registrar messenger]];
return self;
}
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([@"-[SKPaymentQueue canMakePayments:]" isEqualToString:call.method]) {
[self canMakePayments:result];
} else if ([@"-[InAppPurchasePlugin startProductRequest:result:]" isEqualToString:call.method]) {
[self handleProductRequestMethodCall:call result:result];
} else if ([@"-[InAppPurchasePlugin addPayment:result:]" isEqualToString:call.method]) {
[self addPayment:call result:result];
} else if ([@"-[InAppPurchasePlugin finishTransaction:result:]" isEqualToString:call.method]) {
[self finishTransaction:call result:result];
} else if ([@"-[InAppPurchasePlugin restoreTransactions:result:]" isEqualToString:call.method]) {
[self restoreTransactions:call result:result];
} else if ([@"-[InAppPurchasePlugin retrieveReceiptData:result:]" isEqualToString:call.method]) {
[self retrieveReceiptData:call result:result];
} else if ([@"-[InAppPurchasePlugin refreshReceipt:result:]" isEqualToString:call.method]) {
[self refreshReceipt:call result:result];
} else {
result(FlutterMethodNotImplemented);
}
}
- (void)canMakePayments:(FlutterResult)result {
result([NSNumber numberWithBool:[SKPaymentQueue canMakePayments]]);
}
- (void)handleProductRequestMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if (![call.arguments isKindOfClass:[NSArray class]]) {
result([FlutterError errorWithCode:@"storekit_invalid_argument"
message:@"Argument type of startRequest is not array"
details:call.arguments]);
return;
}
NSArray *productIdentifiers = (NSArray *)call.arguments;
SKProductsRequest *request =
[self getProductRequestWithIdentifiers:[NSSet setWithArray:productIdentifiers]];
FIAPRequestHandler *handler = [[FIAPRequestHandler alloc] initWithRequest:request];
[self.requestHandlers addObject:handler];
__weak typeof(self) weakSelf = self;
[handler startProductRequestWithCompletionHandler:^(SKProductsResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
NSString *details = [NSString stringWithFormat:@"Reason:%@\nRecoverSuggestion:%@",
error.localizedFailureReason,
error.localizedRecoverySuggestion];
result([FlutterError errorWithCode:@"storekit_getproductrequest_platform_error"
message:error.description
details:details]);
return;
}
if (!response) {
result([FlutterError errorWithCode:@"storekit_platform_no_response"
message:@"Failed to get SKProductResponse in startRequest "
@"call. Error occured on iOS platform"
details:call.arguments]);
return;
}
for (SKProduct *product in response.products) {
[self.productsCache setObject:product forKey:product.productIdentifier];
}
result([FIAObjectTranslator getMapFromSKProductsResponse:response]);
[weakSelf.requestHandlers removeObject:handler];
}];
}
- (void)addPayment:(FlutterMethodCall *)call result:(FlutterResult)result {
if (![call.arguments isKindOfClass:[NSDictionary class]]) {
result([FlutterError errorWithCode:@"storekit_invalid_argument"
message:@"Argument type of addPayment is not a Dictionary"
details:call.arguments]);
return;
}
NSDictionary *paymentMap = (NSDictionary *)call.arguments;
NSString *productID = [paymentMap objectForKey:@"productIdentifier"];
// When a product is already fetched, we create a payment object with
// the product to process the payment.
SKProduct *product = [self getProduct:productID];
if (product) {
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
payment.applicationUsername = [paymentMap objectForKey:@"applicationUsername"];
NSNumber *quantity = [paymentMap objectForKey:@"quantity"];
payment.quantity = quantity ? quantity.integerValue : 1;
if (@available(iOS 8.3, *)) {
payment.simulatesAskToBuyInSandbox =
[[paymentMap objectForKey:@"simulatesAskToBuyInSandBox"] boolValue];
}
[self.paymentQueueHandler addPayment:payment];
result(nil);
return;
}
result([FlutterError
errorWithCode:@"storekit_invalid_payment_object"
message:@"You have requested a payment for an invalid product. Either the "
@"`productIdentifier` of the payment is not valid or the product has not been "
@"fetched before adding the payment to the payment queue."
details:call.arguments]);
}
- (void)finishTransaction:(FlutterMethodCall *)call result:(FlutterResult)result {
if (![call.arguments isKindOfClass:[NSString class]]) {
result([FlutterError errorWithCode:@"storekit_invalid_argument"
message:@"Argument type of finishTransaction is not a string."
details:call.arguments]);
return;
}
NSString *identifier = call.arguments;
SKPaymentTransaction *transaction =
[self.paymentQueueHandler.transactions objectForKey:identifier];
if (!transaction) {
result([FlutterError
errorWithCode:@"storekit_platform_invalid_transaction"
message:[NSString
stringWithFormat:@"The transaction with transactionIdentifer:%@ does not "
@"exist. Note that if the transactionState is "
@"purchasing, the transactionIdentifier will be "
@"nil(null).",
transaction.transactionIdentifier]
details:call.arguments]);
return;
}
@try {
// finish transaction will throw exception if the transaction type is purchasing. Notify dart
// about this exception.
[self.paymentQueueHandler finishTransaction:transaction];
} @catch (NSException *e) {
result([FlutterError errorWithCode:@"storekit_finish_transaction_exception"
message:e.name
details:e.description]);
return;
}
result(nil);
}
- (void)restoreTransactions:(FlutterMethodCall *)call result:(FlutterResult)result {
if (call.arguments && ![call.arguments isKindOfClass:[NSString class]]) {
result([FlutterError
errorWithCode:@"storekit_invalid_argument"
message:@"Argument is not nil and the type of finishTransaction is not a string."
details:call.arguments]);
return;
}
[self.paymentQueueHandler restoreTransactions:call.arguments];
}
- (void)retrieveReceiptData:(FlutterMethodCall *)call result:(FlutterResult)result {
FlutterError *error = nil;
NSString *receiptData = [self.receiptManager retrieveReceiptWithError:&error];
if (error) {
result(error);
return;
}
result(receiptData);
}
- (void)refreshReceipt:(FlutterMethodCall *)call result:(FlutterResult)result {
NSDictionary *arguments = call.arguments;
SKReceiptRefreshRequest *request;
if (arguments) {
if (![arguments isKindOfClass:[NSDictionary class]]) {
result([FlutterError errorWithCode:@"storekit_invalid_argument"
message:@"Argument type of startRequest is not array"
details:call.arguments]);
return;
}
NSMutableDictionary *properties = [NSMutableDictionary new];
properties[SKReceiptPropertyIsExpired] = arguments[@"isExpired"];
properties[SKReceiptPropertyIsRevoked] = arguments[@"isRevoked"];
properties[SKReceiptPropertyIsVolumePurchase] = arguments[@"isVolumePurchase"];
request = [self getRefreshReceiptRequest:properties];
} else {
request = [self getRefreshReceiptRequest:nil];
}
FIAPRequestHandler *handler = [[FIAPRequestHandler alloc] initWithRequest:request];
[self.requestHandlers addObject:handler];
__weak typeof(self) weakSelf = self;
[handler startProductRequestWithCompletionHandler:^(SKProductsResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
result([FlutterError errorWithCode:@"storekit_refreshreceiptrequest_platform_error"
message:error.description
details:error.userInfo]);
return;
}
result(nil);
[weakSelf.requestHandlers removeObject:handler];
}];
}
#pragma mark - delegates
- (void)handleTransactionsUpdated:(NSArray<SKPaymentTransaction *> *)transactions {
NSMutableArray *maps = [NSMutableArray new];
for (SKPaymentTransaction *transaction in transactions) {
[maps addObject:[FIAObjectTranslator getMapFromSKPaymentTransaction:transaction]];
}
[self.callbackChannel invokeMethod:@"updatedTransactions" arguments:maps];
}
- (void)handleTransactionsRemoved:(NSArray<SKPaymentTransaction *> *)transactions {
NSMutableArray *maps = [NSMutableArray new];
for (SKPaymentTransaction *transaction in transactions) {
[maps addObject:[FIAObjectTranslator getMapFromSKPaymentTransaction:transaction]];
}
[self.callbackChannel invokeMethod:@"removedTransactions" arguments:maps];
}
- (void)handleTransactionRestoreFailed:(NSError *)error {
FlutterError *fltError = [FlutterError errorWithCode:error.domain
message:error.description
details:error.description];
[self.callbackChannel invokeMethod:@"restoreCompletedTransactions" arguments:fltError];
}
- (void)restoreCompletedTransactionsFinished {
[self.callbackChannel invokeMethod:@"paymentQueueRestoreCompletedTransactionsFinished"
arguments:nil];
}
- (void)updatedDownloads:(NSArray<SKDownload *> *)downloads {
NSMutableArray *maps = [NSMutableArray new];
for (SKDownload *download in downloads) {
[maps addObject:[FIAObjectTranslator getMapFromSKDownload:download]];
}
[self.callbackChannel invokeMethod:@"updatedDownloads" arguments:maps];
}
- (BOOL)shouldAddStorePayment:(SKPayment *)payment product:(SKProduct *)product {
// We always return NO here. And we send the message to dart to process the payment; and we will
// have a interception method that deciding if the payment should be processed (implemented by the
// programmer).
[self.productsCache setObject:product forKey:product.productIdentifier];
[self.callbackChannel invokeMethod:@"shouldAddStorePayment"
arguments:@{
@"payment" : [FIAObjectTranslator getMapFromSKPayment:payment],
@"product" : [FIAObjectTranslator getMapFromSKProduct:product]
}];
return NO;
}
#pragma mark - dependency injection (for unit testing)
- (SKProductsRequest *)getProductRequestWithIdentifiers:(NSSet *)identifiers {
return [[SKProductsRequest alloc] initWithProductIdentifiers:identifiers];
}
- (SKProduct *)getProduct:(NSString *)productID {
return [self.productsCache objectForKey:productID];
}
- (SKReceiptRefreshRequest *)getRefreshReceiptRequest:(NSDictionary *)properties {
return [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:properties];
}
#pragma mark - getter
- (NSSet *)requestHandlers {
if (!_requestHandlers) {
_requestHandlers = [NSMutableSet new];
}
return _requestHandlers;
}
- (NSMutableDictionary *)productsCache {
if (!_productsCache) {
_productsCache = [NSMutableDictionary new];
}
return _productsCache;
}
@end