TheNorthMemory / whats-alipay

支付宝 Alipay OpenAPI SDK
MIT License
15 stars 2 forks source link

签名错误 #5

Closed yviscool closed 3 years ago

yviscool commented 3 years ago
const whats = new Alipay({
    privateKey,
    publicCert,
    params: { app_id, }
})

whats
    .alipay.trade.precreate({
        out_trade_no: "",
        subject: "测试",
        total_amount: 0.01,
    })
    .then(({ data }) => data)
    .catch(({ response: { data } }) => data)
    .then(console.log)

证书模式, 会提示签名错误, 与 alipaysdk 官方提示一样, 但是如果 alipaysdk 加上根证书和商家公钥就是好的,

const AlipaySdk = require('alipay-sdk').default
const alipaySdk = new AlipaySdk({   
    appId: app_id,
    privateKey: readFileSync('/Users/tmac/Desktop/应用密钥_RSA2_PKCS8/应用私钥_RSA2_PKCS8.txt', 'ascii'),
    appCertPath: path.join(__dirname, 'appCertPublicKey.cer'),
    alipayRootCertPath: path.join(__dirname, 'alipayRootCert.cer'),
    alipayPublicCertPath: path.join(__dirname, 'alipayCertPublicKey_RSA2.cer'),
    keyType: 'PKCS8'
});

        const res = await alipaySdk.exec('alipay.trade.precreate', {
            bizContent: {
                out_trade_no: _get_out_trade_no(),
                total_amount: 0.01,
                subject: 'just for test',
            }
        });

不知道这边怎么操作, 我看 constructor 签名里面有这 app_cert_sn alipay_root_cert_sn 两个参数,但是好像没用到. 那我还是先用 alipay 的整一整

TheNorthMemory commented 3 years ago

嗯,这个lib是按照标准私钥来的,标准私钥文件格式是:

-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----

可以看下 tests/fixtures/mock-app-pkcs1.keytests/fixtures/mock-app-pkcs8.key

app_cert_snalipay_root_cert_sn 这俩是计算出来的几乎是定值(证书不换,这个值就一直一样),需要通过命令行计算换取:

./node_modules/.bin/certHelper SN ./alipayCertPublicKey_RSA2.cer