修复退款幂等与补偿重试

This commit is contained in:
yml2213
2026-06-14 07:55:02 +08:00
parent 607d2415a1
commit fec8e37181
11 changed files with 772 additions and 143 deletions
+8 -25
View File
@@ -5,7 +5,6 @@ import (
"gorm.io/gorm"
"hfb_sys/backend/internal/model"
"log"
"time"
)
func (r *Repository) HandleLeshuaNotify(ctx context.Context, params map[string]string, rawPayload string, contentType string) (*NotifyResult, error) {
@@ -58,30 +57,14 @@ func (r *Repository) HandleRefundNotify(ctx context.Context, provider string, pa
raw := withNotifyDiagnostic(params, rawPayload, contentType, verify, "verified")
status := normalizeNotifyRefundStatus(provider, params["status"])
switch status {
case "refunded":
now := time.Now()
if err := r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", payment.ID).Updates(map[string]any{
"status": "refunded",
"paid_at": now,
"notified_at": now,
"raw_response": jsonMap(raw),
}).Error; err != nil {
return nil, err
}
_ = r.updateOrderRefundStatus(ctx, payment.OrderID, payment.AmountCent)
case "failed":
r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", payment.ID).Updates(map[string]any{
"status": "failed",
"notified_at": time.Now(),
"raw_response": jsonMap(raw),
})
_ = r.markOrderRefundFailed(ctx, payment.OrderID, payment.AmountCent)
default:
r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", payment.ID).Updates(map[string]any{
"status": "refunding",
"raw_response": jsonMap(raw),
})
if err := r.applyRefundChannelStatus(ctx, payment, refundChannelStatusUpdate{
Status: status,
ProviderRefundID: firstNonEmpty(params["provider_refund_id"], params["leshua_refund_id"]),
RefundTime: params["refund_time"],
Raw: raw,
Source: channelSourceNotify,
}); err != nil {
return nil, err
}
return &NotifyResult{OK: true, Message: "000000"}, nil
}