首页 > 解决方案 > 出现错误:测试 webhook 错误:尝试将测试事件发送到 webhook 端点时出现 400

问题描述

我正在尝试按照本教程中的说明发送测试 webhook

但是当我去做时,我在第一个链接中看到了错误,下面是:

测试 webhook 错误:400

这是我部署到 firebase 函数的 index.ts 代码和函数。

import * as functions from 'firebase-functions';
​
​
// 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); // Validate the request
    
    return admin.database().ref('/events').push(event) // Add the event to the database
      .then((snapshot: { ref: { toString: () => any; }; }) => {
        // Return a successful response to acknowledge the event was processed successfully
        return response.json({ received: true, ref: snapshot.ref.toString() });
      })
      .catch((err: any) => {
        console.error(err) // Catch any errors saving to the database
        return response.status(500).end();
      });
  }
  catch (err) {
    return response.status(400).end(); // Signing signature failure, return an error 400
  }
  
});
​
exports.exampleDatabaseTrigger = functions.database.ref('/events/{eventId}').onCreate((snapshot, context) => {
  return console.log({
    eventId: context.params.eventId,
    data: snapshot.val()
  });
});

如何解决此问题并成功运行测试?

我目前的想法是这个问题可能与:

我是如何写下这一行的:snapshot: { ref: { toString: () => any; };

更新:

从我的测试来看,情况似乎并非如此。

标签: javascripttypescriptfirebasestripe-payments

解决方案


我不相信“测试网络钩子”正确地签署了它们;您应该为此使用Stripe CLI 。


推荐阅读