我正在为计费项目使用 GoCardless(沙盒帐户)webhook。我已经按照 本指南中的步骤在本地使用 Nodejs(使用 ngrok)来处理 webhook,并且它可以工作,我的意图是使用 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();
}
});
});
谢谢!