@@ -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){ |
@@ -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){ |
@@ -139,7 +139,9 @@ discard block |
||
139 | 139 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
140 | 140 | $data["notify_url"] = $this->config["notify_url"]; |
141 | 141 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
142 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
142 | + if(!$openid) { |
|
143 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
144 | + } |
|
143 | 145 | $data["openid"] = $openid; |
144 | 146 | $result = $this->unifiedOrder($data); |
145 | 147 | return $result["prepay_id"]; |
@@ -187,7 +189,9 @@ discard block |
||
187 | 189 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
188 | 190 | $data["notify_url"] = $this->config["notify_url"]; |
189 | 191 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
190 | - if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
192 | + if(!$product_id) { |
|
193 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
194 | + } |
|
191 | 195 | $data["product_id"] = $product_id; |
192 | 196 | $result = $this->unifiedOrder($data); |
193 | 197 | return $result["code_url"]; |
@@ -211,7 +215,9 @@ discard block |
||
211 | 215 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
212 | 216 | $data["notify_url"] = $this->config["notify_url"]; |
213 | 217 | $data["trade_type"] = self::TRADETYPE_MWEB; |
214 | - if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
218 | + if(!isset($this->config['h5_scene_info'])) { |
|
219 | + throw new Exception('h5_scene_info should be configured'); |
|
220 | + } |
|
215 | 221 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
216 | 222 | $result = $this->unifiedOrder($data); |
217 | 223 | return $result["mweb_url"]; |
@@ -241,11 +247,15 @@ discard block |
||
241 | 247 | $data["notify_url"] = $this->config["notify_url"]; |
242 | 248 | $data["trade_type"] = $params['trade_type']; |
243 | 249 | if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){ |
244 | - if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
250 | + if(!isset($params['product_id'])) { |
|
251 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
252 | + } |
|
245 | 253 | $data["product_id"] = $params['product_id']; |
246 | 254 | } |
247 | 255 | if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
248 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
256 | + if(!isset($params['openid'])) { |
|
257 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
258 | + } |
|
249 | 259 | $data["openid"] = $params['openid']; |
250 | 260 | } |
251 | 261 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -488,7 +498,9 @@ discard block |
||
488 | 498 | $data["send_name"] = $send_name; |
489 | 499 | $data["re_openid"] = $re_openid; |
490 | 500 | $data["total_amount"] = $total_amount; |
491 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
501 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
502 | + throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
503 | + } |
|
492 | 504 | $data["total_num"] = $total_num; |
493 | 505 | $data["wishing"] = $wishing; |
494 | 506 | $data["act_name"] = $act_name; |
@@ -524,7 +536,9 @@ discard block |
||
524 | 536 | $data["send_name"] = $send_name; |
525 | 537 | $data["re_openid"] = $re_openid; |
526 | 538 | $data["total_amount"] = $total_amount; |
527 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
539 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
540 | + throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
541 | + } |
|
528 | 542 | $data["total_num"] = $total_num; |
529 | 543 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
530 | 544 | $data["wishing"] = $wishing; |
@@ -639,7 +653,7 @@ discard block |
||
639 | 653 | if($callback && is_callable($callback)){ |
640 | 654 | return call_user_func_array( $callback , [$notify_data] ); |
641 | 655 | } |
642 | - }else{ |
|
656 | + } else{ |
|
643 | 657 | throw new Exception('Invalid paid notify data'); |
644 | 658 | } |
645 | 659 | } |
@@ -659,7 +673,7 @@ discard block |
||
659 | 673 | if($callback && is_callable($callback)){ |
660 | 674 | return call_user_func_array( $callback ,[$notify_data] ); |
661 | 675 | } |
662 | - }else{ |
|
676 | + } else{ |
|
663 | 677 | throw new Exception('Invalid refunded notify data'); |
664 | 678 | } |
665 | 679 | } |
@@ -692,8 +706,11 @@ discard block |
||
692 | 706 | $data["return_msg"] = $return_msg; |
693 | 707 | } |
694 | 708 | $xml = $this->array2xml($data); |
695 | - if($print === true) print $xml; |
|
696 | - else return $xml; |
|
709 | + if($print === true) { |
|
710 | + print $xml; |
|
711 | + } else { |
|
712 | + return $xml; |
|
713 | + } |
|
697 | 714 | } |
698 | 715 | |
699 | 716 | /** |
@@ -721,12 +738,24 @@ discard block |
||
721 | 738 | $data["return_code"] = $return_code; |
722 | 739 | $data["result_code"] = $result_code; |
723 | 740 | $data["user_ip"] = $user_ip; |
724 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
725 | - if($time) $data["time"] = $time; |
|
726 | - if($device_info) $data["device_info"] = $device_info; |
|
727 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
728 | - if($err_code) $data["err_code"] = $err_code; |
|
729 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
741 | + if($out_trade_no) { |
|
742 | + $data["out_trade_no"] = $out_trade_no; |
|
743 | + } |
|
744 | + if($time) { |
|
745 | + $data["time"] = $time; |
|
746 | + } |
|
747 | + if($device_info) { |
|
748 | + $data["device_info"] = $device_info; |
|
749 | + } |
|
750 | + if($return_msg) { |
|
751 | + $data["return_msg"] = $return_msg; |
|
752 | + } |
|
753 | + if($err_code) { |
|
754 | + $data["err_code"] = $err_code; |
|
755 | + } |
|
756 | + if($err_code_des) { |
|
757 | + $data["err_code_des"] = $err_code_des; |
|
758 | + } |
|
730 | 759 | $result = $this->post(self::URL_REPORT, $data, false); |
731 | 760 | return $result; |
732 | 761 | } |
@@ -774,7 +803,9 @@ discard block |
||
774 | 803 | */ |
775 | 804 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
776 | 805 | $data = array(); |
777 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
806 | + if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) { |
|
807 | + throw new Exception('Real name is required'); |
|
808 | + } |
|
778 | 809 | $data["mch_appid"] = $this->config["app_id"]; |
779 | 810 | $data["mchid"] = $this->config["mch_id"]; |
780 | 811 | $data["partner_trade_no"] = $partner_trade_no; |
@@ -816,7 +847,9 @@ discard block |
||
816 | 847 | * @throws Exception |
817 | 848 | */ |
818 | 849 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
819 | - if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code: $bank_code"); |
|
850 | + if(!in_array($bank_code,array_values(self::$BANKCODE))) { |
|
851 | + throw new Exception("Unsupported bank code: $bank_code"); |
|
852 | + } |
|
820 | 853 | $data = array(); |
821 | 854 | $data["partner_trade_no"] = $partner_trade_no; |
822 | 855 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -944,13 +977,16 @@ discard block |
||
944 | 977 | } |
945 | 978 | |
946 | 979 | public function rsaEncrypt($data,$pubkey = null){ |
947 | - if(!$pubkey) $pubkey = $this->getPublicKey(); |
|
980 | + if(!$pubkey) { |
|
981 | + $pubkey = $this->getPublicKey(); |
|
982 | + } |
|
948 | 983 | $encrypted = null; |
949 | 984 | $pubkey = openssl_get_publickey($pubkey); |
950 | - if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
951 | - $data = base64_encode($encrypted); |
|
952 | - else |
|
953 | - throw new Exception('Unable to encrypt data'); |
|
985 | + if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
986 | + $data = base64_encode($encrypted); |
|
987 | + } else { |
|
988 | + throw new Exception('Unable to encrypt data'); |
|
989 | + } |
|
954 | 990 | return $data; |
955 | 991 | } |
956 | 992 | |
@@ -973,7 +1009,9 @@ discard block |
||
973 | 1009 | * @return array |
974 | 1010 | */ |
975 | 1011 | public function getSignPackage($url, $ticket = null){ |
976 | - if(!$ticket) $ticket = $this->getTicket(); |
|
1012 | + if(!$ticket) { |
|
1013 | + $ticket = $this->getTicket(); |
|
1014 | + } |
|
977 | 1015 | $timestamp = time(); |
978 | 1016 | $nonceStr = $this->getNonceStr(); |
979 | 1017 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -1015,9 +1053,15 @@ discard block |
||
1015 | 1053 | } |
1016 | 1054 | |
1017 | 1055 | private function post($url, $data,$cert = true) { |
1018 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
1019 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
1020 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
1056 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
1057 | + $data["mch_id"] = $this->config["mch_id"]; |
|
1058 | + } |
|
1059 | + if(!isset($data['nonce_str'])) { |
|
1060 | + $data["nonce_str"] = $this->getNonceStr(); |
|
1061 | + } |
|
1062 | + if(!isset($data['sign'])) { |
|
1063 | + $data['sign'] = $this->sign($data); |
|
1064 | + } |
|
1021 | 1065 | $this->requestXML = $this->responseXML = null; |
1022 | 1066 | $this->requestArray = $this->responseArray = null; |
1023 | 1067 | |
@@ -1045,12 +1089,16 @@ discard block |
||
1045 | 1089 | $url = "{$host}/sandboxnew{$url}"; |
1046 | 1090 | } |
1047 | 1091 | $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
1048 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
1092 | + if(!$content) { |
|
1093 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
1094 | + } |
|
1049 | 1095 | |
1050 | 1096 | $this->responseXML = $content; |
1051 | - if($processResponse) |
|
1052 | - return $this->processResponseXML($this->responseXML); |
|
1053 | - else return $this->responseXML; |
|
1097 | + if($processResponse) { |
|
1098 | + return $this->processResponseXML($this->responseXML); |
|
1099 | + } else { |
|
1100 | + return $this->responseXML; |
|
1101 | + } |
|
1054 | 1102 | |
1055 | 1103 | } |
1056 | 1104 | |
@@ -1069,13 +1117,13 @@ discard block |
||
1069 | 1117 | $this->errCode = $result['err_code']; |
1070 | 1118 | $this->errCodeDes = $result['err_code_des']; |
1071 | 1119 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
1072 | - }else{ |
|
1120 | + } else{ |
|
1073 | 1121 | return $result; |
1074 | 1122 | } |
1075 | 1123 | } else { |
1076 | 1124 | if($result["return_code"] == "FAIL"){ |
1077 | 1125 | throw new Exception($this->returnMsg); |
1078 | - }else{ |
|
1126 | + } else{ |
|
1079 | 1127 | $this->resultCode = $result['result_code']; |
1080 | 1128 | $this->errCode = $result['err_code']; |
1081 | 1129 | $this->errCodeDes = $result['err_code_des']; |
@@ -1095,17 +1143,20 @@ discard block |
||
1095 | 1143 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
1096 | 1144 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
1097 | 1145 | $sign = strtoupper(md5($stringSignTemp)); |
1098 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
1146 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
1099 | 1147 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
1100 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
1148 | + } else { |
|
1149 | + throw new Exception("Not supported sign type - $sign_type"); |
|
1150 | + } |
|
1101 | 1151 | return $sign; |
1102 | 1152 | } |
1103 | 1153 | |
1104 | 1154 | private function array2xml($array) { |
1105 | 1155 | $xml = "<xml>" . PHP_EOL; |
1106 | 1156 | foreach ($array as $k => $v) { |
1107 | - if($v && trim($v)!='') |
|
1108 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
1157 | + if($v && trim($v)!='') { |
|
1158 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
1159 | + } |
|
1109 | 1160 | } |
1110 | 1161 | $xml .= "</xml>"; |
1111 | 1162 | return $xml; |
@@ -1116,7 +1167,7 @@ discard block |
||
1116 | 1167 | $tmp = array(); |
1117 | 1168 | try{ |
1118 | 1169 | $tmp = (array) simplexml_load_string($xml); |
1119 | - }catch(Exception $e){ |
|
1170 | + } catch(Exception $e){ |
|
1120 | 1171 | throw $e; |
1121 | 1172 | } |
1122 | 1173 | foreach ( $tmp as $k => $v) { |
@@ -64,7 +64,9 @@ discard block |
||
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 |
||
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×tamp=$timestamp&url=$url"; |