签名
let crypto = require('crypto');
const iconv = require('iconv-lite');
// RSA2签名
function getSign( beforeSignStr ) {
let sign = crypto.createSign('RSA-SHA256');
return sign.update( iconv.encode( beforeSignStr, "gbk" ) ).sign( 私钥字符串, "base64" );
}
发送请求表单
//请求表单
function getRequestForm( requestBody ) {
let form = "<form id='alipaysubmit' name='alipaysubmit' action='" + 请求支付宝url + "' accept-charset='gbk' method='POST'>";
for( let key in requestBody ) {
form += "<input type='hidden' name='" + key + "' value='" + requestBody[key] + "'/>";
}
form += "<input type='submit' value='ok' style='display:none;'></form><script>document.forms['alipaysubmit'].submit();</script>";
return form;
}
验签
let crypto = require('crypto');
const urlencode = require('urlencode');
const iconv = require('iconv-lite');
// 得到验签结果
function getVerifySign( beforeVerifyStr ) {
let verify = crypto.createVerify('RSA-SHA256');
return verify.update( iconv.encode( urlencode.decode( beforeVerifyStr, "gbk" ), "gbk" ) ).verify( 公钥字符串, 支付宝响应签名, "base64" );
}
总结