Skip to content

Commit

Permalink
Synced with develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Solovev committed Jun 17, 2024
2 parents ae0279a + e92bcac commit c7d65b8
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 44 deletions.
62 changes: 36 additions & 26 deletions OmiseSDK/Sources/Models/OmiseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -500,27 +500,9 @@ extension OmiseError.APIErrorCode.BadRequestReason: Decodable {
static let nameIsTooLong: NSRegularExpression! = try? NSRegularExpression(pattern: "name is too long \\(maximum is ([\\d]+) characters\\)", options: [])
}

// swiftlint:disable:next cyclomatic_complexity
init(message: String, currency: Currency?) throws {
if message.hasPrefix("amount must be ") {
if let lessThanValidAmountMatch = ErrorMessageRegularExpression.amountLessThanValidAmount
.firstMatch(in: message, range: NSRange(message.startIndex..<message.endIndex, in: message)),
lessThanValidAmountMatch.numberOfRanges == 2,
let amountRange = Range(lessThanValidAmountMatch.range(at: 1), in: message) {
self = .amountIsLessThanValidAmount(validAmount: Int64(message[amountRange]), currency: currency)
} else if let greaterThanValidAmountMatch = ErrorMessageRegularExpression.amountGreaterThanValidAmount
.firstMatch(in: message, range: NSRange(message.startIndex..<message.endIndex, in: message)),
greaterThanValidAmountMatch.numberOfRanges == 2,
let amountRange = Range(greaterThanValidAmountMatch.range(at: 1), in: message) {
self = .amountIsGreaterThanValidAmount(validAmount: Int64(message[amountRange]), currency: currency)
} else if let atLeastValidAmountMatch = ErrorMessageRegularExpression.amountAtLeastValidAmount
.firstMatch(in: message, range: NSRange(message.startIndex..<message.endIndex, in: message)),
atLeastValidAmountMatch.numberOfRanges == 2,
let amountRange = Range(atLeastValidAmountMatch.range(at: 1), in: message) {
self = .amountIsLessThanValidAmount(validAmount: Int64(message[amountRange]), currency: currency)
} else {
self = .other(message)
}
self = Self.processAmountMessage(message: message, currency: currency)
} else if message.contains("currency must be") {
self = .invalidCurrency
} else if message.contains("type") {
Expand All @@ -529,12 +511,8 @@ extension OmiseError.APIErrorCode.BadRequestReason: Decodable {
self = .currencyNotSupported
} else if message.contains("name") && message.contains("blank") {
self = .emptyName
} else if message.hasPrefix("name is too long"),
let lessThanValidAmountMatch = ErrorMessageRegularExpression.nameIsTooLong
.firstMatch(in: message, range: NSRange(message.startIndex..<message.endIndex, in: message)),
lessThanValidAmountMatch.numberOfRanges == 2,
let amountRange = Range(lessThanValidAmountMatch.range(at: 1), in: message) {
self = .nameIsTooLong(maximum: Int(message[amountRange]))
} else if message.hasPrefix("name is too long"), let reason = Self.processNameTooLongMessage(message: message) {
self = reason
} else if message.contains("name") {
self = .nameIsTooLong(maximum: nil)
} else if message.contains("email") {
Expand All @@ -545,7 +523,39 @@ extension OmiseError.APIErrorCode.BadRequestReason: Decodable {
self = .other(message)
}
}


private static func processAmountMessage(message: String, currency: Currency?) -> Self {
if let lessThanValidAmountMatch = ErrorMessageRegularExpression.amountLessThanValidAmount
.firstMatch(in: message, range: NSRange(message.startIndex..<message.endIndex, in: message)),
lessThanValidAmountMatch.numberOfRanges == 2,
let amountRange = Range(lessThanValidAmountMatch.range(at: 1), in: message) {
return .amountIsLessThanValidAmount(validAmount: Int64(message[amountRange]), currency: currency)
} else if let greaterThanValidAmountMatch = ErrorMessageRegularExpression.amountGreaterThanValidAmount
.firstMatch(in: message, range: NSRange(message.startIndex..<message.endIndex, in: message)),
greaterThanValidAmountMatch.numberOfRanges == 2,
let amountRange = Range(greaterThanValidAmountMatch.range(at: 1), in: message) {
return .amountIsGreaterThanValidAmount(validAmount: Int64(message[amountRange]), currency: currency)
} else if let atLeastValidAmountMatch = ErrorMessageRegularExpression.amountAtLeastValidAmount
.firstMatch(in: message, range: NSRange(message.startIndex..<message.endIndex, in: message)),
atLeastValidAmountMatch.numberOfRanges == 2,
let amountRange = Range(atLeastValidAmountMatch.range(at: 1), in: message) {
return .amountIsLessThanValidAmount(validAmount: Int64(message[amountRange]), currency: currency)
} else {
return .other(message)
}
}

private static func processNameTooLongMessage(message: String) -> Self? {
let matchRange = NSRange(message.startIndex..<message.endIndex, in: message)
guard let match = ErrorMessageRegularExpression.nameIsTooLong.firstMatch(in: message, range: matchRange),
match.numberOfRanges == 2,
let range = Range(match.range(at: 1), in: message) else {
return nil
}

return .nameIsTooLong(maximum: Int(message[range]))
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

Expand Down
6 changes: 4 additions & 2 deletions OmiseSDK/Sources/OmiseSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class OmiseSDK {
public static var shared = OmiseSDK(publicKey: "pkey_")

/// OmiseSDK version
public let version: String = "5.2.0"
public let version: String = "5.2.1"

/// Public Key associated with this instance of OmiseSDK
public let publicKey: String
Expand Down Expand Up @@ -219,7 +219,9 @@ public class OmiseSDK {

private extension OmiseSDK {
private func preloadCapabilityAPI() {
client.capability { _ in }
client.capability { _ in
// Preload capability and auto cache it as client.latestLoadedCapability
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions OmiseSDK/Sources/Views/Components/TextFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class TextFieldView: UIView {
}()

var onTextFieldShouldReturn: () -> (Bool) = { return false }
var onTextChanged: () -> Void = { }
var onBeginEditing: () -> Void = { }
var onEndEditing: () -> Void = { }
var onTextChanged: () -> Void = { /* Non-optional default empty implementation */ }
var onBeginEditing: () -> Void = { /* Non-optional default empty implementation */ }
var onEndEditing: () -> Void = { /* Non-optional default empty implementation */ }

// swiftlint:disable attributes
@ProxyProperty(\TextFieldView.textField.keyboardType) var keyboardType: UIKeyboardType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class OmiseTextField: UITextField {
}

public var onTextFieldShouldReturn: () -> (Bool) = { return false }
public var onValueChanged: () -> Void = { }
public var onValueChanged: () -> Void = { /* Non-optional default empty implementation */ }

@IBInspectable var borderWidth: CGFloat {
get {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AtomePaymentFormViewModel: AtomePaymentFormViewModelProtocol, CountryListV
}
}

var onSelectCountry: (Country) -> Void = { _ in }
var onSelectCountry: (Country) -> Void = { _ in /* Non-optional default empty implementation */ }

var countryListViewModel: CountryListViewModelProtocol { return self }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import UIKit

class PaymentFormController: UIViewController {
var onSubmitButtonTappedClosure: () -> Void = { }
var onSubmitButtonTappedClosure: () -> Void = { /* Non-optional default empty implementation */ }

@ProxyProperty(\PaymentFormController.headerTextLabel.text)
var details: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class FPXPaymentFormController: UIViewController, PaymentFormUIController {

@IBAction private func submitForm(_ sender: AnyObject) {
emailValue = emailTextField.text?.trimmingCharacters(in: CharacterSet.whitespaces)
delegate?.fpxDidCompleteWith(email: emailValue) {}
delegate?.fpxDidCompleteWith(email: emailValue) { /* no action is required */ }
}

@IBAction private func validateFieldData(_ textField: OmiseTextField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class SelectPaymentController: UITableViewController {

let viewModel: SelectPaymentPresentableProtocol

var customizeCellAtIndexPathClosure: (UITableViewCell, IndexPath) -> Void = { _, _ in }
var customizeCellAtIndexPathClosure: (UITableViewCell, IndexPath) -> Void = { _, _ in
// Non-optional default empty implementation
}

init(viewModel: SelectPaymentPresentableProtocol) {
self.viewModel = viewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ extension SelectPaymentPresentableProtocol {
var errorMessage: String? { nil }
var viewShowsCloseButton: Bool { false }
var viewDisplayLargeTitle: Bool { false }
func viewDidTapClose() {}
func viewDidTapClose() { /* Default empty implementation */ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import UIKit
class SelectDuitNowOBWBankViewModel {
private weak var delegate: SelectSourcePaymentDelegate?

private var viewOnDataReloadHandler: () -> Void = { } {
private var viewOnDataReloadHandler: () -> Void = { /* Non-optional default empty implementation */ } {
didSet {
self.viewOnDataReloadHandler()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SelectFPXBankViewModel {

let errorMessage: String?

private var viewOnDataReloadHandler: () -> Void = { } {
private var viewOnDataReloadHandler: () -> Void = { /* Non-optional default empty implementation */ } {
didSet {
self.viewOnDataReloadHandler()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SelectInstallmentTermsViewModel {
weak var delegate: SelectSourcePaymentDelegate?
let sourceType: SourceType

private var viewOnDataReloadHandler: () -> Void = { } {
private var viewOnDataReloadHandler: () -> Void = { /* Non-optional default empty implementation */ } {
didSet {
self.viewOnDataReloadHandler()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SelectPaymentMethodViewModel {
private let filter: Filter
private weak var delegate: SelectPaymentMethodDelegate?

private var viewOnDataReloadHandler: () -> Void = { } {
private var viewOnDataReloadHandler: () -> Void = { /* Non-optional default empty implementation */ } {
didSet {
self.viewOnDataReloadHandler()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SelectSourceTypePaymentViewModel {
private weak var delegate: SelectSourceTypeDelegate?
private let title: String

private var viewOnDataReloadHandler: () -> Void = { } {
private var viewOnDataReloadHandler: () -> Void = { /* Non-optional default empty implementation */ } {
didSet {
self.viewOnDataReloadHandler()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CCVInfoController: UIViewController {
}
}

var onCloseTapped: () -> Void = { }
var onCloseTapped: () -> Void = { /* Non-optional default empty implementation */ }

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class CreditCardPaymentViewModel: CreditCardPaymentViewModelProtocol, CountryLis
}
}
}
var onSelectCountry: (Country) -> Void = { _ in }

var onSelectCountry: (Country) -> Void = { _ in /* Non-optional default empty implementation */ }

func error(for field: AddressField, validate text: String?) -> String? {
guard isAddressFieldsVisible else { return nil }
Expand Down
32 changes: 32 additions & 0 deletions OmiseSDKTests/OmiseErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import XCTest
@testable import OmiseSDK

class OmiseErrorTests: XCTestCase {

func testBadRequestReason() throws {
let currencies: [Currency?] = [nil, .jpy, .myr, .thb, .usd]
for currency in currencies {
let expectedResults: [String: OmiseError.APIErrorCode.BadRequestReason] = [
"amount must be at least 500": .amountIsLessThanValidAmount(validAmount: 500, currency: currency),
"amount must be less than 500": .amountIsGreaterThanValidAmount(validAmount: 500, currency: currency),
"amount must be greater than 500": .amountIsLessThanValidAmount(validAmount: 500, currency: currency),
"name is too long (maximum is 50 characters)": .nameIsTooLong(maximum: 50),
"name is too long ... 20": .nameIsTooLong(maximum: nil), // check if it should be .invalidName instead,
"... name ... blank ...": .emptyName,
"... name ...": .nameIsTooLong(maximum: nil), // check if it should be .invalidName instead,
"... email ...": .invalidEmail,
"... phone ...": .invalidPhoneNumber,
"... type ...": .typeNotSupported,
"... currency must be...": .invalidCurrency,
"... currency ...": .currencyNotSupported,
"Something else": .other("Something else")
]

for (testString, expectedResult) in expectedResults {
let result = try OmiseError.APIErrorCode.BadRequestReason(message: testString, currency: currency)
XCTAssertEqual(result, expectedResult)
}
}
}
}
4 changes: 4 additions & 0 deletions dev.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
75D13E202B8703F80073A831 /* CreditCardPaymentController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D13E1E2B8703F70073A831 /* CreditCardPaymentController.swift */; };
75D13E212B8703F80073A831 /* CreditCardPaymentController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 75D13E1F2B8703F80073A831 /* CreditCardPaymentController.xib */; };
75D2C3EE2C19A80C006072D9 /* ThreeDS_SDK in Frameworks */ = {isa = PBXBuildFile; productRef = 75D2C3ED2C19A80C006072D9 /* ThreeDS_SDK */; };
75D4E7062C05F50500ECCE72 /* OmiseErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D4E7052C05F50500ECCE72 /* OmiseErrorTests.swift */; };
75DAD8902A0BB8D80098AF96 /* LocalConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75DAD88F2A0BB8D80098AF96 /* LocalConfig.swift */; };
75E0EB712B7A904100E3198A /* SourceFlowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75E0EB702B7A904100E3198A /* SourceFlowTests.swift */; };
75E0EB722B7A962600E3198A /* CreateSourcePayloadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75B4208D2B78C2120036134D /* CreateSourcePayloadTests.swift */; };
Expand Down Expand Up @@ -436,6 +437,7 @@
75D13E1C2B86FF8C0073A831 /* CreditCardPaymentDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreditCardPaymentDelegate.swift; sourceTree = "<group>"; };
75D13E1E2B8703F70073A831 /* CreditCardPaymentController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreditCardPaymentController.swift; sourceTree = "<group>"; };
75D13E1F2B8703F80073A831 /* CreditCardPaymentController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CreditCardPaymentController.xib; sourceTree = "<group>"; };
75D4E7052C05F50500ECCE72 /* OmiseErrorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OmiseErrorTests.swift; sourceTree = "<group>"; };
75DAD88F2A0BB8D80098AF96 /* LocalConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalConfig.swift; sourceTree = "<group>"; };
75DAD8922A0BC9540098AF96 /* Config.local.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Config.local.plist; sourceTree = "<group>"; };
75E0EB702B7A904100E3198A /* SourceFlowTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SourceFlowTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1215,6 +1217,7 @@
75CFC4D32B73B21100422A8F /* ClientTests.swift */,
750708F32B7A765500A48DD0 /* OmiseSDKTests.swift */,
75F8C0B62B1F78E300AE78D9 /* PaymentChooserViewControllerTests.swift */,
75D4E7052C05F50500ECCE72 /* OmiseErrorTests.swift */,
);
path = OmiseSDKTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1694,6 +1697,7 @@
7509D4E72A1C8E3D0050AB38 /* AtomeFormViewModelTests.swift in Sources */,
7509D4E22A1C876B0050AB38 /* AtomeFormViewContextMockup.swift in Sources */,
750708E32B790B8300A48DD0 /* String+JSON.swift in Sources */,
75D4E7062C05F50500ECCE72 /* OmiseErrorTests.swift in Sources */,
750708E52B790BD600A48DD0 /* SampleData.swift in Sources */,
758A4E972BE38AFD005E7B5A /* SHA512Tests.swift in Sources */,
750708E02B7909BB00A48DD0 /* SourceTests.swift in Sources */,
Expand Down

0 comments on commit c7d65b8

Please sign in to comment.