master
elf 2 years ago
parent 3f20260523
commit 22996d03e8

@ -25,7 +25,7 @@ class Client
public function post($url, $params): string 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)); Log::write('REQUEST_DATA: ' . json_encode($params));
$curl = curl_init(); $curl = curl_init();
if (stripos($url, 'https://') !== false){ if (stripos($url, 'https://') !== false){

@ -40,6 +40,9 @@ class Request
'merchantCode' => $this->merchantCode, 'merchantCode' => $this->merchantCode,
'totalBizType' => $this->totalBizType, 'totalBizType' => $this->totalBizType,
], $this->params); ], $this->params);
foreach ($params as $key => $value) {
$params[$key] = $this->strToEncoding($value, 'GBK');
}
$params[Sign::SIGN_KEY] = Sign::generate($params, $this->signFields, $this->secretKey); $params[Sign::SIGN_KEY] = Sign::generate($params, $this->signFields, $this->secretKey);
return $params; return $params;
} }
@ -47,4 +50,13 @@ class Request
public function getUrl() { public function getUrl() {
return Config::get('base_url') . $this->url; 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);
}
}
} }

@ -25,25 +25,13 @@ class Sign
} }
private static function hmacMd5($signString, $secretKey) { private static function hmacMd5($signString, $secretKey) {
$data = self::strToEncoding($signString, 'GBK');
$key = self::strToEncoding($secretKey, 'GBK');
if (function_exists('hash_hmac')) { 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)); $secretKey = (strlen($secretKey) > 64) ? pack('H32', 'md5') : str_pad($secretKey, 64, chr(0));
$ipad = substr($key,0, 64) ^ str_repeat(chr(0x36), 64); $ipad = substr($secretKey,0, 64) ^ str_repeat(chr(0x36), 64);
$opad = substr($key,0, 64) ^ str_repeat(chr(0x5C), 64); $opad = substr($secretKey,0, 64) ^ str_repeat(chr(0x5C), 64);
return md5($opad.pack('H32', md5($ipad.$data))); return md5($opad.pack('H32', md5($ipad. $signString)));
}
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);
}
} }
} }
Loading…
Cancel
Save