Module: Pingpp::WxPubOauth

Defined in:
lib/pingpp/wx_pub_oauth.rb

Class Method Summary collapse

Class Method Details

.create_oauth_url_for_code(app_id, redirect_url, more_info = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/pingpp/wx_pub_oauth.rb', line 5

def self.create_oauth_url_for_code(app_id, redirect_url, more_info = false)
  query_parts = {
    'appid' => app_id,
    'redirect_uri' => redirect_url,
    'response_type' => 'code',
    'scope' => more_info ? 'snsapi_userinfo' : 'snsapi_base'
  }
  query_str = Util.encode_parameters(query_parts)
  'https://open.weixin.qq.com/connect/oauth2/authorize?' + query_str + '#wechat_redirect'
end

.create_oauth_url_for_openid(app_id, app_secret, code) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/pingpp/wx_pub_oauth.rb', line 26

def self.create_oauth_url_for_openid(app_id, app_secret, code)
  query_parts = {
    'appid' => app_id,
    'secret' => app_secret,
    'code' => code,
    'grant_type' => 'authorization_code'
  }
  query_str = Util.encode_parameters(query_parts)
  'https://api.weixin.qq.com/sns/oauth2/access_token?' + query_str
end

.get_jsapi_ticket(app_id, app_secret) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pingpp/wx_pub_oauth.rb', line 51

def self.get_jsapi_ticket(app_id, app_secret)
  query_parts = {
    'appid' => app_id,
    'secret' => app_secret,
    'grant_type' => 'client_credential'
  }
  query_str = Util.encode_parameters(query_parts)
  access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?' + query_str
  resp = get_request(access_token_url)
  if !resp['errcode'].nil? then
    return resp
  end
  query_parts = {
    'access_token' => resp['access_token'],
    'type' => 'jsapi'
  }
  query_str = Util.encode_parameters(query_parts)
  jsapi_ticket_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?' + query_str
  jsapi_ticket = get_request(jsapi_ticket_url)

  return jsapi_ticket
end

.get_openid(app_id, app_secret, code) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/pingpp/wx_pub_oauth.rb', line 16

def self.get_openid(app_id, app_secret, code)
  url = create_oauth_url_for_openid(app_id, app_secret, code)
  response = get_request(url)
  if response['openid'].nil? then
    return nil, response
  else
    return response['openid'], nil
  end
end

.get_request(url) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pingpp/wx_pub_oauth.rb', line 37

def self.get_request(url)
  request_opts = {
    :url => url,
    :verify_ssl => false,
    :ssl_version => 'TLSv1',
    :method => 'GET',
    :headers => false,
    :open_timeout => 30,
    :timeout => 30
  }
  response = RestClient::Request.execute(request_opts)
  response = JSON.parse(response.body)
end

.get_signature(charge, jsapi_ticket, url) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pingpp/wx_pub_oauth.rb', line 74

def self.get_signature(charge, jsapi_ticket, url)
  if charge['credential'].nil? || charge['credential']['wx_pub'].nil? then
    return nil
  end
  credential = charge['credential']['wx_pub']
  array_to_sign = [
    'jsapi_ticket=' + jsapi_ticket,
    'noncestr=' + credential['nonceStr'],
    'timestamp=' + credential['timeStamp'].to_s,
    'url=' + url.split('#')[0]
  ]
  signature = Digest::SHA1.hexdigest(array_to_sign.join('&'))

  return signature
end