@@ -28,8 +28,12 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | public function get($url,$params = array(),$headers = array(),$opts = array()) { |
| 31 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
| 32 | - if($params && count($params) > 0) $url .= '?' . http_build_query($params); |
|
| 31 | + if (!$this->instance) { |
|
| 32 | + $this->initInstance($this->timeout); |
|
| 33 | + } |
|
| 34 | + if($params && count($params) > 0) { |
|
| 35 | + $url .= '?' . http_build_query($params); |
|
| 36 | + } |
|
| 33 | 37 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 34 | 38 | curl_setopt($this->instance, CURLOPT_HTTPGET, true); |
| 35 | 39 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
@@ -41,7 +45,9 @@ discard block |
||
| 41 | 45 | } |
| 42 | 46 | |
| 43 | 47 | public function post($url, $params = array(),$headers = array(),$opts = array()) { |
| 44 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
| 48 | + if (!$this->instance) { |
|
| 49 | + $this->initInstance($this->timeout); |
|
| 50 | + } |
|
| 45 | 51 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 46 | 52 | curl_setopt($this->instance, CURLOPT_POST, true); |
| 47 | 53 | curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params); |
@@ -66,7 +66,9 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
| 69 | - if(!$accessToken) $accessToken = $this->getAccessToken(); |
|
| 69 | + if(!$accessToken) { |
|
| 70 | + $accessToken = $this->getAccessToken(); |
|
| 71 | + } |
|
| 70 | 72 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
| 71 | 73 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
| 72 | 74 | $this->responseJSON = $this->httpClient->get($url); |
@@ -85,7 +87,9 @@ discard block |
||
| 85 | 87 | (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ? "https://" : "http://"; |
| 86 | 88 | $url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; |
| 87 | 89 | } |
| 88 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 90 | + if(!$ticket) { |
|
| 91 | + $ticket = $this->getTicket(); |
|
| 92 | + } |
|
| 89 | 93 | $timestamp = time(); |
| 90 | 94 | $nonceStr = $this->getNonceStr(); |
| 91 | 95 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -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"]; |
@@ -934,13 +975,16 @@ discard block |
||
| 934 | 975 | } |
| 935 | 976 | |
| 936 | 977 | public function rsaEncrypt($data,$pubkey = null){ |
| 937 | - if(!$pubkey) $pubkey = $this->getPublicKey(); |
|
| 978 | + if(!$pubkey) { |
|
| 979 | + $pubkey = $this->getPublicKey(); |
|
| 980 | + } |
|
| 938 | 981 | $encrypted = null; |
| 939 | 982 | $pubkey = openssl_get_publickey($pubkey); |
| 940 | - if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 941 | - $data = base64_encode($encrypted); |
|
| 942 | - else |
|
| 943 | - throw new Exception('Unable to encrypt data'); |
|
| 983 | + if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
| 984 | + $data = base64_encode($encrypted); |
|
| 985 | + } else { |
|
| 986 | + throw new Exception('Unable to encrypt data'); |
|
| 987 | + } |
|
| 944 | 988 | return $data; |
| 945 | 989 | } |
| 946 | 990 | |
@@ -963,7 +1007,9 @@ discard block |
||
| 963 | 1007 | * @return array |
| 964 | 1008 | */ |
| 965 | 1009 | public function getSignPackage($url, $ticket = null){ |
| 966 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 1010 | + if(!$ticket) { |
|
| 1011 | + $ticket = $this->getTicket(); |
|
| 1012 | + } |
|
| 967 | 1013 | $timestamp = time(); |
| 968 | 1014 | $nonceStr = $this->getNonceStr(); |
| 969 | 1015 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -1005,9 +1051,15 @@ discard block |
||
| 1005 | 1051 | } |
| 1006 | 1052 | |
| 1007 | 1053 | private function post($url, $data,$cert = true) { |
| 1008 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
| 1009 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
| 1010 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
| 1054 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 1055 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 1056 | + } |
|
| 1057 | + if(!isset($data['nonce_str'])) { |
|
| 1058 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 1059 | + } |
|
| 1060 | + if(!isset($data['sign'])) { |
|
| 1061 | + $data['sign'] = $this->sign($data); |
|
| 1062 | + } |
|
| 1011 | 1063 | $this->requestXML = $this->responseXML = null; |
| 1012 | 1064 | $this->requestArray = $this->responseArray = null; |
| 1013 | 1065 | |
@@ -1029,15 +1081,21 @@ discard block |
||
| 1029 | 1081 | if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){ |
| 1030 | 1082 | $processResponse = false; |
| 1031 | 1083 | } |
| 1032 | - if($this->sandbox === true) $url = "sandboxnew/{$url}"; |
|
| 1084 | + if($this->sandbox === true) { |
|
| 1085 | + $url = "sandboxnew/{$url}"; |
|
| 1086 | + } |
|
| 1033 | 1087 | |
| 1034 | 1088 | $content = $this->httpClient->post(self::API_ENDPOINT . $url,$this->requestXML,[],$opts); |
| 1035 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1089 | + if(!$content) { |
|
| 1090 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1091 | + } |
|
| 1036 | 1092 | |
| 1037 | 1093 | $this->responseXML = $content; |
| 1038 | - if($processResponse) |
|
| 1039 | - return $this->processResponseXML($this->responseXML); |
|
| 1040 | - else return $this->responseXML; |
|
| 1094 | + if($processResponse) { |
|
| 1095 | + return $this->processResponseXML($this->responseXML); |
|
| 1096 | + } else { |
|
| 1097 | + return $this->responseXML; |
|
| 1098 | + } |
|
| 1041 | 1099 | } |
| 1042 | 1100 | |
| 1043 | 1101 | /** |
@@ -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; |
@@ -10,8 +10,11 @@ |
||
| 10 | 10 | private $cacheDir = null; |
| 11 | 11 | |
| 12 | 12 | public function __construct($cacheDir = null){ |
| 13 | - if(!$cacheDir) $this->cacheDir = __DIR__; |
|
| 14 | - else $this->cacheDir = $cacheDir; |
|
| 13 | + if(!$cacheDir) { |
|
| 14 | + $this->cacheDir = __DIR__; |
|
| 15 | + } else { |
|
| 16 | + $this->cacheDir = $cacheDir; |
|
| 17 | + } |
|
| 15 | 18 | } |
| 16 | 19 | |
| 17 | 20 | public function set($key,$jsonobj,$expireAt = null){ |