所以我想做的是使用支付宝https://global.alipay.com/docs/ac/ams/api API进行一些 API 调用
我按照集成指南创建了 nessassary 私钥/公钥和客户端 ID。我还按照这里的生成签名指南:https ://global.alipay.com/docs/ac/ams/digital_signature#gNWs0
我还复制了支付宝开发者工具iTest的部分代码,所以实际上功能signWithSHA256RSA
来自支付宝。
我知道我的私钥应该可以工作,因为我能够使用支付宝的开发者工具成功测试相同的请求。
// generate ISO 8601 date
$oDateTime = new \DateTime();
$sDate = $oDateTime->format('c');
$privatekey = '<private_key_removed>';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://open-eu.alipay.com/ams/api/v1/payments/pay');
$reqBody = json_encode(array('productCode' => 'IN_STORE_PAYMENT'));
$headers = array();
$headers[] = "Content-Type:application/json; charset=UTF-8";
$headers[] = "Request-Time:".$sDate;
$headers[] = "client-id:<client_id_removed>";
$headers[] = "Signature:". "algorithm=RSA256,keyVersion=1,signature=".signWithSHA256RSA($sDate, $reqBody, $privatekey);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $reqBody);
$rspContent = curl_exec($curl);
function signWithSHA256RSA($timeString, $reqBody, $privateKey){
$priKey = "-----BEGIN RSA PRIVATE KEY-----\n".
wordwrap($privateKey, 64, "\n", true).
"\n-----END RSA PRIVATE KEY-----";
$signContent = "POST /ams/api/v1/payments/pay"."\n<client_id_removed>".".".$timeString. ".".$reqBody;
openssl_sign($signContent, $signValue, $priKey, OPENSSL_ALGO_SHA256);
return base64_encode($signValue);
}
我得到的错误代码是:
{"result":
{
"resultCode":"PARAM_ILLEGAL",
"resultMessage":"illegal parameter:OpenapiV2签名字段异常: algorithm=RSA256,keyVersion=1,signature=<signature_removed>",
"resultStatus":"F"}
}