@@ -186,11 +186,15 @@ discard block |
||
| 186 | 186 | $data["notify_url"] = $this->config["notify_url"]; |
| 187 | 187 | $data["trade_type"] = $params['trade_type']; |
| 188 | 188 | if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){ |
| 189 | - if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 189 | + if(!isset($params['product_id'])) { |
|
| 190 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 191 | + } |
|
| 190 | 192 | $data["product_id"] = $params['product_id']; |
| 191 | 193 | } |
| 192 | 194 | if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
| 193 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 195 | + if(!isset($params['openid'])) { |
|
| 196 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 197 | + } |
|
| 194 | 198 | $data["openid"] = $params['openid']; |
| 195 | 199 | } |
| 196 | 200 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -397,8 +401,12 @@ discard block |
||
| 397 | 401 | * @throws Exception |
| 398 | 402 | */ |
| 399 | 403 | public function onPaidNotify($notify_data,callable $callback = null){ |
| 400 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
| 401 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid paid notify data'); |
|
| 404 | + if(!is_array($notify_data)) { |
|
| 405 | + $notify_data = $this->xml2array($notify_data); |
|
| 406 | + } |
|
| 407 | + if(!$this->validateSign($notify_data)) { |
|
| 408 | + throw new Exception('Invalid paid notify data'); |
|
| 409 | + } |
|
| 402 | 410 | if($callback && is_callable($callback)){ |
| 403 | 411 | return call_user_func_array( $callback , [$notify_data] ); |
| 404 | 412 | } |
@@ -413,8 +421,12 @@ discard block |
||
| 413 | 421 | * @throws Exception |
| 414 | 422 | */ |
| 415 | 423 | public function onRefundedNotify($notify_data,callable $callback = null){ |
| 416 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
| 417 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid refund notify data'); |
|
| 424 | + if(!is_array($notify_data)) { |
|
| 425 | + $notify_data = $this->xml2array($notify_data); |
|
| 426 | + } |
|
| 427 | + if(!$this->validateSign($notify_data)) { |
|
| 428 | + throw new Exception('Invalid refund notify data'); |
|
| 429 | + } |
|
| 418 | 430 | if($callback && is_callable($callback)){ |
| 419 | 431 | return call_user_func_array( $callback ,[$notify_data] ); |
| 420 | 432 | } |
@@ -450,8 +462,11 @@ discard block |
||
| 450 | 462 | $data["return_msg"] = $return_msg; |
| 451 | 463 | } |
| 452 | 464 | $xml = $this->array2xml($data); |
| 453 | - if($print === true) print $xml; |
|
| 454 | - else return $xml; |
|
| 465 | + if($print === true) { |
|
| 466 | + print $xml; |
|
| 467 | + } else { |
|
| 468 | + return $xml; |
|
| 469 | + } |
|
| 455 | 470 | } |
| 456 | 471 | |
| 457 | 472 | /** |
@@ -479,12 +494,24 @@ discard block |
||
| 479 | 494 | $data["return_code"] = $return_code; |
| 480 | 495 | $data["result_code"] = $result_code; |
| 481 | 496 | $data["user_ip"] = $user_ip; |
| 482 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
| 483 | - if($time) $data["time"] = $time; |
|
| 484 | - if($device_info) $data["device_info"] = $device_info; |
|
| 485 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
| 486 | - if($err_code) $data["err_code"] = $err_code; |
|
| 487 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
| 497 | + if($out_trade_no) { |
|
| 498 | + $data["out_trade_no"] = $out_trade_no; |
|
| 499 | + } |
|
| 500 | + if($time) { |
|
| 501 | + $data["time"] = $time; |
|
| 502 | + } |
|
| 503 | + if($device_info) { |
|
| 504 | + $data["device_info"] = $device_info; |
|
| 505 | + } |
|
| 506 | + if($return_msg) { |
|
| 507 | + $data["return_msg"] = $return_msg; |
|
| 508 | + } |
|
| 509 | + if($err_code) { |
|
| 510 | + $data["err_code"] = $err_code; |
|
| 511 | + } |
|
| 512 | + if($err_code_des) { |
|
| 513 | + $data["err_code_des"] = $err_code_des; |
|
| 514 | + } |
|
| 488 | 515 | return $this->post(self::URL_REPORT, $data, false); |
| 489 | 516 | } |
| 490 | 517 | |
@@ -521,7 +548,9 @@ discard block |
||
| 521 | 548 | * @return array |
| 522 | 549 | */ |
| 523 | 550 | public function getSignPackage($url, $ticket = null){ |
| 524 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 551 | + if(!$ticket) { |
|
| 552 | + $ticket = $this->getTicket(); |
|
| 553 | + } |
|
| 525 | 554 | $timestamp = time(); |
| 526 | 555 | $nonceStr = $this->getNonceStr(); |
| 527 | 556 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -563,9 +592,15 @@ discard block |
||
| 563 | 592 | } |
| 564 | 593 | |
| 565 | 594 | protected function post($url, $data,$cert = true) { |
| 566 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
| 567 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
| 568 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
| 595 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 596 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 597 | + } |
|
| 598 | + if(!isset($data['nonce_str'])) { |
|
| 599 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 600 | + } |
|
| 601 | + if(!isset($data['sign'])) { |
|
| 602 | + $data['sign'] = $this->sign($data); |
|
| 603 | + } |
|
| 569 | 604 | $this->requestXML = $this->responseXML = null; |
| 570 | 605 | $this->requestArray = $this->responseArray = null; |
| 571 | 606 | |
@@ -587,15 +622,21 @@ discard block |
||
| 587 | 622 | if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){ |
| 588 | 623 | $processResponse = false; |
| 589 | 624 | } |
| 590 | - if($this->sandbox === true) $url = "sandboxnew/{$url}"; |
|
| 625 | + if($this->sandbox === true) { |
|
| 626 | + $url = "sandboxnew/{$url}"; |
|
| 627 | + } |
|
| 591 | 628 | |
| 592 | 629 | $content = $this->httpClient->post(self::API_ENDPOINT . $url,$this->requestXML,[],$opts); |
| 593 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 630 | + if(!$content) { |
|
| 631 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 632 | + } |
|
| 594 | 633 | |
| 595 | 634 | $this->responseXML = $content; |
| 596 | - if($processResponse) |
|
| 597 | - return $this->processResponseXML($this->responseXML); |
|
| 598 | - else return $this->responseXML; |
|
| 635 | + if($processResponse) { |
|
| 636 | + return $this->processResponseXML($this->responseXML); |
|
| 637 | + } else { |
|
| 638 | + return $this->responseXML; |
|
| 639 | + } |
|
| 599 | 640 | } |
| 600 | 641 | |
| 601 | 642 | /** |
@@ -637,17 +678,20 @@ discard block |
||
| 637 | 678 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
| 638 | 679 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
| 639 | 680 | $sign = strtoupper(md5($stringSignTemp)); |
| 640 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 681 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 641 | 682 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
| 642 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
| 683 | + } else { |
|
| 684 | + throw new Exception("Not supported sign type - $sign_type"); |
|
| 685 | + } |
|
| 643 | 686 | return $sign; |
| 644 | 687 | } |
| 645 | 688 | |
| 646 | 689 | private function array2xml($array) { |
| 647 | 690 | $xml = "<xml>" . PHP_EOL; |
| 648 | 691 | foreach ($array as $k => $v) { |
| 649 | - if($v && trim($v)!='') |
|
| 650 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 692 | + if($v && trim($v)!='') { |
|
| 693 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 694 | + } |
|
| 651 | 695 | } |
| 652 | 696 | $xml .= "</xml>"; |
| 653 | 697 | return $xml; |