@@ -40,8 +40,11 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
4040 FlutterMethodChannel *channel =
4141 [FlutterMethodChannel methodChannelWithName: @" plugins.flutter.io/in_app_purchase"
4242 binaryMessenger: [registrar messenger ]];
43+
4344 InAppPurchasePlugin *instance = [[InAppPurchasePlugin alloc ] initWithRegistrar: registrar];
4445 [registrar addMethodCallDelegate: instance channel: channel];
46+ [registrar addApplicationDelegate: instance];
47+ SetUpInAppPurchaseAPI (registrar.messenger , instance);
4548}
4649
4750- (instancetype )initWithReceiptManager : (FIAPReceiptManager *)receiptManager {
@@ -85,16 +88,8 @@ - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar
8588}
8689
8790- (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]) {
9592 [self handleProductRequestMethodCall: call result: result];
96- } else if ([@" -[InAppPurchasePlugin addPayment:result:]" isEqualToString: call.method]) {
97- [self addPayment: call result: result];
9893 } else if ([@" -[InAppPurchasePlugin finishTransaction:result:]" isEqualToString: call.method]) {
9994 [self finishTransaction: call result: result];
10095 } else if ([@" -[InAppPurchasePlugin restoreTransactions:result:]" isEqualToString: call.method]) {
@@ -127,34 +122,29 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
127122 }
128123}
129124
130- - (void )canMakePayments:(FlutterResult)result {
131- result (@([SKPaymentQueue canMakePayments ]));
125+ - (nullable NSNumber *)canMakePaymentsWithError:
126+ (FlutterError *_Nullable __autoreleasing *_Nonnull)error {
127+ return @([SKPaymentQueue canMakePayments ]);
132128}
133129
134- - (void )getPendingTransactions:(FlutterResult)result {
130+ - (nullable NSArray <SKPaymentTransactionMessage *> *)transactionsWithError:
131+ (FlutterError *_Nullable *_Nonnull)error {
135132 NSArray <SKPaymentTransaction *> *transactions =
136133 [self .paymentQueueHandler getUnfinishedTransactions ];
137134 NSMutableArray *transactionMaps = [[NSMutableArray alloc ] init ];
138135 for (SKPaymentTransaction *transaction in transactions) {
139- [transactionMaps addObject: [FIAObjectTranslator getMapFromSKPaymentTransaction : transaction]];
136+ [transactionMaps addObject: [FIAObjectTranslator convertTransactionToPigeon : transaction]];
140137 }
141- result ( transactionMaps) ;
138+ return transactionMaps;
142139}
143140
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 ;
153146 }
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];
158148}
159149
160150- (void )handleProductRequestMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
@@ -193,28 +183,23 @@ - (void)handleProductRequestMethodCall:(FlutterMethodCall *)call result:(Flutter
193183 }];
194184}
195185
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 {
204188 NSString *productID = [paymentMap objectForKey: @" productIdentifier" ];
205189 // When a product is already fetched, we create a payment object with
206190 // the product to process the payment.
207191 SKProduct *product = [self getProduct: productID];
208192 if (!product) {
209- result ( [FlutterError
193+ *error = [FlutterError
210194 errorWithCode: @" storekit_invalid_payment_object"
211195 message:
212196 @" You have requested a payment for an invalid product. Either the "
213197 @" `productIdentifier` of the payment is not valid or the product has not been "
214198 @" fetched before adding the payment to the payment queue."
215- details: call.arguments]) ;
199+ details: paymentMap] ;
216200 return ;
217201 }
202+
218203 SKMutablePayment *payment = [SKMutablePayment paymentWithProduct: product];
219204 payment.applicationUsername = [paymentMap objectForKey: @" applicationUsername" ];
220205 NSNumber *quantity = [paymentMap objectForKey: @" quantity" ];
@@ -227,34 +212,32 @@ - (void)addPayment:(FlutterMethodCall *)call result:(FlutterResult)result {
227212 if (@available (iOS 12.2 , *)) {
228213 NSDictionary *paymentDiscountMap = [self getNonNullValueFromDictionary: paymentMap
229214 forKey: @" paymentDiscount" ];
230- NSString *error = nil ;
215+ NSString *errorMsg = nil ;
231216 SKPaymentDiscount *paymentDiscount =
232- [FIAObjectTranslator getSKPaymentDiscountFromMap: paymentDiscountMap withError: &error ];
217+ [FIAObjectTranslator getSKPaymentDiscountFromMap: paymentDiscountMap withError: &errorMsg ];
233218
234- if (error ) {
235- result ( [FlutterError
219+ if (errorMsg ) {
220+ *error = [FlutterError
236221 errorWithCode: @" storekit_invalid_payment_discount_object"
237222 message: [NSString stringWithFormat: @" You have requested a payment and specified a "
238223 @" payment discount with invalid properties. %@ " ,
239- error ]
240- details: call.arguments]) ;
224+ errorMsg ]
225+ details: paymentMap] ;
241226 return ;
242227 }
243228
244229 payment.paymentDiscount = paymentDiscount;
245230 }
246-
247231 if (![self .paymentQueueHandler addPayment: payment]) {
248- result ( [FlutterError
232+ *error = [FlutterError
249233 errorWithCode: @" storekit_duplicate_product_object"
250234 message: @" There is a pending transaction for the same product identifier. Please "
251235 @" either wait for it to be finished or finish it manually using "
252236 @" `completePurchase` to avoid edge cases."
253237
254- details: call.arguments]) ;
238+ details: paymentMap] ;
255239 return ;
256240 }
257- result (nil );
258241}
259242
260243- (void )finishTransaction:(FlutterMethodCall *)call result:(FlutterResult)result {
@@ -465,5 +448,4 @@ - (SKProduct *)getProduct:(NSString *)productID {
465448- (SKReceiptRefreshRequest *)getRefreshReceiptRequest:(NSDictionary *)properties {
466449 return [[SKReceiptRefreshRequest alloc ] initWithReceiptProperties: properties];
467450}
468-
469451@end
0 commit comments