Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add enum for specifying directory server ID #40

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Primer3DS (2.0.2)
- Primer3DS (2.2.1)

DEPENDENCIES:
- Primer3DS (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Primer3DS: 9838b509513dcdb41a8b2e5023dfabe3dae761c9
Primer3DS: d4e8e60b34b695be117737640f05583513efb17d

PODFILE CHECKSUM: 4f7264ca4fa7ed996327b5aeee4d7cddd56cebe7

Expand Down
2 changes: 1 addition & 1 deletion Example/Primer3DS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ViewController: UIViewController {

do {
try primer3DS.initializeSDK(apiKey: "")
_ = try primer3DS.createTransaction(directoryServerId: "", supportedThreeDsProtocolVersions: [""])
_ = try primer3DS.createTransaction(directoryServerNetwork: .visa, supportedThreeDsProtocolVersions: [""])

primer3DS.performChallenge(threeDSAuthData: ThreeDSAuth(),
threeDsAppRequestorUrl: URL(string: ""),
Expand Down
5 changes: 2 additions & 3 deletions Example/Tests/Primer3DSChallengeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ final class Primer3DSChallengeTests: XCTestCase {
}

private func setupSDK(withTransaction transaction: Transaction? = nil) throws {
let directoryServerId = "DirectoryServerId"
let protocolVersion = "2.2"

if let transaction = transaction {
sdkProvider.transactions = ["\(directoryServerId):\(protocolVersion)": transaction]
sdkProvider.transactions = ["\(DirectoryServerNetwork.visa.directoryServerId!):\(protocolVersion)": transaction]
}

try primer3DS.initializeSDK(apiKey: "ApiKey")
_ = try primer3DS.createTransaction(directoryServerId: directoryServerId, supportedThreeDsProtocolVersions: [protocolVersion])
_ = try primer3DS.createTransaction(directoryServerNetwork: .visa, supportedThreeDsProtocolVersions: [protocolVersion])
}

}
8 changes: 3 additions & 5 deletions Example/Tests/Primer3DSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ final class Primer3DSTests: XCTestCase {
}

func testSDKCreateTransaction_Success() throws {

let directoryServerId = "DirectoryServerId"
let protocolVersion = "2.2"

let transaction = MockTransaction()
sdkProvider.transactions = ["\(directoryServerId):\(protocolVersion)": transaction]
sdkProvider.transactions = ["\(DirectoryServerNetwork.visa.directoryServerId!):\(protocolVersion)": transaction]

try primer3DS.initializeSDK(apiKey: "ApiKey")
let authResult = try primer3DS.createTransaction(directoryServerId: directoryServerId, supportedThreeDsProtocolVersions: [protocolVersion])
let authResult = try primer3DS.createTransaction(directoryServerNetwork: .visa, supportedThreeDsProtocolVersions: [protocolVersion])

XCTAssertEqual(authResult.authData.sdkAppId, transaction.mockAuthRequestParameters.getSDKAppID())
XCTAssertEqual(authResult.authData.sdkTransactionId, transaction.mockAuthRequestParameters.getSDKTransactionId())
Expand Down
24 changes: 20 additions & 4 deletions Sources/Primer3DS/Classes/Primer3DS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,23 @@ public class Primer3DS: NSObject, Primer3DSProtocol {
}
}

public func createTransaction(directoryServerId: String,
var fallbackDirectoryServerId: String? {
guard environment != .production else {
return nil
}
return "A999999999"
}

public func createTransaction(directoryServerNetwork: DirectoryServerNetwork,
supportedThreeDsProtocolVersions: [String]) throws -> SDKAuthResult {
guard let directoryServerId = directoryServerNetwork.directoryServerId ?? fallbackDirectoryServerId else {
throw Primer3DSError.missingDsRid(cardNetwork: directoryServerNetwork.rawValue)
}
guard let maxSupportedThreeDsProtocolVersion = getMaxValidSupportedThreeDSVersion(supportedThreeDsProtocolVersions) else {
let err = Primer3DSError.unsupportedProtocolVersion(supportedProtocols: supportedThreeDsProtocolVersions)
throw err
}

do {
transaction = try sdkProvider.createTransaction(
directoryServerId: directoryServerId,
Expand All @@ -119,8 +129,14 @@ public class Primer3DS: NSObject, Primer3DSProtocol {
maxSupportedThreeDsProtocolVersion: maxSupportedThreeDsProtocolVersion)

} catch let error {
let err = Primer3DSError.failedToCreateTransaction(error: error)
throw err
if let error = error as? Primer3DSError {
throw error
}
if let error = error as? NSError {
let err = Primer3DSError.failedToCreateTransaction(error: error)
throw err
}
throw Primer3DSError.unknown(description: "Transaction failed but an invalid error was provided")
}
}

Expand Down
5 changes: 2 additions & 3 deletions Sources/Primer3DS/Classes/Primer3DSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ public enum Primer3DSError: CustomNSError, LocalizedError {
}

public var errorUserInfo: [String : Any] {
guard let underlyingError = underlyingError else { return [:] }
let nsErr = underlyingError as NSError
return nsErr.userInfo
guard let underlyingError = underlyingError as? NSError else { return [:] }
return underlyingError.userInfo
}

public var threeDsErrorCode: Int? {
Expand Down
17 changes: 9 additions & 8 deletions Sources/Primer3DS/Classes/Primer3DSProtocols.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Foundation
import UIKit

@objc public protocol Primer3DSProtocol {
@objc func initializeSDK(apiKey: String, certificates: [Primer3DSCertificate]?) throws
@objc func createTransaction(directoryServerId: String,
supportedThreeDsProtocolVersions: [String]) throws -> SDKAuthResult
@objc func performChallenge(threeDSAuthData: Primer3DSServerAuthData,
threeDsAppRequestorUrl: URL?,
presentOn viewController: UIViewController,
completion: @escaping (Primer3DSCompletion?, Error?) -> Void)
public protocol Primer3DSProtocol {
func initializeSDK(apiKey: String,
certificates: [Primer3DSCertificate]?) throws
func createTransaction(directoryServerNetwork: DirectoryServerNetwork,
supportedThreeDsProtocolVersions: [String]) throws -> SDKAuthResult
func performChallenge(threeDSAuthData: Primer3DSServerAuthData,
threeDsAppRequestorUrl: URL?,
presentOn viewController: UIViewController,
completion: @escaping (Primer3DSCompletion?, Error?) -> Void)
}

@objc public protocol Primer3DSCertificate {
Expand Down
35 changes: 35 additions & 0 deletions Sources/Primer3DS/Classes/Primer3DSStructures.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import Foundation
import ThreeDS_SDK

// Exposed structures

Expand All @@ -10,6 +11,40 @@ public enum Environment: String, Codable {
case local = "LOCAL"
}

public enum DirectoryServerNetwork: String {
case masterCard = "MASTERCARD"
case visa = "VISA"
case amex = "AMEX"
case jcb = "JCB"
case diners = "DINERS_CLUB"
case discover = "DISCOVER"
case unionpay = "UNIONPAY"
case unknown = "UNKNOWN"

var directoryServerId: String? {
switch self {
case .visa:
DsRidValues.visa
case .masterCard:
DsRidValues.mastercard
case .amex:
DsRidValues.amex
case .jcb:
DsRidValues.jcb
case .diners, .discover:
DsRidValues.diners
case .unionpay:
DsRidValues.union
case .unknown:
nil
}
}

public static func from(cardNetworkIdentifier: String) -> Self {
Self(rawValue: cardNetworkIdentifier) ?? .unknown
}
}

@objc internal class SDKAuthData: NSObject, Primer3DSSDKGeneratedAuthData {

var sdkAppId: String
Expand Down
Loading