問題描述:支付寶沖錢接口可以,但是退款接口會報這個錯誤
百度到的這個,然而沒用到 https://openclub.alipay.com/read.php?tid=2333&fid=46
問題定位:從描述內容可以看到問題sign取不到
解決:我的問題是代碼比較舊,支付寶現在都用RSA2的,但是代碼里面只有 MD5、RSA兩種代碼實現,故找不到RSA2的實現方式也就會返回空
protected function makeSign($signStr)
{
$sign = '';
switch ($this->signType) {
case 'MD5':
$signStr .= $this->md5Key;// 此處不需要通過 & 符號鏈接
$sign = md5($signStr);
break;
case 'RSA':
case 'RSA2': //原來沒RSA2的處理,會直接default sign返回 '' 導致現在這個報錯,RSA用不着了,直接改成給RSA2用
$rsa_private_key = @file_get_contents($this->rsaPrivatePath);
$rsa = new RsaEncrypt($rsa_private_key);
$sign = $rsa->encrypt($signStr);
break;
default:
$sign = '';
}
return $sign;
}
然后修改
public function encrypt($data) { if ($this->key === false) { return ''; } $res = openssl_get_privatekey($this->key); if (empty($res)) { throw new \Exception('您使用的私鑰格式錯誤,請檢查RSA私鑰配置'); } openssl_sign($data, $sign, $res,OPENSSL_ALGO_SHA256); //請注意,原先是只有3個入參,現在多一個入參 openssl_free_key($res); //base64編碼 $sign = base64_encode($sign); return $sign; }