@@ -5,9 +5,9 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
@@ -28,8 +28,12 @@ discard block |
||
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 |
||
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); |
@@ -1,13 +1,13 @@ |
||
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(); |
@@ -5,8 +5,8 @@ |
||
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 |
@@ -8,8 +8,11 @@ discard block |
||
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 = null){ |
@@ -18,7 +21,9 @@ discard block |
||
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){ |
@@ -6,15 +6,15 @@ discard block |
||
6 | 6 | * @author zhangv |
7 | 7 | */ |
8 | 8 | namespace zhangv\wechat\cache; |
9 | -class JsonFileCacheProvider implements CacheProvider{ |
|
9 | +class JsonFileCacheProvider implements CacheProvider { |
|
10 | 10 | private $cacheDir = null; |
11 | 11 | |
12 | - public function __construct($cacheDir = null){ |
|
13 | - if(!$cacheDir) $this->cacheDir = __DIR__; |
|
12 | + public function __construct($cacheDir = null) { |
|
13 | + if (!$cacheDir) $this->cacheDir = __DIR__; |
|
14 | 14 | else $this->cacheDir = $cacheDir; |
15 | 15 | } |
16 | 16 | |
17 | - public function set($key,$jsonobj,$expireAt = null){ |
|
17 | + public function set($key, $jsonobj, $expireAt = null) { |
|
18 | 18 | $data = $jsonobj; |
19 | 19 | $data->expires_at = $expireAt; |
20 | 20 | $file = "{$this->cacheDir}/{$key}.json"; |
@@ -23,12 +23,12 @@ discard block |
||
23 | 23 | if ($fp) fclose($fp); |
24 | 24 | } |
25 | 25 | |
26 | - public function get($key){ |
|
26 | + public function get($key) { |
|
27 | 27 | $file = "{$this->cacheDir}/{$key}.json"; |
28 | 28 | $cache = null; |
29 | - if(file_exists($file)){ |
|
29 | + if (file_exists($file)) { |
|
30 | 30 | $cache = json_decode(file_get_contents($file)); |
31 | - if($cache->expires_at < time()){ |
|
31 | + if ($cache->expires_at < time()) { |
|
32 | 32 | $cache = null; |
33 | 33 | $this->clear($key); |
34 | 34 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | return $cache; |
37 | 37 | } |
38 | 38 | |
39 | - public function clear($key){ |
|
39 | + public function clear($key) { |
|
40 | 40 | $file = "{$this->cacheDir}/{$key}.json"; |
41 | 41 | if (file_exists($file)) { |
42 | 42 | unlink($file); |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | namespace zhangv\wechat; |
9 | 9 | |
10 | 10 | class WechatOAuth { |
11 | - const TICKETTYPE_JSAPI = 'jsapi',TICKETTYPE_WXCARD = 'wx_card'; |
|
11 | + const TICKETTYPE_JSAPI = 'jsapi', TICKETTYPE_WXCARD = 'wx_card'; |
|
12 | 12 | public $responseJSON = null; |
13 | 13 | public $errCode = null; |
14 | 14 | public $errMsg = null; |
@@ -18,46 +18,46 @@ discard block |
||
18 | 18 | private $httpClient = null; |
19 | 19 | private $accessToken = null; |
20 | 20 | |
21 | - public function __construct($appId,$appSecret) { |
|
21 | + public function __construct($appId, $appSecret) { |
|
22 | 22 | $this->appId = $appId; |
23 | 23 | $this->appSecret = $appSecret; |
24 | 24 | $this->httpClient = new HttpClient(); |
25 | 25 | } |
26 | 26 | |
27 | - public function setHttpClient($httpClient){ |
|
27 | + public function setHttpClient($httpClient) { |
|
28 | 28 | $this->httpClient = $httpClient; |
29 | 29 | } |
30 | 30 | |
31 | - public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){ |
|
31 | + public function authorizeURI($redirectURI, $scope = 'snsapi_userinfo', $state = '') { |
|
32 | 32 | $redirectURI = urlencode($redirectURI); |
33 | 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 | 34 | } |
35 | 35 | |
36 | - public function authorize($code){ |
|
36 | + public function authorize($code) { |
|
37 | 37 | $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code"; |
38 | 38 | $this->responseJSON = $this->httpClient->get($url); |
39 | 39 | return json_decode($this->responseJSON); |
40 | 40 | } |
41 | 41 | |
42 | - public function getUserInfo($openId){ |
|
42 | + public function getUserInfo($openId) { |
|
43 | 43 | $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN"; |
44 | 44 | $this->responseJSON = $this->httpClient->get($url); |
45 | 45 | return json_decode($this->responseJSON); |
46 | 46 | } |
47 | 47 | |
48 | - public function refreshToken($refreshToken){ |
|
48 | + public function refreshToken($refreshToken) { |
|
49 | 49 | $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken"; |
50 | 50 | $this->responseJSON = $this->httpClient->get($url); |
51 | 51 | return json_decode($this->responseJSON); |
52 | 52 | } |
53 | 53 | |
54 | - public function verifyToken($accessToken,$openId){ |
|
54 | + public function verifyToken($accessToken, $openId) { |
|
55 | 55 | $url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId"; |
56 | 56 | $this->responseJSON = $this->httpClient->get($url); |
57 | 57 | return json_decode($this->responseJSON); |
58 | 58 | } |
59 | 59 | |
60 | - public function getAccessToken(){ |
|
60 | + public function getAccessToken() { |
|
61 | 61 | $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"; |
62 | 62 | $this->responseJSON = $this->httpClient->get($url); |
63 | 63 | $json = json_decode($this->responseJSON); |
@@ -65,27 +65,27 @@ discard block |
||
65 | 65 | return $this->accessToken; |
66 | 66 | } |
67 | 67 | |
68 | - public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
|
69 | - if(!$accessToken) $accessToken = $this->getAccessToken(); |
|
68 | + public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null) { |
|
69 | + if (!$accessToken) $accessToken = $this->getAccessToken(); |
|
70 | 70 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
71 | 71 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
72 | 72 | $this->responseJSON = $this->httpClient->get($url); |
73 | 73 | return json_decode($this->responseJSON); |
74 | 74 | } |
75 | 75 | |
76 | - public function getSession($code){ |
|
76 | + public function getSession($code) { |
|
77 | 77 | $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appId}&secret={$this->appSecret}&js_code=$code&grant_type=authorization_code"; |
78 | 78 | $this->responseJSON = $this->httpClient->get($url); |
79 | 79 | return json_decode($this->responseJSON); |
80 | 80 | } |
81 | 81 | |
82 | - public function getSignPackage($url = null, $ticket = null){ |
|
83 | - if(!$url){ |
|
82 | + public function getSignPackage($url = null, $ticket = null) { |
|
83 | + if (!$url) { |
|
84 | 84 | $url = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || |
85 | 85 | (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ? "https://" : "http://"; |
86 | 86 | $url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; |
87 | 87 | } |
88 | - if(!$ticket) $ticket = $this->getTicket(); |
|
88 | + if (!$ticket) $ticket = $this->getTicket(); |
|
89 | 89 | $timestamp = time(); |
90 | 90 | $nonceStr = $this->getNonceStr(); |
91 | 91 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -103,6 +103,6 @@ discard block |
||
103 | 103 | } |
104 | 104 | |
105 | 105 | private function getNonceStr() { |
106 | - return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32); |
|
106 | + return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 32); |
|
107 | 107 | } |
108 | 108 | } |
@@ -66,7 +66,9 @@ discard block |
||
66 | 66 | } |
67 | 67 | |
68 | 68 | public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
69 | - if(!$accessToken) $accessToken = $this->getAccessToken(); |
|
69 | + if(!$accessToken) { |
|
70 | + $accessToken = $this->getAccessToken(); |
|
71 | + } |
|
70 | 72 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
71 | 73 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
72 | 74 | $this->responseJSON = $this->httpClient->get($url); |
@@ -85,7 +87,9 @@ discard block |
||
85 | 87 | (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ? "https://" : "http://"; |
86 | 88 | $url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; |
87 | 89 | } |
88 | - if(!$ticket) $ticket = $this->getTicket(); |
|
90 | + if(!$ticket) { |
|
91 | + $ticket = $this->getTicket(); |
|
92 | + } |
|
89 | 93 | $timestamp = time(); |
90 | 94 | $nonceStr = $this->getNonceStr(); |
91 | 95 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -6,25 +6,25 @@ |
||
6 | 6 | * @author zhangv |
7 | 7 | */ |
8 | 8 | namespace zhangv\wechat\cache; |
9 | -class RedisCacheProvider implements CacheProvider{ |
|
9 | +class RedisCacheProvider implements CacheProvider { |
|
10 | 10 | /** @var Redis */ |
11 | 11 | private $redis = null; |
12 | 12 | |
13 | - public function __construct($redis = null){ |
|
13 | + public function __construct($redis = null) { |
|
14 | 14 | $this->redis = $redis; |
15 | 15 | } |
16 | 16 | |
17 | - public function set($key,$jsonobj,$expireAt){ |
|
17 | + public function set($key, $jsonobj, $expireAt) { |
|
18 | 18 | $data = $jsonobj; |
19 | 19 | $data->expires_at = $expireAt; |
20 | 20 | $this->redis->set($key, json_encode($data)); |
21 | 21 | } |
22 | 22 | |
23 | - public function get($key){ |
|
23 | + public function get($key) { |
|
24 | 24 | return $this->redis->get($key); |
25 | 25 | } |
26 | 26 | |
27 | - public function clear($key){ |
|
27 | + public function clear($key) { |
|
28 | 28 | $this->redis->delete($key); |
29 | 29 | } |
30 | 30 | } |
31 | 31 | \ No newline at end of file |