Files
utility_go/package/pay/alipay.go
2025-02-28 17:45:44 +08:00

43 lines
1.1 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 pay
import (
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay"
"github.com/gogf/gf/v2/frame/g"
)
type AliPay struct {
Client *alipay.Client
}
func Alipay() *AliPay {
var pay = &AliPay{}
var err error
cfg, err := g.Cfg().Get(ctx, "pay.alipay")
cfgMap := cfg.MapStrStr()
appId := cfgMap["appid"]
privateKey := cfgMap["privateKey"]
isProd, _ := g.Cfg().Get(ctx, "pay.alipay.isProd")
// 初始化支付宝客户端
// appid应用ID
// privateKey应用私钥支持PKCS1和PKCS8
// isProd是否是正式环境沙箱环境请选择新版沙箱应用。
pay.Client, err = alipay.NewClient(appId, privateKey, isProd.Bool())
if err != nil {
g.Log().Error(ctx, err)
return nil
}
// 自定义配置http请求接收返回结果body大小默认 10MB
//pay.Client.SetBodySize() // 没有特殊需求,可忽略此配置
// 打开Debug开关输出日志默认关闭
pay.Client.DebugSwitch = gopay.DebugOn
pay.Client.SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8
SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2
return pay
}