@@ -10,8 +10,11 @@ |
||
10 | 10 | private $cacheDir = null; |
11 | 11 | |
12 | 12 | public function __construct($cacheDir = null){ |
13 | - if(!$cacheDir) $this->cacheDir = __DIR__; |
|
14 | - else $this->cacheDir = $cacheDir; |
|
13 | + if(!$cacheDir) { |
|
14 | + $this->cacheDir = __DIR__; |
|
15 | + } else { |
|
16 | + $this->cacheDir = $cacheDir; |
|
17 | + } |
|
15 | 18 | } |
16 | 19 | |
17 | 20 | public function set($key,$jsonobj,$expireAt = null){ |
@@ -43,7 +43,9 @@ |
||
43 | 43 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
44 | 44 | $data["notify_url"] = $this->config["notify_url"]; |
45 | 45 | $data["trade_type"] = self::TRADETYPE_MWEB; |
46 | - if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
46 | + if(!isset($this->config['h5_scene_info'])) { |
|
47 | + throw new Exception('h5_scene_info should be configured'); |
|
48 | + } |
|
47 | 49 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
48 | 50 | $result = $this->unifiedOrder($data); |
49 | 51 | return $result["mweb_url"]; |
@@ -36,7 +36,9 @@ discard block |
||
36 | 36 | $data["send_name"] = $send_name; |
37 | 37 | $data["re_openid"] = $re_openid; |
38 | 38 | $data["total_amount"] = $total_amount; |
39 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
39 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
40 | + throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
41 | + } |
|
40 | 42 | $data["total_num"] = $total_num; |
41 | 43 | $data["wishing"] = $wishing; |
42 | 44 | $data["act_name"] = $act_name; |
@@ -72,7 +74,9 @@ discard block |
||
72 | 74 | $data["send_name"] = $send_name; |
73 | 75 | $data["re_openid"] = $re_openid; |
74 | 76 | $data["total_amount"] = $total_amount; |
75 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
77 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
78 | + throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
79 | + } |
|
76 | 80 | $data["total_num"] = $total_num; |
77 | 81 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
78 | 82 | $data["wishing"] = $wishing; |
@@ -46,7 +46,9 @@ |
||
46 | 46 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
47 | 47 | $data["notify_url"] = $this->config["notify_url"]; |
48 | 48 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
49 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
49 | + if(!$openid) { |
|
50 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
51 | + } |
|
50 | 52 | $data["openid"] = $openid; |
51 | 53 | $result = $this->unifiedOrder($data); |
52 | 54 | return $result["prepay_id"]; |
@@ -46,7 +46,9 @@ |
||
46 | 46 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
47 | 47 | $data["notify_url"] = $this->config["notify_url"]; |
48 | 48 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
49 | - if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
49 | + if(!$product_id) { |
|
50 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
51 | + } |
|
50 | 52 | $data["product_id"] = $product_id; |
51 | 53 | $result = $this->unifiedOrder($data); |
52 | 54 | return $result["code_url"]; |
@@ -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"; |
@@ -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); |
@@ -46,7 +46,9 @@ |
||
46 | 46 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
47 | 47 | $data["notify_url"] = $this->config["notify_url"]; |
48 | 48 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
49 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
49 | + if(!$openid) { |
|
50 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
51 | + } |
|
50 | 52 | $data["openid"] = $openid; |
51 | 53 | $result = $this->unifiedOrder($data); |
52 | 54 | return $result["prepay_id"]; |
@@ -187,11 +187,15 @@ discard block |
||
187 | 187 | $data["notify_url"] = $this->config["notify_url"]; |
188 | 188 | $data["trade_type"] = $params['trade_type']; |
189 | 189 | if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){ |
190 | - if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
190 | + if(!isset($params['product_id'])) { |
|
191 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
192 | + } |
|
191 | 193 | $data["product_id"] = $params['product_id']; |
192 | 194 | } |
193 | 195 | if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
194 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
196 | + if(!isset($params['openid'])) { |
|
197 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
198 | + } |
|
195 | 199 | $data["openid"] = $params['openid']; |
196 | 200 | } |
197 | 201 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -411,8 +415,12 @@ discard block |
||
411 | 415 | * @throws Exception |
412 | 416 | */ |
413 | 417 | public function onPaidNotify($notify_data,callable $callback = null){ |
414 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
415 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid paid notify data'); |
|
418 | + if(!is_array($notify_data)) { |
|
419 | + $notify_data = $this->xml2array($notify_data); |
|
420 | + } |
|
421 | + if(!$this->validateSign($notify_data)) { |
|
422 | + throw new Exception('Invalid paid notify data'); |
|
423 | + } |
|
416 | 424 | if($callback && is_callable($callback)){ |
417 | 425 | return call_user_func_array( $callback , [$notify_data] ); |
418 | 426 | } |
@@ -426,8 +434,12 @@ discard block |
||
426 | 434 | * @throws Exception |
427 | 435 | */ |
428 | 436 | public function onRefundedNotify($notify_data,callable $callback = null){ |
429 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
430 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid refund notify data'); |
|
437 | + if(!is_array($notify_data)) { |
|
438 | + $notify_data = $this->xml2array($notify_data); |
|
439 | + } |
|
440 | + if(!$this->validateSign($notify_data)) { |
|
441 | + throw new Exception('Invalid refund notify data'); |
|
442 | + } |
|
431 | 443 | if($callback && is_callable($callback)){ |
432 | 444 | return call_user_func_array( $callback ,[$notify_data] ); |
433 | 445 | } |
@@ -461,8 +473,11 @@ discard block |
||
461 | 473 | $data["return_msg"] = $return_msg; |
462 | 474 | } |
463 | 475 | $xml = $this->array2xml($data); |
464 | - if($print === true) print $xml; |
|
465 | - else return $xml; |
|
476 | + if($print === true) { |
|
477 | + print $xml; |
|
478 | + } else { |
|
479 | + return $xml; |
|
480 | + } |
|
466 | 481 | } |
467 | 482 | |
468 | 483 | /** |
@@ -490,12 +505,24 @@ discard block |
||
490 | 505 | $data["return_code"] = $return_code; |
491 | 506 | $data["result_code"] = $result_code; |
492 | 507 | $data["user_ip"] = $user_ip; |
493 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
494 | - if($time) $data["time"] = $time; |
|
495 | - if($device_info) $data["device_info"] = $device_info; |
|
496 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
497 | - if($err_code) $data["err_code"] = $err_code; |
|
498 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
508 | + if($out_trade_no) { |
|
509 | + $data["out_trade_no"] = $out_trade_no; |
|
510 | + } |
|
511 | + if($time) { |
|
512 | + $data["time"] = $time; |
|
513 | + } |
|
514 | + if($device_info) { |
|
515 | + $data["device_info"] = $device_info; |
|
516 | + } |
|
517 | + if($return_msg) { |
|
518 | + $data["return_msg"] = $return_msg; |
|
519 | + } |
|
520 | + if($err_code) { |
|
521 | + $data["err_code"] = $err_code; |
|
522 | + } |
|
523 | + if($err_code_des) { |
|
524 | + $data["err_code_des"] = $err_code_des; |
|
525 | + } |
|
499 | 526 | return $this->post(self::URL_REPORT, $data, false); |
500 | 527 | } |
501 | 528 | |
@@ -532,7 +559,9 @@ discard block |
||
532 | 559 | * @return array |
533 | 560 | */ |
534 | 561 | public function getSignPackage($url, $ticket = null){ |
535 | - if(!$ticket) $ticket = $this->getTicket(); |
|
562 | + if(!$ticket) { |
|
563 | + $ticket = $this->getTicket(); |
|
564 | + } |
|
536 | 565 | $timestamp = time(); |
537 | 566 | $nonceStr = $this->getNonceStr(); |
538 | 567 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -574,9 +603,15 @@ discard block |
||
574 | 603 | } |
575 | 604 | |
576 | 605 | protected function post($url, $data,$cert = true) { |
577 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
578 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
579 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
606 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
607 | + $data["mch_id"] = $this->config["mch_id"]; |
|
608 | + } |
|
609 | + if(!isset($data['nonce_str'])) { |
|
610 | + $data["nonce_str"] = $this->getNonceStr(); |
|
611 | + } |
|
612 | + if(!isset($data['sign'])) { |
|
613 | + $data['sign'] = $this->sign($data); |
|
614 | + } |
|
580 | 615 | $this->requestXML = $this->responseXML = null; |
581 | 616 | $this->requestArray = $this->responseArray = null; |
582 | 617 | |
@@ -598,15 +633,21 @@ discard block |
||
598 | 633 | if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){ |
599 | 634 | $processResponse = false; |
600 | 635 | } |
601 | - if($this->sandbox === true) $url = "sandboxnew/{$url}"; |
|
636 | + if($this->sandbox === true) { |
|
637 | + $url = "sandboxnew/{$url}"; |
|
638 | + } |
|
602 | 639 | |
603 | 640 | $content = $this->httpClient->post(self::API_ENDPOINT . $url,$this->requestXML,[],$opts); |
604 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
641 | + if(!$content) { |
|
642 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
643 | + } |
|
605 | 644 | |
606 | 645 | $this->responseXML = $content; |
607 | - if($processResponse) |
|
608 | - return $this->processResponseXML($this->responseXML); |
|
609 | - else return $this->responseXML; |
|
646 | + if($processResponse) { |
|
647 | + return $this->processResponseXML($this->responseXML); |
|
648 | + } else { |
|
649 | + return $this->responseXML; |
|
650 | + } |
|
610 | 651 | } |
611 | 652 | |
612 | 653 | /** |
@@ -629,7 +670,7 @@ discard block |
||
629 | 670 | $this->errCode = $result['err_code']; |
630 | 671 | $this->errCodeDes = $result['err_code_des']; |
631 | 672 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
632 | - }else{ |
|
673 | + } else{ |
|
633 | 674 | return $result; |
634 | 675 | } |
635 | 676 | } else if($this->returnCode == 'FAIL'){ |
@@ -648,17 +689,20 @@ discard block |
||
648 | 689 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
649 | 690 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
650 | 691 | $sign = strtoupper(md5($stringSignTemp)); |
651 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
692 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
652 | 693 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
653 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
694 | + } else { |
|
695 | + throw new Exception("Not supported sign type - $sign_type"); |
|
696 | + } |
|
654 | 697 | return $sign; |
655 | 698 | } |
656 | 699 | |
657 | 700 | private function array2xml($array) { |
658 | 701 | $xml = "<xml>" . PHP_EOL; |
659 | 702 | foreach ($array as $k => $v) { |
660 | - if($v && trim($v)!='') |
|
661 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
703 | + if($v && trim($v)!='') { |
|
704 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
705 | + } |
|
662 | 706 | } |
663 | 707 | $xml .= "</xml>"; |
664 | 708 | return $xml; |