蛋蛋星球-客户端
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.
 
 
 
 
 

73 line
3.0 KiB

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