微信相关能力
This commit is contained in:
@@ -1,38 +1,51 @@
|
||||
package callback
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/valyala/fasthttp/fasthttpadaptor"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
orderService "github.com/break/junhong_cmp_fiber/internal/service/order"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/wechat"
|
||||
)
|
||||
|
||||
type PaymentHandler struct {
|
||||
orderService *orderService.Service
|
||||
orderService *orderService.Service
|
||||
wechatPayment wechat.PaymentServiceInterface
|
||||
}
|
||||
|
||||
func NewPaymentHandler(orderService *orderService.Service) *PaymentHandler {
|
||||
return &PaymentHandler{orderService: orderService}
|
||||
}
|
||||
|
||||
type WechatPayCallbackRequest struct {
|
||||
OrderNo string `json:"order_no" xml:"out_trade_no"`
|
||||
func NewPaymentHandler(orderService *orderService.Service, wechatPayment wechat.PaymentServiceInterface) *PaymentHandler {
|
||||
return &PaymentHandler{
|
||||
orderService: orderService,
|
||||
wechatPayment: wechatPayment,
|
||||
}
|
||||
}
|
||||
|
||||
// WechatPayCallback 微信支付回调(带签名验证)
|
||||
// POST /api/callback/wechat-pay
|
||||
func (h *PaymentHandler) WechatPayCallback(c *fiber.Ctx) error {
|
||||
var req WechatPayCallbackRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
if h.wechatPayment == nil {
|
||||
return errors.New(errors.CodeWechatCallbackInvalid, "微信支付服务未配置")
|
||||
}
|
||||
|
||||
if req.OrderNo == "" {
|
||||
return errors.New(errors.CodeInvalidParam, "订单号不能为空")
|
||||
}
|
||||
var httpReq http.Request
|
||||
fasthttpadaptor.ConvertRequest(c.Context(), &httpReq, true)
|
||||
|
||||
if err := h.orderService.HandlePaymentCallback(c.UserContext(), req.OrderNo, model.PaymentMethodWechat); err != nil {
|
||||
return err
|
||||
ctx := context.Background()
|
||||
_, err := h.wechatPayment.HandlePaymentNotify(&httpReq, func(result *wechat.PaymentNotifyResult) error {
|
||||
if result.TradeState != "SUCCESS" {
|
||||
return nil
|
||||
}
|
||||
return h.orderService.HandlePaymentCallback(ctx, result.OutTradeNo, model.PaymentMethodWechat)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeWechatCallbackInvalid, err, "处理微信支付回调失败")
|
||||
}
|
||||
|
||||
return response.Success(c, map[string]string{"return_code": "SUCCESS"})
|
||||
|
||||
Reference in New Issue
Block a user