@@ -8,10 +8,10 @@ discard block |
||
| 8 | 8 | use \Exception; |
| 9 | 9 | |
| 10 | 10 | class WechatPay { |
| 11 | - const TRADETYPE_JSAPI = 'JSAPI',TRADETYPE_NATIVE = 'NATIVE',TRADETYPE_APP = 'APP',TRADETYPE_MWEB = 'MWEB'; |
|
| 11 | + const TRADETYPE_JSAPI = 'JSAPI', TRADETYPE_NATIVE = 'NATIVE', TRADETYPE_APP = 'APP', TRADETYPE_MWEB = 'MWEB'; |
|
| 12 | 12 | const SIGNTYPE_MD5 = 'MD5', SIGNTYPE_HMACSHA256 = 'HMAC-SHA256'; |
| 13 | - const CHECKNAME_FORCECHECK = 'FORCE_CHECK',CHECKNAME_NOCHECK = 'NO_CHECK'; |
|
| 14 | - const ACCOUNTTYPE_BASIC = 'Basic',ACCOUNTTYPE_OPERATION = 'Operation',ACCOUNTTYPE_FEES = 'Fees'; |
|
| 13 | + const CHECKNAME_FORCECHECK = 'FORCE_CHECK', CHECKNAME_NOCHECK = 'NO_CHECK'; |
|
| 14 | + const ACCOUNTTYPE_BASIC = 'Basic', ACCOUNTTYPE_OPERATION = 'Operation', ACCOUNTTYPE_FEES = 'Fees'; |
|
| 15 | 15 | /** 支付 */ |
| 16 | 16 | const URL_UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder"; |
| 17 | 17 | const URL_ORDERQUERY = "https://api.mch.weixin.qq.com/pay/orderquery"; |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | const URL_GETPUBLICKEY = 'https://fraud.mch.weixin.qq.com/risk/getpublickey'; |
| 44 | 44 | public static $BANKCODE = ['工商银行' => '1002', '农业银行' => '1005', '中国银行' => '1026', '建设银行' => '1003', '招商银行' => '1001', |
| 45 | 45 | '邮储银行' => '1066', '交通银行' => '1020', '浦发银行' => '1004', '民生银行' => '1006', '兴业银行' => '1009', '平安银行' => '1010', |
| 46 | - '中信银行' => '1021', '华夏银行' => '1025', '广发银行' => '1027', '光大银行' => '1022', '北京银行' => '1032', '宁波银行' => '1056',]; |
|
| 46 | + '中信银行' => '1021', '华夏银行' => '1025', '广发银行' => '1027', '光大银行' => '1022', '北京银行' => '1032', '宁波银行' => '1056', ]; |
|
| 47 | 47 | |
| 48 | 48 | public $getSignKeyUrl = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey"; |
| 49 | 49 | public $sandbox = false; |
@@ -81,26 +81,26 @@ discard block |
||
| 81 | 81 | $this->httpClient = new HttpClient(5); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - public function setWechatOAuth($wechatOAuth){ |
|
| 84 | + public function setWechatOAuth($wechatOAuth) { |
|
| 85 | 85 | $this->wechatOAuth = $wechatOAuth; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - public function getWechatOAuth(){ |
|
| 89 | - if(!$this->wechatOAuth){ |
|
| 90 | - $this->wechatOAuth = new WechatOAuth($this->config['app_id'],$this->config['app_secret']); |
|
| 88 | + public function getWechatOAuth() { |
|
| 89 | + if (!$this->wechatOAuth) { |
|
| 90 | + $this->wechatOAuth = new WechatOAuth($this->config['app_id'], $this->config['app_secret']); |
|
| 91 | 91 | } |
| 92 | 92 | return $this->wechatOAuth; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - public function setConfig($config){ |
|
| 95 | + public function setConfig($config) { |
|
| 96 | 96 | $this->config = $config; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - public function getConfig(){ |
|
| 99 | + public function getConfig() { |
|
| 100 | 100 | return $this->config; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - public function setHttpClient($httpClient){ |
|
| 103 | + public function setHttpClient($httpClient) { |
|
| 104 | 104 | $this->httpClient = $httpClient; |
| 105 | 105 | } |
| 106 | 106 | |
@@ -116,16 +116,16 @@ discard block |
||
| 116 | 116 | * @return string |
| 117 | 117 | * @throws \Exception |
| 118 | 118 | */ |
| 119 | - public function getPrepayId($body,$out_trade_no,$total_fee,$openid,$spbill_create_ip = null,$ext = null) { |
|
| 120 | - $data = ($ext && is_array($ext))?$ext:array(); |
|
| 119 | + public function getPrepayId($body, $out_trade_no, $total_fee, $openid, $spbill_create_ip = null, $ext = null) { |
|
| 120 | + $data = ($ext && is_array($ext)) ? $ext : array(); |
|
| 121 | 121 | $data["body"] = $body; |
| 122 | 122 | $data["out_trade_no"] = $out_trade_no; |
| 123 | 123 | $data["total_fee"] = $total_fee; |
| 124 | - $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
|
| 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'); |
|
| 128 | - $data["openid"] = $openid; |
|
| 127 | + if (!$openid) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 128 | + $data["openid"] = $openid; |
|
| 129 | 129 | $result = $this->unifiedOrder($data); |
| 130 | 130 | return $result["prepay_id"]; |
| 131 | 131 | } |
@@ -140,8 +140,8 @@ discard block |
||
| 140 | 140 | * @param $ext array |
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | - public function getPrepayIdAPP($body,$out_trade_no,$total_fee,$spbill_create_ip,$ext = null) { |
|
| 144 | - $data = ($ext && is_array($ext))?$ext:array(); |
|
| 143 | + public function getPrepayIdAPP($body, $out_trade_no, $total_fee, $spbill_create_ip, $ext = null) { |
|
| 144 | + $data = ($ext && is_array($ext)) ? $ext : array(); |
|
| 145 | 145 | $data["body"] = $body; |
| 146 | 146 | $data["out_trade_no"] = $out_trade_no; |
| 147 | 147 | $data["total_fee"] = $total_fee; |
@@ -164,15 +164,15 @@ discard block |
||
| 164 | 164 | * @return string |
| 165 | 165 | * @throws Exception |
| 166 | 166 | */ |
| 167 | - public function getCodeUrl($body,$out_trade_no,$total_fee,$product_id,$spbill_create_ip = null,$ext = null){ |
|
| 168 | - $data = ($ext && is_array($ext))?$ext:array(); |
|
| 167 | + public function getCodeUrl($body, $out_trade_no, $total_fee, $product_id, $spbill_create_ip = null, $ext = null) { |
|
| 168 | + $data = ($ext && is_array($ext)) ? $ext : array(); |
|
| 169 | 169 | $data["body"] = $body; |
| 170 | 170 | $data["out_trade_no"] = $out_trade_no; |
| 171 | 171 | $data["total_fee"] = $total_fee; |
| 172 | - $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
|
| 172 | + $data["spbill_create_ip"] = $spbill_create_ip ?: $_SERVER["SERVER_ADDR"]; |
|
| 173 | 173 | $data["notify_url"] = $this->config["notify_url"]; |
| 174 | 174 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
| 175 | - if(!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 175 | + if (!$product_id) throw new Exception('product_id is required when trade_type is NATIVE'); |
|
| 176 | 176 | $data["product_id"] = $product_id; |
| 177 | 177 | $result = $this->unifiedOrder($data); |
| 178 | 178 | return $result["code_url"]; |
@@ -188,15 +188,15 @@ discard block |
||
| 188 | 188 | * @return string |
| 189 | 189 | * @throws Exception |
| 190 | 190 | */ |
| 191 | - public function getMwebUrl($body,$out_trade_no,$total_fee,$ext = null){ |
|
| 192 | - $data = ($ext && is_array($ext))?$ext:array(); |
|
| 191 | + public function getMwebUrl($body, $out_trade_no, $total_fee, $ext = null) { |
|
| 192 | + $data = ($ext && is_array($ext)) ? $ext : array(); |
|
| 193 | 193 | $data["body"] = $body; |
| 194 | 194 | $data["out_trade_no"] = $out_trade_no; |
| 195 | 195 | $data["total_fee"] = $total_fee; |
| 196 | - $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
|
| 196 | + $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : ''; |
|
| 197 | 197 | $data["notify_url"] = $this->config["notify_url"]; |
| 198 | 198 | $data["trade_type"] = self::TRADETYPE_MWEB; |
| 199 | - if(!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
| 199 | + if (!isset($this->config['h5_scene_info'])) throw new Exception('h5_scene_info should be configured'); |
|
| 200 | 200 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
| 201 | 201 | $result = $this->unifiedOrder($data); |
| 202 | 202 | return $result["mweb_url"]; |
@@ -212,25 +212,25 @@ discard block |
||
| 212 | 212 | public function unifiedOrder($params) { |
| 213 | 213 | $data = array(); |
| 214 | 214 | $data["appid"] = $this->config["app_id"]; |
| 215 | - $data["device_info"] = (isset($params['device_info'])&&trim($params['device_info'])!='')?$params['device_info']:null; |
|
| 215 | + $data["device_info"] = (isset($params['device_info']) && trim($params['device_info']) != '') ? $params['device_info'] : null; |
|
| 216 | 216 | $data["body"] = $params['body']; |
| 217 | - $data["detail"] = isset($params['detail'])?$params['detail']:null;//optional |
|
| 218 | - $data["attach"] = isset($params['attach'])?$params['attach']:null;//optional |
|
| 219 | - $data["out_trade_no"] = isset($params['out_trade_no'])?$params['out_trade_no']:null; |
|
| 220 | - $data["fee_type"] = isset($params['fee_type'])?$params['fee_type']:'CNY'; |
|
| 217 | + $data["detail"] = isset($params['detail']) ? $params['detail'] : null; //optional |
|
| 218 | + $data["attach"] = isset($params['attach']) ? $params['attach'] : null; //optional |
|
| 219 | + $data["out_trade_no"] = isset($params['out_trade_no']) ? $params['out_trade_no'] : null; |
|
| 220 | + $data["fee_type"] = isset($params['fee_type']) ? $params['fee_type'] : 'CNY'; |
|
| 221 | 221 | $data["total_fee"] = $params['total_fee']; |
| 222 | 222 | $data["spbill_create_ip"] = $params['spbill_create_ip']; |
| 223 | - $data["time_start"] = isset($params['time_start'])?$params['time_start']:null;//optional |
|
| 224 | - $data["time_expire"] = isset($params['time_expire'])?$params['time_expire']:null;//optional |
|
| 225 | - $data["goods_tag"] = isset($params['goods_tag'])?$params['goods_tag']:null; |
|
| 223 | + $data["time_start"] = isset($params['time_start']) ? $params['time_start'] : null; //optional |
|
| 224 | + $data["time_expire"] = isset($params['time_expire']) ? $params['time_expire'] : null; //optional |
|
| 225 | + $data["goods_tag"] = isset($params['goods_tag']) ? $params['goods_tag'] : null; |
|
| 226 | 226 | $data["notify_url"] = $this->config["notify_url"]; |
| 227 | 227 | $data["trade_type"] = $params['trade_type']; |
| 228 | - 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'); |
|
| 228 | + 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'); |
|
| 230 | 230 | $data["product_id"] = $params['product_id']; |
| 231 | 231 | } |
| 232 | - if($params['trade_type'] == WechatPay::TRADETYPE_JSAPI){ |
|
| 233 | - if(!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 232 | + if ($params['trade_type'] == WechatPay::TRADETYPE_JSAPI) { |
|
| 233 | + if (!isset($params['openid'])) throw new Exception('openid is required when trade_type is JSAPI'); |
|
| 234 | 234 | $data["openid"] = $params['openid']; |
| 235 | 235 | } |
| 236 | 236 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | * @param $transaction_id string 微信订单号 |
| 244 | 244 | * @return array |
| 245 | 245 | */ |
| 246 | - public function queryOrderByTransactionId($transaction_id){ |
|
| 246 | + public function queryOrderByTransactionId($transaction_id) { |
|
| 247 | 247 | $data = array(); |
| 248 | 248 | $data["appid"] = $this->config["app_id"]; |
| 249 | 249 | $data["transaction_id"] = $transaction_id; |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | * @param $out_trade_no string 商户订单号 |
| 258 | 258 | * @return array |
| 259 | 259 | */ |
| 260 | - public function queryOrderByOutTradeNo($out_trade_no){ |
|
| 260 | + public function queryOrderByOutTradeNo($out_trade_no) { |
|
| 261 | 261 | $data = array(); |
| 262 | 262 | $data["appid"] = $this->config["app_id"]; |
| 263 | 263 | $data["out_trade_no"] = $out_trade_no; |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | * @param $offset int 偏移 |
| 273 | 273 | * @return array |
| 274 | 274 | */ |
| 275 | - public function queryRefundByTransactionId($transaction_id,$offset = 0){ |
|
| 275 | + public function queryRefundByTransactionId($transaction_id, $offset = 0) { |
|
| 276 | 276 | $data = array(); |
| 277 | 277 | $data["appid"] = $this->config["app_id"]; |
| 278 | 278 | $data["transaction_id"] = $transaction_id; |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @param $offset int 偏移 |
| 289 | 289 | * @return array |
| 290 | 290 | */ |
| 291 | - public function queryRefundByOutTradeNo($out_trade_no,$offset = 0){ |
|
| 291 | + public function queryRefundByOutTradeNo($out_trade_no, $offset = 0) { |
|
| 292 | 292 | $data = array(); |
| 293 | 293 | $data["appid"] = $this->config["app_id"]; |
| 294 | 294 | $data["out_trade_no"] = $out_trade_no; |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | * @param $offset int 偏移 |
| 305 | 305 | * @return array |
| 306 | 306 | */ |
| 307 | - public function queryRefundByRefundId($refund_id,$offset = 0){ |
|
| 307 | + public function queryRefundByRefundId($refund_id, $offset = 0) { |
|
| 308 | 308 | $data = array(); |
| 309 | 309 | $data["appid"] = $this->config["app_id"]; |
| 310 | 310 | $data["refund_id"] = $refund_id; |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | * @param $offset int 偏移 |
| 321 | 321 | * @return array |
| 322 | 322 | */ |
| 323 | - public function queryRefundByOutRefundNo($out_refund_no,$offset = 0){ |
|
| 323 | + public function queryRefundByOutRefundNo($out_refund_no, $offset = 0) { |
|
| 324 | 324 | $data = array(); |
| 325 | 325 | $data["appid"] = $this->config["app_id"]; |
| 326 | 326 | $data["out_refund_no"] = $out_refund_no; |
@@ -335,11 +335,11 @@ discard block |
||
| 335 | 335 | * @param $out_trade_no string 商户订单号 |
| 336 | 336 | * @return array |
| 337 | 337 | */ |
| 338 | - public function closeOrder($out_trade_no){ |
|
| 338 | + public function closeOrder($out_trade_no) { |
|
| 339 | 339 | $data = array(); |
| 340 | 340 | $data["appid"] = $this->config["app_id"]; |
| 341 | 341 | $data["out_trade_no"] = $out_trade_no; |
| 342 | - $result = $this->post(self::URL_CLOSEORDER, $data,false); |
|
| 342 | + $result = $this->post(self::URL_CLOSEORDER, $data, false); |
|
| 343 | 343 | return $result; |
| 344 | 344 | } |
| 345 | 345 | |
@@ -354,14 +354,14 @@ discard block |
||
| 354 | 354 | * @param $ext array 扩展数组 |
| 355 | 355 | * @return array |
| 356 | 356 | */ |
| 357 | - public function refundByOutTradeNo($out_trade_no,$out_refund_no,$total_fee,$refund_fee,$ext = array()){ |
|
| 358 | - $data = ($ext && is_array($ext))?$ext:array(); |
|
| 357 | + public function refundByOutTradeNo($out_trade_no, $out_refund_no, $total_fee, $refund_fee, $ext = array()) { |
|
| 358 | + $data = ($ext && is_array($ext)) ? $ext : array(); |
|
| 359 | 359 | $data["appid"] = $this->config["app_id"]; |
| 360 | 360 | $data["out_trade_no"] = $out_trade_no; |
| 361 | 361 | $data["out_refund_no"] = $out_refund_no; |
| 362 | 362 | $data["total_fee"] = $total_fee; |
| 363 | 363 | $data["refund_fee"] = $refund_fee; |
| 364 | - $result = $this->post(self::URL_REFUND, $data,true); |
|
| 364 | + $result = $this->post(self::URL_REFUND, $data, true); |
|
| 365 | 365 | return $result; |
| 366 | 366 | } |
| 367 | 367 | |
@@ -376,14 +376,14 @@ discard block |
||
| 376 | 376 | * @param $ext array 扩展数组 |
| 377 | 377 | * @return array |
| 378 | 378 | */ |
| 379 | - public function refundByTransactionId($transaction_id,$out_refund_no,$total_fee,$refund_fee,$ext = array()){ |
|
| 380 | - $data = ($ext && is_array($ext))?$ext:array(); |
|
| 379 | + public function refundByTransactionId($transaction_id, $out_refund_no, $total_fee, $refund_fee, $ext = array()) { |
|
| 380 | + $data = ($ext && is_array($ext)) ? $ext : array(); |
|
| 381 | 381 | $data["appid"] = $this->config["app_id"]; |
| 382 | 382 | $data["transaction_id"] = $transaction_id; |
| 383 | 383 | $data["out_refund_no"] = $out_refund_no; |
| 384 | 384 | $data["total_fee"] = $total_fee; |
| 385 | 385 | $data["refund_fee"] = $refund_fee; |
| 386 | - $result = $this->post(self::URL_REFUND, $data,true); |
|
| 386 | + $result = $this->post(self::URL_REFUND, $data, true); |
|
| 387 | 387 | return $result; |
| 388 | 388 | } |
| 389 | 389 | |
@@ -393,11 +393,11 @@ discard block |
||
| 393 | 393 | * @param $out_trade_no string 商户订单号 |
| 394 | 394 | * @return array |
| 395 | 395 | */ |
| 396 | - public function reverseByOutTradeNo($out_trade_no){ |
|
| 396 | + public function reverseByOutTradeNo($out_trade_no) { |
|
| 397 | 397 | $data = array(); |
| 398 | 398 | $data["appid"] = $this->config["app_id"]; |
| 399 | 399 | $data["out_trade_no"] = $out_trade_no; |
| 400 | - $result = $this->post(self::URL_REVERSE, $data,true); |
|
| 400 | + $result = $this->post(self::URL_REVERSE, $data, true); |
|
| 401 | 401 | return $result; |
| 402 | 402 | } |
| 403 | 403 | |
@@ -407,11 +407,11 @@ discard block |
||
| 407 | 407 | * @param $transaction_id string 微信订单号 |
| 408 | 408 | * @return array |
| 409 | 409 | */ |
| 410 | - public function reverseByTransactionId($transaction_id){ |
|
| 410 | + public function reverseByTransactionId($transaction_id) { |
|
| 411 | 411 | $data = array(); |
| 412 | 412 | $data["appid"] = $this->config["app_id"]; |
| 413 | 413 | $data["transaction_id"] = $transaction_id; |
| 414 | - $result = $this->post(self::URL_REVERSE, $data,true); |
|
| 414 | + $result = $this->post(self::URL_REVERSE, $data, true); |
|
| 415 | 415 | return $result; |
| 416 | 416 | } |
| 417 | 417 | |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | * @param $bill_type string 类型 ALL|SUCCESS |
| 423 | 423 | * @return array |
| 424 | 424 | */ |
| 425 | - public function downloadBill($bill_date,$bill_type = 'ALL'){ |
|
| 425 | + public function downloadBill($bill_date, $bill_type = 'ALL') { |
|
| 426 | 426 | $data = array(); |
| 427 | 427 | $data["appid"] = $this->config["app_id"]; |
| 428 | 428 | $data["bill_date"] = $bill_date; |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | * @param $tar_type string 压缩账单 |
| 440 | 440 | * @return array |
| 441 | 441 | */ |
| 442 | - public function downloadFundFlow($bill_date,$account_type = self::ACCOUNTTYPE_BASIC,$tar_type = 'GZIP'){ |
|
| 442 | + public function downloadFundFlow($bill_date, $account_type = self::ACCOUNTTYPE_BASIC, $tar_type = 'GZIP') { |
|
| 443 | 443 | $data = array(); |
| 444 | 444 | $data["appid"] = $this->config["app_id"]; |
| 445 | 445 | $data["bill_date"] = $bill_date; |
@@ -466,14 +466,14 @@ discard block |
||
| 466 | 466 | * @return array |
| 467 | 467 | * @throws Exception |
| 468 | 468 | */ |
| 469 | - public function sendRedPack($mch_billno,$send_name,$re_openid,$total_amount,$total_num,$wishing,$act_name,$remark,$scene_id = '',$riskinfo = '',$consume_mch_id = ''){ |
|
| 469 | + public function sendRedPack($mch_billno, $send_name, $re_openid, $total_amount, $total_num, $wishing, $act_name, $remark, $scene_id = '', $riskinfo = '', $consume_mch_id = '') { |
|
| 470 | 470 | $data = array(); |
| 471 | 471 | $data["wxappid"] = $this->config["app_id"]; |
| 472 | 472 | $data["mch_billno"] = $mch_billno; |
| 473 | 473 | $data["send_name"] = $send_name; |
| 474 | 474 | $data["re_openid"] = $re_openid; |
| 475 | 475 | $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"); |
|
| 476 | + if ($total_amount > 20000 && trim($scene_id) == '') throw new Exception("scene_id is required when total_amount beyond 20000"); |
|
| 477 | 477 | $data["total_num"] = $total_num; |
| 478 | 478 | $data["wishing"] = $wishing; |
| 479 | 479 | $data["act_name"] = $act_name; |
@@ -502,14 +502,14 @@ discard block |
||
| 502 | 502 | * @return array |
| 503 | 503 | * @throws Exception |
| 504 | 504 | */ |
| 505 | - public function sendGroupRedPack($mch_billno,$send_name,$re_openid,$total_amount,$total_num,$wishing,$act_name,$remark,$scene_id = '',$riskinfo = '',$consume_mch_id = ''){ |
|
| 505 | + public function sendGroupRedPack($mch_billno, $send_name, $re_openid, $total_amount, $total_num, $wishing, $act_name, $remark, $scene_id = '', $riskinfo = '', $consume_mch_id = '') { |
|
| 506 | 506 | $data = array(); |
| 507 | - $data["wxappid"] = $this->config["app_id"];//NOTE: WXappid |
|
| 507 | + $data["wxappid"] = $this->config["app_id"]; //NOTE: WXappid |
|
| 508 | 508 | $data["mch_billno"] = $mch_billno; |
| 509 | 509 | $data["send_name"] = $send_name; |
| 510 | 510 | $data["re_openid"] = $re_openid; |
| 511 | 511 | $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)"); |
|
| 512 | + if ($total_amount > 20000 && trim($scene_id) == '') throw new Exception("scene_id is required when total_amount beyond 20000(200rmb)"); |
|
| 513 | 513 | $data["total_num"] = $total_num; |
| 514 | 514 | $data["amt_type"] = 'ALL_RAND'; //红包金额设置方式 ALL_RAND—全部随机 |
| 515 | 515 | $data["wishing"] = $wishing; |
@@ -529,7 +529,7 @@ discard block |
||
| 529 | 529 | * @throws Exception |
| 530 | 530 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_6&index=5 |
| 531 | 531 | */ |
| 532 | - public function getHbInfo($mch_billno){ |
|
| 532 | + public function getHbInfo($mch_billno) { |
|
| 533 | 533 | $data = array(); |
| 534 | 534 | $data["mch_billno"] = $mch_billno; |
| 535 | 535 | $data["appid"] = $this->config["app_id"]; |
@@ -547,14 +547,14 @@ discard block |
||
| 547 | 547 | * @param int $limit 条数 |
| 548 | 548 | * @return array |
| 549 | 549 | */ |
| 550 | - public function batchQueryComment($begin_time,$end_time,$offset = 0,$limit = 200){ |
|
| 550 | + public function batchQueryComment($begin_time, $end_time, $offset = 0, $limit = 200) { |
|
| 551 | 551 | $data = array(); |
| 552 | 552 | $data["appid"] = $this->config["app_id"]; |
| 553 | 553 | $data["begin_time"] = $begin_time; |
| 554 | 554 | $data["end_time"] = $end_time; |
| 555 | 555 | $data["offset"] = $offset; |
| 556 | 556 | $data["limit"] = $limit; |
| 557 | - $data["sign"] = $this->sign($data,WechatPay::SIGNTYPE_HMACSHA256); |
|
| 557 | + $data["sign"] = $this->sign($data, WechatPay::SIGNTYPE_HMACSHA256); |
|
| 558 | 558 | $result = $this->post(self::URL_BATCHQUERYCOMMENT, $data, true); //cert is required |
| 559 | 559 | return $result; |
| 560 | 560 | } |
@@ -567,21 +567,21 @@ discard block |
||
| 567 | 567 | */ |
| 568 | 568 | public function getPackage($prepay_id, $trade_type = WechatPay::TRADETYPE_JSAPI) { |
| 569 | 569 | $data = array(); |
| 570 | - if ($trade_type == WechatPay::TRADETYPE_JSAPI){ |
|
| 570 | + if ($trade_type == WechatPay::TRADETYPE_JSAPI) { |
|
| 571 | 571 | $data["package"] = "prepay_id=$prepay_id"; |
| 572 | 572 | $data["timeStamp"] = time(); |
| 573 | 573 | $data["nonceStr"] = $this->getNonceStr(); |
| 574 | 574 | $data["appId"] = $this->config["app_id"]; |
| 575 | 575 | $data["signType"] = "MD5"; |
| 576 | 576 | $data["paySign"] = $this->sign($data); |
| 577 | - } else if ($trade_type == WechatPay::TRADETYPE_APP){ |
|
| 577 | + }else if ($trade_type == WechatPay::TRADETYPE_APP) { |
|
| 578 | 578 | $data["package"] = "Sign=WXPay"; |
| 579 | 579 | $data['prepayid'] = $prepay_id; |
| 580 | 580 | $data['partnerid'] = $this->config["mch_id"]; |
| 581 | 581 | $data["timestamp"] = time(); |
| 582 | 582 | $data["noncestr"] = $this->getNonceStr(); |
| 583 | 583 | $data["appid"] = $this->config["app_id"]; |
| 584 | - $data["sign"] = $this->sign($data); |
|
| 584 | + $data["sign"] = $this->sign($data); |
|
| 585 | 585 | } |
| 586 | 586 | return $data; |
| 587 | 587 | } |
@@ -597,15 +597,15 @@ discard block |
||
| 597 | 597 | * @param array $ext |
| 598 | 598 | * @return array |
| 599 | 599 | */ |
| 600 | - public function microPay($body,$out_trade_no,$total_fee,$spbill_create_ip,$auth_code,$ext = array()){ |
|
| 601 | - $data = (!empty($ext) && is_array($ext))?$ext:array(); |
|
| 600 | + public function microPay($body, $out_trade_no, $total_fee, $spbill_create_ip, $auth_code, $ext = array()) { |
|
| 601 | + $data = (!empty($ext) && is_array($ext)) ? $ext : array(); |
|
| 602 | 602 | $data["appid"] = $this->config["app_id"]; |
| 603 | 603 | $data["body"] = $body; |
| 604 | 604 | $data["out_trade_no"] = $out_trade_no; |
| 605 | 605 | $data["total_fee"] = $total_fee; |
| 606 | 606 | $data["spbill_create_ip"] = $spbill_create_ip; |
| 607 | 607 | $data["auth_code"] = $auth_code; |
| 608 | - $result = $this->post(self::URL_MICROPAY,$data,false); |
|
| 608 | + $result = $this->post(self::URL_MICROPAY, $data, false); |
|
| 609 | 609 | return $result; |
| 610 | 610 | } |
| 611 | 611 | |
@@ -616,17 +616,17 @@ discard block |
||
| 616 | 616 | * @return null |
| 617 | 617 | * @throws Exception |
| 618 | 618 | */ |
| 619 | - public function onPaidNotify($notify_data,callable $callback = null){ |
|
| 620 | - if(!is_array($notify_data)){ |
|
| 619 | + public function onPaidNotify($notify_data, callable $callback = null) { |
|
| 620 | + if (!is_array($notify_data)) { |
|
| 621 | 621 | $notify_data = $this->xml2array($notify_data); |
| 622 | 622 | } |
| 623 | - if($this->validateSign($notify_data)){ |
|
| 624 | - if($callback && is_callable($callback)){ |
|
| 625 | - return call_user_func_array( $callback , [$notify_data] ); |
|
| 626 | - }else{ |
|
| 623 | + if ($this->validateSign($notify_data)) { |
|
| 624 | + if ($callback && is_callable($callback)) { |
|
| 625 | + return call_user_func_array($callback, [$notify_data]); |
|
| 626 | + }else { |
|
| 627 | 627 | $this->responseNotify(); |
| 628 | 628 | } |
| 629 | - }else{ |
|
| 629 | + }else { |
|
| 630 | 630 | throw new Exception('Invalid paid notify data'); |
| 631 | 631 | } |
| 632 | 632 | } |
@@ -638,17 +638,17 @@ discard block |
||
| 638 | 638 | * @return mixed |
| 639 | 639 | * @throws Exception |
| 640 | 640 | */ |
| 641 | - public function onRefundedNotify($notify_data,callable $callback = null){ |
|
| 642 | - if(!is_array($notify_data)){ |
|
| 641 | + public function onRefundedNotify($notify_data, callable $callback = null) { |
|
| 642 | + if (!is_array($notify_data)) { |
|
| 643 | 643 | $notify_data = $this->xml2array($notify_data); |
| 644 | 644 | } |
| 645 | - if($this->validateSign($notify_data)){ |
|
| 646 | - if($callback && is_callable($callback)){ |
|
| 647 | - return call_user_func_array( $callback , $notify_data ); |
|
| 648 | - }else{ |
|
| 645 | + if ($this->validateSign($notify_data)) { |
|
| 646 | + if ($callback && is_callable($callback)) { |
|
| 647 | + return call_user_func_array($callback, $notify_data); |
|
| 648 | + }else { |
|
| 649 | 649 | $this->responseNotify(); |
| 650 | 650 | } |
| 651 | - }else{ |
|
| 651 | + }else { |
|
| 652 | 652 | throw new Exception('Invalid refunded notify data'); |
| 653 | 653 | } |
| 654 | 654 | } |
@@ -672,7 +672,7 @@ discard block |
||
| 672 | 672 | * @param $return_code string 返回状态码 SUCCESS/FAIL |
| 673 | 673 | * @param $return_msg string 返回信息 |
| 674 | 674 | */ |
| 675 | - public function responseNotify($return_code="SUCCESS", $return_msg= 'OK') { |
|
| 675 | + public function responseNotify($return_code = "SUCCESS", $return_msg = 'OK') { |
|
| 676 | 676 | $data = array(); |
| 677 | 677 | $data["return_code"] = $return_code; |
| 678 | 678 | if ($return_msg) { |
@@ -698,8 +698,8 @@ discard block |
||
| 698 | 698 | * @param string $err_code_des |
| 699 | 699 | * @return array |
| 700 | 700 | */ |
| 701 | - public function report($interface_url,$execution_time,$return_code,$result_code,$user_ip,$out_trade_no = null,$time = null,$device_info = null, |
|
| 702 | - $return_msg = null,$err_code = null,$err_code_des = null){ |
|
| 701 | + public function report($interface_url, $execution_time, $return_code, $result_code, $user_ip, $out_trade_no = null, $time = null, $device_info = null, |
|
| 702 | + $return_msg = null, $err_code = null, $err_code_des = null) { |
|
| 703 | 703 | $data = array(); |
| 704 | 704 | $data["appid"] = $this->config["app_id"]; |
| 705 | 705 | $data["interface_url"] = $interface_url; |
@@ -707,12 +707,12 @@ discard block |
||
| 707 | 707 | $data["return_code"] = $return_code; |
| 708 | 708 | $data["result_code"] = $result_code; |
| 709 | 709 | $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; |
|
| 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; |
|
| 716 | 716 | $result = $this->post(self::URL_REPORT, $data, false); |
| 717 | 717 | return $result; |
| 718 | 718 | } |
@@ -723,11 +723,11 @@ discard block |
||
| 723 | 723 | * @param $longurl |
| 724 | 724 | * @return string |
| 725 | 725 | */ |
| 726 | - public function shortUrl($longurl){ |
|
| 726 | + public function shortUrl($longurl) { |
|
| 727 | 727 | $data = array(); |
| 728 | 728 | $data["appid"] = $this->config["app_id"]; |
| 729 | 729 | $data["long_url"] = $longurl; |
| 730 | - $result = $this->post(self::URL_SHORTURL,$data,false); |
|
| 730 | + $result = $this->post(self::URL_SHORTURL, $data, false); |
|
| 731 | 731 | return $result['short_url']; |
| 732 | 732 | } |
| 733 | 733 | |
@@ -737,11 +737,11 @@ discard block |
||
| 737 | 737 | * @param $auth_code |
| 738 | 738 | * @return mixed |
| 739 | 739 | */ |
| 740 | - public function authCodeToOpenId($auth_code){ |
|
| 740 | + public function authCodeToOpenId($auth_code) { |
|
| 741 | 741 | $data = array(); |
| 742 | 742 | $data["appid"] = $this->config["app_id"]; |
| 743 | 743 | $data["auth_code"] = $auth_code; |
| 744 | - $result = $this->post(self::URL_AUTHCODETOOPENID,$data,false); |
|
| 744 | + $result = $this->post(self::URL_AUTHCODETOOPENID, $data, false); |
|
| 745 | 745 | return $result['openid']; |
| 746 | 746 | } |
| 747 | 747 | |
@@ -758,19 +758,19 @@ discard block |
||
| 758 | 758 | * @return array |
| 759 | 759 | * @throws Exception |
| 760 | 760 | */ |
| 761 | - public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
|
| 761 | + public function transferWallet($partner_trade_no, $openid, $amount, $desc, $spbill_create_ip = null, $re_user_name = null, $check_name = WechatPay::CHECKNAME_FORCECHECK) { |
|
| 762 | 762 | $data = array(); |
| 763 | - if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
| 763 | + if ($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
|
| 764 | 764 | $data["mch_appid"] = $this->config["app_id"]; |
| 765 | 765 | $data["mchid"] = $this->config["mch_id"]; |
| 766 | 766 | $data["partner_trade_no"] = $partner_trade_no; |
| 767 | 767 | $data["openid"] = $openid; |
| 768 | 768 | $data["amount"] = $amount; |
| 769 | 769 | $data["desc"] = $desc; |
| 770 | - $data['spbill_create_ip'] = $spbill_create_ip?:$_SERVER['SERVER_ADDR']; |
|
| 770 | + $data['spbill_create_ip'] = $spbill_create_ip ?: $_SERVER['SERVER_ADDR']; |
|
| 771 | 771 | $data["check_name"] = $check_name; |
| 772 | 772 | $data["re_user_name"] = $re_user_name; |
| 773 | - $result = $this->post(self::URL_TRANSFER_WALLET,$data,true); |
|
| 773 | + $result = $this->post(self::URL_TRANSFER_WALLET, $data, true); |
|
| 774 | 774 | return $result; |
| 775 | 775 | } |
| 776 | 776 | |
@@ -780,12 +780,12 @@ discard block |
||
| 780 | 780 | * @param $partner_trade_no |
| 781 | 781 | * @return array |
| 782 | 782 | */ |
| 783 | - public function queryTransferWallet($partner_trade_no){ |
|
| 783 | + public function queryTransferWallet($partner_trade_no) { |
|
| 784 | 784 | $data = array(); |
| 785 | 785 | $data["appid"] = $this->config["app_id"]; |
| 786 | 786 | $data["mch_id"] = $this->config["mch_id"]; |
| 787 | 787 | $data["partner_trade_no"] = $partner_trade_no; |
| 788 | - $result = $this->post(self::URL_QUERY_TRANSFER_WALLET,$data,true); |
|
| 788 | + $result = $this->post(self::URL_QUERY_TRANSFER_WALLET, $data, true); |
|
| 789 | 789 | return $result; |
| 790 | 790 | } |
| 791 | 791 | |
@@ -801,8 +801,8 @@ discard block |
||
| 801 | 801 | * @return array |
| 802 | 802 | * @throws Exception |
| 803 | 803 | */ |
| 804 | - 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"); |
|
| 804 | + 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"); |
|
| 806 | 806 | $data = array(); |
| 807 | 807 | $data["partner_trade_no"] = $partner_trade_no; |
| 808 | 808 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
@@ -812,7 +812,7 @@ discard block |
||
| 812 | 812 | $data["bank_code"] = $bank_code; |
| 813 | 813 | $data["desc"] = $desc; |
| 814 | 814 | $data["amount"] = $amount; |
| 815 | - $result = $this->post(self::URL_TRANSFER_BANKCARD,$data,true); |
|
| 815 | + $result = $this->post(self::URL_TRANSFER_BANKCARD, $data, true); |
|
| 816 | 816 | return $result; |
| 817 | 817 | } |
| 818 | 818 | |
@@ -822,12 +822,12 @@ discard block |
||
| 822 | 822 | * @param $partner_trade_no |
| 823 | 823 | * @return array |
| 824 | 824 | */ |
| 825 | - public function queryTransferBankCard($partner_trade_no){ |
|
| 825 | + public function queryTransferBankCard($partner_trade_no) { |
|
| 826 | 826 | $data = array(); |
| 827 | 827 | $data["appid"] = $this->config["app_id"]; |
| 828 | 828 | $data["mch_id"] = $this->config["mch_id"]; |
| 829 | 829 | $data["partner_trade_no"] = $partner_trade_no; |
| 830 | - $result = $this->post(self::URL_QUERY_TRANSFER_WALLET,$data,true); |
|
| 830 | + $result = $this->post(self::URL_QUERY_TRANSFER_WALLET, $data, true); |
|
| 831 | 831 | return $result; |
| 832 | 832 | } |
| 833 | 833 | |
@@ -841,14 +841,14 @@ discard block |
||
| 841 | 841 | * @param array $ext |
| 842 | 842 | * @return array |
| 843 | 843 | */ |
| 844 | - public function sendCoupon($coupon_stock_id,$open_id,$partner_trade_no,$op_user_id = '',$ext = array()){ |
|
| 845 | - $data = (!empty($ext) && is_array($ext))?$ext:array(); |
|
| 844 | + public function sendCoupon($coupon_stock_id, $open_id, $partner_trade_no, $op_user_id = '', $ext = array()) { |
|
| 845 | + $data = (!empty($ext) && is_array($ext)) ? $ext : array(); |
|
| 846 | 846 | $data["partner_trade_no"] = $partner_trade_no; |
| 847 | 847 | $data["coupon_stock_id"] = $coupon_stock_id; |
| 848 | 848 | $data["openid_count"] = 1; |
| 849 | 849 | $data["open_id"] = $open_id; |
| 850 | 850 | $data["op_user_id"] = $op_user_id; |
| 851 | - $result = $this->post(self::URL_SEND_COUPON,$data,true); |
|
| 851 | + $result = $this->post(self::URL_SEND_COUPON, $data, true); |
|
| 852 | 852 | return $result; |
| 853 | 853 | } |
| 854 | 854 | |
@@ -859,11 +859,11 @@ discard block |
||
| 859 | 859 | * @param string $op_user_id |
| 860 | 860 | * @return array |
| 861 | 861 | */ |
| 862 | - public function queryCouponStock($coupon_stock_id,$op_user_id = ''){ |
|
| 862 | + public function queryCouponStock($coupon_stock_id, $op_user_id = '') { |
|
| 863 | 863 | $data = array(); |
| 864 | 864 | $data["coupon_stock_id"] = $coupon_stock_id; |
| 865 | 865 | $data["op_user_id"] = $op_user_id; |
| 866 | - $result = $this->post(self::URL_QUERY_COUPON_STOCK,$data,false); |
|
| 866 | + $result = $this->post(self::URL_QUERY_COUPON_STOCK, $data, false); |
|
| 867 | 867 | return $result; |
| 868 | 868 | } |
| 869 | 869 | |
@@ -877,13 +877,13 @@ discard block |
||
| 877 | 877 | * @param array $ext |
| 878 | 878 | * @return array |
| 879 | 879 | */ |
| 880 | - public function queryCouponsInfo($coupon_id,$open_id,$stock_id,$op_user_id = '',$ext = array()){ |
|
| 881 | - $data = (!empty($ext) && is_array($ext))?$ext:array(); |
|
| 880 | + public function queryCouponsInfo($coupon_id, $open_id, $stock_id, $op_user_id = '', $ext = array()) { |
|
| 881 | + $data = (!empty($ext) && is_array($ext)) ? $ext : array(); |
|
| 882 | 882 | $data["coupon_id"] = $coupon_id; |
| 883 | 883 | $data["stock_id"] = $stock_id; |
| 884 | 884 | $data["open_id"] = $open_id; |
| 885 | 885 | $data["op_user_id"] = $op_user_id; |
| 886 | - $result = $this->post(self::URL_QUERY_COUPON_INFO,$data,false); |
|
| 886 | + $result = $this->post(self::URL_QUERY_COUPON_INFO, $data, false); |
|
| 887 | 887 | return $result; |
| 888 | 888 | } |
| 889 | 889 | |
@@ -894,7 +894,7 @@ discard block |
||
| 894 | 894 | * @return string |
| 895 | 895 | * @throws Exception |
| 896 | 896 | */ |
| 897 | - public function getPublicKey($refresh = false){ |
|
| 897 | + public function getPublicKey($refresh = false) { |
|
| 898 | 898 | if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) { |
| 899 | 899 | $pubkey = file_get_contents($this->config["rsa_pubkey_path"]); |
| 900 | 900 | return $pubkey; |
@@ -902,7 +902,7 @@ discard block |
||
| 902 | 902 | $data = array(); |
| 903 | 903 | $data["mch_id"] = $this->config["mch_id"]; |
| 904 | 904 | $data["sign_type"] = $this->config["sign_type"]; |
| 905 | - $result = $this->post(self::URL_GETPUBLICKEY,$data,true); |
|
| 905 | + $result = $this->post(self::URL_GETPUBLICKEY, $data, true); |
|
| 906 | 906 | $pubkey = $result['pub_key']; |
| 907 | 907 | $pubkey = $this->convertPKCS1toPKCS8($pubkey); |
| 908 | 908 | $fp = fopen($this->config["rsa_pubkey_path"], "w"); |
@@ -915,20 +915,20 @@ discard block |
||
| 915 | 915 | } |
| 916 | 916 | } |
| 917 | 917 | |
| 918 | - private function convertPKCS1toPKCS8($pkcs1){ |
|
| 918 | + private function convertPKCS1toPKCS8($pkcs1) { |
|
| 919 | 919 | $start_key = $pkcs1; |
| 920 | 920 | $start_key = str_replace('-----BEGIN RSA PUBLIC KEY-----', '', $start_key); |
| 921 | 921 | $start_key = trim(str_replace('-----END RSA PUBLIC KEY-----', '', $start_key)); |
| 922 | - $key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' . str_replace("\n", '', $start_key); |
|
| 923 | - $key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($key, 64, "\n", true) . "\n-----END PUBLIC KEY-----"; |
|
| 922 | + $key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A'.str_replace("\n", '', $start_key); |
|
| 923 | + $key = "-----BEGIN PUBLIC KEY-----\n".wordwrap($key, 64, "\n", true)."\n-----END PUBLIC KEY-----"; |
|
| 924 | 924 | return $key; |
| 925 | 925 | } |
| 926 | 926 | |
| 927 | - public function rsaEncrypt($data){ |
|
| 927 | + public function rsaEncrypt($data) { |
|
| 928 | 928 | $pubkey = $this->getPublicKey(); |
| 929 | 929 | $encrypted = null; |
| 930 | 930 | $pubkey = openssl_get_publickey($pubkey); |
| 931 | - if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 931 | + if (openssl_public_encrypt($data, $encrypted, $pubkey, OPENSSL_PKCS1_OAEP_PADDING)) |
|
| 932 | 932 | $data = base64_encode($encrypted); |
| 933 | 933 | else |
| 934 | 934 | throw new Exception('Unable to encrypt data'); |
@@ -940,10 +940,10 @@ discard block |
||
| 940 | 940 | * @ref https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=23_1 |
| 941 | 941 | * @return array |
| 942 | 942 | */ |
| 943 | - public function getSignKey(){ |
|
| 943 | + public function getSignKey() { |
|
| 944 | 944 | $data = array(); |
| 945 | 945 | $data["mch_id"] = $this->config["mch_id"]; |
| 946 | - $result = $this->post($this->getSignKeyUrl,$data,false); |
|
| 946 | + $result = $this->post($this->getSignKeyUrl, $data, false); |
|
| 947 | 947 | return $result['sandbox_signkey']; |
| 948 | 948 | } |
| 949 | 949 | |
@@ -953,8 +953,8 @@ discard block |
||
| 953 | 953 | * @param string $ticket |
| 954 | 954 | * @return array |
| 955 | 955 | */ |
| 956 | - public function getSignPackage($url, $ticket = null){ |
|
| 957 | - if(!$ticket) $ticket = $this->getTicket(); |
|
| 956 | + public function getSignPackage($url, $ticket = null) { |
|
| 957 | + if (!$ticket) $ticket = $this->getTicket(); |
|
| 958 | 958 | $timestamp = time(); |
| 959 | 959 | $nonceStr = $this->getNonceStr(); |
| 960 | 960 | $rawString = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
@@ -976,19 +976,19 @@ discard block |
||
| 976 | 976 | * @param boolean $cache |
| 977 | 977 | * @return string |
| 978 | 978 | */ |
| 979 | - public function getTicket($cache = true){ |
|
| 979 | + public function getTicket($cache = true) { |
|
| 980 | 980 | $ticket = null; |
| 981 | - if(isset($this->config['jsapi_ticket']) && file_exists($this->config['jsapi_ticket'])){ |
|
| 981 | + if (isset($this->config['jsapi_ticket']) && file_exists($this->config['jsapi_ticket'])) { |
|
| 982 | 982 | $data = json_decode(file_get_contents($this->config['jsapi_ticket'])); |
| 983 | 983 | if ($data && $data->expires_at < time()) { |
| 984 | 984 | $ticket = $data->ticket; |
| 985 | 985 | } |
| 986 | 986 | } |
| 987 | - if(!$ticket){ |
|
| 987 | + if (!$ticket) { |
|
| 988 | 988 | $data = $this->getWechatOAuth()->getTicket(); |
| 989 | 989 | $data = json_decode($data); |
| 990 | 990 | $data->expires_at = time() + $data->expires_in; |
| 991 | - if($cache === true){ |
|
| 991 | + if ($cache === true) { |
|
| 992 | 992 | $fp = fopen($this->config["jsapi_ticket"], "w"); |
| 993 | 993 | fwrite($fp, json_encode($data)); |
| 994 | 994 | if ($fp) fclose($fp); |
@@ -998,10 +998,10 @@ discard block |
||
| 998 | 998 | return $ticket; |
| 999 | 999 | } |
| 1000 | 1000 | |
| 1001 | - 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); |
|
| 1001 | + 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); |
|
| 1005 | 1005 | $this->requestXML = $this->responseXML = null; |
| 1006 | 1006 | $this->requestArray = $this->responseArray = null; |
| 1007 | 1007 | |
@@ -1013,53 +1013,53 @@ discard block |
||
| 1013 | 1013 | CURLOPT_RETURNTRANSFER => true, |
| 1014 | 1014 | CURLOPT_TIMEOUT => 10 |
| 1015 | 1015 | ]; |
| 1016 | - if($cert == true){ |
|
| 1016 | + if ($cert == true) { |
|
| 1017 | 1017 | $opts[CURLOPT_SSLCERTTYPE] = 'PEM'; |
| 1018 | 1018 | $opts[CURLOPT_SSLCERT] = $this->config['ssl_cert_path']; |
| 1019 | 1019 | $opts[CURLOPT_SSLKEYTYPE] = 'PEM'; |
| 1020 | 1020 | $opts[CURLOPT_SSLKEY] = $this->config['ssl_key_path']; |
| 1021 | 1021 | } |
| 1022 | 1022 | $processResponse = true; |
| 1023 | - if(in_array($url,[self::URL_DOWNLOADBILL,self::URL_DOWNLOAD_FUND_FLOW,self::URL_BATCHQUERYCOMMENT])){ |
|
| 1023 | + if (in_array($url, [self::URL_DOWNLOADBILL, self::URL_DOWNLOAD_FUND_FLOW, self::URL_BATCHQUERYCOMMENT])) { |
|
| 1024 | 1024 | $processResponse = false; |
| 1025 | 1025 | } |
| 1026 | - if($this->sandbox === true){ |
|
| 1026 | + if ($this->sandbox === true) { |
|
| 1027 | 1027 | $host = "https://api.mch.weixin.qq.com"; |
| 1028 | - $url = str_replace($host,'',$url); |
|
| 1028 | + $url = str_replace($host, '', $url); |
|
| 1029 | 1029 | $url = "{$host}/sandboxnew{$url}"; |
| 1030 | 1030 | } |
| 1031 | - $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
|
| 1032 | - if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1031 | + $content = $this->httpClient->post($url, $this->requestXML, [], $opts); |
|
| 1032 | + if (!$content) throw new Exception("Empty response with {$this->requestXML}"); |
|
| 1033 | 1033 | |
| 1034 | 1034 | $this->responseXML = $content; |
| 1035 | - if($processResponse) |
|
| 1035 | + if ($processResponse) |
|
| 1036 | 1036 | return $this->processResponseXML($this->responseXML); |
| 1037 | 1037 | else return $this->responseXML; |
| 1038 | 1038 | |
| 1039 | 1039 | } |
| 1040 | 1040 | |
| 1041 | - private function processResponseXML($responseXML){ |
|
| 1041 | + private function processResponseXML($responseXML) { |
|
| 1042 | 1042 | $result = $this->xml2array($responseXML); |
| 1043 | 1043 | $this->responseArray = $result; |
| 1044 | - if(empty($result['return_code'])){ |
|
| 1044 | + if (empty($result['return_code'])) { |
|
| 1045 | 1045 | throw new Exception("No return code presents in {$this->responseXML}"); |
| 1046 | 1046 | } |
| 1047 | 1047 | $this->returnCode = $result["return_code"]; |
| 1048 | - $this->returnMsg = isset($result['return_msg'])?$result['return_msg']:''; |
|
| 1048 | + $this->returnMsg = isset($result['return_msg']) ? $result['return_msg'] : ''; |
|
| 1049 | 1049 | |
| 1050 | 1050 | if ($this->returnCode == "SUCCESS") { |
| 1051 | - if(isset($result['result_code']) && $result['result_code'] == "FAIL"){ |
|
| 1051 | + if (isset($result['result_code']) && $result['result_code'] == "FAIL") { |
|
| 1052 | 1052 | $this->resultCode = $result['result_code']; |
| 1053 | 1053 | $this->errCode = $result['err_code']; |
| 1054 | 1054 | $this->errCodeDes = $result['err_code_des']; |
| 1055 | 1055 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
| 1056 | - }else{ |
|
| 1056 | + }else { |
|
| 1057 | 1057 | return $result; |
| 1058 | 1058 | } |
| 1059 | - } else { |
|
| 1060 | - if($result["return_code"] == "FAIL"){ |
|
| 1059 | + }else { |
|
| 1060 | + if ($result["return_code"] == "FAIL") { |
|
| 1061 | 1061 | throw new Exception($this->returnMsg); |
| 1062 | - }else{ |
|
| 1062 | + }else { |
|
| 1063 | 1063 | $this->resultCode = $result['result_code']; |
| 1064 | 1064 | $this->errCode = $result['err_code']; |
| 1065 | 1065 | $this->errCodeDes = $result['err_code_des']; |
@@ -1068,28 +1068,28 @@ discard block |
||
| 1068 | 1068 | } |
| 1069 | 1069 | } |
| 1070 | 1070 | |
| 1071 | - public function sign($data,$sign_type = WechatPay::SIGNTYPE_MD5) { |
|
| 1071 | + public function sign($data, $sign_type = WechatPay::SIGNTYPE_MD5) { |
|
| 1072 | 1072 | ksort($data); |
| 1073 | 1073 | $string1 = ""; |
| 1074 | 1074 | foreach ($data as $k => $v) { |
| 1075 | - if ($v && trim($v)!='') { |
|
| 1075 | + if ($v && trim($v) != '') { |
|
| 1076 | 1076 | $string1 .= "$k=$v&"; |
| 1077 | 1077 | } |
| 1078 | 1078 | } |
| 1079 | - $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
|
| 1080 | - if($sign_type == WechatPay::SIGNTYPE_MD5){ |
|
| 1079 | + $stringSignTemp = $string1."key=".$this->config["api_key"]; |
|
| 1080 | + if ($sign_type == WechatPay::SIGNTYPE_MD5) { |
|
| 1081 | 1081 | $sign = strtoupper(md5($stringSignTemp)); |
| 1082 | - }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
|
| 1083 | - $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
|
| 1082 | + }elseif ($sign_type == WechatPay::SIGNTYPE_HMACSHA256) { |
|
| 1083 | + $sign = strtoupper(hash_hmac('sha256', $stringSignTemp, $this->config["api_key"])); |
|
| 1084 | 1084 | }else throw new Exception("Not supported sign type - $sign_type"); |
| 1085 | 1085 | return $sign; |
| 1086 | 1086 | } |
| 1087 | 1087 | |
| 1088 | 1088 | private function array2xml($array) { |
| 1089 | - $xml = "<xml>" . PHP_EOL; |
|
| 1089 | + $xml = "<xml>".PHP_EOL; |
|
| 1090 | 1090 | foreach ($array as $k => $v) { |
| 1091 | - if($v && trim($v)!='') |
|
| 1092 | - $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL; |
|
| 1091 | + if ($v && trim($v) != '') |
|
| 1092 | + $xml .= "<$k><![CDATA[$v]]></$k>".PHP_EOL; |
|
| 1093 | 1093 | } |
| 1094 | 1094 | $xml .= "</xml>"; |
| 1095 | 1095 | return $xml; |
@@ -1098,19 +1098,19 @@ discard block |
||
| 1098 | 1098 | private function xml2array($xml) { |
| 1099 | 1099 | $array = array(); |
| 1100 | 1100 | $tmp = array(); |
| 1101 | - try{ |
|
| 1101 | + try { |
|
| 1102 | 1102 | $tmp = (array) simplexml_load_string($xml); |
| 1103 | - }catch(Exception $e){ |
|
| 1103 | + } catch (Exception $e) { |
|
| 1104 | 1104 | throw $e; |
| 1105 | 1105 | } |
| 1106 | - foreach ( $tmp as $k => $v) { |
|
| 1106 | + foreach ($tmp as $k => $v) { |
|
| 1107 | 1107 | $array[$k] = (string) $v; |
| 1108 | 1108 | } |
| 1109 | 1109 | return $array; |
| 1110 | 1110 | } |
| 1111 | 1111 | |
| 1112 | 1112 | private function getNonceStr() { |
| 1113 | - return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32); |
|
| 1113 | + return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 32); |
|
| 1114 | 1114 | } |
| 1115 | 1115 | |
| 1116 | 1116 | } |
| 1117 | 1117 | \ No newline at end of file |
@@ -124,7 +124,9 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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×tamp=$timestamp&url=$url"; |
@@ -991,7 +1024,9 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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) { |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | namespace zhangv\wechat; |
| 7 | 7 | |
| 8 | 8 | class WechatOAuth { |
| 9 | - const TICKETTYPE_JSAPI = 'jsapi',TICKETTYPE_WXCARD = 'wx_card'; |
|
| 9 | + const TICKETTYPE_JSAPI = 'jsapi', TICKETTYPE_WXCARD = 'wx_card'; |
|
| 10 | 10 | public $responseJSON = null; |
| 11 | 11 | public $errCode = null; |
| 12 | 12 | public $errMsg = null; |
@@ -16,50 +16,50 @@ discard block |
||
| 16 | 16 | private $httpClient = null; |
| 17 | 17 | private $accessToken = null; |
| 18 | 18 | |
| 19 | - public function __construct($appId,$appSecret) { |
|
| 19 | + public function __construct($appId, $appSecret) { |
|
| 20 | 20 | $this->appId = $appId; |
| 21 | 21 | $this->appSecret = $appSecret; |
| 22 | 22 | $this->httpClient = new HttpClient(); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - public function setHttpClient($httpClient){ |
|
| 25 | + public function setHttpClient($httpClient) { |
|
| 26 | 26 | $this->httpClient = $httpClient; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - public function setAccessToken($accessToken){ |
|
| 29 | + public function setAccessToken($accessToken) { |
|
| 30 | 30 | $this->accessToken = $accessToken; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){ |
|
| 33 | + public function authorizeURI($redirectURI, $scope = 'snsapi_userinfo', $state = '') { |
|
| 34 | 34 | $redirectURI = urlencode($redirectURI); |
| 35 | 35 | return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$redirectURI}&response_type=code&scope=$scope&state=$state#wechat_redirect"; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - public function authorize($code){ |
|
| 38 | + public function authorize($code) { |
|
| 39 | 39 | $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code"; |
| 40 | 40 | $this->responseJSON = $this->httpClient->get($url); |
| 41 | 41 | return json_decode($this->responseJSON); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public function getUserInfo($openId){ |
|
| 44 | + public function getUserInfo($openId) { |
|
| 45 | 45 | $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN"; |
| 46 | 46 | $this->responseJSON = $this->httpClient->get($url); |
| 47 | 47 | return json_decode($this->responseJSON); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - public function refreshToken($refreshToken){ |
|
| 50 | + public function refreshToken($refreshToken) { |
|
| 51 | 51 | $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken"; |
| 52 | 52 | $this->responseJSON = $this->httpClient->get($url); |
| 53 | 53 | return $this->responseJSON; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - public function verifyToken($accessToken,$openId){ |
|
| 56 | + public function verifyToken($accessToken, $openId) { |
|
| 57 | 57 | $url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId"; |
| 58 | 58 | $this->responseJSON = $this->httpClient->get($url); |
| 59 | 59 | return $this->responseJSON; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - public function getAccessToken(){ |
|
| 62 | + public function getAccessToken() { |
|
| 63 | 63 | $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"; |
| 64 | 64 | $this->responseJSON = $this->httpClient->get($url); |
| 65 | 65 | $json = json_decode($this->responseJSON); |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | return $this->accessToken; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI){ |
|
| 70 | + public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI) { |
|
| 71 | 71 | $accessToken = $this->getAccessToken(); |
| 72 | 72 | // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
| 73 | 73 | $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |