@@ -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 | |
@@ -655,7 +655,7 @@ |
||
| 655 | 655 | * @return array |
| 656 | 656 | */ |
| 657 | 657 | public function report($interface_url,$execution_time,$return_code,$result_code,$user_ip,$out_trade_no = null,$time = null,$device_info = null, |
| 658 | - $return_msg = null,$err_code = null,$err_code_des = null){ |
|
| 658 | + $return_msg = null,$err_code = null,$err_code_des = null){ |
|
| 659 | 659 | $data = array(); |
| 660 | 660 | $data["appid"] = $this->config["app_id"]; |
| 661 | 661 | $data["interface_url"] = $interface_url; |
@@ -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 | |
@@ -938,7 +961,9 @@ discard block |
||
| 938 | 961 | $data = $this->getWechatOAuth()->getTicket(); |
| 939 | 962 | $fp = fopen($this->config["jsapi_ticket"], "w"); |
| 940 | 963 | fwrite($fp, $data); |
| 941 | - if ($fp) fclose($fp); |
|
| 964 | + if ($fp) { |
|
| 965 | + fclose($fp); |
|
| 966 | + } |
|
| 942 | 967 | $data = json_decode($data); |
| 943 | 968 | } |
| 944 | 969 | return $data->jsapi_ticket; |
@@ -946,9 +971,15 @@ discard block |
||
| 946 | 971 | } |
| 947 | 972 | |
| 948 | 973 | private function post($url, $data,$cert = true) { |
| 949 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
| 950 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
| 951 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
| 974 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 975 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 976 | + } |
|
| 977 | + if(!isset($data['nonce_str'])) { |
|
| 978 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 979 | + } |
|
| 980 | + if(!isset($data['sign'])) { |
|
| 981 | + $data['sign'] = $this->sign($data); |
|
| 982 | + } |
|
| 952 | 983 | $this->requestXML = $this->responseXML = null; |
| 953 | 984 | $this->requestArray = $this->responseArray = null; |
| 954 | 985 | |
@@ -976,12 +1007,16 @@ discard block |
||
| 976 | 1007 | $url = "{$host}/sandboxnew{$url}"; |
| 977 | 1008 | } |
| 978 | 1009 | $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
| 979 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1010 | + if(!$content) { |
|
| 1011 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1012 | + } |
|
| 980 | 1013 | |
| 981 | 1014 | $this->responseXML = $content; |
| 982 | - if($processResponse) |
|
| 983 | - return $this->processResponseXML($this->responseXML); |
|
| 984 | - else return $this->responseXML; |
|
| 1015 | + if($processResponse) { |
|
| 1016 | + return $this->processResponseXML($this->responseXML); |
|
| 1017 | + } else { |
|
| 1018 | + return $this->responseXML; |
|
| 1019 | + } |
|
| 985 | 1020 | |
| 986 | 1021 | } |
| 987 | 1022 | |
@@ -1000,13 +1035,13 @@ discard block |
||
| 1000 | 1035 | $this->errCode = $result['err_code']; |
| 1001 | 1036 | $this->errCodeDes = $result['err_code_des']; |
| 1002 | 1037 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
| 1003 | - }else{ |
|
| 1038 | + } else{ |
|
| 1004 | 1039 | return $result; |
| 1005 | 1040 | } |
| 1006 | 1041 | } else { |
| 1007 | 1042 | if($result["return_code"] == "FAIL"){ |
| 1008 | 1043 | throw new Exception($this->returnMsg); |
| 1009 | - }else{ |
|
| 1044 | + } else{ |
|
| 1010 | 1045 | $this->resultCode = $result['result_code']; |
| 1011 | 1046 | $this->errCode = $result['err_code']; |
| 1012 | 1047 | $this->errCodeDes = $result['err_code_des']; |
@@ -1026,17 +1061,20 @@ discard block |
||
| 1026 | 1061 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
| 1027 | 1062 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
| 1028 | 1063 | $sign = strtoupper(md5($stringSignTemp)); |
| 1029 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1064 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1030 | 1065 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
| 1031 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
| 1066 | + } else { |
|
| 1067 | + throw new Exception("Not supported sign type - $sign_type"); |
|
| 1068 | + } |
|
| 1032 | 1069 | return $sign; |
| 1033 | 1070 | } |
| 1034 | 1071 | |
| 1035 | 1072 | private function array2xml($array) { |
| 1036 | 1073 | $xml = "<xml>" . PHP_EOL; |
| 1037 | 1074 | foreach ($array as $k => $v) { |
| 1038 | - if($v && trim($v)!='') |
|
| 1039 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1075 | + if($v && trim($v)!='') { |
|
| 1076 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1077 | + } |
|
| 1040 | 1078 | } |
| 1041 | 1079 | $xml .= "</xml>"; |
| 1042 | 1080 | return $xml; |
@@ -1047,7 +1085,7 @@ discard block |
||
| 1047 | 1085 | $tmp = array(); |
| 1048 | 1086 | try{ |
| 1049 | 1087 | $tmp = (array) simplexml_load_string($xml); |
| 1050 | - }catch(Exception $e){} |
|
| 1088 | + } catch(Exception $e){} |
|
| 1051 | 1089 | foreach ( $tmp as $k => $v) { |
| 1052 | 1090 | $array[$k] = (string) $v; |
| 1053 | 1091 | } |
@@ -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 |