我正在使用GoCardless (沙盒帐户) webhook为一个帐单项目。我已经在this guide上使用Nodejs本地(与ngrok)来处理webhooks的步骤,并且它工作,我的意图是使用firebase函数作为服务器,但是当我在firebase上部署代码并测试它抛出‘超时错误’的代码时,我不知道我是否遗漏了关于firebase函数的某些东西……这是firebase上的代码:
const functions = require('firebase-functions');
const webhooks = require("gocardless-nodejs/webhooks");
const webhookEndpointSecret = "xxxxxx";
exports.events = functions.https.onRequest((request, response) => {
if (request.method !== "POST") {
response.writeHead(405);
response.end();
return;
}
let data = "";
request.on("data", chunk => {
data += chunk;
});
request.on("end", () => {
try {
const signatureHeader = request.headers["webhook-signature"];
const events = webhooks.parse(
data,
webhookEndpointSecret,
signatureHeader
);
events.forEach(event => {
if (event.resource_type !== "mandates") {
//continue;
}
switch (event.action) {
case "created":
console.log(
`Mandate ${event.links.mandate} has been created, yay!`
);
break;
case "cancelled":
console.log(`Oh no, mandate ${event.links.mandate} was cancelled!`);
break;
default:
console.log(`${event.links.mandate} has been ${event.action}`);
}
});
response.writeHead(204);
response.end();
} catch (e) {
response.writeHead(403);
response.end();
}
});
});谢谢!
发布于 2020-07-26 20:00:40
好了,我终于知道怎么回事了.问题是'data‘变量是空的。我解决了获取请求主体的问题,如下所示:
let data = request.rawBody.toString();我希望这能对其他人有用。
https://stackoverflow.com/questions/63058018
复制相似问题