OAuth::getTicket()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * OAuth
4
 *
5
 * @license MIT
6
 * @author zhangv
7
 */
8
namespace zhangv\wechat\pay\util;
9
10
class OAuth {
11
	const TICKETTYPE_JSAPI = 'jsapi',TICKETTYPE_WXCARD = 'wx_card';
12
	public $responseJSON = null;
13
	public $errCode = null;
14
	public $errMsg = null;
15
16
	private $appId = null;
17
	private $appSecret = null;
18
	private $httpClient = null;
19
	private $accessToken = null;
20
21
	public function __construct($appId,$appSecret) {
22
		$this->appId = $appId;
23
		$this->appSecret = $appSecret;
24
		$this->httpClient = new HttpClient();
25
	}
26
27
	public function setHttpClient($httpClient){
28
		$this->httpClient = $httpClient;
29
	}
30
31
	public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){
32
		$redirectURI = urlencode($redirectURI);
33
		return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$redirectURI}&response_type=code&scope=$scope&state=$state#wechat_redirect";
34
	}
35
36
	public function authorize($code){
37
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code";
38
		$this->responseJSON = $this->httpClient->get($url);
39
		return json_decode($this->responseJSON);
40
	}
41
42
	public function getUserInfo($openId){
43
		$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN";
44
		$this->responseJSON = $this->httpClient->get($url);
45
		return json_decode($this->responseJSON);
46
	}
47
48
	public function refreshToken($refreshToken){
49
		$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken";
50
		$this->responseJSON = $this->httpClient->get($url);
51
		return json_decode($this->responseJSON);
52
	}
53
54
	public function verifyToken($accessToken,$openId){
55
		$url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId";
56
		$this->responseJSON = $this->httpClient->get($url);
57
		return json_decode($this->responseJSON);
58
	}
59
60
	public function getAccessToken(){
61
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
62
		$this->responseJSON = $this->httpClient->get($url);
63
		$json = json_decode($this->responseJSON);
64
		$this->accessToken = $json->access_token;
65
		return $this->accessToken;
66
	}
67
68
	public function getTicket($type = OAuth::TICKETTYPE_JSAPI, $accessToken = null){
69
		if(!$accessToken) $accessToken = $this->getAccessToken();
70
		// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
71
		$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken";
72
		$this->responseJSON = $this->httpClient->get($url);
73
		return json_decode($this->responseJSON);
74
	}
75
76
	public function getSession($code){
77
		$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appId}&secret={$this->appSecret}&js_code=$code&grant_type=authorization_code";
78
		$this->responseJSON = $this->httpClient->get($url);
79
		return json_decode($this->responseJSON);
80
	}
81
82
	public function getSignPackage($url = null, $ticket = null){
83
		if(!$url){
84
			$url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ||
85
				(!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ? "https://" : "http://";
86
			$url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
87
		}
88
		if(!$ticket) $ticket = $this->getTicket();
89
		$timestamp = time();
90
		$nonceStr = $this->getNonceStr();
91
		$rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
92
		$signature = sha1($rawString);
93
94
		$signPackage = array(
95
			"appId" => $this->appId,
96
			"nonceStr" => $nonceStr,
97
			"timestamp" => $timestamp,
98
			"url" => $url,
99
			"signature" => $signature,
100
			"rawString" => $rawString
101
		);
102
		return $signPackage;
103
	}
104
105
	private function getNonceStr() {
106
		return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32);
107
	}
108
}
109