Skip to content

Commit

Permalink
fix: support connection options value dictionary (#15)
Browse files Browse the repository at this point in the history
* Support dictionary value in connectionOptions

* Bump version to 2.4.1

* Add tests
  • Loading branch information
luca-gr4vy authored Nov 18, 2024
1 parent 9c297ce commit 1e8fdab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions gr4vy-iOS/Models/Gr4vyConnectionOptionsValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum Gr4vyConnectionOptionsValue: Codable, Equatable {
case int(Int)
case bool(Bool)
case double(Double)
case dictionary([String: String])

private enum CodingError: Error {
case unknownValue
Expand All @@ -26,6 +27,8 @@ public enum Gr4vyConnectionOptionsValue: Codable, Equatable {
self = .double(doubleValue)
} else if let stringValue = try? container.decode(String.self) {
self = .string(stringValue)
} else if let dictionaryValue = try? container.decode([String: String].self) {
self = .dictionary(dictionaryValue)
} else {
throw CodingError.unknownValue
}
Expand All @@ -42,6 +45,8 @@ public enum Gr4vyConnectionOptionsValue: Codable, Equatable {
try container.encode(value)
case .double(let value):
try container.encode(value)
case .dictionary(let value):
try container.encode(value)
}
}
}
13 changes: 11 additions & 2 deletions gr4vy-iOSTests/gr4vy_iOSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ class gr4vy_iOSTests: XCTestCase {
["key1": ["subKey1": .string("value1")], "key2": ["subKey1": .bool(true)]],
["key1": ["subKey1": .int(1)], "key2": ["subKey1": .bool(true)]],
["key1": ["subKey1": .string("value1"), "subKey2": .int(1)], "key2": ["subKey1": .int(1), "subKey2": .bool(true)]],
["key1": ["subKey1": .string("value1"), "subKey2": .bool(true)], "key2": ["subKey1": .int(1), "subKey2": .string("value2")]]
["key1": ["subKey1": .string("value1"), "subKey2": .bool(true)], "key2": ["subKey1": .int(1), "subKey2": .string("value2")]],
["key1": ["subKey1": .dictionary(["key1": "value1"])]],
]

for permutation in permutations {
Expand Down Expand Up @@ -939,6 +940,13 @@ class gr4vy_iOSTests: XCTestCase {

XCTAssertEqual("window.postMessage({ \"channel\": 123, \"type\": \"updateOptions\", \"data\": {\"amount\":100,\"apiHost\":\"api.ID123.gr4vy.app\",\"apiUrl\":\"https:\\/\\/api.ID123.gr4vy.app\",\"buyerId\":\"BUYER123\",\"connectionOptions\":{\"cybersource-anti-fraud\":{\"merchant_defined_data\":\"value\"}},\"country\":\"GB\",\"currency\":\"GBP\",\"supportedApplePayVersion\":0,\"token\":\"TOKEN123\"}})", sut)

setup.connectionOptions = [
"cybersource-anti-fraud": ["merchant_defined_data": .dictionary(["key": "value"])]
]
sut = Gr4vyUtility.generateUpdateOptions(from: setup)

XCTAssertEqual("window.postMessage({ \"channel\": 123, \"type\": \"updateOptions\", \"data\": {\"amount\":100,\"apiHost\":\"api.ID123.gr4vy.app\",\"apiUrl\":\"https:\\/\\/api.ID123.gr4vy.app\",\"buyerId\":\"BUYER123\",\"connectionOptions\":{\"cybersource-anti-fraud\":{\"merchant_defined_data\":{\"key\":\"value\"}}},\"country\":\"GB\",\"currency\":\"GBP\",\"supportedApplePayVersion\":0,\"token\":\"TOKEN123\"}})", sut)

setup.connectionOptions = [:]
sut = Gr4vyUtility.generateUpdateOptions(from: setup)
XCTAssertEqual("window.postMessage({ \"channel\": 123, \"type\": \"updateOptions\", \"data\": {\"amount\":100,\"apiHost\":\"api.ID123.gr4vy.app\",\"apiUrl\":\"https:\\/\\/api.ID123.gr4vy.app\",\"buyerId\":\"BUYER123\",\"connectionOptions\":{},\"country\":\"GB\",\"currency\":\"GBP\",\"supportedApplePayVersion\":0,\"token\":\"TOKEN123\"}})", sut)
Expand All @@ -958,7 +966,8 @@ class gr4vy_iOSTests: XCTestCase {
["key1": ["subKey1": .string("value1")], "key2": ["subKey1": .bool(true)]],
["key1": ["subKey1": .int(1)], "key2": ["subKey1": .bool(true)]],
["key1": ["subKey1": .string("value1"), "subKey2": .int(1)], "key2": ["subKey1": .int(1), "subKey2": .bool(true)]],
["key1": ["subKey1": .string("value1"), "subKey2": .bool(true)], "key2": ["subKey1": .int(1), "subKey2": .string("value2")]]
["key1": ["subKey1": .string("value1"), "subKey2": .bool(true)], "key2": ["subKey1": .int(1), "subKey2": .string("value2")]],
["key1": ["subKey1": .dictionary(["key1": "value1"])]],
]

for permutation in permutations {
Expand Down
2 changes: 1 addition & 1 deletion gr4vy-ios.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'gr4vy-ios'
s.version = '2.4.0'
s.version = '2.4.1'
s.license = 'MIT'
s.summary = 'Quickly embed Gr4vy in your iOS app to store card details, authorize payments, and capture a transaction.'
s.homepage = 'https://github.com/gr4vy/gr4vy-ios'
Expand Down

0 comments on commit 1e8fdab

Please sign in to comment.