@@ -40,8 +40,11 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
40
40
FlutterMethodChannel *channel =
41
41
[FlutterMethodChannel methodChannelWithName: @" plugins.flutter.io/in_app_purchase"
42
42
binaryMessenger: [registrar messenger ]];
43
+
43
44
InAppPurchasePlugin *instance = [[InAppPurchasePlugin alloc ] initWithRegistrar: registrar];
44
45
[registrar addMethodCallDelegate: instance channel: channel];
46
+ [registrar addApplicationDelegate: instance];
47
+ SetUpInAppPurchaseAPI (registrar.messenger , instance);
45
48
}
46
49
47
50
- (instancetype )initWithReceiptManager : (FIAPReceiptManager *)receiptManager {
@@ -85,16 +88,8 @@ - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar
85
88
}
86
89
87
90
- (void )handleMethodCall : (FlutterMethodCall *)call result : (FlutterResult)result {
88
- if ([@" -[SKPaymentQueue canMakePayments:]" isEqualToString: call.method]) {
89
- [self canMakePayments: result];
90
- } else if ([@" -[SKPaymentQueue transactions]" isEqualToString: call.method]) {
91
- [self getPendingTransactions: result];
92
- } else if ([@" -[SKPaymentQueue storefront]" isEqualToString: call.method]) {
93
- [self getStorefront: result];
94
- } else if ([@" -[InAppPurchasePlugin startProductRequest:result:]" isEqualToString: call.method]) {
91
+ if ([@" -[InAppPurchasePlugin startProductRequest:result:]" isEqualToString: call.method]) {
95
92
[self handleProductRequestMethodCall: call result: result];
96
- } else if ([@" -[InAppPurchasePlugin addPayment:result:]" isEqualToString: call.method]) {
97
- [self addPayment: call result: result];
98
93
} else if ([@" -[InAppPurchasePlugin finishTransaction:result:]" isEqualToString: call.method]) {
99
94
[self finishTransaction: call result: result];
100
95
} else if ([@" -[InAppPurchasePlugin restoreTransactions:result:]" isEqualToString: call.method]) {
@@ -127,34 +122,29 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
127
122
}
128
123
}
129
124
130
- - (void )canMakePayments:(FlutterResult)result {
131
- result (@([SKPaymentQueue canMakePayments ]));
125
+ - (nullable NSNumber *)canMakePaymentsWithError:
126
+ (FlutterError *_Nullable __autoreleasing *_Nonnull)error {
127
+ return @([SKPaymentQueue canMakePayments ]);
132
128
}
133
129
134
- - (void )getPendingTransactions:(FlutterResult)result {
130
+ - (nullable NSArray <SKPaymentTransactionMessage *> *)transactionsWithError:
131
+ (FlutterError *_Nullable *_Nonnull)error {
135
132
NSArray <SKPaymentTransaction *> *transactions =
136
133
[self .paymentQueueHandler getUnfinishedTransactions ];
137
134
NSMutableArray *transactionMaps = [[NSMutableArray alloc ] init ];
138
135
for (SKPaymentTransaction *transaction in transactions) {
139
- [transactionMaps addObject: [FIAObjectTranslator getMapFromSKPaymentTransaction : transaction]];
136
+ [transactionMaps addObject: [FIAObjectTranslator convertTransactionToPigeon : transaction]];
140
137
}
141
- result ( transactionMaps) ;
138
+ return transactionMaps;
142
139
}
143
140
144
- - (void )getStorefront:(FlutterResult)result {
145
- if (@available (iOS 13.0 , macOS 10.15 , *)) {
146
- SKStorefront *storefront = self.paymentQueueHandler .storefront ;
147
- if (!storefront) {
148
- result (nil );
149
- return ;
150
- }
151
- result ([FIAObjectTranslator getMapFromSKStorefront: storefront]);
152
- return ;
141
+ - (nullable SKStorefrontMessage *)storefrontWithError:(FlutterError *_Nullable *_Nonnull)error
142
+ API_AVAILABLE (ios (13.0 ), macos (10.15 )) {
143
+ SKStorefront *storefront = self.paymentQueueHandler .storefront ;
144
+ if (!storefront) {
145
+ return nil ;
153
146
}
154
-
155
- NSLog (@" storefront is not avaialbe in iOS below 13.0 or macOS below 10.15." );
156
- result (nil );
157
- return ;
147
+ return [FIAObjectTranslator convertStorefrontToPigeon: storefront];
158
148
}
159
149
160
150
- (void )handleProductRequestMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
@@ -193,28 +183,23 @@ - (void)handleProductRequestMethodCall:(FlutterMethodCall *)call result:(Flutter
193
183
}];
194
184
}
195
185
196
- - (void )addPayment:(FlutterMethodCall *)call result:(FlutterResult)result {
197
- if (![call.arguments isKindOfClass: [NSDictionary class ]]) {
198
- result ([FlutterError errorWithCode: @" storekit_invalid_argument"
199
- message: @" Argument type of addPayment is not a Dictionary"
200
- details: call.arguments]);
201
- return ;
202
- }
203
- NSDictionary *paymentMap = (NSDictionary *)call.arguments ;
186
+ - (void )addPaymentPaymentMap:(nonnull NSDictionary *)paymentMap
187
+ error:(FlutterError *_Nullable __autoreleasing *_Nonnull)error {
204
188
NSString *productID = [paymentMap objectForKey: @" productIdentifier" ];
205
189
// When a product is already fetched, we create a payment object with
206
190
// the product to process the payment.
207
191
SKProduct *product = [self getProduct: productID];
208
192
if (!product) {
209
- result ( [FlutterError
193
+ *error = [FlutterError
210
194
errorWithCode: @" storekit_invalid_payment_object"
211
195
message:
212
196
@" You have requested a payment for an invalid product. Either the "
213
197
@" `productIdentifier` of the payment is not valid or the product has not been "
214
198
@" fetched before adding the payment to the payment queue."
215
- details: call.arguments]) ;
199
+ details: paymentMap] ;
216
200
return ;
217
201
}
202
+
218
203
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct: product];
219
204
payment.applicationUsername = [paymentMap objectForKey: @" applicationUsername" ];
220
205
NSNumber *quantity = [paymentMap objectForKey: @" quantity" ];
@@ -227,34 +212,32 @@ - (void)addPayment:(FlutterMethodCall *)call result:(FlutterResult)result {
227
212
if (@available (iOS 12.2 , *)) {
228
213
NSDictionary *paymentDiscountMap = [self getNonNullValueFromDictionary: paymentMap
229
214
forKey: @" paymentDiscount" ];
230
- NSString *error = nil ;
215
+ NSString *errorMsg = nil ;
231
216
SKPaymentDiscount *paymentDiscount =
232
- [FIAObjectTranslator getSKPaymentDiscountFromMap: paymentDiscountMap withError: &error ];
217
+ [FIAObjectTranslator getSKPaymentDiscountFromMap: paymentDiscountMap withError: &errorMsg ];
233
218
234
- if (error ) {
235
- result ( [FlutterError
219
+ if (errorMsg ) {
220
+ *error = [FlutterError
236
221
errorWithCode: @" storekit_invalid_payment_discount_object"
237
222
message: [NSString stringWithFormat: @" You have requested a payment and specified a "
238
223
@" payment discount with invalid properties. %@ " ,
239
- error ]
240
- details: call.arguments]) ;
224
+ errorMsg ]
225
+ details: paymentMap] ;
241
226
return ;
242
227
}
243
228
244
229
payment.paymentDiscount = paymentDiscount;
245
230
}
246
-
247
231
if (![self .paymentQueueHandler addPayment: payment]) {
248
- result ( [FlutterError
232
+ *error = [FlutterError
249
233
errorWithCode: @" storekit_duplicate_product_object"
250
234
message: @" There is a pending transaction for the same product identifier. Please "
251
235
@" either wait for it to be finished or finish it manually using "
252
236
@" `completePurchase` to avoid edge cases."
253
237
254
- details: call.arguments]) ;
238
+ details: paymentMap] ;
255
239
return ;
256
240
}
257
- result (nil );
258
241
}
259
242
260
243
- (void )finishTransaction:(FlutterMethodCall *)call result:(FlutterResult)result {
@@ -465,5 +448,4 @@ - (SKProduct *)getProduct:(NSString *)productID {
465
448
- (SKReceiptRefreshRequest *)getRefreshReceiptRequest:(NSDictionary *)properties {
466
449
return [[SKReceiptRefreshRequest alloc ] initWithReceiptProperties: properties];
467
450
}
468
-
469
451
@end
0 commit comments