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

fix(GiniHealthSDKExample): Fix decimal edge case + creation of payment request #777

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion GiniComponents/GiniUtilites/Sources/GiniUtilites/Price.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ import Foundation

public struct Price {
// Decimal value
public var value: Decimal
public var value: Decimal {
didSet {
if value > Price.maxValue {
value = Price.maxValue
}
}
}
// Currency code
let currencyCode: String

// Maximum allowed value
private static let maxValue: Decimal = 99999.99

/**
Returns a price structure with decimal value and currency code from extraction string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension OrderDetailView: UITextFieldDelegate {
*/
func updateAmoutToPayWithCurrencyFormat() {
let textField = Self.amountTextField
if textField.hasText, let text = textField.text {
if textField.hasText, let text = textField.text?.replacingOccurrences(of: ".", with: "") {
if let priceValue = text.decimal(),
var price = order?.price {
price.value = priceValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,14 @@ final class OrderDetailViewController: UIViewController {
order.recipient = textFields[NSLocalizedString(Fields.recipient.rawValue, comment: "")]?.text ?? ""
order.purpose = textFields[NSLocalizedString(Fields.purpose.rawValue, comment: "")]?.text ?? ""

var text = textFields[NSLocalizedString(Fields.amountToPay.rawValue, comment: "")]?.text ?? ""
text = text.replacingOccurrences(of: ",", with: ".")
if let decimalAmount = Decimal(string: text) {
var price = Price(extractionString: order.amountToPay) ?? Price(value: decimalAmount, currencyCode: "")
price.value = decimalAmount

order.amountToPay = price.extractionString
} else {
order.amountToPay = Price(value: .zero, currencyCode: "").extractionString
let text = textFields[NSLocalizedString(Fields.amountToPay.rawValue, comment: "")]?.text ?? ""
if let priceValue = text.decimal() {
let price = Price(value: priceValue, currencyCode: "")
if priceValue > 0 {
order.amountToPay = price.extractionString
} else {
order.amountToPay = Price(value: .zero, currencyCode: "").extractionString
}
}
}

Expand Down