package wx import ( "git.qianqiusoft.com/qianqiusoft/light-apiengine/config" "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys" "strconv" "time" "github.com/silenceper/wechat" "github.com/silenceper/wechat/oauth" "github.com/silenceper/wechat/cache" ) var wc *wechat.Wechat var wxoauth *oauth.Oauth var wxConfig struct { WxPayCallback string // 完整微信支付回调url, AppId string // 公众号appid AppSecret string // 公众号appsecret Token string // 设置token EncodingAseKey string // ase key PayMchId string // 商户号id PayKey string // 商户支付设置的key RedirectUrl string // 微信Oauth完整跳转uri Scope string // 微信Oauth的scope ResponseType string // 微信oauth的response type State string // 微信oauth的state } func init() { c := &LocalCache{} wxConfig.WxPayCallback = config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url, wxConfig.AppId = config.AppConfig.GetKey("appid") // 公众号appid wxConfig.AppSecret = config.AppConfig.GetKey("appsecret") // 公众号appsecret wxConfig.Token = config.AppConfig.GetKey("token") wxConfig.EncodingAseKey = config.AppConfig.GetKey("encoding_aes_key") wxConfig.PayMchId = config.AppConfig.GetKey("mch_id") // 商户号id wxConfig.PayKey = config.AppConfig.GetKey("pay_key") // 商户支付设置的key wxConfig.RedirectUrl = config.AppConfig.GetKey("redirect_url") wxConfig.Scope = config.AppConfig.GetKey("scope") wxConfig.ResponseType = config.AppConfig.GetKey("response_type") wxConfig.State = config.AppConfig.GetKey("state") config := &wechat.Config{wxConfig.AppId, wxConfig.AppSecret, wxConfig.Token, wxConfig.EncodingAseKey, wxConfig.PayMchId, wxConfig.WxPayCallback, wxConfig.PayKey, c} wc = wechat.NewWechat(config) wxoauth = wc.GetOauth() } /*********************local cache*************************/ type LocalCache struct { cache.Cache m map[string]string } func (this *LocalCache) Get(key string) interface{} { if this.m == nil { return nil } tokenTime := this.m["timeout"] if tokenTime == "" { return nil } tInt64, _ := strconv.ParseInt(tokenTime, 10, 64) if time.Now().After(time.Unix(tInt64, 0)) { return nil } return this.m[key] } func (this *LocalCache) Set(key string, val interface{}, timeout time.Duration) error { if this.m == nil { this.m = make(map[string]string) } t := time.Now().Add(timeout).Unix() this.m["timeout"] = strconv.FormatInt(t, 10) this.m[key] = val.(string) return nil } /*********************************************************/ // 支付参数 type PayParams struct{ // 请求上下文 Ctx *entitys.CtrlContext // 价格(以分为单位) TotalFee int // 商品描述,默认body Body string // 额外信息,默认位user_id Attach string // 交易单号(最长32位) OutTradeNo string // 用户openid,再jsapi支付下必填,其他的可不填 OpenId string } // jsapy 支付配置,用于返回给网页设置api参数 type JSAPIPayConfig struct { Timestamp string `json:"timeStamp"` // 此处用string,ios下用int是错误的 NonceStr string `json:"nonceStr"` Package string `json:"package"` SignType string `json:"signType"` PaySign string `json:"paySign"` OutTradeNo string `json:"outTradeNo"` } // 微信oauth跳转信息, type WxOAuthConfig struct { AuthUrl string `json:"auth_url"` AppId string `json:"app_id"` // 给微信跳转的url RedirectUrl string `json:"redirect_url"` ResponseType string `json:"response_type"` Scope string `json:"scope"` State string `json:"state"` // 完整的微信Oauth的url OAuthRedirectUrl string `json:"oauth_redirect_url"` }