mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
fix(payment): make Alipay refund request id stable across retries
Alipay refunds used a UnixNano timestamp as out_request_no, so an admin retry after an ambiguous gateway error minted a new request and refunded the user twice. Use the same deterministic order+amount id as the other providers (wxpayRefundID, Stripe idempotency key). Same order and amount now map to the same Alipay refund request; the existing QueryRefund prefix contract is unchanged.
This commit is contained in:
1 parent
ee58c0013c
commit
43511b40e9
2 files changed
+74
-1
No files matched your search
@@ -348,7 +348,7 @@ func (a *Alipay) Refund(ctx context.Context, req payment.RefundRequest) (*paymen
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outRequestNo := fmt.Sprintf("%s%s%d", req.OrderID, alipayRefundRequestSep, time.Now().UnixNano())
|
||||
outRequestNo := fmt.Sprintf("%s%s%s", req.OrderID, alipayRefundRequestSep, alipayRefundRequestNoSuffix(req.Amount))
|
||||
result, err := alipayTradeRefund(ctx, client, alipay.TradeRefund{
|
||||
OutTradeNo: req.OrderID,
|
||||
RefundAmount: req.Amount,
|
||||
@@ -374,6 +374,18 @@ func (a *Alipay) Refund(ctx context.Context, req payment.RefundRequest) (*paymen
|
||||
}, nil
|
||||
}
|
||||
|
||||
// alipayRefundRequestNoSuffix 生成跨重试稳定的 out_request_no 后缀。
|
||||
// 支付宝把相同的 out_request_no 视为同一次退款请求;如果重试换号,
|
||||
// "网关已受理但返回错误"后的重试会变成第二笔真实退款。
|
||||
// 与 wxpayRefundID 一致,用金额区分同一订单的不同退款金额。
|
||||
func alipayRefundRequestNoSuffix(amount string) string {
|
||||
normalized := strings.NewReplacer(".", "", "-", "").Replace(strings.TrimSpace(amount))
|
||||
if normalized == "" {
|
||||
return "0"
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
// QueryRefund 通过 alipay.trade.fastpay.refund.query 查询退款终态。refund_status 为 REFUND_SUCCESS
|
||||
// 表示成功;退款发起超过宽限期仍查不到记录表示支付宝未受理或退款失败;其余情况视为仍在处理中。
|
||||
func (a *Alipay) QueryRefund(ctx context.Context, req payment.RefundQueryRequest) (*payment.RefundResponse, error) {
|
||||
|
||||
@@ -480,6 +480,67 @@ func TestAlipayRefundRecordsOutRequestNoAsRefundID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// out_request_no 必须跨重试稳定:支付宝把相同的 out_request_no 视为同一次退款请求。
|
||||
// 如果重试生成新的请求号,一次"网关已受理但返回错误"的重试会退两次。
|
||||
func TestAlipayRefundOutRequestNoIsStableAcrossRetries(t *testing.T) {
|
||||
origRefund := alipayTradeRefund
|
||||
t.Cleanup(func() { alipayTradeRefund = origRefund })
|
||||
|
||||
var outRequestNos []string
|
||||
alipayTradeRefund = func(_ context.Context, _ *alipay.Client, param alipay.TradeRefund) (*alipay.TradeRefundRsp, error) {
|
||||
outRequestNos = append(outRequestNos, param.OutRequestNo)
|
||||
return &alipay.TradeRefundRsp{TradeNo: "2026101522001", FundChange: alipayFundChangeYes}, nil
|
||||
}
|
||||
|
||||
provider := &Alipay{client: &alipay.Client{}}
|
||||
req := payment.RefundRequest{OrderID: "sub2_7", Amount: "12.50", Reason: "r"}
|
||||
for i := 0; i < 2; i++ {
|
||||
if _, err := provider.Refund(context.Background(), req); err != nil {
|
||||
t.Fatalf("Refund() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(outRequestNos) != 2 {
|
||||
t.Fatalf("got %d refund calls, want 2", len(outRequestNos))
|
||||
}
|
||||
if outRequestNos[0] != "sub2_7-refund-1250" {
|
||||
t.Fatalf("out_request_no = %q, want sub2_7-refund-1250", outRequestNos[0])
|
||||
}
|
||||
if outRequestNos[0] != outRequestNos[1] {
|
||||
t.Fatalf("out_request_no changed across retries: %q != %q", outRequestNos[0], outRequestNos[1])
|
||||
}
|
||||
}
|
||||
|
||||
// 模拟支付宝的幂等语义:相同的 out_request_no 只产生一笔真实退款。
|
||||
// 重试时如果请求号变了,钱会被退两次;稳定请求号下只退一次。
|
||||
func TestAlipayRefundRetryDoesNotDoubleRefund(t *testing.T) {
|
||||
origRefund := alipayTradeRefund
|
||||
t.Cleanup(func() { alipayTradeRefund = origRefund })
|
||||
|
||||
const refundCents = 1250
|
||||
refundedCents := 0
|
||||
seen := make(map[string]struct{})
|
||||
alipayTradeRefund = func(_ context.Context, _ *alipay.Client, param alipay.TradeRefund) (*alipay.TradeRefundRsp, error) {
|
||||
if _, ok := seen[param.OutRequestNo]; !ok {
|
||||
seen[param.OutRequestNo] = struct{}{}
|
||||
refundedCents += refundCents
|
||||
}
|
||||
return &alipay.TradeRefundRsp{TradeNo: "2026101522001", FundChange: alipayFundChangeYes}, nil
|
||||
}
|
||||
|
||||
provider := &Alipay{client: &alipay.Client{}}
|
||||
req := payment.RefundRequest{OrderID: "sub2_7", Amount: "12.50", Reason: "r"}
|
||||
for i := 0; i < 2; i++ {
|
||||
if _, err := provider.Refund(context.Background(), req); err != nil {
|
||||
t.Fatalf("Refund() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if refundedCents != refundCents {
|
||||
t.Fatalf("retry refunded %d cents, want %d (one refund only)", refundedCents, refundCents)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlipayQueryRefundMapsStatus(t *testing.T) {
|
||||
origQuery := alipayTradeFastPayRefundQuery
|
||||
t.Cleanup(func() { alipayTradeFastPayRefundQuery = origQuery })
|
||||
|
||||
Reference in new issue
Block a user