forked from urdt/ussd
Validate that the amount is not greater than the balance
This commit is contained in:
parent
1d578209fc
commit
8d9aaeedbc
@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/engine"
|
"git.defalsify.org/vise.git/engine"
|
||||||
"git.defalsify.org/vise.git/lang"
|
"git.defalsify.org/vise.git/lang"
|
||||||
@ -506,25 +507,46 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
|
|||||||
|
|
||||||
func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
amount := string(input)
|
amountStr := string(input)
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if amount != "0" {
|
balanceStr, err := server.CheckBalance(accountData["PublicKey"])
|
||||||
// mimic invalid amount
|
if err != nil {
|
||||||
if amount == "00" {
|
return res, err
|
||||||
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
|
}
|
||||||
res.Content = amount
|
res.Content = balanceStr
|
||||||
|
|
||||||
|
// Parse the balance
|
||||||
|
balanceParts := strings.Split(balanceStr, " ")
|
||||||
|
if len(balanceParts) != 2 {
|
||||||
|
return res, fmt.Errorf("unexpected balance format: %s", balanceStr)
|
||||||
|
}
|
||||||
|
balanceValue, err := strconv.ParseFloat(balanceParts[0], 64)
|
||||||
|
if err != nil {
|
||||||
|
return res, fmt.Errorf("failed to parse balance: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the input amount
|
||||||
|
if amountStr != "0" {
|
||||||
|
inputAmount, err := strconv.ParseFloat(amountStr, 64)
|
||||||
|
if err != nil {
|
||||||
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
|
||||||
|
res.Content = amountStr
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Content = amount
|
if inputAmount > balanceValue {
|
||||||
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
|
||||||
|
res.Content = amountStr
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
accountData["Amount"] = amount
|
res.Content = amountStr
|
||||||
|
accountData["Amount"] = amountStr
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
err = h.accountFileHandler.WriteAccountData(accountData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user