47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package weChat
|
|
|
|
import (
|
|
"context"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
|
)
|
|
|
|
type RefundRequest struct {
|
|
TransactionId string `json:"transaction_id" comment:"微信订单号"`
|
|
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
|
|
OutRefundNo string `json:"out_refund_no" comment:"退款订单号"`
|
|
Reason string `json:"reason" comment:"退款原因"`
|
|
RefundAmount int64 `json:"refund_amount" comment:"退款金额"`
|
|
PaymentAmountTotal int64 `json:"payment_amount_total" comment:"支付金额"`
|
|
NotifyUrl string `json:"notify_url" comment:"回调地址"`
|
|
}
|
|
|
|
// Refund 退款
|
|
func (r RefundRequest) Refund() (*refunddomestic.Refund, error) {
|
|
client, err := createClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
refundRequest := refunddomestic.CreateRequest{
|
|
TransactionId: core.String(r.TransactionId),
|
|
OutTradeNo: core.String(r.OutTradeNo),
|
|
OutRefundNo: core.String(r.OutRefundNo),
|
|
Reason: core.String(r.Reason),
|
|
NotifyUrl: core.String(r.NotifyUrl),
|
|
Amount: &refunddomestic.AmountReq{
|
|
Currency: core.String("CNY"),
|
|
Refund: core.Int64(r.RefundAmount),
|
|
Total: core.Int64(r.PaymentAmountTotal),
|
|
},
|
|
}
|
|
|
|
svc := refunddomestic.RefundsApiService{Client: client}
|
|
resp, _, err := svc.Create(context.Background(), refundRequest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return resp, nil
|
|
}
|