蛋蛋星球 后台端
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.
 
 
 
 
 

74 lines
3.0 KiB

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