1、完善订单退款时间
2、完善微信支付开通代理会员失败退款
This commit is contained in:
parent
972b10eeae
commit
16e57387db
@ -2,9 +2,13 @@ package pay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"net/http"
|
"net/http"
|
||||||
"qnc-server/app/user/cmd/api/internal/svc"
|
"qnc-server/app/user/cmd/api/internal/svc"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
@ -23,33 +27,87 @@ func NewWechatPayRefundCallbackLogic(ctx context.Context, svcCtx *svc.ServiceCon
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *WechatPayRefundCallbackLogic) WechatPayRefundCallback(w http.ResponseWriter, r *http.Request) error {
|
// handleQueryOrderRefund 处理查询订单退款
|
||||||
notification, err := l.svcCtx.WechatPayService.HandleRefundNotification(l.ctx, r)
|
func (l *WechatPayRefundCallbackLogic) handleQueryOrderRefund(orderNo string, status refunddomestic.Status) error {
|
||||||
|
order, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, orderNo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Errorf("微信退款回调,%v", err)
|
return errors.Wrapf(err, "查找查询订单信息失败: %s", orderNo)
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
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 // 其他状态直接返回
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); err != nil {
|
||||||
|
return errors.Wrapf(err, "更新查询订单状态失败: %s", orderNo)
|
||||||
}
|
}
|
||||||
order, findOrderErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, *notification.OutTradeNo)
|
|
||||||
if findOrderErr != nil {
|
|
||||||
logx.Errorf("微信退款回调,查找订单信息失败: %+v", findOrderErr)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch *notification.Status {
|
// handleAgentOrderRefund 处理代理会员订单退款
|
||||||
case refunddomestic.STATUS_SUCCESS:
|
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 {
|
||||||
order.Status = "refunded"
|
order.Status = "refunded"
|
||||||
case refunddomestic.STATUS_ABNORMAL:
|
} else if status == refunddomestic.STATUS_ABNORMAL {
|
||||||
// 异常
|
return nil // 异常状态直接返回
|
||||||
return nil
|
} else {
|
||||||
default:
|
return nil // 其他状态直接返回
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := l.svcCtx.AgentMembershipRechargeOrderModel.UpdateWithVersion(l.ctx, nil, order); err != nil {
|
||||||
|
return errors.Wrapf(err, "更新代理会员订单状态失败: %s", orderNo)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
|
||||||
logx.Errorf("微信退款回调,更新订单失败%+v", updateErr)
|
// sendSuccessResponse 发送成功响应
|
||||||
return nil
|
func (l *WechatPayRefundCallbackLogic) sendSuccessResponse(w http.ResponseWriter) {
|
||||||
}
|
|
||||||
// 响应微信回调成功
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte("success")) // 确保只写入一次响应
|
_, _ = 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)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
default:
|
||||||
|
// 兼容旧订单,假设没有前缀的是查询订单
|
||||||
|
processErr = l.handleQueryOrderRefund(orderNo, *notification.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 处理错误并响应
|
||||||
|
if processErr != nil {
|
||||||
|
logx.Errorf("处理退款订单失败: %v", processErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 无论处理是否成功,都返回成功响应给微信
|
||||||
|
l.sendSuccessResponse(w)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package queue
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
"qnc-server/pkg/lzkit/lzUtils"
|
"qnc-server/pkg/lzkit/lzUtils"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hibiken/asynq"
|
"github.com/hibiken/asynq"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
@ -181,6 +183,10 @@ func (l *PaySuccessNotifyUserHandler) handleError(ctx context.Context, err error
|
|||||||
logx.Errorf("支付宝退款成功, orderID: %d", order.Id)
|
logx.Errorf("支付宝退款成功, orderID: %d", order.Id)
|
||||||
// 更新订单状态为退款
|
// 更新订单状态为退款
|
||||||
order.Status = "refunded"
|
order.Status = "refunded"
|
||||||
|
order.RefundTime = sql.NullTime{
|
||||||
|
Time: time.Now(),
|
||||||
|
Valid: true,
|
||||||
|
}
|
||||||
updateOrderErr := l.svcCtx.OrderModel.UpdateWithVersion(ctx, nil, order)
|
updateOrderErr := l.svcCtx.OrderModel.UpdateWithVersion(ctx, nil, order)
|
||||||
if updateOrderErr != nil {
|
if updateOrderErr != nil {
|
||||||
logx.Errorf("更新订单状态失败,订单ID: %d, 错误: %v", order.Id, updateOrderErr)
|
logx.Errorf("更新订单状态失败,订单ID: %d, 错误: %v", order.Id, updateOrderErr)
|
||||||
|
Loading…
Reference in New Issue
Block a user