Files
soul-yongping/soul-api/internal/wechat/refund.go
2026-03-07 22:58:43 +08:00

42 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package wechat
import (
"context"
"fmt"
"time"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/refund/request"
)
// RefundOrder 调用微信支付 v3 退款接口(全额退款)
// orderSn: 商户订单号transactionID: 微信支付单号(二选一,有则优先用 transactionIDtotalCents: 订单金额reason: 退款原因
func RefundOrder(ctx context.Context, orderSn, transactionID string, totalCents int, reason string) error {
if paymentApp == nil || paymentApp.Refund == nil {
return fmt.Errorf("支付/退款未初始化")
}
if totalCents <= 0 {
return fmt.Errorf("退款金额必须大于 0")
}
outRefundNo := fmt.Sprintf("RF%s%06d", time.Now().Format("20060102150405"), time.Now().UnixNano()%1000000)
if reason == "" {
reason = "用户申请退款"
}
req := &request.RequestRefund{
OutRefundNo: outRefundNo,
Reason: reason,
Amount: &request.RefundAmount{
Refund: totalCents,
Total: totalCents,
Currency: "CNY",
From: []*request.RefundAmountFrom{},
},
}
if transactionID != "" {
req.TransactionID = transactionID
} else {
req.OutTradeNo = orderSn
}
_, err := paymentApp.Refund.Refund(ctx, req)
return err
}