Skip to content

Commit

Permalink
Expose redirect type
Browse files Browse the repository at this point in the history
  • Loading branch information
nauaros committed Oct 29, 2024
1 parent c5756a8 commit 1a3c809
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
31 changes: 21 additions & 10 deletions AdyenActions/Actions/RedirectAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -33,31 +46,29 @@ 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

private enum CodingKeys: CodingKey {
case url
case paymentData
case nativeRedirectData
case type
case nativeRedirectData
}
}
5 changes: 3 additions & 2 deletions AdyenActions/Components/Redirect/RedirectComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1a3c809

Please sign in to comment.