@@ -27,7 +27,9 @@ discard block |
||
| 27 | 27 | */ |
| 28 | 28 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
| 29 | 29 | $data = array(); |
| 30 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
| 30 | + if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) { |
|
| 31 | + throw new Exception('Real name is required'); |
|
| 32 | + } |
|
| 31 | 33 | $data["mch_appid"] = $this->config["app_id"]; |
| 32 | 34 | $data["mchid"] = $this->config["mch_id"]; |
| 33 | 35 | $data["partner_trade_no"] = $partner_trade_no; |
@@ -69,7 +71,9 @@ discard block |
||
| 69 | 71 | * @throws Exception |
| 70 | 72 | */ |
| 71 | 73 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
| 72 | - if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code: $bank_code"); |
|
| 74 | + if(!in_array($bank_code,array_values(self::$BANKCODE))) { |
|
| 75 | + throw new Exception("Unsupported bank code: $bank_code"); |
|
| 76 | + } |
|
| 73 | 77 | $data = array(); |
| 74 | 78 | $data["partner_trade_no"] = $partner_trade_no; |
| 75 | 79 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -84,13 +88,16 @@ discard block |
||
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | public function rsaEncrypt($data,$pubkey = null){ |
| 87 | - if(!$pubkey) $pubkey = $this->getPublicKey(); |
|
| 91 | + if(!$pubkey) { |
|
| 92 | + $pubkey = $this->getPublicKey(); |
|
| 93 | + } |
|
| 88 | 94 | $encrypted = null; |
| 89 | 95 | $pubkey = openssl_get_publickey($pubkey); |
| 90 | - if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 91 | - $data = base64_encode($encrypted); |
|
| 92 | - else |
|
| 93 | - throw new Exception('Unable to encrypt data'); |
|
| 96 | + if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
| 97 | + $data = base64_encode($encrypted); |
|
| 98 | + } else { |
|
| 99 | + throw new Exception('Unable to encrypt data'); |
|
| 100 | + } |
|
| 94 | 101 | return $data; |
| 95 | 102 | } |
| 96 | 103 | |
@@ -120,7 +127,7 @@ discard block |
||
| 120 | 127 | if(!$this->publicKey) { |
| 121 | 128 | if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) { |
| 122 | 129 | $this->publicKey = file_get_contents($this->config["rsa_pubkey_path"]); |
| 123 | - }else{ |
|
| 130 | + } else{ |
|
| 124 | 131 | $data = array(); |
| 125 | 132 | $data["mch_id"] = $this->config["mch_id"]; |
| 126 | 133 | $data["sign_type"] = $this->config["sign_type"]; |
@@ -187,11 +187,15 @@ discard block |
||
| 187 | 187 | $data["notify_url"] = $this->config["notify_url"]; |
| 188 | 188 | $data["trade_type"] = $params['trade_type']; |
| 189 | 189 | if($params['trade_type'] == WechatPay::TRADETYPE_NATIVE){ |
| 190 | - if(!isset($params['product_id'])) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 190 | + if(!isset($params['product_id'])) { |
|
| 191 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 192 | + } |
|
| 191 | 193 | $data["product_id"] = $params['product_id']; |
| 192 | 194 | } |
| 193 | 195 | if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
| 194 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 196 | + if(!isset($params['openid'])) { |
|
| 197 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 198 | + } |
|
| 195 | 199 | $data["openid"] = $params['openid']; |
| 196 | 200 | } |
| 197 | 201 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -411,8 +415,12 @@ discard block |
||
| 411 | 415 | * @throws Exception |
| 412 | 416 | */ |
| 413 | 417 | public function onPaidNotify($notify_data,callable $callback = null){ |
| 414 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
| 415 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid paid notify data'); |
|
| 418 | + if(!is_array($notify_data)) { |
|
| 419 | + $notify_data = $this->xml2array($notify_data); |
|
| 420 | + } |
|
| 421 | + if(!$this->validateSign($notify_data)) { |
|
| 422 | + throw new Exception('Invalid paid notify data'); |
|
| 423 | + } |
|
| 416 | 424 | if($callback && is_callable($callback)){ |
| 417 | 425 | return call_user_func_array( $callback , [$notify_data] ); |
| 418 | 426 | } |
@@ -426,8 +434,12 @@ discard block |
||
| 426 | 434 | * @throws Exception |
| 427 | 435 | */ |
| 428 | 436 | public function onRefundedNotify($notify_data,callable $callback = null){ |
| 429 | - if(!is_array($notify_data)) $notify_data = $this->xml2array($notify_data); |
|
| 430 | - if(!$this->validateSign($notify_data)) throw new Exception('Invalid refund notify data'); |
|
| 437 | + if(!is_array($notify_data)) { |
|
| 438 | + $notify_data = $this->xml2array($notify_data); |
|
| 439 | + } |
|
| 440 | + if(!$this->validateSign($notify_data)) { |
|
| 441 | + throw new Exception('Invalid refund notify data'); |
|
| 442 | + } |
|
| 431 | 443 | if($callback && is_callable($callback)){ |
| 432 | 444 | return call_user_func_array( $callback ,[$notify_data] ); |
| 433 | 445 | } |
@@ -461,8 +473,11 @@ discard block |
||
| 461 | 473 | $data["return_msg"] = $return_msg; |
| 462 | 474 | } |
| 463 | 475 | $xml = $this->array2xml($data); |
| 464 | - if($print === true) print $xml; |
|
| 465 | - else return $xml; |
|
| 476 | + if($print === true) { |
|
| 477 | + print $xml; |
|
| 478 | + } else { |
|
| 479 | + return $xml; |
|
| 480 | + } |
|
| 466 | 481 | } |
| 467 | 482 | |
| 468 | 483 | /** |
@@ -490,12 +505,24 @@ discard block |
||
| 490 | 505 | $data["return_code"] = $return_code; |
| 491 | 506 | $data["result_code"] = $result_code; |
| 492 | 507 | $data["user_ip"] = $user_ip; |
| 493 | - if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
|
| 494 | - if($time) $data["time"] = $time; |
|
| 495 | - if($device_info) $data["device_info"] = $device_info; |
|
| 496 | - if($return_msg) $data["return_msg"] = $return_msg; |
|
| 497 | - if($err_code) $data["err_code"] = $err_code; |
|
| 498 | - if($err_code_des) $data["err_code_des"] = $err_code_des; |
|
| 508 | + if($out_trade_no) { |
|
| 509 | + $data["out_trade_no"] = $out_trade_no; |
|
| 510 | + } |
|
| 511 | + if($time) { |
|
| 512 | + $data["time"] = $time; |
|
| 513 | + } |
|
| 514 | + if($device_info) { |
|
| 515 | + $data["device_info"] = $device_info; |
|
| 516 | + } |
|
| 517 | + if($return_msg) { |
|
| 518 | + $data["return_msg"] = $return_msg; |
|
| 519 | + } |
|
| 520 | + if($err_code) { |
|
| 521 | + $data["err_code"] = $err_code; |
|
| 522 | + } |
|
| 523 | + if($err_code_des) { |
|
| 524 | + $data["err_code_des"] = $err_code_des; |
|
| 525 | + } |
|
| 499 | 526 | return $this->post(self::URL_REPORT, $data, false); |
| 500 | 527 | } |
| 501 | 528 | |
@@ -532,7 +559,9 @@ discard block |
||
| 532 | 559 | * @return array |
| 533 | 560 | */ |
| 534 | 561 | public function getSignPackage($url, $ticket = null){ |
| 535 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 562 | + if(!$ticket) { |
|
| 563 | + $ticket = $this->getTicket(); |
|
| 564 | + } |
|
| 536 | 565 | $timestamp = time(); |
| 537 | 566 | $nonceStr = $this->getNonceStr(); |
| 538 | 567 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -574,9 +603,15 @@ discard block |
||
| 574 | 603 | } |
| 575 | 604 | |
| 576 | 605 | protected function post($url, $data,$cert = true) { |
| 577 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
| 578 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
| 579 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
| 606 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 607 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 608 | + } |
|
| 609 | + if(!isset($data['nonce_str'])) { |
|
| 610 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 611 | + } |
|
| 612 | + if(!isset($data['sign'])) { |
|
| 613 | + $data['sign'] = $this->sign($data); |
|
| 614 | + } |
|
| 580 | 615 | $this->requestXML = $this->responseXML = null; |
| 581 | 616 | $this->requestArray = $this->responseArray = null; |
| 582 | 617 | |
@@ -598,15 +633,21 @@ discard block |
||
| 598 | 633 | if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){ |
| 599 | 634 | $processResponse = false; |
| 600 | 635 | } |
| 601 | - if($this->sandbox === true) $url = "sandboxnew/{$url}"; |
|
| 636 | + if($this->sandbox === true) { |
|
| 637 | + $url = "sandboxnew/{$url}"; |
|
| 638 | + } |
|
| 602 | 639 | |
| 603 | 640 | $content = $this->httpClient->post(self::API_ENDPOINT . $url,$this->requestXML,[],$opts); |
| 604 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 641 | + if(!$content) { |
|
| 642 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 643 | + } |
|
| 605 | 644 | |
| 606 | 645 | $this->responseXML = $content; |
| 607 | - if($processResponse) |
|
| 608 | - return $this->processResponseXML($this->responseXML); |
|
| 609 | - else return $this->responseXML; |
|
| 646 | + if($processResponse) { |
|
| 647 | + return $this->processResponseXML($this->responseXML); |
|
| 648 | + } else { |
|
| 649 | + return $this->responseXML; |
|
| 650 | + } |
|
| 610 | 651 | } |
| 611 | 652 | |
| 612 | 653 | /** |
@@ -629,7 +670,7 @@ discard block |
||
| 629 | 670 | $this->errCode = $result['err_code']; |
| 630 | 671 | $this->errCodeDes = $result['err_code_des']; |
| 631 | 672 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
| 632 | - }else{ |
|
| 673 | + } else{ |
|
| 633 | 674 | return $result; |
| 634 | 675 | } |
| 635 | 676 | } else if($this->returnCode == 'FAIL'){ |
@@ -648,17 +689,20 @@ discard block |
||
| 648 | 689 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
| 649 | 690 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
| 650 | 691 | $sign = strtoupper(md5($stringSignTemp)); |
| 651 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 692 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 652 | 693 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
| 653 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
| 694 | + } else { |
|
| 695 | + throw new Exception("Not supported sign type - $sign_type"); |
|
| 696 | + } |
|
| 654 | 697 | return $sign; |
| 655 | 698 | } |
| 656 | 699 | |
| 657 | 700 | private function array2xml($array) { |
| 658 | 701 | $xml = "<xml>" . PHP_EOL; |
| 659 | 702 | foreach ($array as $k => $v) { |
| 660 | - if($v && trim($v)!='') |
|
| 661 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 703 | + if($v && trim($v)!='') { |
|
| 704 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 705 | + } |
|
| 662 | 706 | } |
| 663 | 707 | $xml .= "</xml>"; |
| 664 | 708 | return $xml; |