From 1a3c809aeef2dabc59c406ce0b305c54237e004d Mon Sep 17 00:00:00 2001 From: Naufal Aros Date: Tue, 29 Oct 2024 12:10:49 +0100 Subject: [PATCH] Expose redirect type --- AdyenActions/Actions/RedirectAction.swift | 31 +++++++++++++------ .../Redirect/RedirectComponent.swift | 5 +-- .../Redirect/RedirectComponentTests.swift | 4 +++ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/AdyenActions/Actions/RedirectAction.swift b/AdyenActions/Actions/RedirectAction.swift index f4e5938b37..604745eecb 100644 --- a/AdyenActions/Actions/RedirectAction.swift +++ b/AdyenActions/Actions/RedirectAction.swift @@ -9,8 +9,20 @@ import Foundation /// Describes an action in which the user is redirected to a URL. public class RedirectAction: Decodable { - private enum Constants { - static let nativeRedirectType = "nativeRedirect" + public enum RedirectType: Decodable { + case redirect + case nativeRedirect + + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let type = try? container.decode(String.self) + switch type { + case "nativeRedirect": self = .nativeRedirect + case "redirect": self = .redirect + default: + self = .redirect + } + } } /// The URL to which to redirect the user. @@ -19,11 +31,12 @@ public class RedirectAction: Decodable { /// The server-generated payment data that should be submitted to the `/payments/details` endpoint. public let paymentData: String? + /// Redirect type + public let type: RedirectType + /// Native redirect data. public let nativeRedirectData: String? - internal let isNaviteRedirect: Bool - /// Initializes a redirect action. /// /// - Parameters: @@ -33,23 +46,21 @@ public class RedirectAction: Decodable { public init( url: URL, paymentData: String?, + type: RedirectType = .redirect, nativeRedirectData: String? = nil ) { self.url = url self.paymentData = paymentData + self.type = type self.nativeRedirectData = nativeRedirectData - self.isNaviteRedirect = nativeRedirectData != nil - } public required init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.url = try container.decode(URL.self, forKey: .url) self.paymentData = try container.decodeIfPresent(String.self, forKey: .paymentData) + self.type = try container.decode(RedirectType.self, forKey: .type) self.nativeRedirectData = try container.decodeIfPresent(String.self, forKey: .nativeRedirectData) - - let redirectType = try container.decode(String.self, forKey: .type) - self.isNaviteRedirect = redirectType == Constants.nativeRedirectType } // MARK: - Private @@ -57,7 +68,7 @@ public class RedirectAction: Decodable { private enum CodingKeys: CodingKey { case url case paymentData - case nativeRedirectData case type + case nativeRedirectData } } diff --git a/AdyenActions/Components/Redirect/RedirectComponent.swift b/AdyenActions/Components/Redirect/RedirectComponent.swift index dfc4e23a68..ff583de308 100644 --- a/AdyenActions/Components/Redirect/RedirectComponent.swift +++ b/AdyenActions/Components/Redirect/RedirectComponent.swift @@ -172,13 +172,14 @@ public final class RedirectComponent: ActionComponent { } private func didOpen(url returnURL: URL, _ action: RedirectAction) throws { - if action.isNaviteRedirect { + switch action.type { + case .nativeRedirect: try handleNativeMobileRedirect( withReturnURL: returnURL, redirectStateData: action.nativeRedirectData, action ) - } else { + case .redirect: try notifyDelegateDidProvide(redirectDetails: RedirectDetails(returnURL: returnURL), action) } } diff --git a/Tests/IntegrationTests/Actions Tests/Redirect/RedirectComponentTests.swift b/Tests/IntegrationTests/Actions Tests/Redirect/RedirectComponentTests.swift index b6d7efd516..e2eecce1db 100644 --- a/Tests/IntegrationTests/Actions Tests/Redirect/RedirectComponentTests.swift +++ b/Tests/IntegrationTests/Actions Tests/Redirect/RedirectComponentTests.swift @@ -286,6 +286,7 @@ class RedirectComponentTests: XCTestCase { let action = RedirectAction( url: URL(string: "https://google.com")!, paymentData: nil, + type: .nativeRedirect, nativeRedirectData: "test_nativeRedirectData" ) sut.handle(action) @@ -318,6 +319,7 @@ class RedirectComponentTests: XCTestCase { let action = RedirectAction( url: URL(string: "https://google.com")!, paymentData: nil, + type: .nativeRedirect, nativeRedirectData: "test_nativeRedirectData" ) sut.handle(action) @@ -354,6 +356,7 @@ class RedirectComponentTests: XCTestCase { let action = RedirectAction( url: URL(string: "https://google.com")!, paymentData: nil, + type: .nativeRedirect, nativeRedirectData: "test_nativeRedirectData" ) sut.handle(action) @@ -391,6 +394,7 @@ class RedirectComponentTests: XCTestCase { let action = RedirectAction( url: URL(string: "https://google.com")!, paymentData: nil, + type: .nativeRedirect, nativeRedirectData: nil ) sut.handle(action)