From f4bc58f44fdcd2feddc9c3e536cddaae702e6f43 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Tue, 19 Feb 2019 15:21:33 -0800 Subject: [PATCH] # This is a combination of 6 commits. # This is the 1st commit message: review fixes # This is the commit message #2: Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m review fix Co-Authored-By: cyanglaz # This is the commit message #3: Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m Co-Authored-By: cyanglaz # This is the commit message #4: Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m review fix Co-Authored-By: cyanglaz # This is the commit message #5: Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m Co-Authored-By: cyanglaz # This is the commit message #6: Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m Co-Authored-By: cyanglaz --- .../InAppPurchasePluginTest.m | 2 -- .../ios/Classes/FIAPaymentQueueHandler.h | 12 +------ .../ios/Classes/FIAPaymentQueueHandler.m | 12 ------- .../ios/Classes/InAppPurchasePlugin.m | 34 +++++++++---------- 4 files changed, 18 insertions(+), 42 deletions(-) diff --git a/packages/in_app_purchase/example/ios/in_app_purchase_pluginTests/InAppPurchasePluginTest.m b/packages/in_app_purchase/example/ios/in_app_purchase_pluginTests/InAppPurchasePluginTest.m index 5a061bcb3d2e..15244ce34c33 100644 --- a/packages/in_app_purchase/example/ios/in_app_purchase_pluginTests/InAppPurchasePluginTest.m +++ b/packages/in_app_purchase/example/ios/in_app_purchase_pluginTests/InAppPurchasePluginTest.m @@ -121,7 +121,6 @@ - (void)testAddPaymentFailure { } updatedDownloads:nil]; [queue addTransactionObserver:self.plugin.paymentQueueHandler]; - self.plugin.paymentQueueHandler.testing = YES; [self.plugin handleMethodCall:call result:^(id r){ }]; @@ -158,7 +157,6 @@ - (void)testAddPaymentSuccessWithMockQueue { } updatedDownloads:nil]; [queue addTransactionObserver:self.plugin.paymentQueueHandler]; - self.plugin.paymentQueueHandler.testing = YES; [self.plugin handleMethodCall:call result:^(id r){ }]; diff --git a/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.h b/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.h index 9749fb57abe5..692514ab5278 100644 --- a/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.h +++ b/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.h @@ -7,8 +7,6 @@ NS_ASSUME_NONNULL_BEGIN -extern NSString *const TestingProductID; - typedef void (^TransactionsUpdated)(NSArray *transactions); typedef void (^TransactionsRemoved)(NSArray *transactions); typedef void (^RestoreTransactionFailed)(NSError *error); @@ -29,17 +27,9 @@ typedef void (^UpdatedDownloads)(NSArray *downloads); shouldAddStorePayment:(nullable ShouldAddStorePayment)shouldAddStorePayment updatedDownloads:(nullable UpdatedDownloads)updatedDownloads; - (void)addPayment:(SKPayment *)payment; -// Can throw exceptions, should always used in a @try block. +// Can throw exceptions if the transaction type is purchasing, should always used in a @try block. - (void)finishTransaction:(SKPaymentTransaction *)transaction; -// Enable testing. - -// Because payment object in transaction is not KVC, we cannot mock the payment object in the -// transaction. So there is no easay way to create stubs and test the handler. -// When set to true, we always this a constant TestingProductID as product ID -// when storing and accessing the completion block from self.completionMap -@property(assign, nonatomic) BOOL testing; - @end NS_ASSUME_NONNULL_END diff --git a/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.m b/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.m index ea4be4c507eb..bf45dbd1b0ff 100644 --- a/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.m +++ b/packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.m @@ -4,8 +4,6 @@ #import "FIAPaymentQueueHandler.h" -NSString *const TestingProductID = @"testing"; - @interface FIAPaymentQueueHandler () @property(strong, nonatomic) SKPaymentQueue *queue; @@ -46,21 +44,11 @@ - (instancetype)initWithQueue:(nonnull SKPaymentQueue *)queue } - (void)addPayment:(SKPayment *)payment { - NSString *productID = payment.productIdentifier; - if (self.testing) { - productID = TestingProductID; - } [self.queue addPayment:payment]; } - (void)finishTransaction:(SKPaymentTransaction *)transaction { - @try { [self.queue finishTransaction:transaction]; - } @catch (NSException *e) { - // finish transaction will throw exception if the transaction type is purchasing. Raise the - // exception so the InAppPurchasePlugin will get notified. - [e raise]; - } } #pragma mark - observing diff --git a/packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m b/packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m index 258c77479104..b2100e120de8 100644 --- a/packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m +++ b/packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m @@ -134,7 +134,7 @@ - (void)handleProductRequestMethodCall:(FlutterMethodCall *)call result:(Flutter - (void)createPaymentWithProductID:(FlutterMethodCall *)call result:(FlutterResult)result { if (![call.arguments isKindOfClass:[NSString class]]) { result([FlutterError - errorWithCode:@"storekit_invalide_argument" + errorWithCode:@"storekit_invalid_argument" message:@"Argument type of createPaymentWithProductID is not a string." details:call.arguments]); return; @@ -145,7 +145,7 @@ - (void)createPaymentWithProductID:(FlutterMethodCall *)call result:(FlutterResu result([FlutterError errorWithCode:@"storekit_product_not_found" message:@"Cannot find the product. To create a payment of a product, you must query " - @"the product with SKProductRequestMaker.startProductRequest." + @"the product with SKProductRequestMaker.startProductRequest first." details:call.arguments]); return; } @@ -156,7 +156,7 @@ - (void)createPaymentWithProductID:(FlutterMethodCall *)call result:(FlutterResu - (void)addPayment:(FlutterMethodCall *)call result:(FlutterResult)result { if (![call.arguments isKindOfClass:[NSDictionary class]]) { - result([FlutterError errorWithCode:@"storekit_invalide_argument" + result([FlutterError errorWithCode:@"storekit_invalid_argument" message:@"Argument type of addPayment is not a map" details:call.arguments]); return; @@ -164,30 +164,29 @@ - (void)addPayment:(FlutterMethodCall *)call result:(FlutterResult)result { NSDictionary *paymentMap = (NSDictionary *)call.arguments; NSString *productID = [paymentMap objectForKey:@"productID"]; SKPayment *payment = [self.paymentsCache objectForKey:productID]; - // User can use payment object with mutable = true and add simulatesAskToBuyInSandBox = true to + // User can use payment object with usePaymentObject = true and add simulatesAskToBuyInSandBox = true to // test the payment flow. - if (!payment || [paymentMap[@"mutable"] boolValue] == YES) { + if (!payment || [paymentMap[@"usePaymentObject"] boolValue] == YES) { SKMutablePayment *mutablePayment = [[SKMutablePayment alloc] init]; mutablePayment.productIdentifier = productID; NSNumber *quantity = [paymentMap objectForKey:@"quantity"]; - if (quantity) { - mutablePayment.quantity = quantity.integerValue; - } + mutablePayment.quantity = quantity?quantity.integerValue:1; NSString *applicationUsername = [paymentMap objectForKey:@"applicationUsername"]; mutablePayment.applicationUsername = applicationUsername; if (@available(iOS 8.3, *)) { mutablePayment.simulatesAskToBuyInSandbox = [[paymentMap objectForKey:@"simulatesAskToBuyInSandBox"] boolValue]; } - [self.paymentQueueHandler addPayment:payment]; + [self.paymentQueueHandler addPayment:mutablePayment]; } else { [self.paymentQueueHandler addPayment:payment]; } + result(nil); } - (void)finishTransaction:(FlutterMethodCall *)call result:(FlutterResult)result { if (![call.arguments isKindOfClass:[NSString class]]) { - result([FlutterError errorWithCode:@"storekit_invalide_argument" + result([FlutterError errorWithCode:@"storekit_invalid_argument" message:@"Argument type of finishTransaction is not a string." details:call.arguments]); return; @@ -211,24 +210,25 @@ - (void)finishTransaction:(FlutterMethodCall *)call result:(FlutterResult)result details:e.description]); return; } + result(nil); } #pragma mark - delegates - (void)handleTransactionsUpdated:(NSArray *)transactions { NSMutableArray *maps = [NSMutableArray new]; - for (SKPaymentTransaction *transcation in transactions) { - [maps addObject:[FIAObjectTranslator getMapFromSKPaymentTransaction:transcation]]; + for (SKPaymentTransaction *transaction in transactions) { + [maps addObject:[FIAObjectTranslator getMapFromSKPaymentTransaction:transaction]]; } - [self.callbackChannel invokeMethod:@"updatedTransaction" arguments:maps]; + [self.callbackChannel invokeMethod:@"updatedTransactions" arguments:maps]; } - (void)handleTransactionsRemoved:(NSArray *)transactions { NSMutableArray *maps = [NSMutableArray new]; - for (SKPaymentTransaction *transcation in transactions) { - [maps addObject:[FIAObjectTranslator getMapFromSKPaymentTransaction:transcation]]; + for (SKPaymentTransaction *transaction in transactions) { + [maps addObject:[FIAObjectTranslator getMapFromSKPaymentTransaction:transaction]]; } - [self.callbackChannel invokeMethod:@"removedTransaction" arguments:maps]; + [self.callbackChannel invokeMethod:@"removedTransactions" arguments:maps]; } - (void)handleTransactionRestoreFailed:(NSError *)error { @@ -252,7 +252,7 @@ - (void)updatedDownloads:(NSArray *)downloads { } - (BOOL)shouldAddStorePayment:(SKPayment *)payment product:(SKProduct *)product { - // We alwasy return NO here. And we send the message to dart to process the payment; and we will + // We always return NO here. And we send the message to dart to process the payment; and we will // have a incerpection method that deciding if the payment should be processed (implemented by the // programmer). [self.productsCache setObject:product forKey:product.productIdentifier];