@@ -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 |
@@ -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'; |
@@ -64,7 +64,9 @@ |
||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
| 67 | - if(!$accessToken) $accessToken = $this->getAccessToken(); |
|
| 67 | + if(!$accessToken) { |
|
| 68 | + $accessToken = $this->getAccessToken(); |
|
| 69 | + } |
|
| 68 | 70 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
| 69 | 71 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
| 70 | 72 | $this->responseJSON = $this->httpClient->get($url); |
@@ -126,7 +126,9 @@ discard block |
||
| 126 | 126 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
| 127 | 127 | $data["notify_url"] = $this->config["notify_url"]; |
| 128 | 128 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
| 129 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 129 | + if(!$openid) { |
|
| 130 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 131 | + } |
|
| 130 | 132 | $data["openid"] = $openid; |
| 131 | 133 | $result = $this->unifiedOrder($data); |
| 132 | 134 | return $result["prepay_id"]; |
@@ -174,7 +176,9 @@ discard block |
||
| 174 | 176 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
| 175 | 177 | $data["notify_url"] = $this->config["notify_url"]; |
| 176 | 178 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
| 177 | - if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 179 | + if(!$product_id) { |
|
| 180 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 181 | + } |
|
| 178 | 182 | $data["product_id"] = $product_id; |
| 179 | 183 | $result = $this->unifiedOrder($data); |
| 180 | 184 | return $result["code_url"]; |
@@ -198,7 +202,9 @@ discard block |
||
| 198 | 202 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
| 199 | 203 | $data["notify_url"] = $this->config["notify_url"]; |
| 200 | 204 | $data["trade_type"] = self::TRADETYPE_MWEB; |
| 201 | - if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
| 205 | + if(!isset($this->config['h5_scene_info'])) { |
|
| 206 | + throw new Exception('h5_scene_info should be configured'); |
|
| 207 | + } |
|
| 202 | 208 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
| 203 | 209 | $result = $this->unifiedOrder($data); |
| 204 | 210 | return $result["mweb_url"]; |
@@ -228,11 +234,15 @@ discard block |
||
| 228 | 234 | $data["notify_url"] = $this->config["notify_url"]; |
| 229 | 235 | $data["trade_type"] = $params['trade_type']; |
| 230 | 236 | if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){ |
| 231 | - if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 237 | + if(!isset($params['product_id'])) { |
|
| 238 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 239 | + } |
|
| 232 | 240 | $data["product_id"] = $params['product_id']; |
| 233 | 241 | } |
| 234 | 242 | if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
| 235 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 243 | + if(!isset($params['openid'])) { |
|
| 244 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 245 | + } |
|
| 236 | 246 | $data["openid"] = $params['openid']; |
| 237 | 247 | } |
| 238 | 248 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -475,7 +485,9 @@ discard block |
||
| 475 | 485 | $data["send_name"] = $send_name; |
| 476 | 486 | $data["re_openid"] = $re_openid; |
| 477 | 487 | $data["total_amount"] = $total_amount; |
| 478 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
| 488 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 489 | + throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
| 490 | + } |
|
| 479 | 491 | $data["total_num"] = $total_num; |
| 480 | 492 | $data["wishing"] = $wishing; |
| 481 | 493 | $data["act_name"] = $act_name; |
@@ -511,7 +523,9 @@ discard block |
||
| 511 | 523 | $data["send_name"] = $send_name; |
| 512 | 524 | $data["re_openid"] = $re_openid; |
| 513 | 525 | $data["total_amount"] = $total_amount; |
| 514 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 526 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 527 | + throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 528 | + } |
|
| 515 | 529 | $data["total_num"] = $total_num; |
| 516 | 530 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
| 517 | 531 | $data["wishing"] = $wishing; |
@@ -626,7 +640,7 @@ discard block |
||
| 626 | 640 | if($callback && is_callable($callback)){ |
| 627 | 641 | return call_user_func_array( $callback , [$notify_data] ); |
| 628 | 642 | } |
| 629 | - }else{ |
|
| 643 | + } else{ |
|
| 630 | 644 | throw new Exception('Invalid paid notify data'); |
| 631 | 645 | } |
| 632 | 646 | } |
@@ -646,7 +660,7 @@ discard block |
||
| 646 | 660 | if($callback && is_callable($callback)){ |
| 647 | 661 | return call_user_func_array( $callback ,[$notify_data] ); |
| 648 | 662 | } |
| 649 | - }else{ |
|
| 663 | + } else{ |
|
| 650 | 664 | throw new Exception('Invalid refunded notify data'); |
| 651 | 665 | } |
| 652 | 666 | } |
@@ -679,8 +693,11 @@ discard block |
||
| 679 | 693 | $data["return_msg"] = $return_msg; |
| 680 | 694 | } |
| 681 | 695 | $xml = $this->array2xml($data); |
| 682 | - if($print === true) print $xml; |
|
| 683 | - else return $xml; |
|
| 696 | + if($print === true) { |
|
| 697 | + print $xml; |
|
| 698 | + } else { |
|
| 699 | + return $xml; |
|
| 700 | + } |
|
| 684 | 701 | } |
| 685 | 702 | |
| 686 | 703 | /** |
@@ -708,12 +725,24 @@ discard block |
||
| 708 | 725 | $data["return_code"] = $return_code; |
| 709 | 726 | $data["result_code"] = $result_code; |
| 710 | 727 | $data["user_ip"] = $user_ip; |
| 711 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
| 712 | - if($time) $data["time"] = $time; |
|
| 713 | - if($device_info) $data["device_info"] = $device_info; |
|
| 714 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
| 715 | - if($err_code) $data["err_code"] = $err_code; |
|
| 716 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
| 728 | + if($out_trade_no) { |
|
| 729 | + $data["out_trade_no"] = $out_trade_no; |
|
| 730 | + } |
|
| 731 | + if($time) { |
|
| 732 | + $data["time"] = $time; |
|
| 733 | + } |
|
| 734 | + if($device_info) { |
|
| 735 | + $data["device_info"] = $device_info; |
|
| 736 | + } |
|
| 737 | + if($return_msg) { |
|
| 738 | + $data["return_msg"] = $return_msg; |
|
| 739 | + } |
|
| 740 | + if($err_code) { |
|
| 741 | + $data["err_code"] = $err_code; |
|
| 742 | + } |
|
| 743 | + if($err_code_des) { |
|
| 744 | + $data["err_code_des"] = $err_code_des; |
|
| 745 | + } |
|
| 717 | 746 | $result = $this->post(self::URL_REPORT, $data, false); |
| 718 | 747 | return $result; |
| 719 | 748 | } |
@@ -761,7 +790,9 @@ discard block |
||
| 761 | 790 | */ |
| 762 | 791 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
| 763 | 792 | $data = array(); |
| 764 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
| 793 | + if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) { |
|
| 794 | + throw new Exception('Real name is required'); |
|
| 795 | + } |
|
| 765 | 796 | $data["mch_appid"] = $this->config["app_id"]; |
| 766 | 797 | $data["mchid"] = $this->config["mch_id"]; |
| 767 | 798 | $data["partner_trade_no"] = $partner_trade_no; |
@@ -803,7 +834,9 @@ discard block |
||
| 803 | 834 | * @throws Exception |
| 804 | 835 | */ |
| 805 | 836 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
| 806 | - if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code: $bank_code"); |
|
| 837 | + if(!in_array($bank_code,array_values(self::$BANKCODE))) { |
|
| 838 | + throw new Exception("Unsupported bank code: $bank_code"); |
|
| 839 | + } |
|
| 807 | 840 | $data = array(); |
| 808 | 841 | $data["partner_trade_no"] = $partner_trade_no; |
| 809 | 842 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -931,13 +964,16 @@ discard block |
||
| 931 | 964 | } |
| 932 | 965 | |
| 933 | 966 | public function rsaEncrypt($data,$pubkey = null){ |
| 934 | - if(!$pubkey) $pubkey = $this->getPublicKey(); |
|
| 967 | + if(!$pubkey) { |
|
| 968 | + $pubkey = $this->getPublicKey(); |
|
| 969 | + } |
|
| 935 | 970 | $encrypted = null; |
| 936 | 971 | $pubkey = openssl_get_publickey($pubkey); |
| 937 | - if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 938 | - $data = base64_encode($encrypted); |
|
| 939 | - else |
|
| 940 | - throw new Exception('Unable to encrypt data'); |
|
| 972 | + if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
| 973 | + $data = base64_encode($encrypted); |
|
| 974 | + } else { |
|
| 975 | + throw new Exception('Unable to encrypt data'); |
|
| 976 | + } |
|
| 941 | 977 | return $data; |
| 942 | 978 | } |
| 943 | 979 | |
@@ -960,7 +996,9 @@ discard block |
||
| 960 | 996 | * @return array |
| 961 | 997 | */ |
| 962 | 998 | public function getSignPackage($url, $ticket = null){ |
| 963 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 999 | + if(!$ticket) { |
|
| 1000 | + $ticket = $this->getTicket(); |
|
| 1001 | + } |
|
| 964 | 1002 | $timestamp = time(); |
| 965 | 1003 | $nonceStr = $this->getNonceStr(); |
| 966 | 1004 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -996,7 +1034,9 @@ discard block |
||
| 996 | 1034 | if($cache === true){ |
| 997 | 1035 | $fp = fopen($this->config["jsapi_ticket"], "w"); |
| 998 | 1036 | fwrite($fp, json_encode($data)); |
| 999 | - if ($fp) fclose($fp); |
|
| 1037 | + if ($fp) { |
|
| 1038 | + fclose($fp); |
|
| 1039 | + } |
|
| 1000 | 1040 | } |
| 1001 | 1041 | $ticket = $data->ticket; |
| 1002 | 1042 | } |
@@ -1004,9 +1044,15 @@ discard block |
||
| 1004 | 1044 | } |
| 1005 | 1045 | |
| 1006 | 1046 | 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); |
|
| 1047 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 1048 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 1049 | + } |
|
| 1050 | + if(!isset($data['nonce_str'])) { |
|
| 1051 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 1052 | + } |
|
| 1053 | + if(!isset($data['sign'])) { |
|
| 1054 | + $data['sign'] = $this->sign($data); |
|
| 1055 | + } |
|
| 1010 | 1056 | $this->requestXML = $this->responseXML = null; |
| 1011 | 1057 | $this->requestArray = $this->responseArray = null; |
| 1012 | 1058 | |
@@ -1034,12 +1080,16 @@ discard block |
||
| 1034 | 1080 | $url = "{$host}/sandboxnew{$url}"; |
| 1035 | 1081 | } |
| 1036 | 1082 | $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
| 1037 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1083 | + if(!$content) { |
|
| 1084 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1085 | + } |
|
| 1038 | 1086 | |
| 1039 | 1087 | $this->responseXML = $content; |
| 1040 | - if($processResponse) |
|
| 1041 | - return $this->processResponseXML($this->responseXML); |
|
| 1042 | - else return $this->responseXML; |
|
| 1088 | + if($processResponse) { |
|
| 1089 | + return $this->processResponseXML($this->responseXML); |
|
| 1090 | + } else { |
|
| 1091 | + return $this->responseXML; |
|
| 1092 | + } |
|
| 1043 | 1093 | |
| 1044 | 1094 | } |
| 1045 | 1095 | |
@@ -1058,13 +1108,13 @@ discard block |
||
| 1058 | 1108 | $this->errCode = $result['err_code']; |
| 1059 | 1109 | $this->errCodeDes = $result['err_code_des']; |
| 1060 | 1110 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
| 1061 | - }else{ |
|
| 1111 | + } else{ |
|
| 1062 | 1112 | return $result; |
| 1063 | 1113 | } |
| 1064 | 1114 | } else { |
| 1065 | 1115 | if($result["return_code"] == "FAIL"){ |
| 1066 | 1116 | throw new Exception($this->returnMsg); |
| 1067 | - }else{ |
|
| 1117 | + } else{ |
|
| 1068 | 1118 | $this->resultCode = $result['result_code']; |
| 1069 | 1119 | $this->errCode = $result['err_code']; |
| 1070 | 1120 | $this->errCodeDes = $result['err_code_des']; |
@@ -1084,17 +1134,20 @@ discard block |
||
| 1084 | 1134 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
| 1085 | 1135 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
| 1086 | 1136 | $sign = strtoupper(md5($stringSignTemp)); |
| 1087 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1137 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1088 | 1138 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
| 1089 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
| 1139 | + } else { |
|
| 1140 | + throw new Exception("Not supported sign type - $sign_type"); |
|
| 1141 | + } |
|
| 1090 | 1142 | return $sign; |
| 1091 | 1143 | } |
| 1092 | 1144 | |
| 1093 | 1145 | private function array2xml($array) { |
| 1094 | 1146 | $xml = "<xml>" . PHP_EOL; |
| 1095 | 1147 | foreach ($array as $k => $v) { |
| 1096 | - if($v && trim($v)!='') |
|
| 1097 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1148 | + if($v && trim($v)!='') { |
|
| 1149 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1150 | + } |
|
| 1098 | 1151 | } |
| 1099 | 1152 | $xml .= "</xml>"; |
| 1100 | 1153 | return $xml; |
@@ -1105,7 +1158,7 @@ discard block |
||
| 1105 | 1158 | $tmp = array(); |
| 1106 | 1159 | try{ |
| 1107 | 1160 | $tmp = (array) simplexml_load_string($xml); |
| 1108 | - }catch(Exception $e){ |
|
| 1161 | + } catch(Exception $e){ |
|
| 1109 | 1162 | throw $e; |
| 1110 | 1163 | } |
| 1111 | 1164 | foreach ( $tmp as $k => $v) { |
@@ -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); |