@@ -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'; |
@@ -116,7 +116,9 @@ discard block |
||
116 | 116 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
117 | 117 | $data["notify_url"] = $this->config["notify_url"]; |
118 | 118 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
119 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
119 | + if(!$openid) { |
|
120 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
121 | + } |
|
120 | 122 | $data["openid"] = $openid; |
121 | 123 | $result = $this->unifiedOrder($data); |
122 | 124 | return $result["prepay_id"]; |
@@ -164,7 +166,9 @@ discard block |
||
164 | 166 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
165 | 167 | $data["notify_url"] = $this->config["notify_url"]; |
166 | 168 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
167 | - if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
169 | + if(!$product_id) { |
|
170 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
171 | + } |
|
168 | 172 | $data["product_id"] = $product_id; |
169 | 173 | $result = $this->unifiedOrder($data); |
170 | 174 | return $result["code_url"]; |
@@ -188,7 +192,9 @@ discard block |
||
188 | 192 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
189 | 193 | $data["notify_url"] = $this->config["notify_url"]; |
190 | 194 | $data["trade_type"] = self::TRADETYPE_MWEB; |
191 | - if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
195 | + if(!isset($this->config['h5_scene_info'])) { |
|
196 | + throw new Exception('h5_scene_info should be configured'); |
|
197 | + } |
|
192 | 198 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
193 | 199 | $result = $this->unifiedOrder($data); |
194 | 200 | return $result["mweb_url"]; |
@@ -218,11 +224,15 @@ discard block |
||
218 | 224 | $data["notify_url"] = $this->config["notify_url"]; |
219 | 225 | $data["trade_type"] = $params['trade_type']; |
220 | 226 | if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){ |
221 | - if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
227 | + if(!isset($params['product_id'])) { |
|
228 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
229 | + } |
|
222 | 230 | $data["product_id"] = $params['product_id']; |
223 | 231 | } |
224 | 232 | if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
225 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
233 | + if(!isset($params['openid'])) { |
|
234 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
235 | + } |
|
226 | 236 | $data["openid"] = $params['openid']; |
227 | 237 | } |
228 | 238 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -465,7 +475,9 @@ discard block |
||
465 | 475 | $data["send_name"] = $send_name; |
466 | 476 | $data["re_openid"] = $re_openid; |
467 | 477 | $data["total_amount"] = $total_amount; |
468 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
478 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
479 | + throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
480 | + } |
|
469 | 481 | $data["total_num"] = $total_num; |
470 | 482 | $data["wishing"] = $wishing; |
471 | 483 | $data["act_name"] = $act_name; |
@@ -501,7 +513,9 @@ discard block |
||
501 | 513 | $data["send_name"] = $send_name; |
502 | 514 | $data["re_openid"] = $re_openid; |
503 | 515 | $data["total_amount"] = $total_amount; |
504 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
516 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
517 | + throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
518 | + } |
|
505 | 519 | $data["total_num"] = $total_num; |
506 | 520 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
507 | 521 | $data["wishing"] = $wishing; |
@@ -614,10 +628,10 @@ discard block |
||
614 | 628 | if($this->validateSign($notify_data)){ |
615 | 629 | if($callback && is_callable($callback)){ |
616 | 630 | return call_user_func_array( $callback , [$notify_data] ); |
617 | - }else{ |
|
631 | + } else{ |
|
618 | 632 | $this->responseNotify(); |
619 | 633 | } |
620 | - }else{ |
|
634 | + } else{ |
|
621 | 635 | throw new Exception('Invalid paid notify data'); |
622 | 636 | } |
623 | 637 | } |
@@ -636,10 +650,10 @@ discard block |
||
636 | 650 | if($this->validateSign($notify_data)){ |
637 | 651 | if($callback && is_callable($callback)){ |
638 | 652 | return call_user_func_array( $callback , $notify_data ); |
639 | - }else{ |
|
653 | + } else{ |
|
640 | 654 | $this->responseNotify(); |
641 | 655 | } |
642 | - }else{ |
|
656 | + } else{ |
|
643 | 657 | throw new Exception('Invalid refunded notify data'); |
644 | 658 | } |
645 | 659 | } |
@@ -698,12 +712,24 @@ discard block |
||
698 | 712 | $data["return_code"] = $return_code; |
699 | 713 | $data["result_code"] = $result_code; |
700 | 714 | $data["user_ip"] = $user_ip; |
701 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
702 | - if($time) $data["time"] = $time; |
|
703 | - if($device_info) $data["device_info"] = $device_info; |
|
704 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
705 | - if($err_code) $data["err_code"] = $err_code; |
|
706 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
715 | + if($out_trade_no) { |
|
716 | + $data["out_trade_no"] = $out_trade_no; |
|
717 | + } |
|
718 | + if($time) { |
|
719 | + $data["time"] = $time; |
|
720 | + } |
|
721 | + if($device_info) { |
|
722 | + $data["device_info"] = $device_info; |
|
723 | + } |
|
724 | + if($return_msg) { |
|
725 | + $data["return_msg"] = $return_msg; |
|
726 | + } |
|
727 | + if($err_code) { |
|
728 | + $data["err_code"] = $err_code; |
|
729 | + } |
|
730 | + if($err_code_des) { |
|
731 | + $data["err_code_des"] = $err_code_des; |
|
732 | + } |
|
707 | 733 | $result = $this->post(self::URL_REPORT, $data, false); |
708 | 734 | return $result; |
709 | 735 | } |
@@ -751,7 +777,9 @@ discard block |
||
751 | 777 | */ |
752 | 778 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
753 | 779 | $data = array(); |
754 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
780 | + if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) { |
|
781 | + throw new Exception('Real name is required'); |
|
782 | + } |
|
755 | 783 | $data["mch_appid"] = $this->config["app_id"]; |
756 | 784 | $data["mchid"] = $this->config["mch_id"]; |
757 | 785 | $data["partner_trade_no"] = $partner_trade_no; |
@@ -793,7 +821,9 @@ discard block |
||
793 | 821 | * @throws Exception |
794 | 822 | */ |
795 | 823 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
796 | - if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code: $bank_code"); |
|
824 | + if(!in_array($bank_code,array_values(self::$BANKCODE))) { |
|
825 | + throw new Exception("Unsupported bank code: $bank_code"); |
|
826 | + } |
|
797 | 827 | $data = array(); |
798 | 828 | $data["partner_trade_no"] = $partner_trade_no; |
799 | 829 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -901,7 +931,7 @@ discard block |
||
901 | 931 | fwrite($fp, $pubkey); |
902 | 932 | fclose($fp); |
903 | 933 | return $pubkey; |
904 | - }else { |
|
934 | + } else { |
|
905 | 935 | throw new Exception("RSA public key not found"); |
906 | 936 | } |
907 | 937 | } |
@@ -919,10 +949,11 @@ discard block |
||
919 | 949 | $pubkey = $this->getPublicKey(); |
920 | 950 | $encrypted = null; |
921 | 951 | $pubkey = openssl_get_publickey($pubkey); |
922 | - if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
923 | - $data = base64_encode($encrypted); |
|
924 | - else |
|
925 | - throw new Exception('Unable to encrypt data'); |
|
952 | + if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
953 | + $data = base64_encode($encrypted); |
|
954 | + } else { |
|
955 | + throw new Exception('Unable to encrypt data'); |
|
956 | + } |
|
926 | 957 | return $data; |
927 | 958 | } |
928 | 959 | |
@@ -971,7 +1002,9 @@ discard block |
||
971 | 1002 | $data = $this->getWechatOAuth()->getTicket(); |
972 | 1003 | $fp = fopen($this->config["jsapi_ticket"], "w"); |
973 | 1004 | fwrite($fp, $data); |
974 | - if ($fp) fclose($fp); |
|
1005 | + if ($fp) { |
|
1006 | + fclose($fp); |
|
1007 | + } |
|
975 | 1008 | $data = json_decode($data); |
976 | 1009 | } |
977 | 1010 | return $data->jsapi_ticket; |
@@ -979,9 +1012,15 @@ discard block |
||
979 | 1012 | } |
980 | 1013 | |
981 | 1014 | private function post($url, $data,$cert = true) { |
982 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
983 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
984 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
1015 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
1016 | + $data["mch_id"] = $this->config["mch_id"]; |
|
1017 | + } |
|
1018 | + if(!isset($data['nonce_str'])) { |
|
1019 | + $data["nonce_str"] = $this->getNonceStr(); |
|
1020 | + } |
|
1021 | + if(!isset($data['sign'])) { |
|
1022 | + $data['sign'] = $this->sign($data); |
|
1023 | + } |
|
985 | 1024 | $this->requestXML = $this->responseXML = null; |
986 | 1025 | $this->requestArray = $this->responseArray = null; |
987 | 1026 | |
@@ -1009,12 +1048,16 @@ discard block |
||
1009 | 1048 | $url = "{$host}/sandboxnew{$url}"; |
1010 | 1049 | } |
1011 | 1050 | $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
1012 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
1051 | + if(!$content) { |
|
1052 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
1053 | + } |
|
1013 | 1054 | |
1014 | 1055 | $this->responseXML = $content; |
1015 | - if($processResponse) |
|
1016 | - return $this->processResponseXML($this->responseXML); |
|
1017 | - else return $this->responseXML; |
|
1056 | + if($processResponse) { |
|
1057 | + return $this->processResponseXML($this->responseXML); |
|
1058 | + } else { |
|
1059 | + return $this->responseXML; |
|
1060 | + } |
|
1018 | 1061 | |
1019 | 1062 | } |
1020 | 1063 | |
@@ -1033,13 +1076,13 @@ discard block |
||
1033 | 1076 | $this->errCode = $result['err_code']; |
1034 | 1077 | $this->errCodeDes = $result['err_code_des']; |
1035 | 1078 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
1036 | - }else{ |
|
1079 | + } else{ |
|
1037 | 1080 | return $result; |
1038 | 1081 | } |
1039 | 1082 | } else { |
1040 | 1083 | if($result["return_code"] == "FAIL"){ |
1041 | 1084 | throw new Exception($this->returnMsg); |
1042 | - }else{ |
|
1085 | + } else{ |
|
1043 | 1086 | $this->resultCode = $result['result_code']; |
1044 | 1087 | $this->errCode = $result['err_code']; |
1045 | 1088 | $this->errCodeDes = $result['err_code_des']; |
@@ -1059,17 +1102,20 @@ discard block |
||
1059 | 1102 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
1060 | 1103 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
1061 | 1104 | $sign = strtoupper(md5($stringSignTemp)); |
1062 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
1105 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
1063 | 1106 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
1064 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
1107 | + } else { |
|
1108 | + throw new Exception("Not supported sign type - $sign_type"); |
|
1109 | + } |
|
1065 | 1110 | return $sign; |
1066 | 1111 | } |
1067 | 1112 | |
1068 | 1113 | private function array2xml($array) { |
1069 | 1114 | $xml = "<xml>" . PHP_EOL; |
1070 | 1115 | foreach ($array as $k => $v) { |
1071 | - if($v && trim($v)!='') |
|
1072 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
1116 | + if($v && trim($v)!='') { |
|
1117 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
1118 | + } |
|
1073 | 1119 | } |
1074 | 1120 | $xml .= "</xml>"; |
1075 | 1121 | return $xml; |
@@ -1080,7 +1126,7 @@ discard block |
||
1080 | 1126 | $tmp = array(); |
1081 | 1127 | try{ |
1082 | 1128 | $tmp = (array) simplexml_load_string($xml); |
1083 | - }catch(Exception $e){ |
|
1129 | + } catch(Exception $e){ |
|
1084 | 1130 | throw $e; |
1085 | 1131 | } |
1086 | 1132 | foreach ( $tmp as $k => $v) { |