首页 > 解决方案 > Stripe 错误消息 405 -“将 .json 附加到 uri 以使用 rest api”

问题描述

我正在使用 Stripe,并尝试将测试 webhook 发送到我的 URL 和 Firebase 托管的数据库。当我“发送测试 webhook”时,我在 Stripe 控制台中收到以下错误消息:

测试 Webhook 错误:405

“将 .json 附加到您的请求 URI 以使用其余 API”

我的代码是教程的直接副本:https ://github.com/GaryH21/Stripe-Webhooks-Tutorial/blob/master/functions/index.js

这是我的 index.js 的代码:

const functions = require('firebase-functions');
const stripe = require("stripe")(functions.config().keys.webhooks);
const admin = require('firebase-admin')
admin.initializeApp();
const endpointSecret = functions.config().keys.signing;



exports.events = functions.https.onRequest((request, response) => {

    let sig = request.headers["stripe-signature"];
    try {
        let event = stripe.webhooks.constructEvent(request.rawBody, sig, endpointSecret)
        return admin.database().ref('/events').push(event)
            .then((snapshot) => {
                return response.json({ received: true, ref: snapshot.ref.toString() })
            })
            .catch((err) => {
                console.error(err)
                return response.status(500).end() // error saving to database
            })
    } catch (err) {
        return response.status(400).end() // signing signature failed
    }

})


exports.exampleDataBaseTrigger = functions.database.ref('/events/{eventId}').onCreate((snapshot, context) => {
    return console.log({
        eventId: context.params.eventid,
        data: snapshot.val()
    })
})


本教程和我的代码中唯一使用 .json 的时间是:return response.json({ received: true, ref: snapshot.ref.toString() })

我是否应该将 .json 附加到某处的“请求”上,例如在 request.RawBody 中?

签名密钥没有问题,因为这会给出 400 错误消息,我已经处理并修复了该消息。

我很乐意在我的应用程序中分享其他文件的代码,但据我所知,其余的都与问题无关。非常感谢。

标签: jsonfirebase

解决方案


推荐阅读