f
This commit is contained in:
@@ -16,9 +16,13 @@ import (
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
)
|
||||
|
||||
// AlipayBakRefundCutoff bak 支付宝仅用于该时间点之前的订单退款(2026年2月2日 18:26 下午,CST)
|
||||
var AlipayBakRefundCutoff = time.Date(2026, 2, 2, 18, 26, 0, 0, time.FixedZone("CST", 8*3600))
|
||||
|
||||
type AliPayService struct {
|
||||
config config.AlipayConfig
|
||||
AlipayClient *alipay.Client
|
||||
config config.AlipayConfig
|
||||
AlipayClient *alipay.Client
|
||||
AlipayClientBak *alipay.Client // 仅用于 2026-02-02 18:26 前订单的退款
|
||||
}
|
||||
|
||||
// NewAliPayService 是一个构造函数,用于初始化 AliPayService
|
||||
@@ -44,10 +48,30 @@ func NewAliPayService(c config.Config) *AliPayService {
|
||||
panic(fmt.Sprintf("加载根证书失败: %v", err))
|
||||
}
|
||||
|
||||
return &AliPayService{
|
||||
svc := &AliPayService{
|
||||
config: c.Alipay,
|
||||
AlipayClient: client,
|
||||
}
|
||||
|
||||
// 初始化 bak 支付宝客户端(仅用于 2026-02-02 18:26 前订单的退款)
|
||||
if c.Alipay.AppIDBak != "" && c.Alipay.PrivateKeyBak != "" {
|
||||
bakClient, err := alipay.New(c.Alipay.AppIDBak, c.Alipay.PrivateKeyBak, c.Alipay.IsProduction)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("创建支付宝 bak 客户端失败: %v", err))
|
||||
}
|
||||
if err = bakClient.LoadAppCertPublicKeyFromFile(c.Alipay.AppCertPathBak); err != nil {
|
||||
panic(fmt.Sprintf("加载 bak 应用公钥证书失败: %v", err))
|
||||
}
|
||||
if err = bakClient.LoadAlipayCertPublicKeyFromFile(c.Alipay.AlipayCertPathBak); err != nil {
|
||||
panic(fmt.Sprintf("加载 bak 支付宝公钥证书失败: %v", err))
|
||||
}
|
||||
if err = bakClient.LoadAliPayRootCertFromFile(c.Alipay.AlipayRootCertPathBak); err != nil {
|
||||
panic(fmt.Sprintf("加载 bak 根证书失败: %v", err))
|
||||
}
|
||||
svc.AlipayClientBak = bakClient
|
||||
}
|
||||
|
||||
return svc
|
||||
}
|
||||
|
||||
func (a *AliPayService) CreateAlipayAppOrder(amount float64, subject string, outTradeNo string) (string, error) {
|
||||
@@ -116,16 +140,20 @@ func (a *AliPayService) CreateAlipayOrder(ctx context.Context, amount float64, s
|
||||
}
|
||||
}
|
||||
|
||||
// AliRefund 发起支付宝退款
|
||||
func (a *AliPayService) AliRefund(ctx context.Context, outTradeNo string, refundAmount float64) (*alipay.TradeRefundRsp, error) {
|
||||
// AliRefund 发起支付宝退款。orderPayTime 为订单支付时间(或创建时间),若在 2026-02-02 18:26 之前则使用 bak 商户号退款;传 nil 则使用主商户号。
|
||||
func (a *AliPayService) AliRefund(ctx context.Context, outTradeNo string, refundAmount float64, orderPayTime *time.Time) (*alipay.TradeRefundRsp, error) {
|
||||
client := a.AlipayClient
|
||||
if orderPayTime != nil && !orderPayTime.After(AlipayBakRefundCutoff) && a.AlipayClientBak != nil {
|
||||
client = a.AlipayClientBak
|
||||
}
|
||||
|
||||
refund := alipay.TradeRefund{
|
||||
OutTradeNo: outTradeNo,
|
||||
RefundAmount: lzUtils.ToAlipayAmount(refundAmount),
|
||||
OutRequestNo: fmt.Sprintf("refund-%s", outTradeNo),
|
||||
}
|
||||
|
||||
// 发起退款请求
|
||||
refundResp, err := a.AlipayClient.TradeRefund(ctx, refund)
|
||||
refundResp, err := client.TradeRefund(ctx, refund)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("支付宝退款请求错误:%v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user