@@ -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 | |
@@ -183,7 +183,9 @@ discard block |
||
| 183 | 183 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
| 184 | 184 | $data["notify_url"] = $this->config["notify_url"]; |
| 185 | 185 | $data["trade_type"] = self::TRADETYPE_MWEB; |
| 186 | - if(!isset($this->config['h5_scene_info'])) throw new \Exception('h5_scene_info should be configured'); |
|
| 186 | + if(!isset($this->config['h5_scene_info'])) { |
|
| 187 | + throw new \Exception('h5_scene_info should be configured'); |
|
| 188 | + } |
|
| 187 | 189 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
| 188 | 190 | $result = $this->unifiedOrder($data); |
| 189 | 191 | return $result["mweb_url"]; |
@@ -430,7 +432,9 @@ discard block |
||
| 430 | 432 | $data["send_name"] = $send_name; |
| 431 | 433 | $data["re_openid"] = $re_openid; |
| 432 | 434 | $data["total_amount"] = $total_amount; |
| 433 | - if($total_amount > 20000 && trim($scene_id)=='') throw new \Exception("scene_id is required when total_amount beyond 20000"); |
|
| 435 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 436 | + throw new \Exception("scene_id is required when total_amount beyond 20000"); |
|
| 437 | + } |
|
| 434 | 438 | $data["total_num"] = $total_num; |
| 435 | 439 | $data["wishing"] = $wishing; |
| 436 | 440 | $data["act_name"] = $act_name; |
@@ -466,7 +470,9 @@ discard block |
||
| 466 | 470 | $data["send_name"] = $send_name; |
| 467 | 471 | $data["re_openid"] = $re_openid; |
| 468 | 472 | $data["total_amount"] = $total_amount; |
| 469 | - if($total_amount > 20000 && trim($scene_id)=='') throw new \Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 473 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 474 | + throw new \Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 475 | + } |
|
| 470 | 476 | $data["total_num"] = $total_num; |
| 471 | 477 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
| 472 | 478 | $data["wishing"] = $wishing; |
@@ -579,10 +585,10 @@ discard block |
||
| 579 | 585 | if($this->validateSign($notify_data)){ |
| 580 | 586 | if($callback && is_callable($callback)){ |
| 581 | 587 | return call_user_func_array( $callback , [$notify_data] ); |
| 582 | - }else{ |
|
| 588 | + } else{ |
|
| 583 | 589 | $this->responseNotify(); |
| 584 | 590 | } |
| 585 | - }else{ |
|
| 591 | + } else{ |
|
| 586 | 592 | throw new \Exception('Invalid paid notify data'); |
| 587 | 593 | } |
| 588 | 594 | } |
@@ -601,10 +607,10 @@ discard block |
||
| 601 | 607 | if($this->validateSign($notify_data)){ |
| 602 | 608 | if($callback && is_callable($callback)){ |
| 603 | 609 | return call_user_func_array( $callback , $notify_data ); |
| 604 | - }else{ |
|
| 610 | + } else{ |
|
| 605 | 611 | $this->responseNotify(); |
| 606 | 612 | } |
| 607 | - }else{ |
|
| 613 | + } else{ |
|
| 608 | 614 | throw new \Exception('Invalid refunded notify data'); |
| 609 | 615 | } |
| 610 | 616 | } |
@@ -663,12 +669,24 @@ discard block |
||
| 663 | 669 | $data["return_code"] = $return_code; |
| 664 | 670 | $data["result_code"] = $result_code; |
| 665 | 671 | $data["user_ip"] = $user_ip; |
| 666 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
| 667 | - if($time) $data["time"] = $time; |
|
| 668 | - if($device_info) $data["device_info"] = $device_info; |
|
| 669 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
| 670 | - if($err_code) $data["err_code"] = $err_code; |
|
| 671 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
| 672 | + if($out_trade_no) { |
|
| 673 | + $data["out_trade_no"] = $out_trade_no; |
|
| 674 | + } |
|
| 675 | + if($time) { |
|
| 676 | + $data["time"] = $time; |
|
| 677 | + } |
|
| 678 | + if($device_info) { |
|
| 679 | + $data["device_info"] = $device_info; |
|
| 680 | + } |
|
| 681 | + if($return_msg) { |
|
| 682 | + $data["return_msg"] = $return_msg; |
|
| 683 | + } |
|
| 684 | + if($err_code) { |
|
| 685 | + $data["err_code"] = $err_code; |
|
| 686 | + } |
|
| 687 | + if($err_code_des) { |
|
| 688 | + $data["err_code_des"] = $err_code_des; |
|
| 689 | + } |
|
| 672 | 690 | $result = $this->post(self::URL_REPORT, $data, false); //cert is NOT required |
| 673 | 691 | return $result; |
| 674 | 692 | } |
@@ -715,7 +733,9 @@ discard block |
||
| 715 | 733 | */ |
| 716 | 734 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
| 717 | 735 | $data = array(); |
| 718 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
| 736 | + if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) { |
|
| 737 | + throw new Exception('Real name is required'); |
|
| 738 | + } |
|
| 719 | 739 | $data["mch_appid"] = $this->config["app_id"]; |
| 720 | 740 | $data["mchid"] = $this->config["mch_id"]; |
| 721 | 741 | $data["partner_trade_no"] = $partner_trade_no; |
@@ -757,7 +777,9 @@ discard block |
||
| 757 | 777 | * @throws Exception |
| 758 | 778 | */ |
| 759 | 779 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
| 760 | - if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code - $bank_code"); |
|
| 780 | + if(!in_array($bank_code,array_values(self::$BANKCODE))) { |
|
| 781 | + throw new Exception("Unsupported bank code - $bank_code"); |
|
| 782 | + } |
|
| 761 | 783 | $data = array(); |
| 762 | 784 | $data["partner_trade_no"] = $partner_trade_no; |
| 763 | 785 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -846,7 +868,9 @@ discard block |
||
| 846 | 868 | $pubkey = $this->convertPKCS1toPKCS8($pubkey); |
| 847 | 869 | $fp = fopen($this->config["rsa_pubkey_path"], "w"); |
| 848 | 870 | fwrite($fp, $pubkey); |
| 849 | - if ($fp) fclose($fp); |
|
| 871 | + if ($fp) { |
|
| 872 | + fclose($fp); |
|
| 873 | + } |
|
| 850 | 874 | return $pubkey; |
| 851 | 875 | } |
| 852 | 876 | |
@@ -863,10 +887,11 @@ discard block |
||
| 863 | 887 | $pubkey = $this->getPublicKey(); |
| 864 | 888 | $encrypted = null; |
| 865 | 889 | $pubkey = openssl_get_publickey($pubkey); |
| 866 | - if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 867 | - $data = base64_encode($encrypted); |
|
| 868 | - else |
|
| 869 | - throw new Exception('Unable to encrypt data'); |
|
| 890 | + if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
| 891 | + $data = base64_encode($encrypted); |
|
| 892 | + } else { |
|
| 893 | + throw new Exception('Unable to encrypt data'); |
|
| 894 | + } |
|
| 870 | 895 | return $data; |
| 871 | 896 | } |
| 872 | 897 | |
@@ -922,7 +947,9 @@ discard block |
||
| 922 | 947 | $data = $this->getWechatOAuth()->getTicket(); |
| 923 | 948 | $fp = fopen($this->config["jsapi_ticket"], "w"); |
| 924 | 949 | fwrite($fp, $data); |
| 925 | - if ($fp) fclose($fp); |
|
| 950 | + if ($fp) { |
|
| 951 | + fclose($fp); |
|
| 952 | + } |
|
| 926 | 953 | $data = json_decode($data); |
| 927 | 954 | } |
| 928 | 955 | return $data->jsapi_ticket; |
@@ -930,9 +957,15 @@ discard block |
||
| 930 | 957 | } |
| 931 | 958 | |
| 932 | 959 | private function post($url, $data,$cert = true) { |
| 933 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
| 934 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
| 935 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
| 960 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 961 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 962 | + } |
|
| 963 | + if(!isset($data['nonce_str'])) { |
|
| 964 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 965 | + } |
|
| 966 | + if(!isset($data['sign'])) { |
|
| 967 | + $data['sign'] = $this->sign($data); |
|
| 968 | + } |
|
| 936 | 969 | $this->requestXML = $this->responseXML = null; |
| 937 | 970 | $this->requestArray = $this->responseArray = null; |
| 938 | 971 | |
@@ -960,12 +993,16 @@ discard block |
||
| 960 | 993 | $url = "{$host}/sandboxnew{$url}"; |
| 961 | 994 | } |
| 962 | 995 | $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
| 963 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 996 | + if(!$content) { |
|
| 997 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 998 | + } |
|
| 964 | 999 | |
| 965 | 1000 | $this->responseXML = $content; |
| 966 | - if($processResponse) |
|
| 967 | - return $this->processResponseXML($this->responseXML); |
|
| 968 | - else return $this->responseXML; |
|
| 1001 | + if($processResponse) { |
|
| 1002 | + return $this->processResponseXML($this->responseXML); |
|
| 1003 | + } else { |
|
| 1004 | + return $this->responseXML; |
|
| 1005 | + } |
|
| 969 | 1006 | |
| 970 | 1007 | } |
| 971 | 1008 | |
@@ -984,13 +1021,13 @@ discard block |
||
| 984 | 1021 | $this->errCode = $result['err_code']; |
| 985 | 1022 | $this->errCodeDes = $result['err_code_des']; |
| 986 | 1023 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
| 987 | - }else{ |
|
| 1024 | + } else{ |
|
| 988 | 1025 | return $result; |
| 989 | 1026 | } |
| 990 | 1027 | } else { |
| 991 | 1028 | if($result["return_code"] == "FAIL"){ |
| 992 | 1029 | throw new Exception($this->returnMsg); |
| 993 | - }else{ |
|
| 1030 | + } else{ |
|
| 994 | 1031 | $this->resultCode = $result['result_code']; |
| 995 | 1032 | $this->errCode = $result['err_code']; |
| 996 | 1033 | $this->errCodeDes = $result['err_code_des']; |
@@ -1010,17 +1047,20 @@ discard block |
||
| 1010 | 1047 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
| 1011 | 1048 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
| 1012 | 1049 | $sign = strtoupper(md5($stringSignTemp)); |
| 1013 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1050 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1014 | 1051 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
| 1015 | - }else throw new \Exception("Not supported sign type - $sign_type"); |
|
| 1052 | + } else { |
|
| 1053 | + throw new \Exception("Not supported sign type - $sign_type"); |
|
| 1054 | + } |
|
| 1016 | 1055 | return $sign; |
| 1017 | 1056 | } |
| 1018 | 1057 | |
| 1019 | 1058 | private function array2xml($array) { |
| 1020 | 1059 | $xml = "<xml>" . PHP_EOL; |
| 1021 | 1060 | foreach ($array as $k => $v) { |
| 1022 | - if($v && trim($v)!='') |
|
| 1023 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1061 | + if($v && trim($v)!='') { |
|
| 1062 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1063 | + } |
|
| 1024 | 1064 | } |
| 1025 | 1065 | $xml .= "</xml>"; |
| 1026 | 1066 | return $xml; |
@@ -1031,7 +1071,7 @@ discard block |
||
| 1031 | 1071 | $tmp = null; |
| 1032 | 1072 | try{ |
| 1033 | 1073 | $tmp = (array) simplexml_load_string($xml); |
| 1034 | - }catch(\Exception $e){} |
|
| 1074 | + } catch(\Exception $e){} |
|
| 1035 | 1075 | if($tmp && is_array($tmp)){ |
| 1036 | 1076 | foreach ( $tmp as $k => $v) { |
| 1037 | 1077 | $array[$k] = (string) $v; |
@@ -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 |