[更新]修改微信支付签名

This commit is contained in:
Admin 2018-04-09 19:07:00 +08:00 committed by Anyon
parent cccd44dad5
commit e05fe6bb24

View File

@ -196,7 +196,7 @@ class Pay
public function createTransfers(array $options) public function createTransfers(array $options)
{ {
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
return $this->callPostApi($url, $options, true, false); return $this->callPostApi($url, $options, true, 'MD5', false);
} }
/** /**
@ -208,7 +208,7 @@ class Pay
public function queryTransfers($partner_trade_no) public function queryTransfers($partner_trade_no)
{ {
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo'; $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo';
return $this->callPostApi($url, ['partner_trade_no' => $partner_trade_no], true, false); return $this->callPostApi($url, ['partner_trade_no' => $partner_trade_no], true, 'MD5', false);
} }
/** /**
@ -230,9 +230,10 @@ class Pay
/** /**
* 生成支付签名 * 生成支付签名
* @param array $data * @param array $data
* @param string $signType
* @return string * @return string
*/ */
public function getPaySign(array $data) public function getPaySign(array $data, $signType = 'MD5')
{ {
unset($data['sign']); unset($data['sign']);
ksort($data); ksort($data);
@ -240,6 +241,9 @@ class Pay
foreach ($data as $k => $v) { foreach ($data as $k => $v) {
$str .= "{$k}={$v}&"; $str .= "{$k}={$v}&";
} }
if ($signType === 'MD5') {
return strtoupper(md5("{$str}key={$key}"));
}
return strtoupper(hash_hmac('SHA256', "{$str}key={$key}", $key)); return strtoupper(hash_hmac('SHA256', "{$str}key={$key}", $key));
} }
@ -248,11 +252,12 @@ class Pay
* @param string $url 请求 * @param string $url 请求
* @param array $data 接口参数 * @param array $data 接口参数
* @param bool $isCert 是否需要使用双向证书 * @param bool $isCert 是否需要使用双向证书
* @param string $signType 数据签名类型 MD5|SHA256
* @param bool $needSignType 是否需要传签名类型参数 * @param bool $needSignType 是否需要传签名类型参数
* @return array * @return array
* @throws InvalidResponseException * @throws InvalidResponseException
*/ */
public function callPostApi($url, array $data, $isCert = false, $needSignType = true) public function callPostApi($url, array $data, $isCert = false, $signType = 'HMAC-SHA256', $needSignType = true)
{ {
$option = []; $option = [];
if ($isCert) { if ($isCert) {
@ -266,9 +271,9 @@ class Pay
} }
$params = $this->params->merge($data); $params = $this->params->merge($data);
if ($needSignType) { if ($needSignType) {
$params['sign_type'] = 'HMAC-SHA256'; $params['sign_type'] = strtoupper($signType);
} }
$params['sign'] = $this->getPaySign($params); $params['sign'] = $this->getPaySign($params, $signType);
$result = Tools::xml2arr(Tools::post($url, Tools::arr2xml($params), $option)); $result = Tools::xml2arr(Tools::post($url, Tools::arr2xml($params), $option));
if ($result['return_code'] !== 'SUCCESS') { if ($result['return_code'] !== 'SUCCESS') {
throw new InvalidResponseException($result['return_msg'], '0'); throw new InvalidResponseException($result['return_msg'], '0');