Files
2026-05-22 15:54:54 +08:00

48 lines
1.4 KiB
Go
Raw Permalink 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 alipay
import (
"context"
"time"
"github.com/smartwalle/alipay/v3"
"github.com/break/junhong_cmp_fiber/internal/model"
apperrors "github.com/break/junhong_cmp_fiber/pkg/errors"
)
// BuildWapPayURL 生成支付宝手机网站支付 URL。
// payment.PaymentNo 作为 out_trade_nopayment.Amount转为元字符串
// payment.ExpireAt 若非空则转换为支付宝 TimeExpire 格式yyyy-MM-dd HH:mm
// 返回签名后的完整 WAP 支付 URLqr_link 与 copy_link 可使用同一个 URL。
func BuildWapPayURL(ctx context.Context, cfg *model.WechatConfig, payment *model.Payment, subject string) (string, error) {
client, err := NewClientFromConfig(cfg)
if err != nil {
return "", err
}
totalAmount := FenToYuan(payment.Amount)
param := alipay.TradeWapPay{
Trade: alipay.Trade{
NotifyURL: cfg.AliNotifyURL,
ReturnURL: cfg.AliReturnURL,
Subject: subject,
OutTradeNo: payment.PaymentNo,
TotalAmount: totalAmount,
ProductCode: "QUICK_WAP_WAY",
},
}
// 设置支付链接过期时间
if payment.ExpireAt != nil && payment.ExpireAt.After(time.Now()) {
param.TimeExpire = payment.ExpireAt.Format("2006-01-02 15:04")
}
payURL, err := client.TradeWapPay(param)
if err != nil {
return "", apperrors.Wrap(apperrors.CodeNoPaymentConfig, err, "支付宝配置不可用:生成支付链接失败")
}
return payURL.String(), nil
}