package gopay import ( "applet/app/db" "applet/app/utils/cache" "applet/app/utils/logx" "code.fnuoos.com/EggPlanet/egg_models.git/src/dao" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" "github.com/go-pay/gopay" "github.com/go-pay/gopay/alipay" "github.com/go-pay/gopay/pkg/xlog" ) type InitAlipayStruct struct { IsProd bool `json:"is_prod" label:"是否生产环境"` AlipayAppId string `json:"alipay_app_id" label:"支付宝商家应用appid"` AlipayPrivateKey string `json:"alipay_private_key" label:"支付宝商家应用私钥"` AlipayPublicKey string `json:"alipay_public_key" label:"支付宝商家应用公钥"` AlipayPublicContentRSA2 []byte `json:"alipay_public_content_rsa_2" label:"支付宝公钥证书"` AlipayRootContent []byte `json:"alipay_root_content" label:"支付宝根证书"` AppPublicContent []byte `json:"app_public_content" label:"应用公钥证书"` } // InitAlipay 初始化支付宝客户端 // appid:应用ID // privateKey:应用私钥,支持PKCS1和PKCS8 // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。 func InitAlipay(initData *InitAlipayStruct) (client *alipay.Client, err error) { if initData == nil { sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get()) defer func(sysCfgDb dao.SysCfgDao) { err := sysCfgDb.Close() if err != nil { logx.Error("redis close err:" + err.Error()) // 记录错误信息 } }(sysCfgDb) sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent) initData = &InitAlipayStruct{ IsProd: true, AlipayAppId: sysCfgMap[enum.AlipayAppId], AlipayPrivateKey: sysCfgMap[enum.AlipayPrivateKey], AlipayPublicKey: sysCfgMap[enum.AlipayPublicKey], AlipayPublicContentRSA2: []byte(sysCfgMap[enum.AlipayPublicContentRSA2]), AlipayRootContent: []byte(sysCfgMap[enum.AlipayRootContent]), AppPublicContent: []byte(sysCfgMap[enum.AppPublicContent]), } } client, err = alipay.NewClient(initData.AlipayAppId, initData.AlipayPrivateKey, initData.IsProd) if err != nil { xlog.Error(err) return } // 自定义配置http请求接收返回结果body大小,默认 10MB client.SetBodySize(10) // 没有特殊需求,可忽略此配置 // 打开Debug开关,输出日志,默认关闭 client.DebugSwitch = gopay.DebugOn client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间 SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8 SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2 // SetAppAuthToken("") //授权token //SetReturnUrl("https://www.fmm.ink"). // 设置返回URL // SetNotifyUrl("https://www.fmm.ink"). // 设置异步通知URL // 传入 支付宝公钥证书 alipayPublicCert.crt 内容 client.AutoVerifySign(initData.AlipayPublicContentRSA2) // 传入证书内容 err = client.SetCertSnByContent(initData.AppPublicContent, initData.AlipayRootContent, initData.AlipayPublicContentRSA2) if err != nil { xlog.Debug("SetCertSn:", err) return } return }