182 lines
5.4 KiB
Go
182 lines
5.4 KiB
Go
package v1
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/dto"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/middleware"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/service"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/app"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/errcode"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/wechat"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// OrderHandler 订单与退款相关接口。
|
|
type OrderHandler struct {
|
|
svc *service.OrderService
|
|
}
|
|
|
|
// NewOrderHandler 创建订单 handler。
|
|
func NewOrderHandler() *OrderHandler {
|
|
return &OrderHandler{svc: service.NewOrderService()}
|
|
}
|
|
|
|
// Create 创建订单并发起微信下单。
|
|
func (h *OrderHandler) Create(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.OrderCreateRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.Create(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// VirtualCreate 创建虚拟支付订单(小程序虚拟支付,覆盖 iOS/Android 全终端)。
|
|
func (h *OrderHandler) VirtualCreate(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.VirtualOrderCreateRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.VirtualOrderCreate(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// List 分页查询我的订单。
|
|
func (h *OrderHandler) List(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.OrderListRequest
|
|
if err := c.ShouldBindQuery(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.List(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// Cancel 取消订单(orderNo 来自路径参数)。
|
|
func (h *OrderHandler) Cancel(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
request := dto.OrderCancelRequest{OrderNo: c.Param("orderNo")}
|
|
if request.OrderNo == "" {
|
|
response.ToErrorResponse(errcode.InvalidParams)
|
|
return
|
|
}
|
|
out, e := h.svc.Cancel(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// RefundApply 申请退款。
|
|
func (h *OrderHandler) RefundApply(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.RefundApplyRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.RefundApply(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// PayNotify 微信支付结果回调(公开,按微信规范返回受理结果)。
|
|
func (h *OrderHandler) PayNotify(c *gin.Context) {
|
|
if e := h.svc.PayNotify(c.Request.Context(), c.Request); e != nil {
|
|
wechatCallbackFail(c, e.Msg())
|
|
return
|
|
}
|
|
wechatCallbackSuccess(c)
|
|
}
|
|
|
|
// RefundNotify 微信退款结果回调(公开,按微信规范返回受理结果)。
|
|
func (h *OrderHandler) RefundNotify(c *gin.Context) {
|
|
if e := h.svc.RefundNotify(c.Request.Context(), c.Request); e != nil {
|
|
wechatCallbackFail(c, e.Msg())
|
|
return
|
|
}
|
|
wechatCallbackSuccess(c)
|
|
}
|
|
|
|
// XPayVerify 虚拟支付消息推送 URL 配置验证(GET):校验签名并原样回显 echostr。
|
|
func (h *OrderHandler) XPayVerify(c *gin.Context) {
|
|
timestamp := c.Query("timestamp")
|
|
nonce := c.Query("nonce")
|
|
signature := c.Query("signature")
|
|
echostr := c.Query("echostr")
|
|
if wechat.VerifyNotifyURL(timestamp, nonce, signature) {
|
|
c.String(http.StatusOK, echostr)
|
|
return
|
|
}
|
|
c.String(http.StatusUnauthorized, "invalid signature")
|
|
}
|
|
|
|
// XPayNotify 虚拟支付消息推送回调(POST,公开,按虚拟支付规范返回 ErrCode 应答)。
|
|
func (h *OrderHandler) XPayNotify(c *gin.Context) {
|
|
raw, err := io.ReadAll(c.Request.Body)
|
|
if err != nil {
|
|
c.Data(http.StatusOK, "application/json; charset=utf-8", wechat.BuildNotifyResponse(false))
|
|
return
|
|
}
|
|
resp := h.svc.XPayNotify(c.Request.Context(), raw)
|
|
c.Data(http.StatusOK, "application/json; charset=utf-8", resp)
|
|
}
|
|
|
|
// wechatCallbackSuccess 按微信规范返回受理成功,避免微信重复回调。
|
|
func wechatCallbackSuccess(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": "成功"})
|
|
}
|
|
|
|
// wechatCallbackFail 按微信规范返回受理失败,微信将按策略重试。
|
|
func wechatCallbackFail(c *gin.Context, message string) {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": "FAIL", "message": message})
|
|
}
|