package svc import ( "applet/app/e" "applet/app/lib/wxpay" "applet/app/pay/md" svcComm "applet/app/svc" "fmt" "github.com/gin-gonic/gin" "github.com/iGoogle-ink/gopay" v3 "github.com/iGoogle-ink/gopay/wechat/v3" "github.com/iGoogle-ink/gotil/xlog" "github.com/tidwall/gjson" ) // app支付v2 func WxAppPay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) { appId := svcComm.SysCfgGet(c, "pay_wx_appid") mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id") apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key") client := wxpay.NewClient(appId, mchId, apiKey, true) notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY) r, err := wxpay.TradeAppPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl) return r, err } // H5支付v2 func WxH5Pay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) { mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id") apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key") //读公众号的 appId := svcComm.SysCfgGet(c, "wx_official_account_app_id") fmt.Println(appId) fmt.Println(mchId) fmt.Println(apiKey) fmt.Println(params) ip := c.ClientIP() fmt.Println(ip) client := wxpay.NewClient(appId, mchId, apiKey, true) notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY) r, err := wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, ip) return r, err } // 小程序v2 func WxMiniProgPay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) { //读取小程序设置的 wxAppletKey := svcComm.SysCfgGet(c, "wx_applet_key") var appId string if wxAppletKey != "" { appId = gjson.Get(wxAppletKey, "appId").String() } fmt.Println(appId) mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id") apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key") client := wxpay.NewClient(appId, mchId, apiKey, true) notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY) // 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType(因为该路由未经过jwt-auth中间件) user, err := svcComm.CheckUser(c) if user == nil || err != nil { return nil, e.NewErr(403000, "需要登录才能支付") } if user.Profile.ThirdPartyWechatMiniOpenid == "" { return nil, e.NewErr(403000, "openid error") } r, err := wxpay.TradeMiniProgPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, user.Profile.ThirdPartyWechatMiniOpenid) return r, err } // app支付V3 func WxAppPayV3(c *gin.Context, params *md.WxPayParams) (map[string]string, error) { appId := svcComm.SysCfgGet(c, "pay_wx_miniprog_appid") mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id") SerialNo := svcComm.SysCfgGet(c, "pay_wx_api_key") ApiV3Key := svcComm.SysCfgGet(c, "pay_wx_api_key") PKContent := svcComm.SysCfgGet(c, "pay_wx_api_key") client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent) if err != nil { xlog.Error(err) return nil, err } client.DebugSwitch = gopay.DebugOff notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY) r, err := wxpay.TradeAppPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl) return r, err } // 微信JSAPI支付 func WxAppJSAPIPay(c *gin.Context, params *md.WxPayParams) (map[string]string, error) { mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id") apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key") //读公众号的 appId := svcComm.SysCfgGet(c, "wx_official_account_app_id") client := wxpay.NewClient(appId, mchId, apiKey, true) notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY) // 兼容未登录支付 api/v1/unlogin/pay/:payMethod/:orderType(因为该路由未经过jwt-auth中间件) user, err := svcComm.CheckUser(c) if user == nil || err != nil { return nil, e.NewErr(403000, "需要登录才能支付") } if user.Profile.ThirdPartyWechatH5Openid == "" { return nil, e.NewErr(403000, "openid error") } r, err := wxpay.TradeJSAPIPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, user.Profile.ThirdPartyWechatH5Openid) return r, err } // H5支付V3 func WxH5PayV3(c *gin.Context, params *md.WxPayParams) (string, error) { appId := svcComm.SysCfgGet(c, "pay_wx_appid") mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id") apiKey := svcComm.SysCfgGet(c, "pay_wx_api_key") client := wxpay.NewClient(appId, mchId, apiKey, false) notifyUrl := "" ip := c.ClientIP() _, err := wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, ip) return "", err } // 小程序V3 func WxMiniProgPayV3(c *gin.Context, params *md.WxPayParams) (string, error) { appId := svcComm.SysCfgGet(c, "pay_wx_miniprog_appid") mchId := svcComm.SysCfgGet(c, "pay_wx_mch_id") SerialNo := svcComm.SysCfgGet(c, "pay_wx_api_key") ApiV3Key := svcComm.SysCfgGet(c, "pay_wx_api_key") PKContent := svcComm.SysCfgGet(c, "pay_wx_api_key") client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent) if err != nil { xlog.Error(err) return "", err } client.DebugSwitch = gopay.DebugOff notifyUrl := fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params.OrderType, md.WX_PAY) r, err := wxpay.TradeMiniProgPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl) return r, err }