| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 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/cache"
- )
- var wc *wechat.Wechat
- func init() {
- c := &LocalCache{}
- wxpayCallback := config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
- appId := config.AppConfig.GetKey("app_id") // 公众号appid
- appSecret := config.AppConfig.GetKey("app_secret") // 公众号app secret
- token := config.AppConfig.GetKey("token")
- encodingAseKey := config.AppConfig.GetKey("encoding_aes_key")
- payMchId := config.AppConfig.GetKey("mch_id") // 商户号id
- payKey := config.AppConfig.GetKey("pay_key") // 商户支付设置的key
- config := &wechat.Config{appId, appSecret, token, encodingAseKey, payMchId, wxpayCallback, payKey, c}
- wc = wechat.NewWechat(config)
- }
- /*********************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"`
- }
|