-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[QL] Consolidate Return URL Logic (#1271)
* updates to move ReturnURL logic to one place * update fallback logic to handle properly * update plist for app installed check * add webBrowser based BTPayPalReturnURL tests * PR feedback: update docstrings and enum name
- Loading branch information
1 parent
3121388
commit 346e4e9
Showing
7 changed files
with
169 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import Foundation | ||
|
||
#if canImport(BraintreeCore) | ||
import BraintreeCore | ||
#endif | ||
|
||
enum BTPayPalReturnURLState { | ||
case unknownPath | ||
case succeeded | ||
case canceled | ||
} | ||
|
||
/// This class interprets URLs received from the PayPal app via app switch returns and web returns via ASWebAuthenticationSession. | ||
/// | ||
/// PayPal app switch and ASWebAuthenticationSession authorization requests should result in success or user-initiated cancelation. These states are communicated in the url. | ||
struct BTPayPalReturnURL { | ||
|
||
/// The overall status of the app switch - success, cancelation, or an unknown path | ||
var state: BTPayPalReturnURLState = .unknownPath | ||
|
||
/// Initializes a new `BTPayPalReturnURL` | ||
/// - Parameter url: an incoming app switch or ASWebAuthenticationSession url | ||
init?(_ redirectType: PayPalRedirectType) { | ||
switch redirectType { | ||
case .payPalApp(let url), .webBrowser(let url): | ||
if url.path.contains("success") { | ||
state = .succeeded | ||
} else if url.path.contains("cancel") { | ||
state = .canceled | ||
} else { | ||
state = .unknownPath | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Static Methods | ||
|
||
/// Evaluates whether the url represents a valid PayPal return URL. | ||
/// - Parameter url: an app switch or ASWebAuthenticationSession return URL | ||
/// - Returns: `true` if the url represents a valid PayPal app switch return | ||
static func isValid(_ url: URL) -> Bool { | ||
url.scheme == "https" && (url.path.contains("cancel") || url.path.contains("success")) | ||
} | ||
|
||
static func isValidURLAction(url: URL, linkType: String?) -> Bool { | ||
guard let host = url.host, let scheme = url.scheme, !scheme.isEmpty else { | ||
return false | ||
} | ||
|
||
var hostAndPath = host | ||
.appending(url.path) | ||
.components(separatedBy: "/") | ||
.dropLast(1) // remove the action (`success`, `cancel`, etc) | ||
.joined(separator: "/") | ||
|
||
if hostAndPath.count > 0 { | ||
hostAndPath.append("/") | ||
} | ||
|
||
/// If we are using the deeplink/ASWeb based PayPal flow we want to check that the host and path matches | ||
/// the static callbackURLHostAndPath. For the universal link flow we do not care about this check. | ||
if hostAndPath != BTPayPalRequest.callbackURLHostAndPath && linkType == "deeplink" { | ||
return false | ||
} | ||
|
||
guard let action = action(from: url), | ||
let query = url.query, | ||
query.count > 0, | ||
action.count >= 0, | ||
["success", "cancel", "authenticate"].contains(action) else { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
static func action(from url: URL) -> String? { | ||
guard let action = url.lastPathComponent.components(separatedBy: "?").first, !action.isEmpty else { | ||
return url.host | ||
} | ||
|
||
return action | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
UnitTests/BraintreePayPalTests/BTPayPalAppSwitchReturnURL_Tests.swift
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.