Commit e477f067 by Rune Madsen

Merge pull request #72 from Shopify/runmad.improve-safari-checkout-handling

Sample app Safari view controller checkout logic improvements
parents c87c7478 6940a665
...@@ -195,7 +195,13 @@ NSString * const MerchantId = @""; ...@@ -195,7 +195,13 @@ NSString * const MerchantId = @"";
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller
{ {
[self.navigationController popToRootViewControllerAnimated:YES]; [self getCompletedCheckout:^{
if (self.checkout.order) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showCheckoutConfirmation];
});
}
}];
} }
#pragma mark Native Checkout #pragma mark Native Checkout
...@@ -240,7 +246,7 @@ NSString * const MerchantId = @""; ...@@ -240,7 +246,7 @@ NSString * const MerchantId = @"";
NSLog(@"Successfully got completion status: %lu", (unsigned long)completionStatus); NSLog(@"Successfully got completion status: %lu", (unsigned long)completionStatus);
[self getCompletedCheckout]; [self getCompletedCheckout:NULL];
} }
- (void)operation:(GetCompletionStatusOperation *)operation failedToReceiveCompletionStatus:(NSError *)error - (void)operation:(GetCompletionStatusOperation *)operation failedToReceiveCompletionStatus:(NSError *)error
...@@ -308,7 +314,7 @@ NSString * const MerchantId = @""; ...@@ -308,7 +314,7 @@ NSString * const MerchantId = @"";
[self.applePayHelper paymentAuthorizationViewController:controller didAuthorizePayment:payment completion:completion]; [self.applePayHelper paymentAuthorizationViewController:controller didAuthorizePayment:payment completion:completion];
self.checkout = self.applePayHelper.checkout; self.checkout = self.applePayHelper.checkout;
[self getCompletedCheckout]; [self getCompletedCheckout:NULL];
} }
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
...@@ -358,27 +364,42 @@ NSString * const MerchantId = @""; ...@@ -358,27 +364,42 @@ NSString * const MerchantId = @"";
{ {
NSURL *url = notification.userInfo[@"url"]; NSURL *url = notification.userInfo[@"url"];
__weak CheckoutViewController *welf = self; if ([self.presentedViewController isKindOfClass:[SFSafariViewController class]]) {
[self dismissViewControllerAnimated:self.presentedViewController completion:^{
[self getCompletionStatusAndCompletedCheckoutWithURL:url];
}];
} else {
[self getCompletionStatusAndCompletedCheckoutWithURL:url];
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:CheckoutCallbackNotification object:nil];
}
- (void)getCompletionStatusAndCompletedCheckoutWithURL:(NSURL*)url
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
__weak CheckoutViewController *welf = self;
[self.client getCompletionStatusOfCheckoutURL:url completion:^(BUYStatus status, NSError *error) { [self.client getCompletionStatusOfCheckoutURL:url completion:^(BUYStatus status, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (error == nil && status == BUYStatusComplete) { if (error == nil && status == BUYStatusComplete) {
NSLog(@"Successfully completed checkout"); NSLog(@"Successfully completed checkout");
[welf getCompletedCheckout]; [welf getCompletedCheckout:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self showCheckoutConfirmation];
});
}];
} }
else { else {
NSLog(@"Error completing checkout: %@", error); NSLog(@"Error completing checkout: %@", error);
} }
}]; }];
[[NSNotificationCenter defaultCenter] removeObserver:self name:CheckoutCallbackNotification object:nil];
} }
- (void)getCompletedCheckout - (void)getCompletedCheckout:(void (^)(void))completionBlock
{ {
__weak CheckoutViewController *welf = self; __weak CheckoutViewController *welf = self;
...@@ -394,9 +415,12 @@ NSString * const MerchantId = @""; ...@@ -394,9 +415,12 @@ NSString * const MerchantId = @"";
} }
if (checkout) { if (checkout) {
welf.checkout = checkout; welf.checkout = checkout;
[welf showCheckoutConfirmation];
NSLog(@"%@", checkout); NSLog(@"%@", checkout);
} }
if (completionBlock) {
completionBlock();
}
}]; }];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment