使用 firebase 函数处理 gocardless webhook

Handling gocardless webhook with firebase functions

我正在使用 GoCardless(沙盒帐户)webhook 进行计费项目。 我已经按照 this guide 上的步骤在本地使用 Nodejs(使用 ngrok)处理 webhooks 并且它有效,我的目的是使用 firebase 函数作为服务器,但是当我在 firebase 上部署代码并测试代码时throws 'timeout error',我不知道我是否遗漏了一些关于 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();
        }
    });
});

谢谢!

好吧,我终于弄明白是怎么回事了……问题是 'data' 变量是空的。我解决了这样获取请求正文的问题:

let data = request.rawBody.toString();

我希望这对其他人有用。