diff --git a/Sources/PayPal/Models/Order/OrderItem.swift b/Sources/PayPal/Models/Order/OrderItem.swift index cb0ef44b..27a9904f 100644 --- a/Sources/PayPal/Models/Order/OrderItem.swift +++ b/Sources/PayPal/Models/Order/OrderItem.swift @@ -69,15 +69,23 @@ extension Order { public init(from decoder: Decoder)throws { let container = try decoder.container(keyedBy: Order.Item.CodingKeys.self) - guard let quantityValue = try Int(container.decode(String.self, forKey: .quantity)) else { - throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "Cannot get int from given string") + let quantity: Int + do { + quantity = try container.decode(Int.self, forKey: .quantity) + } catch let error as DecodingError { + guard case DecodingError.typeMismatch(_, _) = error else { throw error } + guard let value = try Int(container.decode(String.self, forKey: .quantity)) else { + throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "Cannot get int from given string") + } + quantity = value } - guard let priceValue = try Decimal(string: container.decode(String.self, forKey: .price)) else { + + guard let price = try Decimal(string: container.decode(String.self, forKey: .price)) else { throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "Cannot get decimal from given string") } - self.price = try Failable>(priceValue) - self.quantity = try Failable>(quantityValue) + self.price = try price.failable() + self.quantity = try quantity.failable() self.sku = try container.decode(Optional127String.self, forKey: .sku) self.name = try container.decode(Optional127String.self, forKey: .name) self.description = try container.decode(Optional127String.self, forKey: .description) diff --git a/Sources/PayPal/Models/Payment/Items/PaymentItem.swift b/Sources/PayPal/Models/Payment/Items/PaymentItem.swift index d0bc5ebd..c2ba93ff 100644 --- a/Sources/PayPal/Models/Payment/Items/PaymentItem.swift +++ b/Sources/PayPal/Models/Payment/Items/PaymentItem.swift @@ -71,13 +71,19 @@ extension Payment { public init(from decoder: Decoder)throws { let container = try decoder.container(keyedBy: Payment.Item.CodingKeys.self) - let strInt = try container.decode(String.self, forKey: .quantity) - let strDec = try container.decode(String.self, forKey: .price) - guard let quantity = Int(strInt) else { - throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "String not convertible to int") + let quantity: Int + do { + quantity = try container.decode(Int.self, forKey: .quantity) + } catch let error as DecodingError { + guard case DecodingError.typeMismatch(_, _) = error else { throw error } + guard let value = try Int(container.decode(String.self, forKey: .quantity)) else { + throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "Given string not convertible to int") + } + quantity = value } - guard let price = Decimal(string: strDec) else { - throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "String not convertible to decimal") + + guard let price = try Decimal(string: container.decode(String.self, forKey: .price)) else { + throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "Given string not convertible to decimal") } self.quantity = try quantity.failable()