Completed
Push — master ( 87d470...619388 )
by Wei
04:23
created
src/WechatPay.php 1 patch
Spacing   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -30,10 +30,10 @@  discard block
 block discarded – undo
30 30
  * @method static service\Coupon    Coupon(array $config)
31 31
  */
32 32
 class WechatPay {
33
-	const TRADETYPE_JSAPI = 'JSAPI',TRADETYPE_NATIVE = 'NATIVE',TRADETYPE_APP = 'APP',TRADETYPE_MWEB = 'MWEB';
33
+	const TRADETYPE_JSAPI = 'JSAPI', TRADETYPE_NATIVE = 'NATIVE', TRADETYPE_APP = 'APP', TRADETYPE_MWEB = 'MWEB';
34 34
 	const SIGNTYPE_MD5 = 'MD5', SIGNTYPE_HMACSHA256 = 'HMAC-SHA256';
35
-	const CHECKNAME_FORCECHECK = 'FORCE_CHECK',CHECKNAME_NOCHECK = 'NO_CHECK';
36
-	const ACCOUNTTYPE_BASIC = 'Basic',ACCOUNTTYPE_OPERATION = 'Operation',ACCOUNTTYPE_FEES = 'Fees';
35
+	const CHECKNAME_FORCECHECK = 'FORCE_CHECK', CHECKNAME_NOCHECK = 'NO_CHECK';
36
+	const ACCOUNTTYPE_BASIC = 'Basic', ACCOUNTTYPE_OPERATION = 'Operation', ACCOUNTTYPE_FEES = 'Fees';
37 37
 	const API_ENDPOINT = 'https://api.mch.weixin.qq.com/';
38 38
 	/** 支付 */
39 39
 	const URL_UNIFIEDORDER = 'pay/unifiedorder';
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 	 * @return mixed
119 119
 	 */
120 120
 	private static function load($name, $config) {
121
-		$service = __NAMESPACE__ . "\\service\\{$name}";
121
+		$service = __NAMESPACE__."\\service\\{$name}";
122 122
 		return new $service($config);
123 123
 	}
124 124
 
@@ -132,34 +132,34 @@  discard block
 block discarded – undo
132 132
 		return self::load($name, ...$config);
133 133
 	}
134 134
 
135
-	public function setOAuth($oauth){
135
+	public function setOAuth($oauth) {
136 136
 		$this->oauth = $oauth;
137 137
 	}
138 138
 
139
-	public function getOAuth(){
140
-		if(!$this->oauth){
141
-			$this->oauth = new OAuth($this->config['app_id'],$this->config['app_secret']);
139
+	public function getOAuth() {
140
+		if (!$this->oauth) {
141
+			$this->oauth = new OAuth($this->config['app_id'], $this->config['app_secret']);
142 142
 		}
143 143
 		return $this->oauth;
144 144
 	}
145 145
 
146
-	public function setConfig($config){
146
+	public function setConfig($config) {
147 147
 		$this->config = $config;
148 148
 	}
149 149
 
150
-	public function getConfig(){
150
+	public function getConfig() {
151 151
 		return $this->config;
152 152
 	}
153 153
 
154
-	public function setHttpClient($httpClient){
154
+	public function setHttpClient($httpClient) {
155 155
 		$this->httpClient = $httpClient;
156 156
 	}
157 157
 
158
-	public function setCacheProvider($cacheProvider){
158
+	public function setCacheProvider($cacheProvider) {
159 159
 		$this->cacheProvider = $cacheProvider;
160 160
 	}
161 161
 
162
-	public function getCacheProvider(){
162
+	public function getCacheProvider() {
163 163
 		return $this->cacheProvider;
164 164
 	}
165 165
 
@@ -172,25 +172,25 @@  discard block
 block discarded – undo
172 172
 	public function unifiedOrder($params) {
173 173
 		$data = array();
174 174
 		$data["appid"] = $this->config["app_id"];
175
-		$data["device_info"] = (isset($params['device_info'])&&trim($params['device_info'])!='')?$params['device_info']:null;
175
+		$data["device_info"] = (isset($params['device_info']) && trim($params['device_info']) != '') ? $params['device_info'] : null;
176 176
 		$data["body"] = $params['body'];
177
-		$data["detail"] = isset($params['detail'])?$params['detail']:null;//optional
178
-		$data["attach"] = isset($params['attach'])?$params['attach']:null;//optional
179
-		$data["out_trade_no"] = isset($params['out_trade_no'])?$params['out_trade_no']:null;
180
-		$data["fee_type"] = isset($params['fee_type'])?$params['fee_type']:'CNY';
177
+		$data["detail"] = isset($params['detail']) ? $params['detail'] : null; //optional
178
+		$data["attach"] = isset($params['attach']) ? $params['attach'] : null; //optional
179
+		$data["out_trade_no"] = isset($params['out_trade_no']) ? $params['out_trade_no'] : null;
180
+		$data["fee_type"] = isset($params['fee_type']) ? $params['fee_type'] : 'CNY';
181 181
 		$data["total_fee"]    = $params['total_fee'];
182 182
 		$data["spbill_create_ip"] = $params['spbill_create_ip'];
183
-		$data["time_start"] = isset($params['time_start'])?$params['time_start']:null;//optional
184
-		$data["time_expire"] = isset($params['time_expire'])?$params['time_expire']:null;//optional
185
-		$data["goods_tag"] = isset($params['goods_tag'])?$params['goods_tag']:null;
183
+		$data["time_start"] = isset($params['time_start']) ? $params['time_start'] : null; //optional
184
+		$data["time_expire"] = isset($params['time_expire']) ? $params['time_expire'] : null; //optional
185
+		$data["goods_tag"] = isset($params['goods_tag']) ? $params['goods_tag'] : null;
186 186
 		$data["notify_url"] = $this->config["notify_url"];
187 187
 		$data["trade_type"] = $params['trade_type'];
188
-		if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){
189
-			if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE');
188
+		if ($params['trade_type'] == WechatPay::TRADETYPE_NATIVE) {
189
+			if (!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE');
190 190
 			$data["product_id"] = $params['product_id'];
191 191
 		}
192
-		if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){
193
-			if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI');
192
+		if ($params['trade_type'] == WechatPay::TRADETYPE_JSAPI) {
193
+			if (!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI');
194 194
 			$data["openid"] = $params['openid'];
195 195
 		}
196 196
 		$result = $this->post(self::URL_UNIFIEDORDER, $data);
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 	 * @return array
204 204
 	 * @throws Exception
205 205
 	 */
206
-	public function queryOrderByTransactionId($transaction_id){
206
+	public function queryOrderByTransactionId($transaction_id) {
207 207
 		$data = array();
208 208
 		$data["appid"] = $this->config["app_id"];
209 209
 		$data["transaction_id"] = $transaction_id;
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
 	 * @return array
218 218
 	 * @throws Exception
219 219
 	 */
220
-	public function queryOrderByOutTradeNo($out_trade_no){
220
+	public function queryOrderByOutTradeNo($out_trade_no) {
221 221
 		$data = array();
222 222
 		$data["appid"] = $this->config["app_id"];
223 223
 		$data["out_trade_no"] = $out_trade_no;
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
 	 * @return array
232 232
 	 * @throws Exception
233 233
 	 */
234
-	public function queryRefundByTransactionId($transaction_id,$offset = 0){
234
+	public function queryRefundByTransactionId($transaction_id, $offset = 0) {
235 235
 		$data = array();
236 236
 		$data["appid"] = $this->config["app_id"];
237 237
 		$data["transaction_id"] = $transaction_id;
@@ -246,7 +246,7 @@  discard block
 block discarded – undo
246 246
 	 * @return array
247 247
 	 * @throws Exception
248 248
 	 */
249
-	public function queryRefundByOutTradeNo($out_trade_no,$offset = 0){
249
+	public function queryRefundByOutTradeNo($out_trade_no, $offset = 0) {
250 250
 		$data = array();
251 251
 		$data["appid"] = $this->config["app_id"];
252 252
 		$data["out_trade_no"] = $out_trade_no;
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 	 * @return array
262 262
 	 * @throws Exception
263 263
 	 */
264
-	public function queryRefundByRefundId($refund_id,$offset = 0){
264
+	public function queryRefundByRefundId($refund_id, $offset = 0) {
265 265
 		$data = array();
266 266
 		$data["appid"] = $this->config["app_id"];
267 267
 		$data["refund_id"] = $refund_id;
@@ -276,7 +276,7 @@  discard block
 block discarded – undo
276 276
 	 * @return array
277 277
 	 * @throws Exception
278 278
 	 */
279
-	public function queryRefundByOutRefundNo($out_refund_no,$offset = 0){
279
+	public function queryRefundByOutRefundNo($out_refund_no, $offset = 0) {
280 280
 		$data = array();
281 281
 		$data["appid"] = $this->config["app_id"];
282 282
 		$data["out_refund_no"] = $out_refund_no;
@@ -290,11 +290,11 @@  discard block
 block discarded – undo
290 290
 	 * @return array
291 291
 	 * @throws Exception
292 292
 	 */
293
-	public function closeOrder($out_trade_no){
293
+	public function closeOrder($out_trade_no) {
294 294
 		$data = array();
295 295
 		$data["appid"] = $this->config["app_id"];
296 296
 		$data["out_trade_no"] = $out_trade_no;
297
-		return $this->post(self::URL_CLOSEORDER, $data,false);
297
+		return $this->post(self::URL_CLOSEORDER, $data, false);
298 298
 	}
299 299
 
300 300
 	/**
@@ -307,14 +307,14 @@  discard block
 block discarded – undo
307 307
 	 * @return array
308 308
 	 * @throws Exception
309 309
 	 */
310
-	public function refundByOutTradeNo($out_trade_no,$out_refund_no,$total_fee,$refund_fee,$ext = array()){
311
-		$data = ($ext && is_array($ext))?$ext:array();
310
+	public function refundByOutTradeNo($out_trade_no, $out_refund_no, $total_fee, $refund_fee, $ext = array()) {
311
+		$data = ($ext && is_array($ext)) ? $ext : array();
312 312
 		$data["appid"] = $this->config["app_id"];
313 313
 		$data["out_trade_no"] = $out_trade_no;
314 314
 		$data["out_refund_no"] = $out_refund_no;
315 315
 		$data["total_fee"] = $total_fee;
316 316
 		$data["refund_fee"] = $refund_fee;
317
-		return $this->post(self::URL_REFUND, $data,true);
317
+		return $this->post(self::URL_REFUND, $data, true);
318 318
 	}
319 319
 
320 320
 	/**
@@ -327,14 +327,14 @@  discard block
 block discarded – undo
327 327
 	 * @return array
328 328
 	 * @throws Exception
329 329
 	 */
330
-	public function refundByTransactionId($transaction_id,$out_refund_no,$total_fee,$refund_fee,$ext = array()){
331
-		$data = ($ext && is_array($ext))?$ext:array();
330
+	public function refundByTransactionId($transaction_id, $out_refund_no, $total_fee, $refund_fee, $ext = array()) {
331
+		$data = ($ext && is_array($ext)) ? $ext : array();
332 332
 		$data["appid"] = $this->config["app_id"];
333 333
 		$data["transaction_id"] = $transaction_id;
334 334
 		$data["out_refund_no"] = $out_refund_no;
335 335
 		$data["total_fee"] = $total_fee;
336 336
 		$data["refund_fee"] = $refund_fee;
337
-		return $this->post(self::URL_REFUND, $data,true);
337
+		return $this->post(self::URL_REFUND, $data, true);
338 338
 	}
339 339
 
340 340
 	/**
@@ -344,7 +344,7 @@  discard block
 block discarded – undo
344 344
 	 * @return array
345 345
 	 * @throws Exception
346 346
 	 */
347
-	public function downloadBill($bill_date,$bill_type = 'ALL'){
347
+	public function downloadBill($bill_date, $bill_type = 'ALL') {
348 348
 		$data = array();
349 349
 		$data["appid"] = $this->config["app_id"];
350 350
 		$data["bill_date"] = $bill_date;
@@ -360,7 +360,7 @@  discard block
 block discarded – undo
360 360
 	 * @return array
361 361
 	 * @throws Exception
362 362
 	 */
363
-	public function downloadFundFlow($bill_date,$account_type = self::ACCOUNTTYPE_BASIC,$tar_type = 'GZIP'){
363
+	public function downloadFundFlow($bill_date, $account_type = self::ACCOUNTTYPE_BASIC, $tar_type = 'GZIP') {
364 364
 		$data = array();
365 365
 		$data["appid"] = $this->config["app_id"];
366 366
 		$data["bill_date"] = $bill_date;
@@ -378,14 +378,14 @@  discard block
 block discarded – undo
378 378
 	 * @return array
379 379
 	 * @throws Exception
380 380
 	 */
381
-	public function batchQueryComment($begin_time,$end_time,$offset = 0,$limit = 200){
381
+	public function batchQueryComment($begin_time, $end_time, $offset = 0, $limit = 200) {
382 382
 		$data = array();
383 383
 		$data["appid"] = $this->config["app_id"];
384 384
 		$data["begin_time"] = $begin_time;
385 385
 		$data["end_time"] = $end_time;
386 386
 		$data["offset"] = $offset;
387 387
 		$data["limit"] = $limit;
388
-		$data["sign"] = $this->sign($data,WechatPay::SIGNTYPE_HMACSHA256);
388
+		$data["sign"] = $this->sign($data, WechatPay::SIGNTYPE_HMACSHA256);
389 389
 		return $this->post(self::URL_BATCHQUERYCOMMENT, $data, true); //cert is required
390 390
 	}
391 391
 
@@ -396,11 +396,11 @@  discard block
 block discarded – undo
396 396
 	 * @return null
397 397
 	 * @throws Exception
398 398
 	 */
399
-	public function onPaidNotify($notify_data,callable $callback = null){
400
-		if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data);
401
-		if(!$this->validateSign($notify_data)) throw new Exception('Invalid paid notify data');
402
-		if($callback && is_callable($callback)){
403
-			return call_user_func_array( $callback , [$notify_data] );
399
+	public function onPaidNotify($notify_data, callable $callback = null) {
400
+		if (!is_array($notify_data)) $notify_data = $this->xml2array($notify_data);
401
+		if (!$this->validateSign($notify_data)) throw new Exception('Invalid paid notify data');
402
+		if ($callback && is_callable($callback)) {
403
+			return call_user_func_array($callback, [$notify_data]);
404 404
 		}
405 405
 		return null;
406 406
 	}
@@ -412,11 +412,11 @@  discard block
 block discarded – undo
412 412
 	 * @return mixed
413 413
 	 * @throws Exception
414 414
 	 */
415
-	public function onRefundedNotify($notify_data,callable $callback = null){
416
-		if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data);
417
-		if(!$this->validateSign($notify_data)) throw new Exception('Invalid refund notify data');
418
-		if($callback && is_callable($callback)){
419
-			return call_user_func_array( $callback ,[$notify_data] );
415
+	public function onRefundedNotify($notify_data, callable $callback = null) {
416
+		if (!is_array($notify_data)) $notify_data = $this->xml2array($notify_data);
417
+		if (!$this->validateSign($notify_data)) throw new Exception('Invalid refund notify data');
418
+		if ($callback && is_callable($callback)) {
419
+			return call_user_func_array($callback, [$notify_data]);
420 420
 		}
421 421
 		return null;
422 422
 	}
@@ -444,13 +444,13 @@  discard block
 block discarded – undo
444 444
 	 * @param bool $print
445 445
 	 * @return string
446 446
 	 */
447
-	public function responseNotify($print = true,$data = [],$return_code="SUCCESS", $return_msg= 'OK') {
447
+	public function responseNotify($print = true, $data = [], $return_code = "SUCCESS", $return_msg = 'OK') {
448 448
 		$data["return_code"] = $return_code;
449 449
 		if ($return_msg) {
450 450
 			$data["return_msg"] = $return_msg;
451 451
 		}
452 452
 		$xml = $this->array2xml($data);
453
-		if($print === true) print $xml;
453
+		if ($print === true) print $xml;
454 454
 		else return $xml;
455 455
 	}
456 456
 
@@ -470,8 +470,8 @@  discard block
 block discarded – undo
470 470
 	 * @return array
471 471
 	 * @throws Exception
472 472
 	 */
473
-	public function report($interface_url,$execution_time,$return_code,$result_code,$user_ip,$out_trade_no = null,$time = null,$device_info = null,
474
-	                       $return_msg = null,$err_code = null,$err_code_des = null){
473
+	public function report($interface_url, $execution_time, $return_code, $result_code, $user_ip, $out_trade_no = null, $time = null, $device_info = null,
474
+	                       $return_msg = null, $err_code = null, $err_code_des = null) {
475 475
 		$data = array();
476 476
 		$data["appid"] = $this->config["app_id"];
477 477
 		$data["interface_url"] = $interface_url;
@@ -479,12 +479,12 @@  discard block
 block discarded – undo
479 479
 		$data["return_code"] = $return_code;
480 480
 		$data["result_code"] = $result_code;
481 481
 		$data["user_ip"] = $user_ip;
482
-		if($out_trade_no) $data["out_trade_no"] = $out_trade_no;
483
-		if($time) $data["time"] = $time;
484
-		if($device_info) $data["device_info"] = $device_info;
485
-		if($return_msg) $data["return_msg"] = $return_msg;
486
-		if($err_code) $data["err_code"] = $err_code;
487
-		if($err_code_des) $data["err_code_des"] = $err_code_des;
482
+		if ($out_trade_no) $data["out_trade_no"] = $out_trade_no;
483
+		if ($time) $data["time"] = $time;
484
+		if ($device_info) $data["device_info"] = $device_info;
485
+		if ($return_msg) $data["return_msg"] = $return_msg;
486
+		if ($err_code) $data["err_code"] = $err_code;
487
+		if ($err_code_des) $data["err_code_des"] = $err_code_des;
488 488
 		return $this->post(self::URL_REPORT, $data, false);
489 489
 	}
490 490
 
@@ -494,11 +494,11 @@  discard block
 block discarded – undo
494 494
 	 * @return string
495 495
 	 * @throws Exception
496 496
 	 */
497
-	public function shortUrl($longurl){
497
+	public function shortUrl($longurl) {
498 498
 		$data = array();
499 499
 		$data["appid"] = $this->config["app_id"];
500 500
 		$data["long_url"] = $longurl;
501
-		$result = $this->post(self::URL_SHORTURL,$data,false);
501
+		$result = $this->post(self::URL_SHORTURL, $data, false);
502 502
 		return $result['short_url'];
503 503
 	}
504 504
 
@@ -507,10 +507,10 @@  discard block
 block discarded – undo
507 507
 	 * @return array
508 508
 	 * @throws Exception
509 509
 	 */
510
-	public function getSignKey(){
510
+	public function getSignKey() {
511 511
 		$data = array();
512 512
 		$data["mch_id"] = $this->config["mch_id"];
513
-		$result = $this->post($this->getSignKeyUrl,$data,false);
513
+		$result = $this->post($this->getSignKeyUrl, $data, false);
514 514
 		return $result['sandbox_signkey'];
515 515
 	}
516 516
 
@@ -520,8 +520,8 @@  discard block
 block discarded – undo
520 520
 	 * @param string $ticket
521 521
 	 * @return array
522 522
 	 */
523
-	public function getSignPackage($url, $ticket = null){
524
-		if(!$ticket) $ticket = $this->getTicket();
523
+	public function getSignPackage($url, $ticket = null) {
524
+		if (!$ticket) $ticket = $this->getTicket();
525 525
 		$timestamp = time();
526 526
 		$nonceStr = $this->getNonceStr();
527 527
 		$rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
@@ -543,29 +543,29 @@  discard block
 block discarded – undo
543 543
 	 * @param boolean $cache
544 544
 	 * @return string
545 545
 	 */
546
-	public function getTicket($cache = true){
546
+	public function getTicket($cache = true) {
547 547
 		$ticket = null;
548 548
 		$cacheKey = 'jsapi_ticket';
549
-		if($cache === true){
549
+		if ($cache === true) {
550 550
 			$data = $this->cacheProvider->get($cacheKey);
551 551
 			if ($data && $data->expires_at > time()) {
552 552
 				$ticket = $data->ticket;
553 553
 			}
554 554
 		}
555
-		if(!$ticket){
555
+		if (!$ticket) {
556 556
 			$data = $this->getOAuth()->getTicket();
557
-			if($cache === true){
558
-				$this->cacheProvider->set($cacheKey,$data,time() + $data->expires_in);
557
+			if ($cache === true) {
558
+				$this->cacheProvider->set($cacheKey, $data, time() + $data->expires_in);
559 559
 			}
560 560
 			$ticket = $data->ticket;
561 561
 		}
562 562
 		return $ticket;
563 563
 	}
564 564
 
565
-	protected function post($url, $data,$cert = true) {
566
-		if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"];
567
-		if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr();
568
-		if(!isset($data['sign'])) $data['sign'] = $this->sign($data);
565
+	protected function post($url, $data, $cert = true) {
566
+		if (!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"];
567
+		if (!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr();
568
+		if (!isset($data['sign'])) $data['sign'] = $this->sign($data);
569 569
 		$this->requestXML = $this->responseXML = null;
570 570
 		$this->requestArray = $this->responseArray = null;
571 571
 
@@ -577,23 +577,23 @@  discard block
 block discarded – undo
577 577
 			CURLOPT_RETURNTRANSFER => true,
578 578
 			CURLOPT_TIMEOUT => 10
579 579
 		];
580
-		if($cert == true){
580
+		if ($cert == true) {
581 581
 			$opts[CURLOPT_SSLCERTTYPE] = 'PEM';
582 582
 			$opts[CURLOPT_SSLCERT] = $this->config['ssl_cert_path'];
583 583
 			$opts[CURLOPT_SSLKEYTYPE] = 'PEM';
584 584
 			$opts[CURLOPT_SSLKEY] = $this->config['ssl_key_path'];
585 585
 		}
586 586
 		$processResponse = true;
587
-		if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){
587
+		if (in_array($url, [self::URL_DOWNLOADBILL, self::URL_DOWNLOAD_FUND_FLOW, self::URL_BATCHQUERYCOMMENT])) {
588 588
 			$processResponse = false;
589 589
 		}
590
-		if($this->sandbox === true) $url = "sandboxnew/{$url}";
590
+		if ($this->sandbox === true) $url = "sandboxnew/{$url}";
591 591
 
592
-		$content = $this->httpClient->post(self::API_ENDPOINT . $url,$this->requestXML,[],$opts);
593
-		if(!$content) throw new Exception("Empty response with {$this->requestXML}");
592
+		$content = $this->httpClient->post(self::API_ENDPOINT.$url, $this->requestXML, [], $opts);
593
+		if (!$content) throw new Exception("Empty response with {$this->requestXML}");
594 594
 
595 595
 		$this->responseXML = $content;
596
-		if($processResponse)
596
+		if ($processResponse)
597 597
 			return $this->processResponseXML($this->responseXML);
598 598
 		else return $this->responseXML;
599 599
 	}
@@ -603,17 +603,17 @@  discard block
 block discarded – undo
603 603
 	 * @return array
604 604
 	 * @throws Exception
605 605
 	 */
606
-	private function processResponseXML($responseXML){
606
+	private function processResponseXML($responseXML) {
607 607
 		$result = $this->xml2array($responseXML);
608 608
 		$this->responseArray = $result;
609
-		if(empty($result['return_code'])){
609
+		if (empty($result['return_code'])) {
610 610
 			throw new Exception("No return code presents in {$this->responseXML}");
611 611
 		}
612 612
 		$this->returnCode = $result["return_code"];
613
-		$this->returnMsg = isset($result['return_msg'])?$result['return_msg']:'';
613
+		$this->returnMsg = isset($result['return_msg']) ? $result['return_msg'] : '';
614 614
 
615 615
 		if ($this->returnCode == "SUCCESS") {
616
-			if(isset($result['result_code']) && $result['result_code'] == "FAIL") {
616
+			if (isset($result['result_code']) && $result['result_code'] == "FAIL") {
617 617
 				$this->resultCode = $result['result_code'];
618 618
 				$this->errCode = $result['err_code'];
619 619
 				$this->errCodeDes = $result['err_code_des'];
@@ -621,33 +621,33 @@  discard block
 block discarded – undo
621 621
 				//throw new Exception("[$this->errCode]$this->errCodeDes, with request XML: {$this->requestXML}");
622 622
 			}
623 623
 			return $result;
624
-		} else if($this->returnCode == 'FAIL'){
624
+		}else if ($this->returnCode == 'FAIL') {
625 625
 			throw new Exception($this->returnMsg);
626 626
 		}
627 627
 	}
628 628
 
629
-	public function sign($data,$sign_type = WechatPay::SIGNTYPE_MD5) {
629
+	public function sign($data, $sign_type = WechatPay::SIGNTYPE_MD5) {
630 630
 		ksort($data);
631 631
 		$string1 = "";
632 632
 		foreach ($data as $k => $v) {
633
-			if ($v && trim($v)!='') {
633
+			if ($v && trim($v) != '') {
634 634
 				$string1 .= "$k=$v&";
635 635
 			}
636 636
 		}
637
-		$stringSignTemp = $string1 . "key=" . $this->config["api_key"];
638
-		if($sign_type == WechatPay::SIGNTYPE_MD5){
637
+		$stringSignTemp = $string1."key=".$this->config["api_key"];
638
+		if ($sign_type == WechatPay::SIGNTYPE_MD5) {
639 639
 			$sign = strtoupper(md5($stringSignTemp));
640
-		}elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){
641
-			$sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"]));
640
+		}elseif ($sign_type == WechatPay::SIGNTYPE_HMACSHA256) {
641
+			$sign = strtoupper(hash_hmac('sha256', $stringSignTemp, $this->config["api_key"]));
642 642
 		}else throw new Exception("Not supported sign type - $sign_type");
643 643
 		return $sign;
644 644
 	}
645 645
 
646 646
 	private function array2xml($array) {
647
-		$xml = "<xml>" . PHP_EOL;
647
+		$xml = "<xml>".PHP_EOL;
648 648
 		foreach ($array as $k => $v) {
649
-			if($v && trim($v)!='')
650
-				$xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL;
649
+			if ($v && trim($v) != '')
650
+				$xml .= "<$k><![CDATA[$v]]></$k>".PHP_EOL;
651 651
 		}
652 652
 		$xml .= "</xml>";
653 653
 		return $xml;
@@ -656,14 +656,14 @@  discard block
 block discarded – undo
656 656
 	private function xml2array($xml) {
657 657
 		$array = [];
658 658
 		$tmp = (array) simplexml_load_string($xml);
659
-		foreach ( $tmp as $k => $v) {
659
+		foreach ($tmp as $k => $v) {
660 660
 			$array[$k] = (string) $v;
661 661
 		}
662 662
 		return $array;
663 663
 	}
664 664
 
665 665
 	protected function getNonceStr() {
666
-		return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32);
666
+		return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 32);
667 667
 	}
668 668
 
669 669
 }
670 670
\ No newline at end of file
Please login to merge, or discard this patch.
src/util/HttpClient.php 2 patches
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -7,9 +7,9 @@  discard block
 block discarded – undo
7 7
  */
8 8
 namespace zhangv\wechat\pay\util;
9 9
 
10
-class HttpClient{
10
+class HttpClient {
11 11
 
12
-	const GET = 'get',POST = 'post', DELETE = 'delete',PUT = 'put';
12
+	const GET = 'get', POST = 'post', DELETE = 'delete', PUT = 'put';
13 13
 	private $instance = null;
14 14
 	private $errNo = null;
15 15
 	private $info = null;
@@ -17,17 +17,17 @@  discard block
 block discarded – undo
17 17
 	private $retry = 1;
18 18
 	private $tried = 0;
19 19
 
20
-	public function __construct($timeout = 1,$retry = 1) {
20
+	public function __construct($timeout = 1, $retry = 1) {
21 21
 		$this->timeout = $timeout;
22 22
 		$this->retry = $retry;
23 23
 		$this->initInstance();
24 24
 	}
25 25
 
26
-	public function initInstance(){
27
-		if(!$this->instance) {
26
+	public function initInstance() {
27
+		if (!$this->instance) {
28 28
 			$this->instance = curl_init();
29
-			if($this->timeout < 1) {
30
-				curl_setopt($this->instance, CURLOPT_NOSIGNAL,1);
29
+			if ($this->timeout < 1) {
30
+				curl_setopt($this->instance, CURLOPT_NOSIGNAL, 1);
31 31
 				curl_setopt($this->instance, CURLOPT_TIMEOUT_MS, $this->timeout * 1000);
32 32
 			}else
33 33
 				curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout));
@@ -37,26 +37,26 @@  discard block
 block discarded – undo
37 37
 		}
38 38
 	}
39 39
 
40
-	public function get($url,$params = array(),$headers = array(),$opts = array()) {
40
+	public function get($url, $params = array(), $headers = array(), $opts = array()) {
41 41
 		if (!$this->instance)	$this->initInstance($this->timeout);
42
-		if($params && count($params) > 0) $url .= '?' . http_build_query($params);
42
+		if ($params && count($params) > 0) $url .= '?'.http_build_query($params);
43 43
 		curl_setopt($this->instance, CURLOPT_URL, $url);
44 44
 		curl_setopt($this->instance, CURLOPT_HTTPGET, true);
45 45
 		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
46
-		curl_setopt_array($this->instance,$opts);
46
+		curl_setopt_array($this->instance, $opts);
47 47
 		$result = $this->execute();
48 48
 		curl_close($this->instance);
49 49
 		$this->instance = null;
50 50
 		return $result;
51 51
 	}
52 52
 
53
-	public function post($url, $params = array(),$headers = array(),$opts = array()) {
53
+	public function post($url, $params = array(), $headers = array(), $opts = array()) {
54 54
 		if (!$this->instance)	$this->initInstance($this->timeout);
55 55
 		curl_setopt($this->instance, CURLOPT_URL, $url);
56 56
 		curl_setopt($this->instance, CURLOPT_POST, true);
57 57
 		curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params);
58 58
 		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
59
-		curl_setopt_array($this->instance,$opts);
59
+		curl_setopt_array($this->instance, $opts);
60 60
 		$result = $this->execute();
61 61
 		curl_close($this->instance);
62 62
 		$this->instance = null;
@@ -64,23 +64,23 @@  discard block
 block discarded – undo
64 64
 	}
65 65
 
66 66
 	private function execute() {
67
-		do{
67
+		do {
68 68
 			$result = curl_exec($this->instance);
69 69
 			$this->errNo = curl_errno($this->instance);
70
-		}while(!$result && $this->errNo === CURLE_OPERATION_TIMEDOUT && $this->tried++ < $this->retry);
70
+		} while (!$result && $this->errNo === CURLE_OPERATION_TIMEDOUT && $this->tried++ < $this->retry);
71 71
 		$this->info = curl_getinfo($this->instance);
72 72
 		return $result;
73 73
 	}
74 74
 
75
-	public function getInfo(){
75
+	public function getInfo() {
76 76
 		return $this->info;
77 77
 	}
78 78
 
79
-	public function getErrorNo(){
79
+	public function getErrorNo() {
80 80
 		return $this->errNo;
81 81
 	}
82 82
 
83
-	public function getTried(){
83
+	public function getTried() {
84 84
 		return $this->tried;
85 85
 	}
86 86
 }
87 87
\ No newline at end of file
Please login to merge, or discard this patch.
Braces   +12 added lines, -5 removed lines patch added patch discarded remove patch
@@ -29,8 +29,9 @@  discard block
 block discarded – undo
29 29
 			if($this->timeout < 1) {
30 30
 				curl_setopt($this->instance, CURLOPT_NOSIGNAL,1);
31 31
 				curl_setopt($this->instance, CURLOPT_TIMEOUT_MS, $this->timeout * 1000);
32
-			}else
33
-				curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout));
32
+			} else {
33
+							curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout));
34
+			}
34 35
 			curl_setopt($this->instance, CURLOPT_RETURNTRANSFER, true);
35 36
 			curl_setopt($this->instance, CURLOPT_FOLLOWLOCATION, true);
36 37
 			curl_setopt($this->instance, CURLOPT_SSL_VERIFYPEER, false);
@@ -38,8 +39,12 @@  discard block
 block discarded – undo
38 39
 	}
39 40
 
40 41
 	public function get($url,$params = array(),$headers = array(),$opts = array()) {
41
-		if (!$this->instance)	$this->initInstance($this->timeout);
42
-		if($params && count($params) > 0) $url .= '?' . http_build_query($params);
42
+		if (!$this->instance) {
43
+			$this->initInstance($this->timeout);
44
+		}
45
+		if($params && count($params) > 0) {
46
+			$url .= '?' . http_build_query($params);
47
+		}
43 48
 		curl_setopt($this->instance, CURLOPT_URL, $url);
44 49
 		curl_setopt($this->instance, CURLOPT_HTTPGET, true);
45 50
 		curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers);
@@ -51,7 +56,9 @@  discard block
 block discarded – undo
51 56
 	}
52 57
 
53 58
 	public function post($url, $params = array(),$headers = array(),$opts = array()) {
54
-		if (!$this->instance)	$this->initInstance($this->timeout);
59
+		if (!$this->instance) {
60
+			$this->initInstance($this->timeout);
61
+		}
55 62
 		curl_setopt($this->instance, CURLOPT_URL, $url);
56 63
 		curl_setopt($this->instance, CURLOPT_POST, true);
57 64
 		curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params);
Please login to merge, or discard this patch.
src/util/OAuth.php 2 patches
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
 namespace zhangv\wechat\pay\util;
9 9
 
10 10
 class OAuth {
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
 block discarded – undo
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
 block discarded – undo
65 65
 		return $this->accessToken;
66 66
 	}
67 67
 
68
-	public function getTicket($type = OAuth::TICKETTYPE_JSAPI, $accessToken = null){
69
-		if(!$accessToken) $accessToken = $this->getAccessToken();
68
+	public function getTicket($type = OAuth::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&timestamp=$timestamp&url=$url";
@@ -103,6 +103,6 @@  discard block
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -66,7 +66,9 @@  discard block
 block discarded – undo
66 66
 	}
67 67
 
68 68
 	public function getTicket($type = OAuth::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
 block discarded – undo
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&timestamp=$timestamp&url=$url";
Please login to merge, or discard this patch.