Closed js1121302139 closed 1 year ago
沙箱环境
同样问题,最后怎么解决的?
解决问了吗 遇到同样的问题
constructor(config) { if (!config.appId) { throw Error('config.appId is required'); } if (!config.privateKey) { throw Error('config.privateKey is required'); } const privateKeyType = config.keyType === 'PKCS8' ? 'PRIVATE KEY' : 'RSA PRIVATE KEY'; config.privateKey = this.formatKey(config.privateKey, privateKeyType);
看下sdk的代码,有个参数keyType,初始化的时候传入参数keyType:PKCS8就行了。
PEM 格式一般有 PKCS#8 和 PKCS#1,前者不用加RSA,后者要。
私钥没有进行格式转换
你的邮件我已收到,查收后会尽快回复!:)
alipay.ts `import { Controller } from 'egg'; import AlipaySdk from 'alipay-sdk'; const fs = require('fs');
export default class aliPay extends Controller { async pay() { console.log('===================================='); console.log(fs.readFileSync('./config/private-key.pem')); console.log('===================================='); const { ctx } = this; const alipaySdk = new AlipaySdk({ appId: '2016102100729109', gateway: 'https://openapi.alipaydev.com/gateway.do', privateKey: fs.readFileSync('./config/private-key.pem', 'ascii'), alipayPublicKey: fs.readFileSync('./config/public-key.pem', 'ascii') }); const result = await alipaySdk.exec('alipay.system.oauth.token', { grantType: 'authorization_code', code: 'code', refreshToken: 'token' }); ctx.body = result; // const result = await alipaySdk.exec( // 'alipay.trade.page.pay', // {}, // { formData: formData } // ); // ctx.success({ code: 0, data: { result } }); } }
public-key.pem
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlud7LEg2vsDnYvyUWByjOnpvrSOil6MUSW7DLOn6zuRG2NAy0D0L9ubGVBiDUTn/CowN1x+LhNQ1323dwarH+ZIFYGeUt5UwKmeHqEKMLS1F4lnjGdxhg1PJ2ASSKaLHh3lum2JZU6Yt0T+wgBdV5hbXd+RAL+L1bTYGqr4918W+WNBAw7F019gZN9ZQ9WRpd+bTZNLu+VJvOgyQQsbfgzuiB28cm3aLvVBP2VW7yhgHie0RsxfGhUtK70907LzmDRr4yprq/g+iBvlANRyngG8Uu8BRm0Ryu8I8B/eWNIPN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