Node.js Stripe Webhook constructEvent方法指向ec2实例时返回400错误
在ngrok的帮助下,我在windows操作系统和集成Stripe webhook中开发了我的应用程序。一切都很顺利,我能够从webhook接收事件。但一旦我把它移动到AWS云上的ec2实例,它就给我带来了一个错误 错误:(在条带化仪表板Webhook尝试部分) Webhook错误:第一个参数必须是string类型或Buffer、ArrayBuffer、Array或类似Array的对象的实例。接收未定义 奇怪的是,它没有改变任何一段代码,而且仍然可以在windows上运行 完全错误:Node.js Stripe Webhook constructEvent方法指向ec2实例时返回400错误,node.js,amazon-ec2,stripe-payments,cryptojs,Node.js,Amazon Ec2,Stripe Payments,Cryptojs,在ngrok的帮助下,我在windows操作系统和集成Stripe webhook中开发了我的应用程序。一切都很顺利,我能够从webhook接收事件。但一旦我把它移动到AWS云上的ec2实例,它就给我带来了一个错误 错误:(在条带化仪表板Webhook尝试部分) Webhook错误:第一个参数必须是string类型或Buffer、ArrayBuffer、Array或类似Array的对象的实例。接收未定义 奇怪的是,它没有改变任何一段代码,而且仍然可以在windows上运行 完全错误: TypeE
TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject
. Received undefined
at prepareSecretKey (internal/crypto/keys.js:322:11)
at new Hmac (internal/crypto/hash.js:111:9)
at Object.createHmac (crypto.js:147:10)
at Object._computeSignature (/home/psuser/middleware/node_modules/stripe/lib/Webhooks.js:65:8)
at Object.verifyHeader (/home/psuser/middleware/node_modules/stripe/lib/Webhooks.js:107:36)
at Object.constructEvent (/home/psuser/middleware/node_modules/stripe/lib/Webhooks.js:12:20)
at /home/psuser/middleware/routes/carts.js:210:29
at Layer.handle [as handle_request] (/home/psuser/middleware/node_modules/express/lib/router/layer.js:95:5)
at next (/home/psuser/middleware/node_modules/express/lib/router/route.js:137:13)
at /home/psuser/middleware/node_modules/body-parser/lib/read.js:130:5
at invokeCallback (/home/psuser/middleware/node_modules/raw-body/index.js:224:16)
at done (/home/psuser/middleware/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/home/psuser/middleware/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ERR_INVALID_ARG_TYPE'
}
The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received undefined
// End point Code
router.post('/webhook', async (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.rawBody, sig, process.env.STRIPE_WEBHOOK_SECRET);
}
catch (err) {
console.log(err.message);
return response.status(400).send(`Webhook Error: ${err.message}`);
}
});
// In app.js
app.use(express.json({
// Because Stripe needs the raw body, we compute it but only when hitting the Stripe callback URL.
verify: function (req, res, buf) {
var url = req.originalUrl;
if (url.endsWith('/webhook')) {
req.rawBody = buf.toString()
}
}
}));
当此代码运行时(以
whsec\u开头),能否确认process.env.STRIPE\u WEBHOOK\u SECRET
包含正确的WEBHOOK端点SECRET?不过,不要在这里分享你的webhook秘密!当此代码运行时(以whsec\u开头),能否确认process.env.STRIPE\u WEBHOOK\u SECRET
包含正确的WEBHOOK端点SECRET?不过,不要在这里分享你的webhook秘密!