附近小店
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.

svc_wx.go 3.1 KiB

2 maanden geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/utils/logx"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/pay"
  8. "github.com/gin-gonic/gin"
  9. "strings"
  10. )
  11. // 微信支付回调处理
  12. func wxPayCallback(c *gin.Context) (string, error) {
  13. data, ok := c.Get("callback")
  14. if data == nil || !ok {
  15. return "", e.NewErrCode(e.ERR_INVALID_ARGS)
  16. }
  17. args := data.(*md.WxPayCallback)
  18. _, ok = db.DBs[args.MasterID]
  19. if !ok {
  20. return "", logx.Warn("wxpay Failed : master_id not found")
  21. }
  22. c.Set("mid", args.MasterID)
  23. //回调交易状态失败
  24. if args.ResultCode != "SUCCESS" || args.ReturnCode != "SUCCESS" {
  25. return "", logx.Warn("wxpay Failed : trade status failed")
  26. }
  27. return args.OutTradeNo, nil
  28. }
  29. func CommPayData(c *gin.Context, params map[string]string) (interface{}, error) {
  30. platform := c.GetHeader("Platform")
  31. browser := c.GetHeader("browser")
  32. var r interface{}
  33. var err error
  34. switch platform {
  35. case md.PLATFORM_WX_APPLET:
  36. params = WxMiniProgPayConfig(c, params)
  37. r, err = pay.WxMiniProgPay(params)
  38. case md.PLATFORM_WAP:
  39. if strings.Contains(browser, "wx_pay_browser") {
  40. params = WxJsApiConfig(c, params)
  41. r, err = pay.WxAppJSAPIPay(params)
  42. } else {
  43. params = WxH5PayConfig(c, params)
  44. r, err = pay.WxH5Pay(params)
  45. }
  46. case md.PLATFORM_ANDROID, md.PLATFORM_IOS, md.PLATFORM_JSAPI:
  47. params = WxAPPConfig(c, params)
  48. r, err = pay.WxAppPay(params)
  49. default:
  50. return nil, e.NewErrCode(e.ERR_PLATFORM)
  51. }
  52. if err != nil {
  53. return nil, err
  54. }
  55. return r, nil
  56. }
  57. func WxH5PayConfig(c *gin.Context, params map[string]string) map[string]string {
  58. params["pay_wx_appid"] = SysCfgGet(c, "wx_official_account_app_id")
  59. return params
  60. }
  61. func WxAPPConfig(c *gin.Context, params map[string]string) map[string]string {
  62. params["pay_wx_appid"] = SysCfgGet(c, "pay_wx_appid")
  63. return params
  64. }
  65. // 小程序v2
  66. func WxMiniProgPayConfig(c *gin.Context, params map[string]string) map[string]string {
  67. //读取小程序设置的
  68. wxAppletCfg := db.GetAppletKey(c, MasterDb(c))
  69. params["pay_wx_appid"] = wxAppletCfg["app_id"]
  70. // 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType(因为该路由未经过jwt-auth中间件)
  71. user, err := CheckUser(c)
  72. if user == nil || err != nil {
  73. return params
  74. }
  75. if c.GetHeader("openid") != "" { //前端会传过来
  76. user.Profile.ThirdPartyWechatMiniOpenid = c.GetHeader("openid")
  77. }
  78. if user.Profile.ThirdPartyWechatMiniOpenid == "" {
  79. return params
  80. }
  81. params["third_party_wechat_openid"] = user.Profile.ThirdPartyWechatMiniOpenid
  82. return params
  83. }
  84. func WxJsApiConfig(c *gin.Context, params map[string]string) map[string]string {
  85. params["pay_wx_appid"] = SysCfgGet(c, "wx_official_account_app_id")
  86. // 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType(因为该路由未经过jwt-auth中间件)
  87. user, err := CheckUser(c)
  88. if user == nil || err != nil {
  89. return params
  90. }
  91. if c.GetHeader("openid") != "" { //前端会传过来
  92. user.Profile.ThirdPartyWechatH5Openid = c.GetHeader("openid")
  93. }
  94. if user.Profile.ThirdPartyWechatH5Openid == "" {
  95. return params
  96. }
  97. params["third_party_wechat_openid"] = user.Profile.ThirdPartyWechatH5Openid
  98. return params
  99. }