Skip to content

Commit

Permalink
Define RedirectType
Browse files Browse the repository at this point in the history
  • Loading branch information
nauaros committed Oct 31, 2024
1 parent 6e5b94b commit 45525dc
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions AdyenActions/Actions/RedirectAction.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2022 Adyen N.V.
// Copyright (c) 2019 Adyen N.V.
//
// This file is open source and available under the MIT license. See the LICENSE file for more info.
//
Expand All @@ -8,25 +8,64 @@ import Foundation

/// Describes an action in which the user is redirected to a URL.
public struct RedirectAction: Decodable {


/// Defines the type of redirect flow utilized by the `RedirectAction` object.
public enum RedirectType: String, Decodable {
case redirect
case nativeRedirect

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let type = try container.decode(String.self)
self = RedirectType(rawValue: type) ?? .redirect
}
}

/// The URL to which to redirect the user.
public let url: URL

/// 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?

/// Initializes a redirect action.
///
/// - Parameters:
/// - url: The URL to which to redirect the user.
/// - paymentData: The server-generated payment data that should be submitted to the `/payments/details` endpoint.
/// - nativeRedirectData: Native redirect data.
public init(url: URL, paymentData: String?, nativeRedirectData: String? = nil) {
/// - type: The redirect flow used by the action. Defaults to `redirect`.
/// - nativeRedirectData: Native redirect data. Defaults to `nil`.
public init(
url: URL,
paymentData: String?,
type: RedirectType = .redirect,
nativeRedirectData: String? = nil
) {
self.url = url
self.paymentData = paymentData
self.type = type
self.nativeRedirectData = nativeRedirectData
}

public 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)
}

// MARK: - Private

private enum CodingKeys: CodingKey {
case url
case paymentData
case type
case nativeRedirectData
}
}

0 comments on commit 45525dc

Please sign in to comment.