@@ -10,8 +10,11 @@ |
||
| 10 | 10 | private $cacheDir = null; |
| 11 | 11 | |
| 12 | 12 | public function __construct($cacheDir = null){ |
| 13 | - if(!$cacheDir) $this->cacheDir = __DIR__; |
|
| 14 | - else $this->cacheDir = $cacheDir; |
|
| 13 | + if(!$cacheDir) { |
|
| 14 | + $this->cacheDir = __DIR__; |
|
| 15 | + } else { |
|
| 16 | + $this->cacheDir = $cacheDir; |
|
| 17 | + } |
|
| 15 | 18 | } |
| 16 | 19 | |
| 17 | 20 | public function set($key,$jsonobj,$expireAt = null){ |
@@ -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["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"]; |
@@ -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"]; |
@@ -25,7 +25,9 @@ discard block |
||
| 25 | 25 | * @throws Exception |
| 26 | 26 | */ |
| 27 | 27 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
| 28 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
| 28 | + if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) { |
|
| 29 | + throw new Exception('Real name is required'); |
|
| 30 | + } |
|
| 29 | 31 | $data = [ |
| 30 | 32 | "mch_appid" => $this->config["app_id"], |
| 31 | 33 | "mchid" => $this->config["mch_id"], |
@@ -66,7 +68,9 @@ discard block |
||
| 66 | 68 | * @throws Exception |
| 67 | 69 | */ |
| 68 | 70 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
| 69 | - if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code: $bank_code"); |
|
| 71 | + if(!in_array($bank_code,array_values(self::$BANKCODE))) { |
|
| 72 | + throw new Exception("Unsupported bank code: $bank_code"); |
|
| 73 | + } |
|
| 70 | 74 | $data = [ |
| 71 | 75 | "partner_trade_no" => $partner_trade_no, |
| 72 | 76 | "enc_bank_no" => $this->rsaEncrypt($bank_no), |
@@ -79,13 +83,16 @@ discard block |
||
| 79 | 83 | } |
| 80 | 84 | |
| 81 | 85 | public function rsaEncrypt($data,$pubkey = null){ |
| 82 | - if(!$pubkey) $pubkey = $this->getPublicKey(); |
|
| 86 | + if(!$pubkey) { |
|
| 87 | + $pubkey = $this->getPublicKey(); |
|
| 88 | + } |
|
| 83 | 89 | $encrypted = null; |
| 84 | 90 | $pubkey = openssl_get_publickey($pubkey); |
| 85 | - if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 86 | - $data = base64_encode($encrypted); |
|
| 87 | - else |
|
| 88 | - throw new Exception('Unable to encrypt data'); |
|
| 91 | + if (@openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) { |
|
| 92 | + $data = base64_encode($encrypted); |
|
| 93 | + } else { |
|
| 94 | + throw new Exception('Unable to encrypt data'); |
|
| 95 | + } |
|
| 89 | 96 | return $data; |
| 90 | 97 | } |
| 91 | 98 | |
@@ -114,7 +121,7 @@ discard block |
||
| 114 | 121 | if(!$this->publicKey) { |
| 115 | 122 | if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) { |
| 116 | 123 | $this->publicKey = file_get_contents($this->config["rsa_pubkey_path"]); |
| 117 | - }else{ |
|
| 124 | + } else{ |
|
| 118 | 125 | $data = array(); |
| 119 | 126 | $data["mch_id"] = $this->config["mch_id"]; |
| 120 | 127 | $data["sign_type"] = $this->config["sign_type"]; |
@@ -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; |
@@ -29,8 +29,9 @@ discard block |
||
| 29 | 29 | if($this->timeout < 1) { |
| 30 | 30 | curl_setopt($this->instance, CURLOPT_NOSIGNAL,1); |
| 31 | 31 | curl_setopt($this->instance, CURLOPT_TIMEOUT_MS, $this->timeout * 1000); |
| 32 | - }else |
|
| 33 | - curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout)); |
|
| 32 | + } else { |
|
| 33 | + curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout)); |
|
| 34 | + } |
|
| 34 | 35 | curl_setopt($this->instance, CURLOPT_RETURNTRANSFER, true); |
| 35 | 36 | curl_setopt($this->instance, CURLOPT_FOLLOWLOCATION, true); |
| 36 | 37 | curl_setopt($this->instance, CURLOPT_SSL_VERIFYPEER, false); |
@@ -38,8 +39,12 @@ discard block |
||
| 38 | 39 | } |
| 39 | 40 | |
| 40 | 41 | public function get($url,$params = array(),$headers = array(),$opts = array()) { |
| 41 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
| 42 | - if($params && count($params) > 0) $url .= '?' . http_build_query($params); |
|
| 42 | + if (!$this->instance) { |
|
| 43 | + $this->initInstance($this->timeout); |
|
| 44 | + } |
|
| 45 | + if($params && count($params) > 0) { |
|
| 46 | + $url .= '?' . http_build_query($params); |
|
| 47 | + } |
|
| 43 | 48 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 44 | 49 | curl_setopt($this->instance, CURLOPT_HTTPGET, true); |
| 45 | 50 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
@@ -51,7 +56,9 @@ discard block |
||
| 51 | 56 | } |
| 52 | 57 | |
| 53 | 58 | public function post($url, $params = array(),$headers = array(),$opts = array()) { |
| 54 | - if (!$this->instance) $this->initInstance($this->timeout); |
|
| 59 | + if (!$this->instance) { |
|
| 60 | + $this->initInstance($this->timeout); |
|
| 61 | + } |
|
| 55 | 62 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 56 | 63 | curl_setopt($this->instance, CURLOPT_POST, true); |
| 57 | 64 | curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params); |
@@ -66,7 +66,9 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public function getTicket($type = OAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
| 69 | - if(!$accessToken) $accessToken = $this->getAccessToken(); |
|
| 69 | + if(!$accessToken) { |
|
| 70 | + $accessToken = $this->getAccessToken(); |
|
| 71 | + } |
|
| 70 | 72 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
| 71 | 73 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
| 72 | 74 | $this->responseJSON = $this->httpClient->get($url); |
@@ -85,7 +87,9 @@ discard block |
||
| 85 | 87 | (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ? "https://" : "http://"; |
| 86 | 88 | $url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; |
| 87 | 89 | } |
| 88 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 90 | + if(!$ticket) { |
|
| 91 | + $ticket = $this->getTicket(); |
|
| 92 | + } |
|
| 89 | 93 | $timestamp = time(); |
| 90 | 94 | $nonceStr = $this->getNonceStr(); |
| 91 | 95 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |