Skip to content

Commit

Permalink
# This is a combination of 6 commits.
Browse files Browse the repository at this point in the history
# 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 <tyang8954@gmail.com>
# This is the commit message #3:

Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m

Co-Authored-By: cyanglaz <tyang8954@gmail.com>
# This is the commit message flutter#4:

Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m

review fix

Co-Authored-By: cyanglaz <tyang8954@gmail.com>
# This is the commit message flutter#5:

Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m

Co-Authored-By: cyanglaz <tyang8954@gmail.com>
# This is the commit message flutter#6:

Update packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m

Co-Authored-By: cyanglaz <tyang8954@gmail.com>
  • Loading branch information
Chris Yang and cyanglaz committed Feb 20, 2019
1 parent 431cbc6 commit f4bc58f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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){
}];
Expand Down Expand Up @@ -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){
}];
Expand Down
12 changes: 1 addition & 11 deletions packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

NS_ASSUME_NONNULL_BEGIN

extern NSString *const TestingProductID;

typedef void (^TransactionsUpdated)(NSArray<SKPaymentTransaction *> *transactions);
typedef void (^TransactionsRemoved)(NSArray<SKPaymentTransaction *> *transactions);
typedef void (^RestoreTransactionFailed)(NSError *error);
Expand All @@ -29,17 +27,9 @@ typedef void (^UpdatedDownloads)(NSArray<SKDownload *> *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
12 changes: 0 additions & 12 deletions packages/in_app_purchase/ios/Classes/FIAPaymentQueueHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#import "FIAPaymentQueueHandler.h"

NSString *const TestingProductID = @"testing";

@interface FIAPaymentQueueHandler ()

@property(strong, nonatomic) SKPaymentQueue *queue;
Expand Down Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -156,38 +156,37 @@ - (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;
}
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;
Expand All @@ -211,24 +210,25 @@ - (void)finishTransaction:(FlutterMethodCall *)call result:(FlutterResult)result
details:e.description]);
return;
}
result(nil);
}

#pragma mark - delegates

- (void)handleTransactionsUpdated:(NSArray<SKPaymentTransaction *> *)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<SKPaymentTransaction *> *)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 {
Expand All @@ -252,7 +252,7 @@ - (void)updatedDownloads:(NSArray<SKDownload *> *)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];
Expand Down

0 comments on commit f4bc58f

Please sign in to comment.