Skip to content

Commit

Permalink
Fix an issue where partialAddresses flag was set to false, which woul…
Browse files Browse the repository at this point in the history
…d invalidat the already set (through a updateCheckout: call) shippingRate on a checkout during completeCheckout:
  • Loading branch information
runmad committed Dec 2, 2015
1 parent f5c7af6 commit 837165d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Mobile Buy SDK/Mobile Buy SDK Tests/BUYClientTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ - (void)testPartialAddressesFlag
{
BUYCart *cart = [[BUYCart alloc] init];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:cart];

XCTAssertThrows([checkout setPartialAddresses:NO]);

NSURLSessionDataTask *task = [self.client createCheckout:checkout completion:nil];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:task.originalRequest.HTTPBody options:0 error:nil];
Expand Down
2 changes: 2 additions & 0 deletions Mobile Buy SDK/Mobile Buy SDK/Models/BUYCheckout.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@

/**
* Flag used to inform server that the shipping address is partially filled, suitable to retrieve shipping rates
* with partial shipping addresses provided by PKPaymentAuthorizationViewController.
* Note: This should only ever be set to YES. Setting it to NO throws an exception.
*/
@property (nonatomic, assign) BOOL partialAddresses;

Expand Down
8 changes: 8 additions & 0 deletions Mobile Buy SDK/Mobile Buy SDK/Models/BUYCheckout.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ - (NSDictionary *)jsonDictionaryForCheckout
return @{ @"checkout" : json };
}

-(void)setPartialAddresses:(BOOL)partialAddresses
{
if (partialAddresses == NO) {
@throw [NSException exceptionWithName:@"partialAddress" reason:@"partialAddresses can only be set to true and should never be set to false on a complete address" userInfo:nil];
}
_partialAddresses = partialAddresses;
}

- (BOOL)hasToken
{
return (_token && [_token length] > 0);
Expand Down
12 changes: 8 additions & 4 deletions Mobile Buy SDK/Mobile Buy SDK/Utils/BUYApplePayHelpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController
{
// Update the checkout with the rest of the information. Apple has now provided us with a FULL billing address and a FULL shipping address.
// We now update the checkout with our new found data so that you can ship the products to the right address, and we collect whatever else we need.

self.checkout.partialAddresses = NO;

if ([payment respondsToSelector:@selector(shippingContact)]) {
self.checkout.email = payment.shippingContact.emailAddress;
self.checkout.shippingAddress = self.checkout.requiresShipping ? [BUYAddress buy_addressFromContact:payment.shippingContact] : nil;
Expand Down Expand Up @@ -165,8 +164,13 @@ -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController
#pragma mark -

- (void)updateCheckoutWithAddressCompletion:(void (^)(PKPaymentAuthorizationStatus, NSArray *shippingMethods, NSArray *summaryItems))completion
{
self.checkout.partialAddresses = [self.checkout.shippingAddress isPartialAddress];
{
// This method call is internal to selection of shipping address that are returned as partial from PKPaymentAuthorizationViewController
// However, to ensure we never set partialAddresses to NO, we want to guard the setter. Should PKPaymentAuthorizationViewController ever
// return a full address through it's delegate method, this will still function since a complete address can be used to calculate shipping rates
if ([self.checkout.shippingAddress isPartialAddress] == YES) {
self.checkout.partialAddresses = YES;
}

if ([self.checkout.shippingAddress isValidAddressForShippingRates]) {

Expand Down

0 comments on commit 837165d

Please sign in to comment.