蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.go 3.3 KiB

4 weeks ago
1 month ago
2 weeks ago
1 month ago
1 month ago
4 weeks ago
1 month ago
4 weeks ago
1 month ago
4 weeks ago
1 month ago
4 weeks ago
1 month ago
2 weeks ago
1 month ago
4 weeks ago
1 month ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package gopay
  2. import (
  3. "applet/app/db"
  4. "applet/app/utils/cache"
  5. "applet/app/utils/logx"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/dao"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  9. "github.com/go-pay/gopay"
  10. "github.com/go-pay/gopay/alipay"
  11. "github.com/go-pay/gopay/pkg/xlog"
  12. )
  13. type InitAlipayStruct struct {
  14. IsProd bool `json:"is_prod" label:"是否生产环境"`
  15. AlipayAppId string `json:"alipay_app_id" label:"支付宝商家应用appid"`
  16. AlipayPrivateKey string `json:"alipay_private_key" label:"支付宝商家应用私钥"`
  17. AlipayPublicKey string `json:"alipay_public_key" label:"支付宝商家应用公钥"`
  18. AlipayPublicContentRSA2 []byte `json:"alipay_public_content_rsa_2" label:"支付宝公钥证书"`
  19. AlipayRootContent []byte `json:"alipay_root_content" label:"支付宝根证书"`
  20. AppPublicContent []byte `json:"app_public_content" label:"应用公钥证书"`
  21. }
  22. // InitAlipay 初始化支付宝客户端
  23. // appid:应用ID
  24. // privateKey:应用私钥,支持PKCS1和PKCS8
  25. // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。
  26. func InitAlipay(initData *InitAlipayStruct) (client *alipay.Client, err error) {
  27. if initData == nil {
  28. sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get())
  29. defer func(sysCfgDb dao.SysCfgDao) {
  30. err := sysCfgDb.Close()
  31. if err != nil {
  32. logx.Error("redis close err:" + err.Error()) // 记录错误信息
  33. }
  34. }(sysCfgDb)
  35. sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent)
  36. initData = &InitAlipayStruct{
  37. IsProd: true,
  38. AlipayAppId: sysCfgMap[enum.AlipayAppId],
  39. AlipayPrivateKey: sysCfgMap[enum.AlipayPrivateKey],
  40. AlipayPublicKey: sysCfgMap[enum.AlipayPublicKey],
  41. AlipayPublicContentRSA2: []byte(sysCfgMap[enum.AlipayPublicContentRSA2]),
  42. AlipayRootContent: []byte(sysCfgMap[enum.AlipayRootContent]),
  43. AppPublicContent: []byte(sysCfgMap[enum.AppPublicContent]),
  44. }
  45. }
  46. client, err = alipay.NewClient(initData.AlipayAppId, initData.AlipayPrivateKey, initData.IsProd)
  47. if err != nil {
  48. xlog.Error(err)
  49. return
  50. }
  51. // 自定义配置http请求接收返回结果body大小,默认 10MB
  52. client.SetBodySize(10) // 没有特殊需求,可忽略此配置
  53. // 打开Debug开关,输出日志,默认关闭
  54. client.DebugSwitch = gopay.DebugOn
  55. client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间
  56. SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8
  57. SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2
  58. // SetAppAuthToken("") //授权token
  59. //SetReturnUrl("https://www.fmm.ink"). // 设置返回URL
  60. // SetNotifyUrl("https://www.fmm.ink"). // 设置异步通知URL
  61. // 传入 支付宝公钥证书 alipayPublicCert.crt 内容
  62. client.AutoVerifySign(initData.AlipayPublicContentRSA2)
  63. // 传入证书内容
  64. err = client.SetCertSnByContent(initData.AppPublicContent, initData.AlipayRootContent, initData.AlipayPublicContentRSA2)
  65. if err != nil {
  66. xlog.Debug("SetCertSn:", err)
  67. return
  68. }
  69. return
  70. }