From 22996d03e85a42f597a2246ed47f29f75fd6942a Mon Sep 17 00:00:00 2001 From: elf <360197197@qq.com> Date: Thu, 16 Feb 2023 23:25:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThinkPHP/Library/Org/SumaPay/Client.class.php | 2 +- .../Org/SumaPay/Request/Request.class.php | 12 ++++++++++ ThinkPHP/Library/Org/SumaPay/Sign.class.php | 22 +++++-------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ThinkPHP/Library/Org/SumaPay/Client.class.php b/ThinkPHP/Library/Org/SumaPay/Client.class.php index ea3b26d3..e57c6ecd 100644 --- a/ThinkPHP/Library/Org/SumaPay/Client.class.php +++ b/ThinkPHP/Library/Org/SumaPay/Client.class.php @@ -25,7 +25,7 @@ class Client public function post($url, $params): string { - Log::write('REQUEST_URL: ' . json_encode($url)); + Log::write('REQUEST_URL: ' . $url); Log::write('REQUEST_DATA: ' . json_encode($params)); $curl = curl_init(); if (stripos($url, 'https://') !== false){ diff --git a/ThinkPHP/Library/Org/SumaPay/Request/Request.class.php b/ThinkPHP/Library/Org/SumaPay/Request/Request.class.php index dc6262ed..33adf113 100644 --- a/ThinkPHP/Library/Org/SumaPay/Request/Request.class.php +++ b/ThinkPHP/Library/Org/SumaPay/Request/Request.class.php @@ -40,6 +40,9 @@ class Request 'merchantCode' => $this->merchantCode, 'totalBizType' => $this->totalBizType, ], $this->params); + foreach ($params as $key => $value) { + $params[$key] = $this->strToEncoding($value, 'GBK'); + } $params[Sign::SIGN_KEY] = Sign::generate($params, $this->signFields, $this->secretKey); return $params; } @@ -47,4 +50,13 @@ class Request public function getUrl() { return Config::get('base_url') . $this->url; } + + private function strToEncoding($str, $encoding) { + $encode = mb_detect_encoding($str, ['ASCII', 'GB2312', 'GBK', 'UTF-8']); + if($encode == $encoding){ + return $str; + } else{ + return mb_convert_encoding($str, $encoding, $encode); + } + } } \ No newline at end of file diff --git a/ThinkPHP/Library/Org/SumaPay/Sign.class.php b/ThinkPHP/Library/Org/SumaPay/Sign.class.php index 1784549d..eaa84a7b 100644 --- a/ThinkPHP/Library/Org/SumaPay/Sign.class.php +++ b/ThinkPHP/Library/Org/SumaPay/Sign.class.php @@ -25,25 +25,13 @@ class Sign } private static function hmacMd5($signString, $secretKey) { - $data = self::strToEncoding($signString, 'GBK'); - $key = self::strToEncoding($secretKey, 'GBK'); - if (function_exists('hash_hmac')) { - return hash_hmac('md5', $data, $key); + return hash_hmac('md5', $signString, $secretKey); } - $key = (strlen($key) > 64) ? pack('H32', 'md5') : str_pad($key, 64, chr(0)); - $ipad = substr($key,0, 64) ^ str_repeat(chr(0x36), 64); - $opad = substr($key,0, 64) ^ str_repeat(chr(0x5C), 64); - return md5($opad.pack('H32', md5($ipad.$data))); - } - - private static function strToEncoding($str, $encoding) { - $encode = mb_detect_encoding($str, ['ASCII', 'GB2312', 'GBK', 'UTF-8']); - if($encode == $encoding){ - return $str; - } else{ - return mb_convert_encoding($str, $encoding, $encode); - } + $secretKey = (strlen($secretKey) > 64) ? pack('H32', 'md5') : str_pad($secretKey, 64, chr(0)); + $ipad = substr($secretKey,0, 64) ^ str_repeat(chr(0x36), 64); + $opad = substr($secretKey,0, 64) ^ str_repeat(chr(0x5C), 64); + return md5($opad.pack('H32', md5($ipad. $signString))); } } \ No newline at end of file