Skip to content

Commit

Permalink
Change returnURL type from URL to String pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Solovev committed Mar 20, 2024
1 parent 39ab641 commit 83cee97
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 16 deletions.
8 changes: 6 additions & 2 deletions ExampleApp/AppDeeplink.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import Foundation

enum AppDeeplink: String {
case threeDSChallenge = "omise_3ds_challenge"
case threeDSChallenge = "3ds_challenge"

var scheme: String {
"omiseExampleApp"
}

var urlString: String {
"\(scheme)://\(rawValue)"
}

var url: URL? {
URL(string: "\(scheme)://\(rawValue)")
URL(string: urlString)
}
}
8 changes: 4 additions & 4 deletions ExampleApp/Views/ProductDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ class ProductDetailViewController: BaseViewController {
guard let self = self,
let textField = alertController.textFields?.first,
let text = textField.text,
let url = URL(string: text),
let deeplinkRedirectURL = AppDeeplink.threeDSChallenge.url,
let webRedirectURL = URL(string: "https://exampleapp.opn.ooo") else { return }
let url = URL(string: text) else { return }

let deeplinkRedirectURLPattern = AppDeeplink.threeDSChallenge.urlString
let webRedirectURLPattern = "https://exampleapp.opn.ooo"
self.omiseSDK.presentAuthorizingPayment(
from: self,
authorizeURL: url,
returnURL: [deeplinkRedirectURL, webRedirectURL],
returnURLs: [deeplinkRedirectURLPattern, webRedirectURLPattern],
delegate: self
)
})
Expand Down
18 changes: 12 additions & 6 deletions OmiseSDK/Sources/OmiseSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class OmiseSDK {

public private(set) weak var presentedViewController: UIViewController?

private var handleURLs: [URL] = []
private var handleURLs: [String] = []

/// Creates a new instance of Omise SDK that provides interface to functionallity that SDK provides
///
Expand Down Expand Up @@ -167,14 +167,14 @@ public class OmiseSDK {
from topViewController: UIViewController,
animated: Bool = true,
authorizeURL: URL,
returnURL: [URL],
returnURLs: [String],
delegate: AuthorizingPaymentViewControllerDelegate
) {
dismiss(animated: false)

handleURLs = returnURL
let returnURLComponents = returnURL.compactMap {
URLComponents(url: $0, resolvingAgainstBaseURL: false)
handleURLs = returnURLs
let returnURLComponents = returnURLs.compactMap {
URLComponents(string: $0)
}

let viewController = AuthorizingPaymentViewController(nibName: nil, bundle: .omiseSDK)
Expand Down Expand Up @@ -205,7 +205,13 @@ public class OmiseSDK {
/// Handle URL Callback received by AppDelegate
public func handleURLCallback(_ url: URL) -> Bool {
// Will handle callback with 3ds library
return handleURLs.contains(url)
let containsURL = handleURLs
.map {
url.match(string: $0)
}
.contains(true)

return containsURL
}
}

Expand Down
26 changes: 26 additions & 0 deletions OmiseSDK/Sources/ToolKit/Extensions/URL+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation

extension URL {
func match(string: String) -> Bool {
guard
let expectedURLComponents = URLComponents(string: string),
let components = URLComponents(url: self, resolvingAgainstBaseURL: false) else {
return false
}

return expectedURLComponents.match(components: components)
}
}

extension URLComponents {
func match(string: String) -> Bool {
guard let expectedURLComponents = URLComponents(string: string) else { return false }
return match(components: expectedURLComponents)
}

func match(components expectedURLComponents: URLComponents) -> Bool {
return expectedURLComponents.scheme?.lowercased() == scheme?.lowercased()
&& expectedURLComponents.host?.lowercased() == host?.lowercased()
&& path.lowercased().hasPrefix(expectedURLComponents.path.lowercased())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public class AuthorizingPaymentViewController: UIViewController {
}

return expectedReturnURLPatterns.contains { expectedURLComponents -> Bool in
return expectedURLComponents.scheme == components.scheme
&& expectedURLComponents.host == components.host
&& components.path.hasPrefix(expectedURLComponents.path)
components.match(components: expectedURLComponents)
}
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ You can use the built-in authorizing payment view controller with the `authorize
omiseSDK.presentAuthorizingPayment(
from: self,
authorizeURL: url,
expectedReturnURLPatterns: [expectedReturnURL],
returnURLs: [returnURLs],
delegate: self
)
```
Expand Down
4 changes: 4 additions & 0 deletions dev.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
757B0A742AD71E3100DEAE8F /* OmiseUnitTestKit in Frameworks */ = {isa = PBXBuildFile; productRef = 757B0A732AD71E3100DEAE8F /* OmiseUnitTestKit */; };
757B0A752AD71E3100DEAE8F /* OmiseUnitTestKit in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 757B0A732AD71E3100DEAE8F /* OmiseUnitTestKit */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
7588011E2BAAB37C00F02934 /* AppDeeplink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7588011D2BAAB37C00F02934 /* AppDeeplink.swift */; };
758801202BAAC70200F02934 /* URL+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7588011F2BAAC70200F02934 /* URL+Helpers.swift */; };
759856242A286C880087B605 /* UIScrollView+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759856232A286C880087B605 /* UIScrollView+Helpers.swift */; };
759C7EC02B7DC7F50029D555 /* SourceType+MobileBanking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759C7EBF2B7DC7F50029D555 /* SourceType+MobileBanking.swift */; };
759C7EC22B7DC80A0029D555 /* SourceType+InternetBanking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759C7EC12B7DC80A0029D555 /* SourceType+InternetBanking.swift */; };
Expand Down Expand Up @@ -335,6 +336,7 @@
757B0A712AD71D7B00DEAE8F /* OmiseSwiftUIKit */ = {isa = PBXFileReference; lastKnownFileType = text; name = OmiseSwiftUIKit; path = Helpers/OmiseSwiftUIKit; sourceTree = SOURCE_ROOT; };
757B0A722AD71DCD00DEAE8F /* OmiseUnitTestKit */ = {isa = PBXFileReference; lastKnownFileType = text; name = OmiseUnitTestKit; path = Helpers/OmiseUnitTestKit; sourceTree = SOURCE_ROOT; };
7588011D2BAAB37C00F02934 /* AppDeeplink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDeeplink.swift; sourceTree = "<group>"; };
7588011F2BAAC70200F02934 /* URL+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Helpers.swift"; sourceTree = "<group>"; };
759856232A286C880087B605 /* UIScrollView+Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIScrollView+Helpers.swift"; sourceTree = "<group>"; };
759C7EBF2B7DC7F50029D555 /* SourceType+MobileBanking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SourceType+MobileBanking.swift"; sourceTree = "<group>"; };
759C7EC12B7DC80A0029D555 /* SourceType+InternetBanking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SourceType+InternetBanking.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -671,6 +673,7 @@
75405A772A25D327008C21F6 /* String+Helpers.swift */,
75405A7B2A25EC88008C21F6 /* Array+Helpers.swift */,
75D13DD72B86539C0073A831 /* Optional+Helpers.swift */,
7588011F2BAAC70200F02934 /* URL+Helpers.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1399,6 +1402,7 @@
751E7BEE2B7F7DD20031B4A2 /* PaymentMethod+ViewPresentable.swift in Sources */,
75D13D772B833C200073A831 /* UITableViewCell+ActivityIndicator.swift in Sources */,
986406311CFEA7E4004BCF51 /* CardExpiryDateTextField.swift in Sources */,
758801202BAAC70200F02934 /* URL+Helpers.swift in Sources */,
756C8F262A40698600D53059 /* TextFieldView.swift in Sources */,
986406341CFEA7E4004BCF51 /* CardNameTextField.swift in Sources */,
8AF9857121479F38007E18ED /* CCVInfoController.swift in Sources */,
Expand Down

0 comments on commit 83cee97

Please sign in to comment.