Completed
Push — master ( 76d6fa...9bc9b6 )
by Wei
08:43
created

WechatOAuth::refreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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