59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitea.urkob.com/urko/btc-pay-checker/internal/domain"
|
|
"gitea.urkob.com/urko/btc-pay-checker/internal/services"
|
|
"gitea.urkob.com/urko/btc-pay-checker/internal/services/price"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type OrderHandler struct {
|
|
walletAddress string
|
|
orderSrv *services.Order
|
|
conversor *price.PriceConversor
|
|
}
|
|
|
|
func NewOrderHandler(walletAddress string, orderSrv *services.Order, conversor *price.PriceConversor) *OrderHandler {
|
|
return &OrderHandler{
|
|
walletAddress: walletAddress,
|
|
orderSrv: orderSrv,
|
|
conversor: conversor,
|
|
}
|
|
}
|
|
|
|
type orderReq struct {
|
|
OrderID string `json:"order_id"`
|
|
ClientID string `json:"client_id"`
|
|
Amount float64 `json:"amount"`
|
|
Currency domain.FiatCurrency `json:"currency"`
|
|
}
|
|
|
|
func (hdl *OrderHandler) Post(c *fiber.Ctx) error {
|
|
req := orderReq{}
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return RenderError(c, fmt.Errorf("id is empty"), "")
|
|
}
|
|
|
|
btcAmount, err := hdl.conversor.UsdToBtc(req.Amount)
|
|
if err != nil {
|
|
return RenderError(c, fmt.Errorf("hdl.conversor.UsdToBtc %w", err), "")
|
|
}
|
|
|
|
order, err := hdl.orderSrv.NewOrder(c.Context(), req.OrderID, req.ClientID, btcAmount)
|
|
if err != nil {
|
|
return RenderError(c, fmt.Errorf("hdl.orderSrv.NewOrder %w", err), "")
|
|
}
|
|
|
|
return c.Render("order",
|
|
fiber.Map{
|
|
"order_id": order.ID.Hex(),
|
|
"amount": btcAmount,
|
|
"wallet_address": hdl.walletAddress,
|
|
"expires_at": order.ExpiresAt.Format(time.RFC3339),
|
|
},
|
|
)
|
|
}
|