Exemplo n.º 1
0
/**
 * 获取微信公众号 jsapi_ticket
 * @param $app_id 微信公众号应用唯一标识
 * @param $app_secret 微信公众号应用密钥(注意保密)
 * @return array 包含 jsapi_ticket 的数组或者错误信息
 */
func GetJsapiTicket(app_id string, app_secret string) (ticket string) {
	var accessToken AccessToken
	var jsapiTicket JsapiTicket
	body := url.Values{}
	body.Add("appid", app_id)
	body.Add("secret", app_secret)
	body.Add("grant_type", "client_credential")
	accessTokenUrl := "https://api.weixin.qq.com/cgi-bin/token?" + body.Encode()
	client := &http.Client{}
	response, err := client.Get(accessTokenUrl)
	if err == nil {
		defer response.Body.Close()
		buf := new(bytes.Buffer)
		buf.ReadFrom(response.Body)
		resultbytes := buf.Bytes()
		errs := pingpp.JsonDecode(resultbytes, &accessToken)
		if pingpp.LogLevel > 2 {
			log.Printf("response from wx for AccessToken struct is :%v\n", jsapiTicket)
		}
		if errs != nil {
			if pingpp.LogLevel > 0 {
				log.Printf("Cannot Unmarshal AccessToken struct:", errs)
			}
		}
	} else {
		if pingpp.LogLevel > 0 {
			log.Printf("Http Request for AccessToken Failed")
		}
	}

	body = url.Values{}
	body.Add("access_token", accessToken.Access_token)
	body.Add("type", "jsapi")
	jsapiTicketUrl := "https://api.weixin.qq.com/cgi-bin/ticket/getticket?" + body.Encode()
	client = &http.Client{}
	response, err = client.Get(jsapiTicketUrl)

	if err == nil {
		defer response.Body.Close()
		buf := new(bytes.Buffer)
		buf.ReadFrom(response.Body)
		resultbytes := buf.Bytes()
		err := pingpp.JsonDecode(resultbytes, &jsapiTicket)
		if pingpp.LogLevel > 2 {
			log.Printf("response from wx for jsapiTcicket struct is :%v\n", jsapiTicket)
		}
		if err != nil {
			if pingpp.LogLevel > 0 {
				log.Printf("Cannot Unmarshal JsapiTicket struct:", err)
			}
		}
		ticket = jsapiTicket.Ticket
		return ticket
	} else {
		log.Printf("Http Request for JsapiTicket Failed")
	}
	return
}
Exemplo n.º 2
0
/**
 * 生成微信公众号 js sdk signature
 * @param charge Charge
 * @param jsapi_ticket string
 * @param urls string  当前页面的 url, 必须要动态获取
 * @return signatrue []byte
 */
func GetSignature(charge *pingpp.Charge, jsapi_ticket string, urls string) (signatrue []byte) {
	var wx Wx_pub
	a := charge.Credential
	m := a["wx_pub"]
	s, _ := pingpp.JsonEncode(m)
	pingpp.JsonDecode(s, &wx)
	jsapi_tickets := "jsapi_ticket=" + jsapi_ticket
	nonce_str := "noncestr=" + wx.NonceStr
	time_stamp := "timestamp=" + wx.TimeStamp
	urls = "url=" + urls
	signs := jsapi_tickets + "&" + nonce_str + "&" + time_stamp + "&" + urls
	h := sha1.New()
	h.Write([]byte(signs))
	signatrue = h.Sum(nil)
	return signatrue
}
Exemplo n.º 3
0
func GetOpenId(app_id string, app_secret string, code string) ([]byte, error) {
	var token Token
	request_url := CreateOauthUrlForOpenid(app_id, app_secret, code)
	client := &http.Client{}
	response, err := client.Get(request_url)
	if err == nil {
		defer response.Body.Close()
		buf := new(bytes.Buffer)
		buf.ReadFrom(response.Body)
		resultbytes := buf.Bytes()
		err := pingpp.JsonDecode(resultbytes, &token)
		return resultbytes, err
	} else {
		log.Printf("Http Request for OpenId Failed")
	}
	return nil, err
}