0

我正在为计费项目使用 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();
        }
    });
});

谢谢!

4

1 回答 1

1

好的,我终于弄清楚是怎么回事......问题是“数据”变量是空的。我解决了像这样获取请求正文的问题:

let data = request.rawBody.toString();

我希望这对其他人有用。

于 2020-07-26T12:00:40.123 回答