|
|
|
package order
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cls/internal/application/order"
|
|
|
|
"cls/internal/infrastructure/payment/wechat_pay"
|
|
|
|
"cls/internal/interfaces"
|
|
|
|
"cls/pkg/jwtfx"
|
|
|
|
"cls/pkg/logger"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrderHandler struct {
|
|
|
|
service *order.OrderService
|
|
|
|
payService *wechat_pay.PayService
|
|
|
|
log logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrderHandler(service *order.OrderService, payService *wechat_pay.PayService, log logger.New) *OrderHandler {
|
|
|
|
return &OrderHandler{service, payService, log("cls:interfaces:order")}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ interfaces.Handler = (*OrderHandler)(nil)
|
|
|
|
|
|
|
|
func (o *OrderHandler) RegisterRouters(app gin.IRouter) {
|
|
|
|
auth := app.Group("/order")
|
|
|
|
{
|
|
|
|
auth.POST("/create", o.create)
|
|
|
|
auth.POST("/order_notify", o.orderNotify)
|
|
|
|
auth.POST("/cancel", o.orderCancel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OrderHandler) create(c *gin.Context) {
|
|
|
|
dto := &order.CreateOrderRequest{}
|
|
|
|
err := c.ShouldBindJSON(dto)
|
|
|
|
if err != nil {
|
|
|
|
o.log.Error(err.Error())
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := o.service.CreateOrder(dto, jwtfx.GetUserIdentifier(c))
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
c.AbortWithStatusJSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OrderHandler) orderNotify(c *gin.Context) {
|
|
|
|
payment, err := o.payService.HandlerNotifyRequest(c.Request)
|
|
|
|
if err != nil {
|
|
|
|
o.log.Error(err)
|
|
|
|
c.AbortWithStatus(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = o.service.OrderNotify(payment); err != nil {
|
|
|
|
o.log.Error(err)
|
|
|
|
c.AbortWithStatus(http.StatusNoContent)
|
|
|
|
} else {
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OrderHandler) orderCancel(c *gin.Context) {
|
|
|
|
dto := &order.CreateOrderRequest{}
|
|
|
|
err := c.ShouldBindJSON(dto)
|
|
|
|
if err != nil {
|
|
|
|
o.log.Error(err.Error())
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = o.service.CancelOrder(dto, jwtfx.GetUserIdentifier(c))
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|