Skip to content

Commit

Permalink
Merge pull request #777 from gini/fix-decimal-edge-case
Browse files Browse the repository at this point in the history
fix(GiniHealthSDKExample): Fix decimal edge case + creation of payment request
  • Loading branch information
zladzeyka authored Jan 30, 2025
2 parents f7aae12 + 071e663 commit 2ee7445
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
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 @@ -304,7 +304,7 @@ extension PaymentComponentsController {
It also increments the onboarding count for the selected payment provider.

- Parameter qrCodeData: A qrCode data information for the document associated payment request generated by the payment details.
- Parameter paymentRequestId:The payment request id from generated from the payment info extracted from the invoice
- Parameter paymentRequestId: The payment request id from generated from the payment info extracted from the invoice
- Returns: A configured `BottomSheetViewController` for sharing invoices.
*/
public func shareInvoiceBottomSheet(qrCodeData: Data, paymentRequestId: String) -> BottomSheetViewController {
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

0 comments on commit 2ee7445

Please sign in to comment.