package rechargereward import ( "fmt" "strconv" "strings" ) func parseAmountCents(raw string) (int64, error) { raw = strings.TrimSpace(raw) if raw == "" { return 0, nil } raw = strings.ReplaceAll(raw, ",", "") if strings.HasPrefix(raw, "+") { raw = strings.TrimPrefix(raw, "+") } if strings.HasPrefix(raw, "-") { return 0, fmt.Errorf("amount must be non-negative") } parts := strings.Split(raw, ".") if len(parts) > 2 { return 0, fmt.Errorf("invalid amount") } integerPart := strings.TrimSpace(parts[0]) if integerPart == "" { integerPart = "0" } for _, ch := range integerPart { if ch < '0' || ch > '9' { return 0, fmt.Errorf("invalid amount") } } dollars, err := strconv.ParseInt(integerPart, 10, 64) if err != nil { return 0, err } centsPart := "00" if len(parts) == 2 { fraction := strings.TrimSpace(parts[1]) for _, ch := range fraction { if ch < '0' || ch > '9' { return 0, fmt.Errorf("invalid amount") } } if len(fraction) == 1 { centsPart = fraction + "0" } else if len(fraction) >= 2 { centsPart = fraction[:2] } } cents, err := strconv.ParseInt(centsPart, 10, 64) if err != nil { return 0, err } return dollars*100 + cents, nil } func parseIntegrationAmountCents(value integrationAmount) int64 { cents, err := parseAmountCents(string(value)) if err != nil { return 0 } return cents } func formatAmountCents(cents int64) string { if cents < 0 { cents = 0 } return fmt.Sprintf("%d.%02d", cents/100, cents%100) } type integrationAmount string