Skip to content

Commit

Permalink
Solve issue on generating production and sandbox urls
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarSwanros committed Jan 18, 2016
1 parent 1494452 commit c1f89cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 12 additions & 10 deletions SwiftyOpenPay/SwiftyOpenPay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

public let OpenPayErrorDomain = "com.openpay.ios.lib"

private let api_url = "https://api.openpay.mx/"
private let sandbox_api_url = "https://sandbox-api.openpay.mx/"
private let api_version = "v1"

extension SwiftyOpenPay.Error: CustomStringConvertible {
public var description: String {
switch self {
Expand All @@ -21,6 +17,18 @@ extension SwiftyOpenPay.Error: CustomStringConvertible {
}

public struct SwiftyOpenPay {
// MARK: - Internal declarations
internal static let api_url = "https://api.openpay.mx/"
internal static let sandbox_api_url = "https://sandbox-api.openpay.mx/"
internal static let api_version = "v1"

internal var URLBase: String {
let base = sandboxMode ? SwiftyOpenPay.sandbox_api_url : SwiftyOpenPay.api_url

return base + SwiftyOpenPay.api_version + "/" + merchantId + "/"
}


// MARK: - SwiftyOpenPay supporting data types
public struct Configuration {
public let merchantId: String
Expand Down Expand Up @@ -56,12 +64,6 @@ public struct SwiftyOpenPay {
// MARK: - Private Properties
private static let internalQueue = OperationQueue()

private var URLBase: String {
let base = sandboxMode ? api_url : sandbox_api_url

return base + api_version + "/" + merchantId + "/"
}


// MARK: - Public Properties
public let merchantId: String
Expand Down
18 changes: 17 additions & 1 deletion SwiftyOpenPayTests/SwiftyOpenPayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SwiftyOpenPaytests: XCTestCase {
]
]

guard let token = buildToken(Token.self, data: response) else {
guard let _ = buildToken(Token.self, data: response) else {
assertionFailure("Could not build token")
return
}
Expand All @@ -41,4 +41,20 @@ class SwiftyOpenPaytests: XCTestCase {
func buildToken<T: JSONParselable>(type: T.Type, data: [String:AnyObject]) -> T? {
return T.withData(data)
}

func testSandboxMode() {
let op = SwiftyOpenPay(merchantId: "MyMerchantId", apiKey: "MyAPIKey", sandboxMode: true)

let sandboxUrl = SwiftyOpenPay.sandbox_api_url + SwiftyOpenPay.api_version + "/" + op.merchantId + "/"

assert(sandboxUrl == op.URLBase)
}

func testProdMode() {
let op = SwiftyOpenPay(merchantId: "MyMerchantId", apiKey: "MyAPIKey")

let productionUrl = SwiftyOpenPay.api_url + SwiftyOpenPay.api_version + "/" + op.merchantId + "/"

assert(productionUrl == op.URLBase)
}
}

0 comments on commit c1f89cb

Please sign in to comment.