-
Notifications
You must be signed in to change notification settings - Fork 997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Workaround for SafariVC reporting failure while redirecting to iDEAL #937
Changes from 2 commits
505e549
35316d4
3d897c3
8ad344c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ @interface STPRedirectContext () <SFSafariViewControllerDelegate, STPURLCallback | |
@property (nonatomic, strong) STPSource *source; | ||
@property (nonatomic, strong, nullable) SFSafariViewController *safariVC; | ||
@property (nonatomic, assign, readwrite) STPRedirectContextState state; | ||
/// If we're on iOS 11+ and in the SafariVC flow, this tracks the latest URL loaded/redirected to during the initial load | ||
@property (nonatomic, strong, readwrite, nullable) NSURL *latestSafariVCUrl; | ||
|
||
@property (nonatomic, assign) BOOL subscribedToURLNotifications; | ||
@property (nonatomic, assign) BOOL subscribedToForegroundNotifications; | ||
|
@@ -121,7 +123,8 @@ - (void)startSafariViewControllerRedirectFlowFromViewController:(UIViewControlle | |
if (self.state == STPRedirectContextStateNotStarted) { | ||
_state = STPRedirectContextStateInProgress; | ||
[self subscribeToUrlNotifications]; | ||
self.safariVC = [[SFSafariViewController alloc] initWithURL:self.source.redirect.url]; | ||
self.latestSafariVCUrl = self.source.redirect.url; | ||
self.safariVC = [[SFSafariViewController alloc] initWithURL:self.latestSafariVCUrl]; | ||
self.safariVC.delegate = self; | ||
[presentingViewController presentViewController:self.safariVC | ||
animated:YES | ||
|
@@ -154,14 +157,37 @@ - (void)safariViewControllerDidFinish:(__unused SFSafariViewController *)control | |
} | ||
|
||
- (void)safariViewController:(__unused SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully { | ||
/* | ||
SafariVC is, imo, over-eager to report errors. The way that (for example) girogate.de redirects | ||
can cause SafariVC to report that the initial load failed, even though it completes successfully. | ||
|
||
So, only report failures to complete the initial load if the host was a Stripe domain. | ||
Stripe uses 302 redirects, and this should catch local connection problems as well as | ||
server-side failures from Stripe. | ||
|
||
We can only track the latest URL loaded on iOS 11, because `safariViewController:initialLoadDidRedirectToURL:` | ||
didn't exist prior to that. | ||
*/ | ||
if (didLoadSuccessfully == NO) { | ||
stpDispatchToMainThreadIfNecessary(^{ | ||
[self handleRedirectCompletionWithError:[NSError stp_genericConnectionError] | ||
shouldDismissViewController:YES]; | ||
}); | ||
if (@available(iOS 11, *)) { | ||
stpDispatchToMainThreadIfNecessary(^{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't do anything here on iOS < 11? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there was some way to distinguish between real failures and spurious ones, I'd love to. I wasn't able to find one though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a similar initial reaction mostly by the way it's written. Maybe it needs an else clause with a doc:
In any case, this looks functional and don't have strong opinion on this one. |
||
if ([self.latestSafariVCUrl.host containsString:@"stripe.com"]) { | ||
[self handleRedirectCompletionWithError:[NSError stp_genericConnectionError] | ||
shouldDismissViewController:YES]; | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
- (void)safariViewController:(__unused SFSafariViewController *)controller initialLoadDidRedirectToURL:(NSURL *)URL { | ||
stpDispatchToMainThreadIfNecessary(^{ | ||
// This is only kept up to date during the "initial load", but we only need the value in | ||
// `safariViewController:didCompleteInitialLoad:`, so that's fine. | ||
self.latestSafariVCUrl = URL; | ||
}); | ||
} | ||
|
||
#pragma mark - Private methods - | ||
|
||
- (void)handleWillForegroundNotification { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
latestSafariVCUrl
->latestKnownSafariVCUrl
to better emphasize it's an approximation