@@ -142,7 +142,9 @@ discard block |
||
| 142 | 142 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
| 143 | 143 | $data["notify_url"] = $this->config["notify_url"]; |
| 144 | 144 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
| 145 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 145 | + if(!$openid) { |
|
| 146 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 147 | + } |
|
| 146 | 148 | $data["openid"] = $openid; |
| 147 | 149 | $result = $this->unifiedOrder($data); |
| 148 | 150 | return $result["prepay_id"]; |
@@ -190,7 +192,9 @@ discard block |
||
| 190 | 192 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
| 191 | 193 | $data["notify_url"] = $this->config["notify_url"]; |
| 192 | 194 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
| 193 | - if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 195 | + if(!$product_id) { |
|
| 196 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 197 | + } |
|
| 194 | 198 | $data["product_id"] = $product_id; |
| 195 | 199 | $result = $this->unifiedOrder($data); |
| 196 | 200 | return $result["code_url"]; |
@@ -214,7 +218,9 @@ discard block |
||
| 214 | 218 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
| 215 | 219 | $data["notify_url"] = $this->config["notify_url"]; |
| 216 | 220 | $data["trade_type"] = self::TRADETYPE_MWEB; |
| 217 | - if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
| 221 | + if(!isset($this->config['h5_scene_info'])) { |
|
| 222 | + throw new Exception('h5_scene_info should be configured'); |
|
| 223 | + } |
|
| 218 | 224 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
| 219 | 225 | $result = $this->unifiedOrder($data); |
| 220 | 226 | return $result["mweb_url"]; |
@@ -244,11 +250,15 @@ discard block |
||
| 244 | 250 | $data["notify_url"] = $this->config["notify_url"]; |
| 245 | 251 | $data["trade_type"] = $params['trade_type']; |
| 246 | 252 | if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){ |
| 247 | - if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 253 | + if(!isset($params['product_id'])) { |
|
| 254 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 255 | + } |
|
| 248 | 256 | $data["product_id"] = $params['product_id']; |
| 249 | 257 | } |
| 250 | 258 | if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
| 251 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 259 | + if(!isset($params['openid'])) { |
|
| 260 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 261 | + } |
|
| 252 | 262 | $data["openid"] = $params['openid']; |
| 253 | 263 | } |
| 254 | 264 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -491,7 +501,9 @@ discard block |
||
| 491 | 501 | $data["send_name"] = $send_name; |
| 492 | 502 | $data["re_openid"] = $re_openid; |
| 493 | 503 | $data["total_amount"] = $total_amount; |
| 494 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
| 504 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 505 | + throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
| 506 | + } |
|
| 495 | 507 | $data["total_num"] = $total_num; |
| 496 | 508 | $data["wishing"] = $wishing; |
| 497 | 509 | $data["act_name"] = $act_name; |
@@ -527,7 +539,9 @@ discard block |
||
| 527 | 539 | $data["send_name"] = $send_name; |
| 528 | 540 | $data["re_openid"] = $re_openid; |
| 529 | 541 | $data["total_amount"] = $total_amount; |
| 530 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 542 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 543 | + throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 544 | + } |
|
| 531 | 545 | $data["total_num"] = $total_num; |
| 532 | 546 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
| 533 | 547 | $data["wishing"] = $wishing; |
@@ -635,8 +649,12 @@ discard block |
||
| 635 | 649 | * @throws Exception |
| 636 | 650 | */ |
| 637 | 651 | public function onPaidNotify($notify_data,callable $callback = null){ |
| 638 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
| 639 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid paid notify data'); |
|
| 652 | + if(!is_array($notify_data)) { |
|
| 653 | + $notify_data = $this->xml2array($notify_data); |
|
| 654 | + } |
|
| 655 | + if(!$this->validateSign($notify_data)) { |
|
| 656 | + throw new Exception('Invalid paid notify data'); |
|
| 657 | + } |
|
| 640 | 658 | if($callback && is_callable($callback)){ |
| 641 | 659 | return call_user_func_array( $callback , [$notify_data] ); |
| 642 | 660 | } |
@@ -650,8 +668,12 @@ discard block |
||
| 650 | 668 | * @throws Exception |
| 651 | 669 | */ |
| 652 | 670 | public function onRefundedNotify($notify_data,callable $callback = null){ |
| 653 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
| 654 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid refund notify data'); |
|
| 671 | + if(!is_array($notify_data)) { |
|
| 672 | + $notify_data = $this->xml2array($notify_data); |
|
| 673 | + } |
|
| 674 | + if(!$this->validateSign($notify_data)) { |
|
| 675 | + throw new Exception('Invalid refund notify data'); |
|
| 676 | + } |
|
| 655 | 677 | if($callback && is_callable($callback)){ |
| 656 | 678 | return call_user_func_array( $callback ,[$notify_data] ); |
| 657 | 679 | } |
@@ -685,8 +707,11 @@ discard block |
||
| 685 | 707 | $data["return_msg"] = $return_msg; |
| 686 | 708 | } |
| 687 | 709 | $xml = $this->array2xml($data); |
| 688 | - if($print === true) print $xml; |
|
| 689 | - else return $xml; |
|
| 710 | + if($print === true) { |
|
| 711 | + print $xml; |
|
| 712 | + } else { |
|
| 713 | + return $xml; |
|
| 714 | + } |
|
| 690 | 715 | } |
| 691 | 716 | |
| 692 | 717 | /** |
@@ -714,12 +739,24 @@ discard block |
||
| 714 | 739 | $data["return_code"] = $return_code; |
| 715 | 740 | $data["result_code"] = $result_code; |
| 716 | 741 | $data["user_ip"] = $user_ip; |
| 717 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
| 718 | - if($time) $data["time"] = $time; |
|
| 719 | - if($device_info) $data["device_info"] = $device_info; |
|
| 720 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
| 721 | - if($err_code) $data["err_code"] = $err_code; |
|
| 722 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
| 742 | + if($out_trade_no) { |
|
| 743 | + $data["out_trade_no"] = $out_trade_no; |
|
| 744 | + } |
|
| 745 | + if($time) { |
|
| 746 | + $data["time"] = $time; |
|
| 747 | + } |
|
| 748 | + if($device_info) { |
|
| 749 | + $data["device_info"] = $device_info; |
|
| 750 | + } |
|
| 751 | + if($return_msg) { |
|
| 752 | + $data["return_msg"] = $return_msg; |
|
| 753 | + } |
|
| 754 | + if($err_code) { |
|
| 755 | + $data["err_code"] = $err_code; |
|
| 756 | + } |
|
| 757 | + if($err_code_des) { |
|
| 758 | + $data["err_code_des"] = $err_code_des; |
|
| 759 | + } |
|
| 723 | 760 | return $this->post(self::URL_REPORT, $data, false); |
| 724 | 761 | } |
| 725 | 762 | |
@@ -766,7 +803,9 @@ discard block |
||
| 766 | 803 | */ |
| 767 | 804 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
| 768 | 805 | $data = array(); |
| 769 | - 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 | + } |
|
| 770 | 809 | $data["mch_appid"] = $this->config["app_id"]; |
| 771 | 810 | $data["mchid"] = $this->config["mch_id"]; |
| 772 | 811 | $data["partner_trade_no"] = $partner_trade_no; |
@@ -808,7 +847,9 @@ discard block |
||
| 808 | 847 | * @throws Exception |
| 809 | 848 | */ |
| 810 | 849 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
| 811 | - 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 | + } |
|
| 812 | 853 | $data = array(); |
| 813 | 854 | $data["partner_trade_no"] = $partner_trade_no; |
| 814 | 855 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -904,7 +945,7 @@ discard block |
||
| 904 | 945 | if(!$this->publicKey) { |
| 905 | 946 | if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) { |
| 906 | 947 | $this->publicKey = file_get_contents($this->config["rsa_pubkey_path"]); |
| 907 | - }else{ |
|
| 948 | + } else{ |
|
| 908 | 949 | $data = array(); |
| 909 | 950 | $data["mch_id"] = $this->config["mch_id"]; |
| 910 | 951 | $data["sign_type"] = $this->config["sign_type"]; |
@@ -933,13 +974,16 @@ discard block |
||
| 933 | 974 | } |
| 934 | 975 | |
| 935 | 976 | public function rsaEncrypt($data,$pubkey = null){ |
| 936 | - if(!$pubkey) $pubkey = $this->getPublicKey(); |
|
| 977 | + if(!$pubkey) { |
|
| 978 | + $pubkey = $this->getPublicKey(); |
|
| 979 | + } |
|
| 937 | 980 | $encrypted = null; |
| 938 | 981 | $pubkey = openssl_get_publickey($pubkey); |
| 939 | - if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 940 | - $data = base64_encode($encrypted); |
|
| 941 | - else |
|
| 942 | - throw new Exception('Unable to encrypt data'); |
|
| 982 | + if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
| 983 | + $data = base64_encode($encrypted); |
|
| 984 | + } else { |
|
| 985 | + throw new Exception('Unable to encrypt data'); |
|
| 986 | + } |
|
| 943 | 987 | return $data; |
| 944 | 988 | } |
| 945 | 989 | |
@@ -962,7 +1006,9 @@ discard block |
||
| 962 | 1006 | * @return array |
| 963 | 1007 | */ |
| 964 | 1008 | public function getSignPackage($url, $ticket = null){ |
| 965 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 1009 | + if(!$ticket) { |
|
| 1010 | + $ticket = $this->getTicket(); |
|
| 1011 | + } |
|
| 966 | 1012 | $timestamp = time(); |
| 967 | 1013 | $nonceStr = $this->getNonceStr(); |
| 968 | 1014 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -1004,9 +1050,15 @@ discard block |
||
| 1004 | 1050 | } |
| 1005 | 1051 | |
| 1006 | 1052 | private function post($url, $data,$cert = true) { |
| 1007 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
| 1008 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
| 1009 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
| 1053 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 1054 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 1055 | + } |
|
| 1056 | + if(!isset($data['nonce_str'])) { |
|
| 1057 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 1058 | + } |
|
| 1059 | + if(!isset($data['sign'])) { |
|
| 1060 | + $data['sign'] = $this->sign($data); |
|
| 1061 | + } |
|
| 1010 | 1062 | $this->requestXML = $this->responseXML = null; |
| 1011 | 1063 | $this->requestArray = $this->responseArray = null; |
| 1012 | 1064 | |
@@ -1028,15 +1080,21 @@ discard block |
||
| 1028 | 1080 | if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){ |
| 1029 | 1081 | $processResponse = false; |
| 1030 | 1082 | } |
| 1031 | - if($this->sandbox === true) $url = "sandboxnew/{$url}"; |
|
| 1083 | + if($this->sandbox === true) { |
|
| 1084 | + $url = "sandboxnew/{$url}"; |
|
| 1085 | + } |
|
| 1032 | 1086 | |
| 1033 | 1087 | $content = $this->httpClient->post(self::API_ENDPOINT . $url,$this->requestXML,[],$opts); |
| 1034 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1088 | + if(!$content) { |
|
| 1089 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1090 | + } |
|
| 1035 | 1091 | |
| 1036 | 1092 | $this->responseXML = $content; |
| 1037 | - if($processResponse) |
|
| 1038 | - return $this->processResponseXML($this->responseXML); |
|
| 1039 | - else return $this->responseXML; |
|
| 1093 | + if($processResponse) { |
|
| 1094 | + return $this->processResponseXML($this->responseXML); |
|
| 1095 | + } else { |
|
| 1096 | + return $this->responseXML; |
|
| 1097 | + } |
|
| 1040 | 1098 | |
| 1041 | 1099 | } |
| 1042 | 1100 | |
@@ -1060,7 +1118,7 @@ discard block |
||
| 1060 | 1118 | $this->errCode = $result['err_code']; |
| 1061 | 1119 | $this->errCodeDes = $result['err_code_des']; |
| 1062 | 1120 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
| 1063 | - }else{ |
|
| 1121 | + } else{ |
|
| 1064 | 1122 | return $result; |
| 1065 | 1123 | } |
| 1066 | 1124 | } else if($this->returnCode == 'FAIL'){ |
@@ -1079,17 +1137,20 @@ discard block |
||
| 1079 | 1137 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
| 1080 | 1138 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
| 1081 | 1139 | $sign = strtoupper(md5($stringSignTemp)); |
| 1082 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1140 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1083 | 1141 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
| 1084 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
| 1142 | + } else { |
|
| 1143 | + throw new Exception("Not supported sign type - $sign_type"); |
|
| 1144 | + } |
|
| 1085 | 1145 | return $sign; |
| 1086 | 1146 | } |
| 1087 | 1147 | |
| 1088 | 1148 | private function array2xml($array) { |
| 1089 | 1149 | $xml = "<xml>" . PHP_EOL; |
| 1090 | 1150 | foreach ($array as $k => $v) { |
| 1091 | - if($v && trim($v)!='') |
|
| 1092 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1151 | + if($v && trim($v)!='') { |
|
| 1152 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1153 | + } |
|
| 1093 | 1154 | } |
| 1094 | 1155 | $xml .= "</xml>"; |
| 1095 | 1156 | return $xml; |