Completed
Push — master ( 102abf...4b0145 )
by Wei
02:46
created
src/WechatPay.php 1 patch
Braces   +88 added lines, -40 removed lines patch added patch discarded remove patch
@@ -124,7 +124,9 @@  discard block
 block discarded – undo
124 124
 		$data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"];
125 125
 		$data["notify_url"]   = $this->config["notify_url"];
126 126
 		$data["trade_type"]   = WechatPay::TRADETYPE_JSAPI;
127
-		if(!$openid) throw new Exception('openid is required when trade_type is JSAPI');
127
+		if(!$openid) {
128
+			throw new Exception('openid is required when trade_type is JSAPI');
129
+		}
128 130
 		$data["openid"]   = $openid;
129 131
 		$result = $this->unifiedOrder($data);
130 132
 		return $result["prepay_id"];
@@ -172,7 +174,9 @@  discard block
 block discarded – undo
172 174
 		$data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"];
173 175
 		$data["notify_url"]   = $this->config["notify_url"];
174 176
 		$data["trade_type"]   = self::TRADETYPE_NATIVE;
175
-		if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE');
177
+		if(!$product_id) {
178
+			throw new Exception('product_id is required when trade_type is NATIVE');
179
+		}
176 180
 		$data["product_id"]   = $product_id;
177 181
 		$result = $this->unifiedOrder($data);
178 182
 		return $result["code_url"];
@@ -196,7 +200,9 @@  discard block
 block discarded – undo
196 200
 		$data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:'';
197 201
 		$data["notify_url"]   = $this->config["notify_url"];
198 202
 		$data["trade_type"]   = self::TRADETYPE_MWEB;
199
-		if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured');
203
+		if(!isset($this->config['h5_scene_info'])) {
204
+			throw new Exception('h5_scene_info should be configured');
205
+		}
200 206
 		$data["scene_info"]   = json_encode($this->config['h5_scene_info']);
201 207
 		$result = $this->unifiedOrder($data);
202 208
 		return $result["mweb_url"];
@@ -226,11 +232,15 @@  discard block
 block discarded – undo
226 232
 		$data["notify_url"] = $this->config["notify_url"];
227 233
 		$data["trade_type"] = $params['trade_type'];
228 234
 		if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){
229
-			if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE');
235
+			if(!isset($params['product_id'])) {
236
+				throw new Exception('product_id is required when trade_type is NATIVE');
237
+			}
230 238
 			$data["product_id"] = $params['product_id'];
231 239
 		}
232 240
 		if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){
233
-			if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI');
241
+			if(!isset($params['openid'])) {
242
+				throw new Exception('openid is required when trade_type is JSAPI');
243
+			}
234 244
 			$data["openid"] = $params['openid'];
235 245
 		}
236 246
 		$result = $this->post(self::URL_UNIFIEDORDER, $data);
@@ -473,7 +483,9 @@  discard block
 block discarded – undo
473 483
 		$data["send_name"] = $send_name;
474 484
 		$data["re_openid"] = $re_openid;
475 485
 		$data["total_amount"] = $total_amount;
476
-		if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000");
486
+		if($total_amount > 20000 && trim($scene_id)=='') {
487
+			throw new Exception("scene_id is required when total_amount beyond 20000");
488
+		}
477 489
 		$data["total_num"] = $total_num;
478 490
 		$data["wishing"] = $wishing;
479 491
 		$data["act_name"] = $act_name;
@@ -509,7 +521,9 @@  discard block
 block discarded – undo
509 521
 		$data["send_name"] = $send_name;
510 522
 		$data["re_openid"] = $re_openid;
511 523
 		$data["total_amount"] = $total_amount;
512
-		if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)");
524
+		if($total_amount > 20000 && trim($scene_id)=='') {
525
+			throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)");
526
+		}
513 527
 		$data["total_num"] = $total_num;
514 528
 		$data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机
515 529
 		$data["wishing"] = $wishing;
@@ -623,10 +637,10 @@  discard block
 block discarded – undo
623 637
 		if($this->validateSign($notify_data)){
624 638
 			if($callback && is_callable($callback)){
625 639
 				return call_user_func_array( $callback , [$notify_data] );
626
-			}else{
640
+			} else{
627 641
 				$this->responseNotify();
628 642
 			}
629
-		}else{
643
+		} else{
630 644
 			throw new Exception('Invalid paid notify data');
631 645
 		}
632 646
 	}
@@ -645,10 +659,10 @@  discard block
 block discarded – undo
645 659
 		if($this->validateSign($notify_data)){
646 660
 			if($callback && is_callable($callback)){
647 661
 				return call_user_func_array( $callback , $notify_data );
648
-			}else{
662
+			} else{
649 663
 				$this->responseNotify();
650 664
 			}
651
-		}else{
665
+		} else{
652 666
 			throw new Exception('Invalid refunded notify data');
653 667
 		}
654 668
 	}
@@ -707,12 +721,24 @@  discard block
 block discarded – undo
707 721
 		$data["return_code"] = $return_code;
708 722
 		$data["result_code"] = $result_code;
709 723
 		$data["user_ip"] = $user_ip;
710
-		if($out_trade_no) $data["out_trade_no"] = $out_trade_no;
711
-		if($time) $data["time"] = $time;
712
-		if($device_info) $data["device_info"] = $device_info;
713
-		if($return_msg) $data["return_msg"] = $return_msg;
714
-		if($err_code) $data["err_code"] = $err_code;
715
-		if($err_code_des) $data["err_code_des"] = $err_code_des;
724
+		if($out_trade_no) {
725
+			$data["out_trade_no"] = $out_trade_no;
726
+		}
727
+		if($time) {
728
+			$data["time"] = $time;
729
+		}
730
+		if($device_info) {
731
+			$data["device_info"] = $device_info;
732
+		}
733
+		if($return_msg) {
734
+			$data["return_msg"] = $return_msg;
735
+		}
736
+		if($err_code) {
737
+			$data["err_code"] = $err_code;
738
+		}
739
+		if($err_code_des) {
740
+			$data["err_code_des"] = $err_code_des;
741
+		}
716 742
 		$result = $this->post(self::URL_REPORT, $data, false);
717 743
 		return $result;
718 744
 	}
@@ -760,7 +786,9 @@  discard block
 block discarded – undo
760 786
 	 */
761 787
 	public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){
762 788
 		$data = array();
763
-		if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required');
789
+		if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) {
790
+			throw new Exception('Real name is required');
791
+		}
764 792
 		$data["mch_appid"] = $this->config["app_id"];
765 793
 		$data["mchid"] = $this->config["mch_id"];
766 794
 		$data["partner_trade_no"] = $partner_trade_no;
@@ -802,7 +830,9 @@  discard block
 block discarded – undo
802 830
 	 * @throws Exception
803 831
 	 */
804 832
 	public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){
805
-		if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code: $bank_code");
833
+		if(!in_array($bank_code,array_values(self::$BANKCODE))) {
834
+			throw new Exception("Unsupported bank code: $bank_code");
835
+		}
806 836
 		$data = array();
807 837
 		$data["partner_trade_no"] = $partner_trade_no;
808 838
 		$enc_bank_no = $this->rsaEncrypt($bank_no);
@@ -910,7 +940,7 @@  discard block
 block discarded – undo
910 940
 			fwrite($fp, $pubkey);
911 941
 			fclose($fp);
912 942
 			return $pubkey;
913
-		}else {
943
+		} else {
914 944
 			throw new Exception("RSA public key not found");
915 945
 		}
916 946
 	}
@@ -928,10 +958,11 @@  discard block
 block discarded – undo
928 958
 		$pubkey = $this->getPublicKey();
929 959
 		$encrypted = null;
930 960
 		$pubkey = openssl_get_publickey($pubkey);
931
-		if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING))
932
-			$data = base64_encode($encrypted);
933
-		else
934
-			throw new Exception('Unable to encrypt data');
961
+		if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) {
962
+					$data = base64_encode($encrypted);
963
+		} else {
964
+					throw new Exception('Unable to encrypt data');
965
+		}
935 966
 		return $data;
936 967
 	}
937 968
 
@@ -954,7 +985,9 @@  discard block
 block discarded – undo
954 985
 	 * @return array
955 986
 	 */
956 987
 	public function getSignPackage($url, $ticket = null){
957
-		if(!$ticket) $ticket = $this->getTicket();
988
+		if(!$ticket) {
989
+			$ticket = $this->getTicket();
990
+		}
958 991
 		$timestamp = time();
959 992
 		$nonceStr = $this->getNonceStr();
960 993
 		$rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
@@ -991,7 +1024,9 @@  discard block
 block discarded – undo
991 1024
 			if($cache === true){
992 1025
 				$fp = fopen($this->config["jsapi_ticket"], "w");
993 1026
 				fwrite($fp, json_encode($data));
994
-				if ($fp) fclose($fp);
1027
+				if ($fp) {
1028
+					fclose($fp);
1029
+				}
995 1030
 			}
996 1031
 			$ticket = $data->ticket;
997 1032
 		}
@@ -999,9 +1034,15 @@  discard block
 block discarded – undo
999 1034
 	}
1000 1035
 
1001 1036
 	private function post($url, $data,$cert = true) {
1002
-		if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"];
1003
-		if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr();
1004
-		if(!isset($data['sign'])) $data['sign'] = $this->sign($data);
1037
+		if(!isset($data['mch_id']) && !isset($data['mchid'])) {
1038
+			$data["mch_id"] = $this->config["mch_id"];
1039
+		}
1040
+		if(!isset($data['nonce_str'])) {
1041
+			$data["nonce_str"] = $this->getNonceStr();
1042
+		}
1043
+		if(!isset($data['sign'])) {
1044
+			$data['sign'] = $this->sign($data);
1045
+		}
1005 1046
 		$this->requestXML = $this->responseXML = null;
1006 1047
 		$this->requestArray = $this->responseArray = null;
1007 1048
 
@@ -1029,12 +1070,16 @@  discard block
 block discarded – undo
1029 1070
 			$url = "{$host}/sandboxnew{$url}";
1030 1071
 		}
1031 1072
 		$content = $this->httpClient->post($url,$this->requestXML,[],$opts);
1032
-		if(!$content) throw new Exception("Empty response with {$this->requestXML}");
1073
+		if(!$content) {
1074
+			throw new Exception("Empty response with {$this->requestXML}");
1075
+		}
1033 1076
 
1034 1077
 		$this->responseXML = $content;
1035
-		if($processResponse)
1036
-			return $this->processResponseXML($this->responseXML);
1037
-		else return $this->responseXML;
1078
+		if($processResponse) {
1079
+					return $this->processResponseXML($this->responseXML);
1080
+		} else {
1081
+			return $this->responseXML;
1082
+		}
1038 1083
 
1039 1084
 	}
1040 1085
 
@@ -1053,13 +1098,13 @@  discard block
 block discarded – undo
1053 1098
 				$this->errCode = $result['err_code'];
1054 1099
 				$this->errCodeDes = $result['err_code_des'];
1055 1100
 				throw new Exception("[$this->errCode]$this->errCodeDes");
1056
-			}else{
1101
+			} else{
1057 1102
 				return $result;
1058 1103
 			}
1059 1104
 		} else {
1060 1105
 			if($result["return_code"] == "FAIL"){
1061 1106
 				throw new Exception($this->returnMsg);
1062
-			}else{
1107
+			} else{
1063 1108
 				$this->resultCode = $result['result_code'];
1064 1109
 				$this->errCode = $result['err_code'];
1065 1110
 				$this->errCodeDes = $result['err_code_des'];
@@ -1079,17 +1124,20 @@  discard block
 block discarded – undo
1079 1124
 		$stringSignTemp = $string1 . "key=" . $this->config["api_key"];
1080 1125
 		if($sign_type == WechatPay::SIGNTYPE_MD5){
1081 1126
 			$sign = strtoupper(md5($stringSignTemp));
1082
-		}elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){
1127
+		} elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){
1083 1128
 			$sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"]));
1084
-		}else throw new Exception("Not supported sign type - $sign_type");
1129
+		} else {
1130
+			throw new Exception("Not supported sign type - $sign_type");
1131
+		}
1085 1132
 		return $sign;
1086 1133
 	}
1087 1134
 
1088 1135
 	private function array2xml($array) {
1089 1136
 		$xml = "<xml>" . PHP_EOL;
1090 1137
 		foreach ($array as $k => $v) {
1091
-			if($v && trim($v)!='')
1092
-				$xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL;
1138
+			if($v && trim($v)!='') {
1139
+							$xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL;
1140
+			}
1093 1141
 		}
1094 1142
 		$xml .= "</xml>";
1095 1143
 		return $xml;
@@ -1100,7 +1148,7 @@  discard block
 block discarded – undo
1100 1148
 		$tmp = array();
1101 1149
 		try{
1102 1150
 			$tmp = (array) simplexml_load_string($xml);
1103
-		}catch(Exception $e){
1151
+		} catch(Exception $e){
1104 1152
 			throw $e;
1105 1153
 		}
1106 1154
 		foreach ( $tmp as $k => $v) {
Please login to merge, or discard this patch.