@@ -43,7 +43,9 @@ |
||
| 43 | 43 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
| 44 | 44 | $data["notify_url"] = $this->config["notify_url"]; |
| 45 | 45 | $data["trade_type"] = self::TRADETYPE_MWEB; |
| 46 | - if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
| 46 | + if(!isset($this->config['h5_scene_info'])) { |
|
| 47 | + throw new Exception('h5_scene_info should be configured'); |
|
| 48 | + } |
|
| 47 | 49 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
| 48 | 50 | $result = $this->unifiedOrder($data); |
| 49 | 51 | return $result["mweb_url"]; |
@@ -36,7 +36,9 @@ discard block |
||
| 36 | 36 | $data["send_name"] = $send_name; |
| 37 | 37 | $data["re_openid"] = $re_openid; |
| 38 | 38 | $data["total_amount"] = $total_amount; |
| 39 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
| 39 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 40 | + throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
| 41 | + } |
|
| 40 | 42 | $data["total_num"] = $total_num; |
| 41 | 43 | $data["wishing"] = $wishing; |
| 42 | 44 | $data["act_name"] = $act_name; |
@@ -72,7 +74,9 @@ discard block |
||
| 72 | 74 | $data["send_name"] = $send_name; |
| 73 | 75 | $data["re_openid"] = $re_openid; |
| 74 | 76 | $data["total_amount"] = $total_amount; |
| 75 | - if($total_amount > 20000 && trim($scene_id)=='') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 77 | + if($total_amount > 20000 && trim($scene_id)=='') { |
|
| 78 | + throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 79 | + } |
|
| 76 | 80 | $data["total_num"] = $total_num; |
| 77 | 81 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
| 78 | 82 | $data["wishing"] = $wishing; |
@@ -46,7 +46,9 @@ |
||
| 46 | 46 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
| 47 | 47 | $data["notify_url"] = $this->config["notify_url"]; |
| 48 | 48 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
| 49 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 49 | + if(!$openid) { |
|
| 50 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 51 | + } |
|
| 50 | 52 | $data["openid"] = $openid; |
| 51 | 53 | $result = $this->unifiedOrder($data); |
| 52 | 54 | return $result["prepay_id"]; |
@@ -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); |
@@ -109,7 +113,7 @@ discard block |
||
| 109 | 113 | if(!$this->publicKey) { |
| 110 | 114 | if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) { |
| 111 | 115 | $this->publicKey = file_get_contents($this->config["rsa_pubkey_path"]); |
| 112 | - }else{ |
|
| 116 | + } else{ |
|
| 113 | 117 | $data = array(); |
| 114 | 118 | $data["mch_id"] = $this->config["mch_id"]; |
| 115 | 119 | $data["sign_type"] = $this->config["sign_type"]; |
@@ -46,7 +46,9 @@ |
||
| 46 | 46 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
| 47 | 47 | $data["notify_url"] = $this->config["notify_url"]; |
| 48 | 48 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
| 49 | - if(!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 49 | + if(!$openid) { |
|
| 50 | + throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 51 | + } |
|
| 50 | 52 | $data["openid"] = $openid; |
| 51 | 53 | $result = $this->unifiedOrder($data); |
| 52 | 54 | return $result["prepay_id"]; |
@@ -46,7 +46,9 @@ |
||
| 46 | 46 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
| 47 | 47 | $data["notify_url"] = $this->config["notify_url"]; |
| 48 | 48 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
| 49 | - if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 49 | + if(!$product_id) { |
|
| 50 | + throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 51 | + } |
|
| 50 | 52 | $data["product_id"] = $product_id; |
| 51 | 53 | $result = $this->unifiedOrder($data); |
| 52 | 54 | return $result["code_url"]; |
@@ -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 | |
@@ -514,13 +541,16 @@ discard block |
||
| 514 | 541 | } |
| 515 | 542 | |
| 516 | 543 | public function rsaEncrypt($data,$pubkey = null){ |
| 517 | - if(!$pubkey) $pubkey = $this->getPublicKey(); |
|
| 544 | + if(!$pubkey) { |
|
| 545 | + $pubkey = $this->getPublicKey(); |
|
| 546 | + } |
|
| 518 | 547 | $encrypted = null; |
| 519 | 548 | $pubkey = openssl_get_publickey($pubkey); |
| 520 | - if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 521 | - $data = base64_encode($encrypted); |
|
| 522 | - else |
|
| 523 | - throw new Exception('Unable to encrypt data'); |
|
| 549 | + if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
| 550 | + $data = base64_encode($encrypted); |
|
| 551 | + } else { |
|
| 552 | + throw new Exception('Unable to encrypt data'); |
|
| 553 | + } |
|
| 524 | 554 | return $data; |
| 525 | 555 | } |
| 526 | 556 | |
@@ -543,7 +573,9 @@ discard block |
||
| 543 | 573 | * @return array |
| 544 | 574 | */ |
| 545 | 575 | public function getSignPackage($url, $ticket = null){ |
| 546 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 576 | + if(!$ticket) { |
|
| 577 | + $ticket = $this->getTicket(); |
|
| 578 | + } |
|
| 547 | 579 | $timestamp = time(); |
| 548 | 580 | $nonceStr = $this->getNonceStr(); |
| 549 | 581 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -585,9 +617,15 @@ discard block |
||
| 585 | 617 | } |
| 586 | 618 | |
| 587 | 619 | protected function post($url, $data,$cert = true) { |
| 588 | - if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
|
| 589 | - if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
|
| 590 | - if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
|
| 620 | + if(!isset($data['mch_id']) && !isset($data['mchid'])) { |
|
| 621 | + $data["mch_id"] = $this->config["mch_id"]; |
|
| 622 | + } |
|
| 623 | + if(!isset($data['nonce_str'])) { |
|
| 624 | + $data["nonce_str"] = $this->getNonceStr(); |
|
| 625 | + } |
|
| 626 | + if(!isset($data['sign'])) { |
|
| 627 | + $data['sign'] = $this->sign($data); |
|
| 628 | + } |
|
| 591 | 629 | $this->requestXML = $this->responseXML = null; |
| 592 | 630 | $this->requestArray = $this->responseArray = null; |
| 593 | 631 | |
@@ -609,15 +647,21 @@ discard block |
||
| 609 | 647 | if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){ |
| 610 | 648 | $processResponse = false; |
| 611 | 649 | } |
| 612 | - if($this->sandbox === true) $url = "sandboxnew/{$url}"; |
|
| 650 | + if($this->sandbox === true) { |
|
| 651 | + $url = "sandboxnew/{$url}"; |
|
| 652 | + } |
|
| 613 | 653 | |
| 614 | 654 | $content = $this->httpClient->post(self::API_ENDPOINT . $url,$this->requestXML,[],$opts); |
| 615 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 655 | + if(!$content) { |
|
| 656 | + throw new Exception("Empty response with {$this->requestXML}"); |
|
| 657 | + } |
|
| 616 | 658 | |
| 617 | 659 | $this->responseXML = $content; |
| 618 | - if($processResponse) |
|
| 619 | - return $this->processResponseXML($this->responseXML); |
|
| 620 | - else return $this->responseXML; |
|
| 660 | + if($processResponse) { |
|
| 661 | + return $this->processResponseXML($this->responseXML); |
|
| 662 | + } else { |
|
| 663 | + return $this->responseXML; |
|
| 664 | + } |
|
| 621 | 665 | } |
| 622 | 666 | |
| 623 | 667 | /** |
@@ -640,7 +684,7 @@ discard block |
||
| 640 | 684 | $this->errCode = $result['err_code']; |
| 641 | 685 | $this->errCodeDes = $result['err_code_des']; |
| 642 | 686 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
| 643 | - }else{ |
|
| 687 | + } else{ |
|
| 644 | 688 | return $result; |
| 645 | 689 | } |
| 646 | 690 | } else if($this->returnCode == 'FAIL'){ |
@@ -659,17 +703,20 @@ discard block |
||
| 659 | 703 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
| 660 | 704 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
| 661 | 705 | $sign = strtoupper(md5($stringSignTemp)); |
| 662 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 706 | + } elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 663 | 707 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
| 664 | - }else throw new Exception("Not supported sign type - $sign_type"); |
|
| 708 | + } else { |
|
| 709 | + throw new Exception("Not supported sign type - $sign_type"); |
|
| 710 | + } |
|
| 665 | 711 | return $sign; |
| 666 | 712 | } |
| 667 | 713 | |
| 668 | 714 | private function array2xml($array) { |
| 669 | 715 | $xml = "<xml>" . PHP_EOL; |
| 670 | 716 | foreach ($array as $k => $v) { |
| 671 | - if($v && trim($v)!='') |
|
| 672 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 717 | + if($v && trim($v)!='') { |
|
| 718 | + $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 719 | + } |
|
| 673 | 720 | } |
| 674 | 721 | $xml .= "</xml>"; |
| 675 | 722 | return $xml; |