✨ Multi-currency
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
@@ -34,12 +35,17 @@ func createOrder(c *fiber.Ctx) error {
|
||||
Amount float64 `json:"amount" validate:"required"`
|
||||
PayeeID *uint `json:"payee_id"`
|
||||
PayerID *uint `json:"payer_id"`
|
||||
Currency string `json:"currency" validate:"required"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !lo.Contains([]string{"normal", "golden"}, data.Currency) {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "invalid currency")
|
||||
}
|
||||
|
||||
// Validating client
|
||||
client, err := authkit.GetThirdClientByAlias(gap.Nx, data.ClientID, &data.ClientSecret)
|
||||
if err != nil {
|
||||
@@ -50,6 +56,7 @@ func createOrder(c *fiber.Ctx) error {
|
||||
Status: models.OrderStatusPending,
|
||||
Remark: data.Remark,
|
||||
Amount: decimal.NewFromFloat(data.Amount),
|
||||
Currency: data.Currency,
|
||||
ClientID: &client.ID,
|
||||
}
|
||||
|
||||
@@ -126,7 +133,7 @@ func payOrder(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
if tran, err := services.MakeTransaction(order.Amount.InexactFloat64(), order.Remark, payer, payee); err != nil {
|
||||
if tran, err := services.MakeTransaction(order.Amount.InexactFloat64(), order.Remark, order.Currency, payer, payee); err != nil {
|
||||
return fiber.NewError(fiber.StatusPaymentRequired, err.Error())
|
||||
} else {
|
||||
if err := database.C.Model(&order).Updates(&models.Order{
|
||||
@@ -137,6 +144,7 @@ func payOrder(c *fiber.Ctx) error {
|
||||
_, _ = services.MakeTransaction(
|
||||
order.Amount.InexactFloat64(),
|
||||
fmt.Sprintf("%s - #%d Refund", order.Remark, order.ID),
|
||||
order.Currency,
|
||||
payee,
|
||||
payer,
|
||||
)
|
||||
|
@@ -11,6 +11,7 @@ import (
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func listTransaction(c *fiber.Ctx) error {
|
||||
@@ -77,12 +78,17 @@ func makeTransaction(c *fiber.Ctx) error {
|
||||
Amount float64 `json:"amount" validate:"required"`
|
||||
PayeeID *uint `json:"payee_id"`
|
||||
PayerID *uint `json:"payer_id"`
|
||||
Currency string `json:"currency" validate:"required"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !lo.Contains([]string{"normal", "golden"}, data.Currency) {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "invalid currency")
|
||||
}
|
||||
|
||||
// Validating client
|
||||
client, err := authkit.GetThirdClientByAlias(gap.Nx, data.ClientID, &data.ClientSecret)
|
||||
if err != nil {
|
||||
@@ -110,7 +116,7 @@ func makeTransaction(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "payee and payer cannot be both blank")
|
||||
}
|
||||
|
||||
tran, err := services.MakeTransaction(data.Amount, data.Remark, payer, payee)
|
||||
tran, err := services.MakeTransaction(data.Amount, data.Remark, data.Currency, payer, payee)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
Reference in New Issue
Block a user