treasurer/service/service.go

73 lines
1.5 KiB
Go
Raw Normal View History

2023-05-16 16:21:56 +00:00
package service
import (
"bitbucket.org/skeris/treasurer/dal"
"github.com/gofiber/fiber/v2"
"github.com/themakers/hlog"
)
type Service struct {
log hlog.Logger
d *dal.MongoConnection
}
func New(log hlog.Logger, d *dal.MongoConnection) *Service {
return &Service{
log: log,
d: d,
}
}
func (s *Service) Register(mux fiber.Fiber) fiber.Fiber {
api := mux.Group("/treasurer")
api.Post("/invoice")
return mux
}
type RequestCreateInvoice struct {
RequesterID string `json:"requester_id"`
Amount float64 `json:"amt"`
PayWay string `json:"pw"`
Currency string `json:"cur"`
Email string `json:"email"`
Phone string `json:"phone"`
OnAccept Action `json:"on_accept"`
OnDecline Action `json:"on_decline"`
OnTimeout Action `json:"on_timeout"`
}
type Action struct {
ActionType string `json:"action_type"`
Target string `json:"target"`
Data string `json:"data"`
}
func (s *Service) CreateInvoice(c *fiber.Ctx) error {
c.Accepts("application/json")
req := new(RequestCreateInvoice)
if err := c.BodyParser(req); err != nil {
return err
}
id, err := s.d.InsertPayment(c.Context, &dal.Request{
RequesterID: req.RequesterID,
Email: req.Email,
Phone: req.Phone,
Status: "open",
Amount: req.Amount,
PaymentType: req.PayWay,
Currency: req.Currency,
IsRefill: true,
OnAccept: req.OnAccept,
OnDecline: req.OnDecline,
OnTimeout: req.OnTimeout,
})
if err != nil {
return err
}
return nil
}