面包店
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

141 рядки
5.3 KiB

  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/lib/wxpay"
  5. "applet/app/pay/md"
  6. svcComm "applet/app/svc"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "github.com/iGoogle-ink/gopay"
  10. v3 "github.com/iGoogle-ink/gopay/wechat/v3"
  11. "github.com/iGoogle-ink/gotil/xlog"
  12. "github.com/tidwall/gjson"
  13. )
  14. // app支付v2
  15. func WxAppPay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) {
  16. appId := svcComm.SysCfgGet(c, "pay_wx_appid")
  17. mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id")
  18. apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key")
  19. client := wxpay.NewClient(appId, mchId, apiKey, true)
  20. notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY)
  21. r, err := wxpay.TradeAppPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  22. return r, err
  23. }
  24. // H5支付v2
  25. func WxH5Pay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) {
  26. mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id")
  27. apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key")
  28. //读公众号的
  29. appId := svcComm.SysCfgGet(c, "wx_official_account_app_id")
  30. fmt.Println(appId)
  31. fmt.Println(mchId)
  32. fmt.Println(apiKey)
  33. fmt.Println(params)
  34. ip := c.ClientIP()
  35. fmt.Println(ip)
  36. client := wxpay.NewClient(appId, mchId, apiKey, true)
  37. notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY)
  38. r, err := wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, ip)
  39. return r, err
  40. }
  41. // 小程序v2
  42. func WxMiniProgPay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) {
  43. //读取小程序设置的
  44. wxAppletKey := svcComm.SysCfgGet(c, "wx_applet_key")
  45. var appId string
  46. if wxAppletKey != "" {
  47. appId = gjson.Get(wxAppletKey, "appId").String()
  48. }
  49. fmt.Println(appId)
  50. mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id")
  51. apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key")
  52. client := wxpay.NewClient(appId, mchId, apiKey, true)
  53. notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY)
  54. // 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType(因为该路由未经过jwt-auth中间件)
  55. user, err := svcComm.CheckUser(c)
  56. if user == nil || err != nil {
  57. return nil, e.NewErr(403000, "需要登录才能支付")
  58. }
  59. if user.Profile.ThirdPartyWechatMiniOpenid == "" {
  60. return nil, e.NewErr(403000, "openid error")
  61. }
  62. r, err := wxpay.TradeMiniProgPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, user.Profile.ThirdPartyWechatMiniOpenid)
  63. return r, err
  64. }
  65. // app支付V3
  66. func WxAppPayV3(c *gin.Context, params *md.WxPayParams) (map[string]string, error) {
  67. appId := svcComm.SysCfgGet(c, "pay_wx_miniprog_appid")
  68. mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id")
  69. SerialNo := svcComm.SysCfgGet(c, "pay_wx_api_key")
  70. ApiV3Key := svcComm.SysCfgGet(c, "pay_wx_api_key")
  71. PKContent := svcComm.SysCfgGet(c, "pay_wx_api_key")
  72. client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent)
  73. if err != nil {
  74. xlog.Error(err)
  75. return nil, err
  76. }
  77. client.DebugSwitch = gopay.DebugOff
  78. notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY)
  79. r, err := wxpay.TradeAppPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  80. return r, err
  81. }
  82. // 微信JSAPI支付
  83. func WxAppJSAPIPay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) {
  84. mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id")
  85. apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key")
  86. //读公众号的
  87. appId := svcComm.SysCfgGet(c, "wx_official_account_app_id")
  88. client := wxpay.NewClient(appId, mchId, apiKey, true)
  89. notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY)
  90. // 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType(因为该路由未经过jwt-auth中间件)
  91. user, err := svcComm.CheckUser(c)
  92. if user == nil || err != nil {
  93. return nil, e.NewErr(403000, "需要登录才能支付")
  94. }
  95. if user.Profile.ThirdPartyWechatH5Openid == "" {
  96. return nil, e.NewErr(403000, "openid error")
  97. }
  98. r, err := wxpay.TradeJSAPIPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, user.Profile.ThirdPartyWechatH5Openid)
  99. return r, err
  100. }
  101. // H5支付V3
  102. func WxH5PayV3(c *gin.Context, params *md.WxPayParams) (string, error) {
  103. appId := svcComm.SysCfgGet(c, "pay_wx_appid")
  104. mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id")
  105. apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key")
  106. client := wxpay.NewClient(appId, mchId, apiKey, false)
  107. notifyUrl := ""
  108. ip := c.ClientIP()
  109. _, err := wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, ip)
  110. return "", err
  111. }
  112. // 小程序V3
  113. func WxMiniProgPayV3(c *gin.Context, params *md.WxPayParams) (string, error) {
  114. appId := svcComm.SysCfgGet(c, "pay_wx_miniprog_appid")
  115. mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id")
  116. SerialNo := svcComm.SysCfgGet(c, "pay_wx_api_key")
  117. ApiV3Key := svcComm.SysCfgGet(c, "pay_wx_api_key")
  118. PKContent := svcComm.SysCfgGet(c, "pay_wx_api_key")
  119. client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent)
  120. if err != nil {
  121. xlog.Error(err)
  122. return "", err
  123. }
  124. client.DebugSwitch = gopay.DebugOff
  125. notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY)
  126. r, err := wxpay.TradeMiniProgPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  127. return r, err
  128. }