Completed
Push — master ( c385cb...b0036f )
by Wei
04:12
created
src/HttpClient.php 2 patches
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -5,9 +5,9 @@  discard block
 block discarded – undo
5 5
  */
6 6
 namespace zhangv\wechat;
7 7
 
8
-class HttpClient{
8
+class HttpClient {
9 9
 
10
-	const GET = 'get',POST = 'post', DELETE = 'delete',PUT = 'put';
10
+	const GET = 'get', POST = 'post', DELETE = 'delete', PUT = 'put';
11 11
 	private $instance = null;
12 12
 	private $errNo = null;
13 13
 	private $info = null;
@@ -17,8 +17,8 @@  discard block
 block discarded – undo
17 17
 		$this->initInstance($timeout);
18 18
 	}
19 19
 
20
-	public function initInstance($timeout){
21
-		if(!$this->instance) {
20
+	public function initInstance($timeout) {
21
+		if (!$this->instance) {
22 22
 			$this->instance = curl_init();
23 23
 			curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($timeout));
24 24
 			curl_setopt($this->instance, CURLOPT_RETURNTRANSFER, true);
@@ -27,26 +27,26 @@  discard block
 block discarded – undo
27 27
 		}
28 28
 	}
29 29
 
30
-	public function get($url,$params = array(),$headers = array(),$opts = array()) {
30
+	public function get($url, $params = array(), $headers = array(), $opts = array()) {
31 31
 		if (!$this->instance)	$this->initInstance($this->timeout);
32
-		if($params && count($params) > 0) $url .= '?' . http_build_query($params);
32
+		if ($params && count($params) > 0) $url .= '?'.http_build_query($params);
33 33
 		curl_setopt($this->instance, CURLOPT_URL, $url);
34 34
 		curl_setopt($this->instance, CURLOPT_HTTPGET, true);
35 35
 		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
36
-		curl_setopt_array($this->instance,$opts);
36
+		curl_setopt_array($this->instance, $opts);
37 37
 		$result = $this->execute();
38 38
 		curl_close($this->instance);
39 39
 		$this->instance = null;
40 40
 		return $result;
41 41
 	}
42 42
 
43
-	public function post($url, $params = array(),$headers = array(),$opts = array()) {
43
+	public function post($url, $params = array(), $headers = array(), $opts = array()) {
44 44
 		if (!$this->instance)	$this->initInstance($this->timeout);
45 45
 		curl_setopt($this->instance, CURLOPT_URL, $url);
46 46
 		curl_setopt($this->instance, CURLOPT_POST, true);
47 47
 		curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params);
48 48
 		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
49
-		curl_setopt_array($this->instance,$opts);
49
+		curl_setopt_array($this->instance, $opts);
50 50
 		$result = $this->execute();
51 51
 		curl_close($this->instance);
52 52
 		$this->instance = null;
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 		return $result;
61 61
 	}
62 62
 
63
-	public function getInfo(){
63
+	public function getInfo() {
64 64
 		return $this->info;
65 65
 	}
66 66
 }
67 67
\ No newline at end of file
Please login to merge, or discard this patch.
Braces   +9 added lines, -3 removed lines patch added patch discarded remove patch
@@ -28,8 +28,12 @@  discard block
 block discarded – undo
28 28
 	}
29 29
 
30 30
 	public function get($url,$params = array(),$headers = array(),$opts = array()) {
31
-		if (!$this->instance)	$this->initInstance($this->timeout);
32
-		if($params && count($params) > 0) $url .= '?' . http_build_query($params);
31
+		if (!$this->instance) {
32
+			$this->initInstance($this->timeout);
33
+		}
34
+		if($params && count($params) > 0) {
35
+			$url .= '?' . http_build_query($params);
36
+		}
33 37
 		curl_setopt($this->instance, CURLOPT_URL, $url);
34 38
 		curl_setopt($this->instance, CURLOPT_HTTPGET, true);
35 39
 		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
@@ -41,7 +45,9 @@  discard block
 block discarded – undo
41 45
 	}
42 46
 
43 47
 	public function post($url, $params = array(),$headers = array(),$opts = array()) {
44
-		if (!$this->instance)	$this->initInstance($this->timeout);
48
+		if (!$this->instance) {
49
+			$this->initInstance($this->timeout);
50
+		}
45 51
 		curl_setopt($this->instance, CURLOPT_URL, $url);
46 52
 		curl_setopt($this->instance, CURLOPT_POST, true);
47 53
 		curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params);
Please login to merge, or discard this patch.
demo/paidnotify.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,13 +1,13 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-require_once __DIR__ . "/autoload.php";
3
+require_once __DIR__."/autoload.php";
4 4
 use zhangv\wechat\WechatPay;
5 5
 
6 6
 $xml = file_get_contents("php://input");
7 7
 
8 8
 $cfg = require './config.php';
9 9
 $payment = new WechatPay($cfg);
10
-$payment->onPaidNotify($xml,function($notifydata) use ($payment){
10
+$payment->onPaidNotify($xml, function($notifydata) use ($payment){
11 11
 	//do stuff
12 12
 	print_r($notifydata);
13 13
 	$payment->responseNotify();
Please login to merge, or discard this patch.
src/cache/RedisCacheProvider.php 1 patch
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -4,25 +4,25 @@
 block discarded – undo
4 4
  * Time: 2018/5/29 16:41
5 5
  */
6 6
 namespace zhangv\wechat\cache;
7
-class RedisCacheProvider implements CacheProvider{
7
+class RedisCacheProvider implements CacheProvider {
8 8
 	/** @var Redis */
9 9
 	private $redis = null;
10 10
 
11
-	public function __construct($redis = null){
11
+	public function __construct($redis = null) {
12 12
 		$this->redis = $redis;
13 13
 	}
14 14
 
15
-	public function set($key,$jsonobj,$expireAt){
15
+	public function set($key, $jsonobj, $expireAt) {
16 16
 		$data = $jsonobj;
17 17
 		$data->expires_at = $expireAt;
18 18
 		$this->redis->set($key, json_encode($data));
19 19
 	}
20 20
 
21
-	public function get($key){
21
+	public function get($key) {
22 22
 		return $this->redis->get($key);
23 23
 	}
24 24
 
25
-	public function clear($key){
25
+	public function clear($key) {
26 26
 		$this->redis->delete($key);
27 27
 	}
28 28
 }
29 29
\ No newline at end of file
Please login to merge, or discard this patch.
src/cache/JsonFileCacheProvider.php 2 patches
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -4,15 +4,15 @@  discard block
 block discarded – undo
4 4
  * Time: 2018/5/29 16:41
5 5
  */
6 6
 namespace zhangv\wechat\cache;
7
-class JsonFileCacheProvider implements CacheProvider{
7
+class JsonFileCacheProvider implements CacheProvider {
8 8
 	private $cacheDir = null;
9 9
 
10
-	public function __construct($cacheDir = null){
11
-		if(!$cacheDir) $this->cacheDir = __DIR__;
10
+	public function __construct($cacheDir = null) {
11
+		if (!$cacheDir) $this->cacheDir = __DIR__;
12 12
 		else $this->cacheDir = $cacheDir;
13 13
 	}
14 14
 
15
-	public function set($key,$jsonobj,$expireAt){
15
+	public function set($key, $jsonobj, $expireAt) {
16 16
 		$data = $jsonobj;
17 17
 		$data->expires_at = $expireAt;
18 18
 		$file = "{$this->cacheDir}/{$key}.json";
@@ -21,16 +21,16 @@  discard block
 block discarded – undo
21 21
 		if ($fp) fclose($fp);
22 22
 	}
23 23
 
24
-	public function get($key){
24
+	public function get($key) {
25 25
 		$file = "{$this->cacheDir}/{$key}.json";
26 26
 		$cache = null;
27
-		if(file_exists($file)){
27
+		if (file_exists($file)) {
28 28
 			$cache = json_decode(file_get_contents($file));
29 29
 		}
30 30
 		return $cache;
31 31
 	}
32 32
 
33
-	public function clear($key){
33
+	public function clear($key) {
34 34
 		$file = "{$this->cacheDir}/{$key}.json";
35 35
 		if (file_exists($file)) {
36 36
 			unlink($file);
Please login to merge, or discard this patch.
Braces   +8 added lines, -3 removed lines patch added patch discarded remove patch
@@ -8,8 +8,11 @@  discard block
 block discarded – undo
8 8
 	private $cacheDir = null;
9 9
 
10 10
 	public function __construct($cacheDir = null){
11
-		if(!$cacheDir) $this->cacheDir = __DIR__;
12
-		else $this->cacheDir = $cacheDir;
11
+		if(!$cacheDir) {
12
+			$this->cacheDir = __DIR__;
13
+		} else {
14
+			$this->cacheDir = $cacheDir;
15
+		}
13 16
 	}
14 17
 
15 18
 	public function set($key,$jsonobj,$expireAt){
@@ -18,7 +21,9 @@  discard block
 block discarded – undo
18 21
 		$file = "{$this->cacheDir}/{$key}.json";
19 22
 		$fp = fopen($file, "w");
20 23
 		fwrite($fp, json_encode($data));
21
-		if ($fp) fclose($fp);
24
+		if ($fp) {
25
+			fclose($fp);
26
+		}
22 27
 	}
23 28
 
24 29
 	public function get($key){
Please login to merge, or discard this patch.
src/cache/CacheProvider.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -5,8 +5,8 @@
 block discarded – undo
5 5
  */
6 6
 namespace zhangv\wechat\cache;
7 7
 
8
-interface CacheProvider{
9
-	function set($key,$value,$expireAt);
8
+interface CacheProvider {
9
+	function set($key, $value, $expireAt);
10 10
 	function get($key);
11 11
 	function clear($key);
12 12
 }
13 13
\ No newline at end of file
Please login to merge, or discard this patch.
src/WechatOAuth.php 2 patches
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
 namespace zhangv\wechat;
7 7
 
8 8
 class WechatOAuth {
9
-	const TICKETTYPE_JSAPI = 'jsapi',TICKETTYPE_WXCARD = 'wx_card';
9
+	const TICKETTYPE_JSAPI = 'jsapi', TICKETTYPE_WXCARD = 'wx_card';
10 10
 	public $responseJSON = null;
11 11
 	public $errCode = null;
12 12
 	public $errMsg = null;
@@ -16,46 +16,46 @@  discard block
 block discarded – undo
16 16
 	private $httpClient = null;
17 17
 	private $accessToken = null;
18 18
 
19
-	public function __construct($appId,$appSecret) {
19
+	public function __construct($appId, $appSecret) {
20 20
 		$this->appId = $appId;
21 21
 		$this->appSecret = $appSecret;
22 22
 		$this->httpClient = new HttpClient();
23 23
 	}
24 24
 
25
-	public function setHttpClient($httpClient){
25
+	public function setHttpClient($httpClient) {
26 26
 		$this->httpClient = $httpClient;
27 27
 	}
28 28
 
29
-	public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){
29
+	public function authorizeURI($redirectURI, $scope = 'snsapi_userinfo', $state = '') {
30 30
 		$redirectURI = urlencode($redirectURI);
31 31
 		return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$redirectURI}&response_type=code&scope=$scope&state=$state#wechat_redirect";
32 32
 	}
33 33
 
34
-	public function authorize($code){
34
+	public function authorize($code) {
35 35
 		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code";
36 36
 		$this->responseJSON = $this->httpClient->get($url);
37 37
 		return json_decode($this->responseJSON);
38 38
 	}
39 39
 
40
-	public function getUserInfo($openId){
40
+	public function getUserInfo($openId) {
41 41
 		$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN";
42 42
 		$this->responseJSON = $this->httpClient->get($url);
43 43
 		return json_decode($this->responseJSON);
44 44
 	}
45 45
 
46
-	public function refreshToken($refreshToken){
46
+	public function refreshToken($refreshToken) {
47 47
 		$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken";
48 48
 		$this->responseJSON = $this->httpClient->get($url);
49 49
 		return json_decode($this->responseJSON);
50 50
 	}
51 51
 
52
-	public function verifyToken($accessToken,$openId){
52
+	public function verifyToken($accessToken, $openId) {
53 53
 		$url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId";
54 54
 		$this->responseJSON = $this->httpClient->get($url);
55 55
 		return json_decode($this->responseJSON);
56 56
 	}
57 57
 
58
-	public function getAccessToken(){
58
+	public function getAccessToken() {
59 59
 		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
60 60
 		$this->responseJSON = $this->httpClient->get($url);
61 61
 		$json = json_decode($this->responseJSON);
@@ -63,20 +63,20 @@  discard block
 block discarded – undo
63 63
 		return $this->accessToken;
64 64
 	}
65 65
 
66
-	public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){
67
-		if(!$accessToken) $accessToken = $this->getAccessToken();
66
+	public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null) {
67
+		if (!$accessToken) $accessToken = $this->getAccessToken();
68 68
 		// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
69 69
 		$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken";
70 70
 		$this->responseJSON = $this->httpClient->get($url);
71 71
 		return json_decode($this->responseJSON);
72 72
 	}
73 73
 
74
-	public function getSignPackage($url = null, $ticket = null){
75
-		if(!$url){
74
+	public function getSignPackage($url = null, $ticket = null) {
75
+		if (!$url) {
76 76
 			$url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
77 77
 			$url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
78 78
 		}
79
-		if(!$ticket) $ticket = $this->getTicket();
79
+		if (!$ticket) $ticket = $this->getTicket();
80 80
 		$timestamp = time();
81 81
 		$nonceStr = $this->getNonceStr();
82 82
 		$rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
@@ -94,6 +94,6 @@  discard block
 block discarded – undo
94 94
 	}
95 95
 
96 96
 	private function getNonceStr() {
97
-		return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32);
97
+		return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 32);
98 98
 	}
99 99
 }
Please login to merge, or discard this patch.
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -64,7 +64,9 @@  discard block
 block discarded – undo
64 64
 	}
65 65
 
66 66
 	public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){
67
-		if(!$accessToken) $accessToken = $this->getAccessToken();
67
+		if(!$accessToken) {
68
+			$accessToken = $this->getAccessToken();
69
+		}
68 70
 		// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
69 71
 		$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken";
70 72
 		$this->responseJSON = $this->httpClient->get($url);
@@ -76,7 +78,9 @@  discard block
 block discarded – undo
76 78
 			$url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
77 79
 			$url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
78 80
 		}
79
-		if(!$ticket) $ticket = $this->getTicket();
81
+		if(!$ticket) {
82
+			$ticket = $this->getTicket();
83
+		}
80 84
 		$timestamp = time();
81 85
 		$nonceStr = $this->getNonceStr();
82 86
 		$rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
Please login to merge, or discard this patch.