Skip to content

Commit

Permalink
Update Authorized controller interface and add Custom Scheme to Examp…
Browse files Browse the repository at this point in the history
…le App to handle deeplinking
  • Loading branch information
Andrei Solovev committed Mar 20, 2024
1 parent bd49cff commit 39ab641
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
13 changes: 13 additions & 0 deletions ExampleApp/AppDeeplink.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

enum AppDeeplink: String {
case threeDSChallenge = "omise_3ds_challenge"

var scheme: String {
"omiseExampleApp"
}

var url: URL? {
URL(string: "\(scheme)://\(rawValue)")
}
}
28 changes: 28 additions & 0 deletions ExampleApp/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UIKit
import OmiseSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -9,4 +10,31 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

return true
}

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
// Determine who sent the URL.
print("App open url '\(url)'")

let sendingAppID = options[.sourceApplication]
print("source application = \(sendingAppID ?? "Unknown")")

// Process the URL.
guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true) else {
print("Invalid url")
return false
}

switch components.host {
case AppDeeplink.threeDSChallenge.rawValue:
print("Omise 3DS Challenge Callback")
let result = OmiseSDK.shared.handleURLCallback(url)
if result {
OmiseSDK.shared.dismiss()
}
return result
default:
print("Unknown deeplink params \(url.host ?? "nil")")
return false
}
}
}
13 changes: 13 additions & 0 deletions ExampleApp/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,18 @@
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>co.omise.ExampleApp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>omiseExampleApp</string>
</array>
</dict>
</array>
</dict>
</plist>
7 changes: 4 additions & 3 deletions ExampleApp/Views/ProductDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ class ProductDetailViewController: BaseViewController {
let textField = alertController.textFields?.first,
let text = textField.text,
let url = URL(string: text),
let expectedReturnURL = URLComponents(string: "https://opn.ooo/") else { return }
let deeplinkRedirectURL = AppDeeplink.threeDSChallenge.url,
let webRedirectURL = URL(string: "https://exampleapp.opn.ooo") else { return }

self.omiseSDK.presentAuthorizingPayment(
from: self,
authorizeURL: url,
expectedReturnURLPatterns: [expectedReturnURL],
returnURL: [deeplinkRedirectURL, webRedirectURL],
delegate: self
)
})
Expand All @@ -72,7 +73,7 @@ class ProductDetailViewController: BaseViewController {

extension ProductDetailViewController: AuthorizingPaymentViewControllerDelegate {
func authorizingPaymentViewController(_ viewController: AuthorizingPaymentViewController, didCompleteAuthorizingPaymentWithRedirectedURL redirectedURL: URL) {
print(redirectedURL)
print("Payment is authorized with redirect url `\(redirectedURL)`")
omiseSDK.dismiss()
}

Expand Down
17 changes: 15 additions & 2 deletions OmiseSDK/Sources/OmiseSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class OmiseSDK {

public private(set) weak var presentedViewController: UIViewController?

private var handleURLs: [URL] = []

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

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

let viewController = AuthorizingPaymentViewController(nibName: nil, bundle: .omiseSDK)
viewController.authorizeURL = authorizeURL
viewController.expectedReturnURLPatterns = expectedReturnURLPatterns
viewController.expectedReturnURLPatterns = returnURLComponents
viewController.delegate = delegate
viewController.applyNavigationBarStyle()
viewController.title = "AuthorizingPayment.title".localized()
Expand All @@ -194,6 +201,12 @@ public class OmiseSDK {
guard let presentedViewController = presentedViewController else { return }
presentedViewController.dismiss(animated: animated, completion: completion)
}

/// Handle URL Callback received by AppDelegate
public func handleURLCallback(_ url: URL) -> Bool {
// Will handle callback with 3ds library
return handleURLs.contains(url)
}
}

private extension OmiseSDK {
Expand Down
4 changes: 4 additions & 0 deletions dev.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
756C8F262A40698600D53059 /* TextFieldView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756C8F252A40698600D53059 /* TextFieldView.swift */; };
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 */; };
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 @@ -333,6 +334,7 @@
756C8F252A40698600D53059 /* TextFieldView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldView.swift; sourceTree = "<group>"; };
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>"; };
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 @@ -531,6 +533,7 @@
75DAD8922A0BC9540098AF96 /* Config.local.plist */,
22D480AB1D0EB29C00544CE1 /* Info.plist */,
229E14541D0EBAB5000511DE /* AppDelegate.swift */,
7588011D2BAAB37C00F02934 /* AppDeeplink.swift */,
8A723A672164D80200D902F5 /* Views */,
8AB3B85F216DCF5C006497B5 /* Models */,
);
Expand Down Expand Up @@ -1480,6 +1483,7 @@
buildActionMask = 2147483647;
files = (
8A40DA29216B441E00749F45 /* PaymentSettingTableViewController.swift in Sources */,
7588011E2BAAB37C00F02934 /* AppDeeplink.swift in Sources */,
229E14581D0EBAB5000511DE /* AppDelegate.swift in Sources */,
8A40DA2B216B53E900749F45 /* Tools.swift in Sources */,
229E145A1D0EBAB5000511DE /* ProductDetailViewController.swift in Sources */,
Expand Down

0 comments on commit 39ab641

Please sign in to comment.