@@ -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,12 +17,12 @@ 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 | if ($timeout < 1) { |
| 24 | 24 | curl_setopt($this->instance, CURLOPT_TIMEOUT_MS, intval($timeout * 1000)); |
| 25 | - } else { |
|
| 25 | + }else { |
|
| 26 | 26 | curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($timeout)); |
| 27 | 27 | } |
| 28 | 28 | curl_setopt($this->instance, CURLOPT_RETURNTRANSFER, true); |
@@ -31,26 +31,26 @@ discard block |
||
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - public function get($url,$params = array(),$headers = array(),$opts = array()) { |
|
| 34 | + public function get($url, $params = array(), $headers = array(), $opts = array()) { |
|
| 35 | 35 | if (!$this->instance) $this->initInstance($this->timeout); |
| 36 | - if($params && count($params) > 0) $url .= '?' . http_build_query($params); |
|
| 36 | + if ($params && count($params) > 0) $url .= '?'.http_build_query($params); |
|
| 37 | 37 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 38 | 38 | curl_setopt($this->instance, CURLOPT_HTTPGET, true); |
| 39 | 39 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
| 40 | - curl_setopt_array($this->instance,$opts); |
|
| 40 | + curl_setopt_array($this->instance, $opts); |
|
| 41 | 41 | $result = $this->execute(); |
| 42 | 42 | curl_close($this->instance); |
| 43 | 43 | $this->instance = null; |
| 44 | 44 | return $result; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - public function post($url, $params = array(),$headers = array(),$opts = array()) { |
|
| 47 | + public function post($url, $params = array(), $headers = array(), $opts = array()) { |
|
| 48 | 48 | if (!$this->instance) $this->initInstance($this->timeout); |
| 49 | 49 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 50 | 50 | curl_setopt($this->instance, CURLOPT_POST, true); |
| 51 | 51 | curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params); |
| 52 | 52 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
| 53 | - curl_setopt_array($this->instance,$opts); |
|
| 53 | + curl_setopt_array($this->instance, $opts); |
|
| 54 | 54 | $result = $this->execute(); |
| 55 | 55 | curl_close($this->instance); |
| 56 | 56 | $this->instance = null; |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | curl_setopt_array($this->instance, $optArray); |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - public function getInfo(){ |
|
| 73 | + public function getInfo() { |
|
| 74 | 74 | return $this->info; |
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | \ No newline at end of file |
@@ -32,8 +32,12 @@ discard block |
||
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | public function get($url,$params = array(),$headers = array(),$opts = array()) { |
| 35 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
| 36 | - if($params && count($params) > 0) $url .= '?' . http_build_query($params); |
|
| 35 | + if (!$this->instance) { |
|
| 36 | + $this->initInstance($this->timeout); |
|
| 37 | + } |
|
| 38 | + if($params && count($params) > 0) { |
|
| 39 | + $url .= '?' . http_build_query($params); |
|
| 40 | + } |
|
| 37 | 41 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 38 | 42 | curl_setopt($this->instance, CURLOPT_HTTPGET, true); |
| 39 | 43 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
@@ -45,7 +49,9 @@ discard block |
||
| 45 | 49 | } |
| 46 | 50 | |
| 47 | 51 | public function post($url, $params = array(),$headers = array(),$opts = array()) { |
| 48 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
| 52 | + if (!$this->instance) { |
|
| 53 | + $this->initInstance($this->timeout); |
|
| 54 | + } |
|
| 49 | 55 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 50 | 56 | curl_setopt($this->instance, CURLOPT_POST, true); |
| 51 | 57 | curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params); |
@@ -65,8 +71,12 @@ discard block |
||
| 65 | 71 | } |
| 66 | 72 | |
| 67 | 73 | public function setOpt($optArray) { |
| 68 | - if (!$this->instance) return; |
|
| 69 | - if (!is_array($optArray)) throw new \Exception("Argument is not an array!"); |
|
| 74 | + if (!$this->instance) { |
|
| 75 | + return; |
|
| 76 | + } |
|
| 77 | + if (!is_array($optArray)) { |
|
| 78 | + throw new \Exception("Argument is not an array!"); |
|
| 79 | + } |
|
| 70 | 80 | curl_setopt_array($this->instance, $optArray); |
| 71 | 81 | } |
| 72 | 82 | |
@@ -17,50 +17,50 @@ discard block |
||
| 17 | 17 | private $httpClient = null; |
| 18 | 18 | private $accessToken = null; |
| 19 | 19 | |
| 20 | - public function __construct($appId,$appSecret) { |
|
| 20 | + public function __construct($appId, $appSecret) { |
|
| 21 | 21 | $this->appId = $appId; |
| 22 | 22 | $this->appSecret = $appSecret; |
| 23 | 23 | $this->httpClient = new HttpClient(); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - public function setHttpClient($httpClient){ |
|
| 26 | + public function setHttpClient($httpClient) { |
|
| 27 | 27 | $this->httpClient = $httpClient; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - public function setAccessToken($accessToken){ |
|
| 30 | + public function setAccessToken($accessToken) { |
|
| 31 | 31 | $this->accessToken = $accessToken; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){ |
|
| 34 | + public function authorizeURI($redirectURI, $scope = 'snsapi_userinfo', $state = '') { |
|
| 35 | 35 | $redirectURI = urlencode($redirectURI); |
| 36 | 36 | return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$redirectURI}&response_type=code&scope=$scope&state=$state#wechat_redirect"; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public function authorize($code){ |
|
| 39 | + public function authorize($code) { |
|
| 40 | 40 | $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code"; |
| 41 | 41 | $this->responseJSON = $this->httpClient->get($url); |
| 42 | 42 | return json_decode($this->responseJSON); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - public function getUserInfo($openId){ |
|
| 45 | + public function getUserInfo($openId) { |
|
| 46 | 46 | $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN"; |
| 47 | 47 | $this->responseJSON = $this->httpClient->get($url); |
| 48 | 48 | return json_decode($this->responseJSON); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - public function refreshToken($refreshToken){ |
|
| 51 | + public function refreshToken($refreshToken) { |
|
| 52 | 52 | $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken"; |
| 53 | 53 | $this->responseJSON = $this->httpClient->get($url); |
| 54 | 54 | return $this->responseJSON; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - public function verifyToken($accessToken,$openId){ |
|
| 57 | + public function verifyToken($accessToken, $openId) { |
|
| 58 | 58 | $url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId"; |
| 59 | 59 | $this->responseJSON = $this->httpClient->get($url); |
| 60 | 60 | return $this->responseJSON; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - public function getAccessToken(){ |
|
| 63 | + public function getAccessToken() { |
|
| 64 | 64 | $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"; |
| 65 | 65 | $this->responseJSON = $this->httpClient->get($url); |
| 66 | 66 | $json = json_decode($this->responseJSON); |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | return $this->accessToken; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function getTicket(){ |
|
| 71 | + public function getTicket() { |
|
| 72 | 72 | $accessToken = $this->getAccessToken(); |
| 73 | 73 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
| 74 | 74 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; |
@@ -13,11 +13,11 @@ |
||
| 13 | 13 | |
| 14 | 14 | if(!$at || empty($at->openid)){ |
| 15 | 15 | die('授权失败'); |
| 16 | - }else{ |
|
| 16 | + } else{ |
|
| 17 | 17 | $accesstoken = $at->access_token; |
| 18 | 18 | $openid = $at->openid; |
| 19 | 19 | header('Location: '."http://{$_SERVER['HTTP_HOST']}/wechat-pay/demo/pay.php?openid=$openid"); |
| 20 | 20 | } |
| 21 | -}else{ |
|
| 21 | +} else{ |
|
| 22 | 22 | die('授权失败,请重试。'); |
| 23 | 23 | } |
| 24 | 24 | \ No newline at end of file |
@@ -1,24 +1,24 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | //OAuth授权回调 |
| 4 | -require_once __DIR__ . "/autoload.php"; |
|
| 4 | +require_once __DIR__."/autoload.php"; |
|
| 5 | 5 | use zhangv\wechat\WechatOAuth; |
| 6 | 6 | |
| 7 | -if (isset($_GET['code'])){ |
|
| 7 | +if (isset($_GET['code'])) { |
|
| 8 | 8 | $cfg = require './config.php'; |
| 9 | 9 | |
| 10 | 10 | $code = trim($_GET['code']); |
| 11 | 11 | $state = trim($_GET['state']); |
| 12 | - $oauth = new WechatOAuth($cfg['app_id'],$cfg['app_secret']); |
|
| 12 | + $oauth = new WechatOAuth($cfg['app_id'], $cfg['app_secret']); |
|
| 13 | 13 | $at = $oauth->authorize($code); |
| 14 | 14 | |
| 15 | - if(!$at || empty($at->openid)){ |
|
| 15 | + if (!$at || empty($at->openid)) { |
|
| 16 | 16 | die('授权失败'); |
| 17 | - }else{ |
|
| 17 | + }else { |
|
| 18 | 18 | $accesstoken = $at->access_token; |
| 19 | 19 | $openid = $at->openid; |
| 20 | 20 | header('Location: '."http://{$_SERVER['HTTP_HOST']}/wechat-pay/demo/pay.php?openid=$openid"); |
| 21 | 21 | } |
| 22 | -}else{ |
|
| 22 | +}else { |
|
| 23 | 23 | die('授权失败,请重试。'); |
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -5,14 +5,14 @@ |
||
| 5 | 5 | 'app_id' => 'XXXXXXXXXXXXXXXXXXX', //APPID |
| 6 | 6 | 'app_secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXX', //App Secret |
| 7 | 7 | 'api_key' =>'XXXXXXXXXXXXXXXXXXXXXXX', //支付密钥 |
| 8 | - 'ssl_cert_path' => __DIR__ . '/../cert/apiclient_cert.pem', |
|
| 9 | - 'ssl_key_path' => __DIR__ .'/../cert/apiclient_key.pem', |
|
| 8 | + 'ssl_cert_path' => __DIR__.'/../cert/apiclient_cert.pem', |
|
| 9 | + 'ssl_key_path' => __DIR__.'/../cert/apiclient_key.pem', |
|
| 10 | 10 | 'sign_type' => 'MD5', |
| 11 | 11 | 'notify_url' => 'http://XXX.XXX/paidnotify.php', |
| 12 | 12 | 'refund_notify_url' => 'http://XXX.XXX/refundednotify.php', |
| 13 | 13 | 'h5_scene_info' => [//required in H5 |
| 14 | 14 | 'h5_info' => ['type' => 'Wap', 'wap_url' => 'http://wapurl', 'wap_name' => 'wapname'] |
| 15 | 15 | ], |
| 16 | - 'rsa_pubkey_path' => __DIR__ .'/../cert/pubkey.pem', |
|
| 17 | - 'jsapi_ticket' => __DIR__ .'/jsapi_ticket.json' |
|
| 16 | + 'rsa_pubkey_path' => __DIR__.'/../cert/pubkey.pem', |
|
| 17 | + 'jsapi_ticket' => __DIR__.'/jsapi_ticket.json' |
|
| 18 | 18 | ]; |
| 19 | 19 | \ No newline at end of file |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | //扫码支付 |
| 4 | -require_once __DIR__ . "/autoload.php"; |
|
| 4 | +require_once __DIR__."/autoload.php"; |
|
| 5 | 5 | use zhangv\wechat\WechatPay; |
| 6 | 6 | |
| 7 | 7 | $cfg = include './config.php'; |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | $desc = "desc$stamp"; |
| 13 | 13 | $productid = "testproduct"; |
| 14 | 14 | $amt = 1; |
| 15 | -$codeurl = $payment->getCodeUrl($desc, $orderid, $amt,$productid); |
|
| 15 | +$codeurl = $payment->getCodeUrl($desc, $orderid, $amt, $productid); |
|
| 16 | 16 | |
| 17 | 17 | ?> |
| 18 | 18 | <script type="text/javascript" src="js/jquery.js"></script> |
@@ -1,12 +1,12 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if(file_exists(__DIR__ . '/../vendor/autoload.php')){ |
|
| 3 | +if (file_exists(__DIR__.'/../vendor/autoload.php')) { |
|
| 4 | 4 | /** @var \Composer\Autoload\ClassLoader $loader */ |
| 5 | - $loader = require __DIR__ . '/../vendor/autoload.php'; |
|
| 6 | -}else{//if the composer is not initialized, note: this is not work in phpunit, switch to composer when unit testing |
|
| 5 | + $loader = require __DIR__.'/../vendor/autoload.php'; |
|
| 6 | +}else {//if the composer is not initialized, note: this is not work in phpunit, switch to composer when unit testing |
|
| 7 | 7 | function __autoload($class) { |
| 8 | 8 | $class = str_replace('zhangv\\wechat\\', 'src/', $class); |
| 9 | - $file = __DIR__ . '/../' . $class . '.php'; |
|
| 9 | + $file = __DIR__.'/../'.$class.'.php'; |
|
| 10 | 10 | require_once $file; |
| 11 | 11 | } |
| 12 | 12 | spl_autoload_register('__autoload'); |
@@ -3,7 +3,7 @@ |
||
| 3 | 3 | if(file_exists(__DIR__ . '/../vendor/autoload.php')){ |
| 4 | 4 | /** @var \Composer\Autoload\ClassLoader $loader */ |
| 5 | 5 | $loader = require __DIR__ . '/../vendor/autoload.php'; |
| 6 | -}else{//if the composer is not initialized, note: this is not work in phpunit, switch to composer when unit testing |
|
| 6 | +} else{//if the composer is not initialized, note: this is not work in phpunit, switch to composer when unit testing |
|
| 7 | 7 | function __autoload($class) { |
| 8 | 8 | $class = str_replace('zhangv\\wechat\\', 'src/', $class); |
| 9 | 9 | $file = __DIR__ . '/../' . $class . '.php'; |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | //OAuth授权 |
| 4 | -require_once __DIR__ . "/autoload.php"; |
|
| 4 | +require_once __DIR__."/autoload.php"; |
|
| 5 | 5 | |
| 6 | 6 | use zhangv\wechat\WechatOAuth; |
| 7 | 7 | |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | $redirect = "http://{$_SERVER['HTTP_HOST']}/wechat-pay/demo/wxoauthcallback.php"; |
| 11 | 11 | $scope = 'snsapi_userinfo'; |
| 12 | 12 | $state = ""; |
| 13 | -$oauth =new WechatOAuth($appid,$cfg['app_secret']); |
|
| 14 | -$url = $oauth->authorizeURI($redirect,$scope); |
|
| 13 | +$oauth = new WechatOAuth($appid, $cfg['app_secret']); |
|
| 14 | +$url = $oauth->authorizeURI($redirect, $scope); |
|
| 15 | 15 | |
| 16 | 16 | header('Location: '.$redirect); |
| 17 | 17 | \ No newline at end of file |
@@ -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->onRefundedNotify($xml,function($notifydata) use ($payment){ |
|
| 10 | +$payment->onRefundedNotify($xml, function($notifydata) use ($payment){ |
|
| 11 | 11 | //do stuff |
| 12 | 12 | print_r($notifydata); |
| 13 | 13 | $payment->responseNotify(); |
@@ -1,12 +1,12 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | //公众号支付 |
| 4 | -require_once __DIR__ . "/autoload.php"; |
|
| 4 | +require_once __DIR__."/autoload.php"; |
|
| 5 | 5 | |
| 6 | 6 | use zhangv\wechat\WechatPay; |
| 7 | 7 | $cfg = include './config.php'; |
| 8 | 8 | |
| 9 | -if(empty( $_REQUEST['openid'])) { |
|
| 9 | +if (empty($_REQUEST['openid'])) { |
|
| 10 | 10 | $redirect = "http://{$_SERVER['HTTP_HOST']}/wechat-pay/demo/wxoauth.php"; |
| 11 | 11 | header('Location: '.$redirect); |
| 12 | 12 | exit; |