@@ -32,8 +32,12 @@ discard block |
||
32 | 32 | } |
33 | 33 | |
34 | 34 | public function get($url,$params = array(),$headers = array(),$opts = array()) { |
35 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
36 | - if($params && count($params) > 0) $url .= '?' . http_build_query($params); |
|
35 | + if (!$this->instance) { |
|
36 | + $this->initInstance($this->timeout); |
|
37 | + } |
|
38 | + if($params && count($params) > 0) { |
|
39 | + $url .= '?' . http_build_query($params); |
|
40 | + } |
|
37 | 41 | curl_setopt($this->instance, CURLOPT_URL, $url); |
38 | 42 | curl_setopt($this->instance, CURLOPT_HTTPGET, true); |
39 | 43 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
@@ -45,7 +49,9 @@ discard block |
||
45 | 49 | } |
46 | 50 | |
47 | 51 | public function post($url, $params = array(),$headers = array(),$opts = array()) { |
48 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
52 | + if (!$this->instance) { |
|
53 | + $this->initInstance($this->timeout); |
|
54 | + } |
|
49 | 55 | curl_setopt($this->instance, CURLOPT_URL, $url); |
50 | 56 | curl_setopt($this->instance, CURLOPT_POST, true); |
51 | 57 | curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params); |
@@ -65,8 +71,12 @@ discard block |
||
65 | 71 | } |
66 | 72 | |
67 | 73 | public function setOpt($optArray) { |
68 | - if (!$this->instance) return; |
|
69 | - if (!is_array($optArray)) throw new \Exception("Argument is not an array!"); |
|
74 | + if (!$this->instance) { |
|
75 | + return; |
|
76 | + } |
|
77 | + if (!is_array($optArray)) { |
|
78 | + throw new \Exception("Argument is not an array!"); |
|
79 | + } |
|
70 | 80 | curl_setopt_array($this->instance, $optArray); |
71 | 81 | } |
72 | 82 |
@@ -13,11 +13,11 @@ |
||
13 | 13 | |
14 | 14 | if(!$at || empty($at->openid)){ |
15 | 15 | die('授权失败'); |
16 | - }else{ |
|
16 | + } else{ |
|
17 | 17 | $accesstoken = $at->access_token; |
18 | 18 | $openid = $at->openid; |
19 | 19 | header('Location: '."http://{$_SERVER['HTTP_HOST']}/wechat-pay/demo/pay.php?openid=$openid"); |
20 | 20 | } |
21 | -}else{ |
|
21 | +} else{ |
|
22 | 22 | die('授权失败,请重试。'); |
23 | 23 | } |
24 | 24 | \ No newline at end of file |
@@ -182,7 +182,9 @@ discard block |
||
182 | 182 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
183 | 183 | $data["notify_url"] = $this->config["notify_url"]; |
184 | 184 | $data["trade_type"] = self::TRADETYPE_MWEB; |
185 | - if(!isset($this->config['h5_scene_info'])) throw new \Exception('h5_scene_info should be configured'); |
|
185 | + if(!isset($this->config['h5_scene_info'])) { |
|
186 | + throw new \Exception('h5_scene_info should be configured'); |
|
187 | + } |
|
186 | 188 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
187 | 189 | $result = $this->unifiedOrder($data); |
188 | 190 | return $result["mweb_url"]; |
@@ -433,7 +435,9 @@ discard block |
||
433 | 435 | $data["send_name"] = $send_name; |
434 | 436 | $data["re_openid"] = $re_openid; |
435 | 437 | $data["total_amount"] = $total_amount; |
436 | - if($total_amount > 20000 && trim($scene_id)=='') throw new \Exception("scene_id is required when total_amount beyond 20000"); |
|
438 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
439 | + throw new \Exception("scene_id is required when total_amount beyond 20000"); |
|
440 | + } |
|
437 | 441 | $data["total_num"] = $total_num; |
438 | 442 | $data["wishing"] = $wishing; |
439 | 443 | $data["act_name"] = $act_name; |
@@ -469,7 +473,9 @@ discard block |
||
469 | 473 | $data["send_name"] = $send_name; |
470 | 474 | $data["re_openid"] = $re_openid; |
471 | 475 | $data["total_amount"] = $total_amount; |
472 | - if($total_amount > 20000 && trim($scene_id)=='') throw new \Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
476 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
477 | + throw new \Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
478 | + } |
|
473 | 479 | $data["total_num"] = $total_num; |
474 | 480 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
475 | 481 | $data["wishing"] = $wishing; |
@@ -582,10 +588,10 @@ discard block |
||
582 | 588 | if($this->validateSign($notify_data)){ |
583 | 589 | if($callback && is_callable($callback)){ |
584 | 590 | return call_user_func_array( $callback , [$notify_data] ); |
585 | - }else{ |
|
591 | + } else{ |
|
586 | 592 | $this->responseNotify(); |
587 | 593 | } |
588 | - }else{ |
|
594 | + } else{ |
|
589 | 595 | throw new \Exception('Invalid paid notify data'); |
590 | 596 | } |
591 | 597 | } |
@@ -604,10 +610,10 @@ discard block |
||
604 | 610 | if($this->validateSign($notify_data)){ |
605 | 611 | if($callback && is_callable($callback)){ |
606 | 612 | return call_user_func_array( $callback , $notify_data ); |
607 | - }else{ |
|
613 | + } else{ |
|
608 | 614 | $this->responseNotify(); |
609 | 615 | } |
610 | - }else{ |
|
616 | + } else{ |
|
611 | 617 | throw new \Exception('Invalid refunded notify data'); |
612 | 618 | } |
613 | 619 | } |
@@ -666,12 +672,24 @@ discard block |
||
666 | 672 | $data["return_code"] = $return_code; |
667 | 673 | $data["result_code"] = $result_code; |
668 | 674 | $data["user_ip"] = $user_ip; |
669 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
670 | - if($time) $data["time"] = $time; |
|
671 | - if($device_info) $data["device_info"] = $device_info; |
|
672 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
673 | - if($err_code) $data["err_code"] = $err_code; |
|
674 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
675 | + if($out_trade_no) { |
|
676 | + $data["out_trade_no"] = $out_trade_no; |
|
677 | + } |
|
678 | + if($time) { |
|
679 | + $data["time"] = $time; |
|
680 | + } |
|
681 | + if($device_info) { |
|
682 | + $data["device_info"] = $device_info; |
|
683 | + } |
|
684 | + if($return_msg) { |
|
685 | + $data["return_msg"] = $return_msg; |
|
686 | + } |
|
687 | + if($err_code) { |
|
688 | + $data["err_code"] = $err_code; |
|
689 | + } |
|
690 | + if($err_code_des) { |
|
691 | + $data["err_code_des"] = $err_code_des; |
|
692 | + } |
|
675 | 693 | $result = $this->post(self::URL_REPORT, $data, false); |
676 | 694 | return $result; |
677 | 695 | } |
@@ -718,7 +736,9 @@ discard block |
||
718 | 736 | */ |
719 | 737 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
720 | 738 | $data = array(); |
721 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
739 | + if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) { |
|
740 | + throw new Exception('Real name is required'); |
|
741 | + } |
|
722 | 742 | $data["mch_appid"] = $this->config["app_id"]; |
723 | 743 | $data["mchid"] = $this->config["mch_id"]; |
724 | 744 | $data["partner_trade_no"] = $partner_trade_no; |
@@ -760,7 +780,9 @@ discard block |
||
760 | 780 | * @throws Exception |
761 | 781 | */ |
762 | 782 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
763 | - if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code - $bank_code"); |
|
783 | + if(!in_array($bank_code,array_values(self::$BANKCODE))) { |
|
784 | + throw new Exception("Unsupported bank code - $bank_code"); |
|
785 | + } |
|
764 | 786 | $data = array(); |
765 | 787 | $data["partner_trade_no"] = $partner_trade_no; |
766 | 788 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -853,7 +875,7 @@ discard block |
||
853 | 875 | fwrite($fp, $pubkey); |
854 | 876 | fclose($fp); |
855 | 877 | return $pubkey; |
856 | - }else { |
|
878 | + } else { |
|
857 | 879 | throw new Exception("RSA public key not found"); |
858 | 880 | } |
859 | 881 | } |
@@ -871,10 +893,11 @@ discard block |
||
871 | 893 | $pubkey = $this->getPublicKey(); |
872 | 894 | $encrypted = null; |
873 | 895 | $pubkey = openssl_get_publickey($pubkey); |
874 | - if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
875 | - $data = base64_encode($encrypted); |
|
876 | - else |
|
877 | - throw new Exception('Unable to encrypt data'); |
|
896 | + if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
897 | + $data = base64_encode($encrypted); |
|
898 | + } else { |
|
899 | + throw new Exception('Unable to encrypt data'); |
|
900 | + } |
|
878 | 901 | return $data; |
879 | 902 | } |
880 | 903 | |
@@ -930,7 +953,9 @@ discard block |
||
930 | 953 | $data = $this->getWechatOAuth()->getTicket(); |
931 | 954 | $fp = fopen($this->config["jsapi_ticket"], "w"); |
932 | 955 | fwrite($fp, $data); |
933 | - if ($fp) fclose($fp); |
|
956 | + if ($fp) { |
|
957 | + fclose($fp); |
|
958 | + } |
|
934 | 959 | $data = json_decode($data); |
935 | 960 | } |
936 | 961 | return $data->jsapi_ticket; |
@@ -938,9 +963,15 @@ discard block |
||
938 | 963 | } |
939 | 964 | |
940 | 965 | private function post($url, $data,$cert = true) { |
941 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
942 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
943 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
966 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
967 | + $data["mch_id"] = $this->config["mch_id"]; |
|
968 | + } |
|
969 | + if(!isset($data['nonce_str'])) { |
|
970 | + $data["nonce_str"] = $this->getNonceStr(); |
|
971 | + } |
|
972 | + if(!isset($data['sign'])) { |
|
973 | + $data['sign'] = $this->sign($data); |
|
974 | + } |
|
944 | 975 | $this->requestXML = $this->responseXML = null; |
945 | 976 | $this->requestArray = $this->responseArray = null; |
946 | 977 | |
@@ -968,12 +999,16 @@ discard block |
||
968 | 999 | $url = "{$host}/sandboxnew{$url}"; |
969 | 1000 | } |
970 | 1001 | $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
971 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
1002 | + if(!$content) { |
|
1003 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
1004 | + } |
|
972 | 1005 | |
973 | 1006 | $this->responseXML = $content; |
974 | - if($processResponse) |
|
975 | - return $this->processResponseXML($this->responseXML); |
|
976 | - else return $this->responseXML; |
|
1007 | + if($processResponse) { |
|
1008 | + return $this->processResponseXML($this->responseXML); |
|
1009 | + } else { |
|
1010 | + return $this->responseXML; |
|
1011 | + } |
|
977 | 1012 | |
978 | 1013 | } |
979 | 1014 | |
@@ -992,13 +1027,13 @@ discard block |
||
992 | 1027 | $this->errCode = $result['err_code']; |
993 | 1028 | $this->errCodeDes = $result['err_code_des']; |
994 | 1029 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
995 | - }else{ |
|
1030 | + } else{ |
|
996 | 1031 | return $result; |
997 | 1032 | } |
998 | 1033 | } else { |
999 | 1034 | if($result["return_code"] == "FAIL"){ |
1000 | 1035 | throw new Exception($this->returnMsg); |
1001 | - }else{ |
|
1036 | + } else{ |
|
1002 | 1037 | $this->resultCode = $result['result_code']; |
1003 | 1038 | $this->errCode = $result['err_code']; |
1004 | 1039 | $this->errCodeDes = $result['err_code_des']; |
@@ -1018,17 +1053,20 @@ discard block |
||
1018 | 1053 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
1019 | 1054 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
1020 | 1055 | $sign = strtoupper(md5($stringSignTemp)); |
1021 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
1056 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
1022 | 1057 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
1023 | - }else throw new \Exception("Not supported sign type - $sign_type"); |
|
1058 | + } else { |
|
1059 | + throw new \Exception("Not supported sign type - $sign_type"); |
|
1060 | + } |
|
1024 | 1061 | return $sign; |
1025 | 1062 | } |
1026 | 1063 | |
1027 | 1064 | private function array2xml($array) { |
1028 | 1065 | $xml = "<xml>" . PHP_EOL; |
1029 | 1066 | foreach ($array as $k => $v) { |
1030 | - if($v && trim($v)!='') |
|
1031 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
1067 | + if($v && trim($v)!='') { |
|
1068 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
1069 | + } |
|
1032 | 1070 | } |
1033 | 1071 | $xml .= "</xml>"; |
1034 | 1072 | return $xml; |
@@ -1039,7 +1077,7 @@ discard block |
||
1039 | 1077 | $tmp = array(); |
1040 | 1078 | try{ |
1041 | 1079 | $tmp = (array) simplexml_load_string($xml); |
1042 | - }catch(\Exception $e){} |
|
1080 | + } catch(\Exception $e){} |
|
1043 | 1081 | foreach ( $tmp as $k => $v) { |
1044 | 1082 | $array[$k] = (string) $v; |
1045 | 1083 | } |