修复退款幂等与补偿重试
This commit is contained in:
@@ -3,21 +3,18 @@ package payment
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"hfb_sys/backend/internal/model"
|
||||
)
|
||||
|
||||
func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (*RefundDTO, error) {
|
||||
var originalPayment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).Where("order_id = ? AND status = 'paid' AND biz_type = 'order_pay'", orderID).Order("id DESC").First(&originalPayment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrPaymentNotFound
|
||||
}
|
||||
originalPayment, err := r.findOriginalPayment(ctx, orderID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, &originalPayment)
|
||||
@@ -25,126 +22,99 @@ func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmou
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
|
||||
var existingRefund model.PaymentOrder
|
||||
err = r.db.WithContext(ctx).Where("order_id = ? AND biz_type = ? AND status NOT IN ('failed')", orderID, bizType).Order("id DESC").First(&existingRefund).Error
|
||||
if err == nil {
|
||||
dto := toRefundDTO(existingRefund)
|
||||
return &dto, nil
|
||||
}
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
paymentNo, err := newPaymentNo()
|
||||
refundOrder, existing, err := r.prepareRefundOrder(ctx, originalPayment, *runtimeConfig, refundAmountCent, bizType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
merchantRefundID := "REF" + paymentNo[3:]
|
||||
|
||||
refundOrder := model.PaymentOrder{
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: orderID,
|
||||
OrderNo: originalPayment.OrderNo,
|
||||
UserID: originalPayment.UserID,
|
||||
Provider: runtimeConfig.Provider,
|
||||
MerchantID: runtimeConfig.MerchantID,
|
||||
ThirdOrderID: merchantRefundID,
|
||||
ProviderOrderID: "",
|
||||
PayWay: originalPayment.PayWay,
|
||||
JSPayFlag: originalPayment.JSPayFlag,
|
||||
AmountCent: refundAmountCent,
|
||||
BizType: bizType,
|
||||
Status: "refunding",
|
||||
}
|
||||
|
||||
if runtimeConfig.isMockMode() {
|
||||
refundOrder.ProviderOrderID = "MOCKREF" + merchantRefundID
|
||||
refundOrder.Status = "refunded"
|
||||
now := time.Now()
|
||||
refundOrder.PaidAt = &now
|
||||
if remark != "" {
|
||||
refundOrder.RawResponse = datatypes.JSON([]byte(fmt.Sprintf(`{"mock":"true","remark":"%s"}`, remark)))
|
||||
if existing {
|
||||
latest, syncErr := r.syncRefundPayment(ctx, refundOrder, refundOrder.Status != "refunded")
|
||||
if syncErr != nil {
|
||||
log.Printf("[payment] sync existing refund failed order_id=%d payment_id=%d biz_type=%s err=%v", orderID, refundOrder.ID, bizType, syncErr)
|
||||
dto := toRefundDTO(*refundOrder)
|
||||
return &dto, nil
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Create(&refundOrder).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.recordConfigUsage(ctx, runtimeConfig, &refundOrder)
|
||||
if err := r.updateOrderRefundStatus(ctx, orderID, refundAmountCent); err != nil {
|
||||
log.Printf("[payment] mock update order refund status failed order_id=%d err=%v", orderID, err)
|
||||
}
|
||||
dto := toRefundDTO(refundOrder)
|
||||
dto := toRefundDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
if err := r.db.WithContext(ctx).Create(&refundOrder).Error; err != nil {
|
||||
return nil, err
|
||||
if runtimeConfig.isMockMode() {
|
||||
raw := map[string]string{"mock": "true"}
|
||||
if remark != "" {
|
||||
raw["remark"] = remark
|
||||
}
|
||||
if err := r.applyRefundChannelStatus(ctx, refundOrder, refundChannelStatusUpdate{
|
||||
Status: "refunded",
|
||||
ProviderRefundID: "MOCKREF" + refundOrder.ThirdOrderID,
|
||||
Raw: raw,
|
||||
Source: channelSourceMock,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
latest, err := r.findPaymentByID(ctx, refundOrder.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.recordConfigUsage(ctx, runtimeConfig, latest)
|
||||
dto := toRefundDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
log.Printf("[payment] refund start order_id=%d order_no=%s payment_id=%d biz_type=%s provider=%s amount_cent=%d merchant_refund_id=%s origin_third_order_id=%s origin_provider_order_id=%s",
|
||||
orderID, originalPayment.OrderNo, refundOrder.ID, bizType, runtimeConfig.Provider, refundAmountCent, merchantRefundID, originalPayment.ThirdOrderID, refundOriginProviderOrderID(originalPayment))
|
||||
r.recordConfigUsage(ctx, runtimeConfig, &refundOrder)
|
||||
orderID, originalPayment.OrderNo, refundOrder.ID, bizType, runtimeConfig.Provider, refundAmountCent, refundOrder.ThirdOrderID, originalPayment.ThirdOrderID, refundOriginProviderOrderID(originalPayment))
|
||||
r.recordConfigUsage(ctx, runtimeConfig, refundOrder)
|
||||
if err := r.markOrderRefunding(ctx, orderID, refundAmountCent); err != nil {
|
||||
log.Printf("[payment] mark order refunding failed order_id=%d err=%v", orderID, err)
|
||||
}
|
||||
|
||||
if runtimeConfig.Channel == nil {
|
||||
_ = r.markRefundFailed(ctx, refundOrder.ID, orderID, refundAmountCent, map[string]string{"error": "payment channel unavailable"})
|
||||
if err := r.markRefundFailed(ctx, refundOrder.ID, orderID, refundAmountCent, map[string]string{"error": "payment channel unavailable"}); err != nil {
|
||||
log.Printf("[payment] mark refund failed status failed order_id=%d payment_id=%d err=%v", orderID, refundOrder.ID, err)
|
||||
}
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
resp, err := runtimeConfig.Channel.CreateRefund(ctx, channelCreateRefundRequest{
|
||||
ThirdOrderID: originalPayment.ThirdOrderID,
|
||||
ProviderOrderID: refundOriginProviderOrderID(originalPayment),
|
||||
MerchantRefundID: merchantRefundID,
|
||||
MerchantRefundID: refundOrder.ThirdOrderID,
|
||||
RefundAmountCent: refundAmountCent,
|
||||
NotifyURL: runtimeConfig.NotifyURL,
|
||||
Attach: originalPayment.OrderNo,
|
||||
Remark: remark,
|
||||
})
|
||||
if err != nil {
|
||||
_ = r.markRefundFailed(ctx, refundOrder.ID, orderID, refundAmountCent, map[string]string{"error": err.Error()})
|
||||
if markErr := r.markRefundFailed(ctx, refundOrder.ID, orderID, refundAmountCent, map[string]string{"error": err.Error()}); markErr != nil {
|
||||
log.Printf("[payment] mark refund failed status failed order_id=%d payment_id=%d err=%v", orderID, refundOrder.ID, markErr)
|
||||
}
|
||||
log.Printf("[payment] refund request failed order_id=%d payment_id=%d biz_type=%s provider=%s amount_cent=%d err=%v",
|
||||
orderID, refundOrder.ID, bizType, runtimeConfig.Provider, refundAmountCent, err)
|
||||
return nil, err
|
||||
}
|
||||
if !resp.OK {
|
||||
_ = r.markRefundFailed(ctx, refundOrder.ID, orderID, refundAmountCent, resp.Raw)
|
||||
if markErr := r.markRefundFailed(ctx, refundOrder.ID, orderID, refundAmountCent, resp.Raw); markErr != nil {
|
||||
log.Printf("[payment] mark refund rejected status failed order_id=%d payment_id=%d err=%v", orderID, refundOrder.ID, markErr)
|
||||
}
|
||||
log.Printf("[payment] refund rejected order_id=%d payment_id=%d biz_type=%s provider=%s amount_cent=%d code=%s message=%s",
|
||||
orderID, refundOrder.ID, bizType, runtimeConfig.Provider, refundAmountCent, firstNonEmpty(resp.Raw["code"], resp.Raw["resp_code"], resp.Raw["result_code"]), resp.ErrorMessage)
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
|
||||
refundStatus := "refunding"
|
||||
var paidAt *time.Time
|
||||
if resp.Status == "refunded" {
|
||||
refundStatus = "refunded"
|
||||
now := time.Now()
|
||||
paidAt = &now
|
||||
} else if resp.Status == "failed" {
|
||||
refundStatus = "failed"
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", refundOrder.ID).Updates(map[string]any{
|
||||
"status": refundStatus,
|
||||
"provider_order_id": resp.ProviderRefundID,
|
||||
"raw_request": jsonMap(resp.RawRequest),
|
||||
"raw_response": jsonMap(withRawSource(resp.Raw, channelSourceCreate)),
|
||||
"paid_at": paidAt,
|
||||
}).Error; err != nil {
|
||||
if err := r.applyRefundChannelStatus(ctx, refundOrder, refundChannelStatusUpdate{
|
||||
Status: resp.Status,
|
||||
ProviderRefundID: resp.ProviderRefundID,
|
||||
RawRequest: resp.RawRequest,
|
||||
Raw: resp.Raw,
|
||||
Source: channelSourceCreate,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if refundStatus == "refunded" {
|
||||
_ = r.updateOrderRefundStatus(ctx, orderID, refundAmountCent)
|
||||
refundOrder.PaidAt = paidAt
|
||||
} else if refundStatus == "failed" {
|
||||
_ = r.markOrderRefundFailed(ctx, orderID, refundAmountCent)
|
||||
} else {
|
||||
_ = r.markOrderRefunding(ctx, orderID, refundAmountCent)
|
||||
}
|
||||
refundOrder.Status = refundStatus
|
||||
refundOrder.ProviderOrderID = resp.ProviderRefundID
|
||||
log.Printf("[payment] refund result order_id=%d payment_id=%d biz_type=%s provider=%s amount_cent=%d status=%s provider_refund_id=%s",
|
||||
orderID, refundOrder.ID, bizType, runtimeConfig.Provider, refundAmountCent, refundStatus, resp.ProviderRefundID)
|
||||
orderID, refundOrder.ID, bizType, runtimeConfig.Provider, refundAmountCent, resp.Status, resp.ProviderRefundID)
|
||||
|
||||
dto := toRefundDTO(refundOrder)
|
||||
latest, err := r.findPaymentByID(ctx, refundOrder.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := toRefundDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
func (r *Repository) QueryRefundStatus(ctx context.Context, orderID uint64) (*RefundDTO, error) {
|
||||
@@ -155,49 +125,210 @@ func (r *Repository) QueryRefundStatus(ctx context.Context, orderID uint64) (*Re
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, &payment)
|
||||
latest, err := r.syncRefundPayment(ctx, &payment, payment.Status == "failed")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := toRefundDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
func (r *Repository) SyncRefundStatusByPaymentID(ctx context.Context, paymentID uint64) (*RefundDTO, error) {
|
||||
payment, err := r.findPaymentByID(ctx, paymentID)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrPaymentNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !isRefundBizType(payment.BizType) {
|
||||
return nil, ErrPaymentNotFound
|
||||
}
|
||||
latest, err := r.syncRefundPayment(ctx, payment, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := toRefundDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
func (r *Repository) findOriginalPayment(ctx context.Context, orderID uint64) (model.PaymentOrder, error) {
|
||||
var originalPayment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).Where("order_id = ? AND status = 'paid' AND biz_type = 'order_pay'", orderID).Order("id DESC").First(&originalPayment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return originalPayment, ErrPaymentNotFound
|
||||
}
|
||||
return originalPayment, err
|
||||
}
|
||||
return originalPayment, nil
|
||||
}
|
||||
func (r *Repository) prepareRefundOrder(ctx context.Context, originalPayment model.PaymentOrder, runtimeConfig runtimePaymentConfig, refundAmountCent int64, bizType string) (*model.PaymentOrder, bool, error) {
|
||||
var paymentID uint64
|
||||
existing := false
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var order model.RentalOrder
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&order, originalPayment.OrderID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var existingRefund model.PaymentOrder
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("order_id = ? AND biz_type = ?", originalPayment.OrderID, bizType).
|
||||
Order("id DESC").
|
||||
First(&existingRefund).Error
|
||||
if err == nil {
|
||||
paymentID = existingRefund.ID
|
||||
existing = true
|
||||
return nil
|
||||
}
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
paymentNo, err := newPaymentNo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
merchantRefundID := "REF" + paymentNo[3:]
|
||||
refundOrder := model.PaymentOrder{
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: originalPayment.OrderID,
|
||||
OrderNo: originalPayment.OrderNo,
|
||||
UserID: originalPayment.UserID,
|
||||
Provider: runtimeConfig.Provider,
|
||||
MerchantID: runtimeConfig.MerchantID,
|
||||
ThirdOrderID: merchantRefundID,
|
||||
ProviderOrderID: "",
|
||||
PayWay: originalPayment.PayWay,
|
||||
JSPayFlag: originalPayment.JSPayFlag,
|
||||
AmountCent: refundAmountCent,
|
||||
BizType: bizType,
|
||||
Status: "refunding",
|
||||
}
|
||||
if err := tx.Create(&refundOrder).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
paymentID = refundOrder.ID
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
payment, err := r.findPaymentByID(ctx, paymentID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return payment, existing, nil
|
||||
}
|
||||
func (r *Repository) syncRefundPayment(ctx context.Context, payment *model.PaymentOrder, queryTerminal bool) (*model.PaymentOrder, error) {
|
||||
if payment.Status == "refunded" && !queryTerminal {
|
||||
if err := r.updateOrderRefundStatus(ctx, payment.OrderID, payment.AmountCent); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return payment, nil
|
||||
}
|
||||
if payment.Status == "failed" && !queryTerminal {
|
||||
if err := r.markOrderRefundFailed(ctx, payment.OrderID, payment.AmountCent); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return payment, nil
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, payment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
if payment.Status == "refunded" || payment.Status == "failed" || runtimeConfig.isMockMode() {
|
||||
dto := toRefundDTO(payment)
|
||||
return &dto, nil
|
||||
if runtimeConfig.isMockMode() {
|
||||
if payment.Status == "refunded" {
|
||||
if err := r.updateOrderRefundStatus(ctx, payment.OrderID, payment.AmountCent); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return payment, nil
|
||||
}
|
||||
if runtimeConfig.Channel == nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
resp, err := runtimeConfig.Channel.QueryRefund(ctx, channelQueryRefundRequest{
|
||||
ThirdOrderID: payment.ThirdOrderID,
|
||||
MerchantRefundID: payment.ThirdOrderID,
|
||||
ProviderRefundID: payment.ProviderOrderID,
|
||||
})
|
||||
originalPayment, err := r.findOriginalPayment(ctx, payment.OrderID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Status == "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,
|
||||
"raw_response": jsonMap(withRawSource(resp.Raw, channelSourceQuery)),
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
resp, err := runtimeConfig.Channel.QueryRefund(ctx, refundQueryRequest(*payment, originalPayment))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !resp.OK {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
if err := r.applyRefundChannelStatus(ctx, payment, refundChannelStatusUpdate{
|
||||
Status: resp.Status,
|
||||
ProviderRefundID: resp.ProviderRefundID,
|
||||
RefundTime: resp.RefundTime,
|
||||
Raw: resp.Raw,
|
||||
Source: channelSourceQuery,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.findPaymentByID(ctx, payment.ID)
|
||||
}
|
||||
|
||||
type refundChannelStatusUpdate struct {
|
||||
Status string
|
||||
ProviderRefundID string
|
||||
RefundTime string
|
||||
RawRequest map[string]string
|
||||
Raw map[string]string
|
||||
Source string
|
||||
}
|
||||
|
||||
func (r *Repository) applyRefundChannelStatus(ctx context.Context, payment *model.PaymentOrder, update refundChannelStatusUpdate) error {
|
||||
status := update.Status
|
||||
if status != "refunded" && status != "failed" {
|
||||
status = "refunding"
|
||||
}
|
||||
updates := map[string]any{
|
||||
"status": status,
|
||||
"raw_response": jsonMap(withRawSource(update.Raw, update.Source)),
|
||||
}
|
||||
if update.ProviderRefundID != "" {
|
||||
updates["provider_order_id"] = update.ProviderRefundID
|
||||
}
|
||||
if update.RawRequest != nil {
|
||||
updates["raw_request"] = jsonMap(update.RawRequest)
|
||||
}
|
||||
if status == "refunded" {
|
||||
paidAt := parseChannelTime(update.RefundTime)
|
||||
if paidAt == nil {
|
||||
now := time.Now()
|
||||
paidAt = &now
|
||||
}
|
||||
payment.Status = "refunded"
|
||||
payment.PaidAt = &now
|
||||
_ = r.updateOrderRefundStatus(ctx, orderID, payment.AmountCent)
|
||||
} else if resp.Status == "failed" {
|
||||
if err := r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", payment.ID).Updates(map[string]any{
|
||||
"status": "failed",
|
||||
"raw_response": jsonMap(withRawSource(resp.Raw, channelSourceQuery)),
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
updates["paid_at"] = paidAt
|
||||
}
|
||||
if update.Source == channelSourceNotify {
|
||||
updates["notified_at"] = time.Now()
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", payment.ID).Updates(updates).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
switch status {
|
||||
case "refunded":
|
||||
return r.updateOrderRefundStatus(ctx, payment.OrderID, payment.AmountCent)
|
||||
case "failed":
|
||||
return r.markOrderRefundFailed(ctx, payment.OrderID, payment.AmountCent)
|
||||
default:
|
||||
return r.markOrderRefunding(ctx, payment.OrderID, payment.AmountCent)
|
||||
}
|
||||
}
|
||||
func isRefundBizType(bizType string) bool {
|
||||
for _, item := range refundBizTypes {
|
||||
if item == bizType {
|
||||
return true
|
||||
}
|
||||
payment.Status = "failed"
|
||||
_ = r.markOrderRefundFailed(ctx, orderID, payment.AmountCent)
|
||||
}
|
||||
return false
|
||||
}
|
||||
func refundQueryRequest(payment model.PaymentOrder, originalPayment model.PaymentOrder) channelQueryRefundRequest {
|
||||
return channelQueryRefundRequest{
|
||||
ThirdOrderID: originalPayment.ThirdOrderID,
|
||||
ProviderOrderID: refundOriginProviderOrderID(originalPayment),
|
||||
MerchantRefundID: payment.ThirdOrderID,
|
||||
ProviderRefundID: payment.ProviderOrderID,
|
||||
}
|
||||
dto := toRefundDTO(payment)
|
||||
return &dto, nil
|
||||
}
|
||||
func (r *Repository) updateOrderRefundStatus(ctx context.Context, orderID uint64, refundAmountCent int64) error {
|
||||
now := time.Now()
|
||||
|
||||
Reference in New Issue
Block a user