module Wechat::Utils

Constants

VERSION

Public Class Methods

create_oauth_url_for_code(app_id, redirect_url, more_info = false, state=nil) click to toggle source
# File lib/wechat/utils.rb, line 10
def create_oauth_url_for_code app_id, redirect_url, more_info = false, state=nil
  common_parts = {
    appid: app_id,
    redirect_uri: CGI::escape(redirect_url),
    response_type: 'code',
    scope: more_info ? 'snsapi_userinfo' : 'snsapi_base',
    state: state
  }
  "https://open.weixin.qq.com/connect/oauth2/authorize?#{hash_to_query common_parts}#wechat_redirect"
end
create_oauth_url_for_openid(app_id, app_secret, code) click to toggle source
# File lib/wechat/utils.rb, line 21
def create_oauth_url_for_openid app_id, app_secret, code
  query_parts = {
    appid: app_id,
    secret: app_secret,
    code: code,
    grant_type: 'authorization_code'
  }
  "https://api.weixin.qq.com/sns/oauth2/access_token?#{hash_to_query query_parts}"
end
fetch_global_access_token(appid, secret, request_opts: {}) click to toggle source
# File lib/wechat/utils.rb, line 64
def fetch_global_access_token appid, secret, request_opts: {}
  response = get_request "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}", request_opts
  return response['access_token'], response
end
fetch_jsapi_ticket(access_token, request_opts: {}) click to toggle source
# File lib/wechat/utils.rb, line 59
def fetch_jsapi_ticket access_token, request_opts: {}
  response = get_request "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi", request_opts
  return response['ticket'], response
end
fetch_oauth_user_info(access_token, openid, request_opts: {}) click to toggle source

access_token is get from oauth

# File lib/wechat/utils.rb, line 38
def fetch_oauth_user_info access_token, openid, request_opts: {}
  get_request "https://api.weixin.qq.com/sns/userinfo?access_token=#{access_token}&openid=#{openid}&lang=zh_CN", request_opts
end
fetch_openid_and_access_token(app_id, app_secret, code, request_opts: {}) click to toggle source
# File lib/wechat/utils.rb, line 31
def fetch_openid_and_access_token app_id, app_secret, code, request_opts: {}
  url = create_oauth_url_for_openid app_id, app_secret, code
  response = get_request url, request_opts
  return response['openid'], response['access_token'], response
end
fetch_user_info(access_token, openid, request_opts: {}) click to toggle source

access_token is the global token

# File lib/wechat/utils.rb, line 43
def fetch_user_info access_token, openid, request_opts: {}
  get_request "https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{access_token}&openid=#{openid}&lang=zh_CN", request_opts
end
get_request(url, extra_opts) click to toggle source
# File lib/wechat/utils.rb, line 47
def get_request url, extra_opts
  request_opts = {
    :url => url,
    :verify_ssl => false,
    :ssl_version => 'TLSv1',
    :method => 'GET',
    :headers => false,
    :timeout => 30
  }.merge(extra_opts)
  JSON.parse RestClient::Request.execute(request_opts).body
end
jsapi_params(appid, url, jsapi_ticket) click to toggle source
# File lib/wechat/utils.rb, line 69
def jsapi_params appid, url, jsapi_ticket
  timestamp = Time.now.to_i
  noncestr = SecureRandom.urlsafe_base64(12)
  signature = sign_params timestamp: timestamp, noncestr: noncestr, jsapi_ticket: jsapi_ticket, url: url
  {
    appid: appid,
    timestamp: timestamp,
    noncestr: noncestr,
    signature: signature,
    url: url
  }
end

Private Class Methods

hash_to_query(hash) click to toggle source
# File lib/wechat/utils.rb, line 84
def hash_to_query hash
  hash.map { |k, v| "#{k}=#{v}" }.join('&')
end
sign_params(options) click to toggle source
# File lib/wechat/utils.rb, line 88
def sign_params options
  to_be_singed_string = options.sort.map { |key, value| "#{key}=#{value}" }.join("&")
  Digest::SHA1.hexdigest to_be_singed_string
end