2025-01-10 00:09:25 +08:00
|
|
|
package pay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-05-11 23:43:01 +08:00
|
|
|
"database/sql"
|
2025-04-11 13:10:17 +08:00
|
|
|
"net/http"
|
2025-06-09 12:34:52 +08:00
|
|
|
"qnc-server/app/main/api/internal/svc"
|
2025-05-11 23:43:01 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
2025-04-11 13:10:17 +08:00
|
|
|
|
2025-05-11 23:43:01 +08:00
|
|
|
"github.com/pkg/errors"
|
2025-01-10 00:09:25 +08:00
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WechatPayRefundCallbackLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWechatPayRefundCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WechatPayRefundCallbackLogic {
|
|
|
|
return &WechatPayRefundCallbackLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-11 23:43:01 +08:00
|
|
|
// handleQueryOrderRefund 处理查询订单退款
|
|
|
|
func (l *WechatPayRefundCallbackLogic) handleQueryOrderRefund(orderNo string, status refunddomestic.Status) error {
|
|
|
|
order, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, orderNo)
|
2025-01-10 00:09:25 +08:00
|
|
|
if err != nil {
|
2025-05-11 23:43:01 +08:00
|
|
|
return errors.Wrapf(err, "查找查询订单信息失败: %s", orderNo)
|
2025-01-10 00:09:25 +08:00
|
|
|
}
|
2025-05-11 23:43:01 +08:00
|
|
|
|
|
|
|
if status == refunddomestic.STATUS_SUCCESS {
|
|
|
|
order.Status = "refunded"
|
|
|
|
order.RefundTime = sql.NullTime{
|
|
|
|
Time: time.Now(),
|
|
|
|
Valid: true,
|
|
|
|
}
|
|
|
|
} else if status == refunddomestic.STATUS_ABNORMAL {
|
|
|
|
return nil // 异常状态直接返回
|
|
|
|
} else {
|
|
|
|
return nil // 其他状态直接返回
|
2025-01-10 00:09:25 +08:00
|
|
|
}
|
|
|
|
|
2025-05-11 23:43:01 +08:00
|
|
|
if err := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); err != nil {
|
|
|
|
return errors.Wrapf(err, "更新查询订单状态失败: %s", orderNo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleAgentOrderRefund 处理代理会员订单退款
|
|
|
|
func (l *WechatPayRefundCallbackLogic) handleAgentOrderRefund(orderNo string, status refunddomestic.Status) error {
|
|
|
|
order, err := l.svcCtx.AgentMembershipRechargeOrderModel.FindOneByOrderNo(l.ctx, orderNo)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "查找代理会员订单信息失败: %s", orderNo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == refunddomestic.STATUS_SUCCESS {
|
2025-01-10 00:09:25 +08:00
|
|
|
order.Status = "refunded"
|
2025-05-11 23:43:01 +08:00
|
|
|
} else if status == refunddomestic.STATUS_ABNORMAL {
|
|
|
|
return nil // 异常状态直接返回
|
|
|
|
} else {
|
|
|
|
return nil // 其他状态直接返回
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.svcCtx.AgentMembershipRechargeOrderModel.UpdateWithVersion(l.ctx, nil, order); err != nil {
|
|
|
|
return errors.Wrapf(err, "更新代理会员订单状态失败: %s", orderNo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendSuccessResponse 发送成功响应
|
|
|
|
func (l *WechatPayRefundCallbackLogic) sendSuccessResponse(w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write([]byte("success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *WechatPayRefundCallbackLogic) WechatPayRefundCallback(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
// 1. 处理微信退款通知
|
|
|
|
notification, err := l.svcCtx.WechatPayService.HandleRefundNotification(l.ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
logx.Errorf("微信退款回调处理失败: %v", err)
|
|
|
|
l.sendSuccessResponse(w)
|
2025-01-10 00:09:25 +08:00
|
|
|
return nil
|
2025-05-11 23:43:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
orderNo := *notification.OutTradeNo
|
|
|
|
var processErr error
|
|
|
|
|
|
|
|
// 2. 根据订单号前缀处理不同类型的订单
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(orderNo, "Q_"):
|
|
|
|
processErr = l.handleQueryOrderRefund(orderNo, *notification.Status)
|
|
|
|
case strings.HasPrefix(orderNo, "A_"):
|
|
|
|
processErr = l.handleAgentOrderRefund(orderNo, *notification.Status)
|
2025-01-10 00:09:25 +08:00
|
|
|
default:
|
2025-05-11 23:43:01 +08:00
|
|
|
// 兼容旧订单,假设没有前缀的是查询订单
|
|
|
|
processErr = l.handleQueryOrderRefund(orderNo, *notification.Status)
|
2025-01-10 00:09:25 +08:00
|
|
|
}
|
2025-05-11 23:43:01 +08:00
|
|
|
|
|
|
|
// 3. 处理错误并响应
|
|
|
|
if processErr != nil {
|
|
|
|
logx.Errorf("处理退款订单失败: %v", processErr)
|
2025-01-10 00:09:25 +08:00
|
|
|
}
|
2025-05-11 23:43:01 +08:00
|
|
|
|
|
|
|
// 无论处理是否成功,都返回成功响应给微信
|
|
|
|
l.sendSuccessResponse(w)
|
2025-01-10 00:09:25 +08:00
|
|
|
return nil
|
|
|
|
}
|