42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
|
|
package wechat
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"fmt"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/refund/request"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// RefundOrder 调用微信支付 v3 退款接口(全额退款)
|
|||
|
|
// orderSn: 商户订单号;transactionID: 微信支付单号(二选一,有则优先用 transactionID);totalCents: 订单金额(分);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
|
|||
|
|
}
|