我有一个webhook,我用它来写数据到firebase时,条纹购买发生,我不能让它工作。我试着在所有东西周围添加try/catch语句,我似乎无法捕获错误。
我正在使用nextJS支持的调试,如下所示:https://nextjs.org/docs/advanced-features/debugging
我的问题是为什么?所有API密钥都是正确的。
错误:
[ERROR] Failed to POST: Post "http://localhost:3000/api/webhook": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
Webhook:
import { buffer } from "micro";
import * as admin from "firebase-admin";
//secure a connection to firebase
const serviceAccount = require("../../../permissions.json");
const app = !admin.apps.length
? admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})
: admin.app();
//connect to stripe
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const endpointSecret = process.env.STRIPE_SIGNING_SECRET;
const fullfillOrder = async (session) => {
try {
return app
.firestore()
.collection("users")
.doc(session.metadata.email)
.collection("orders")
.doc(session.id)
.set({
amount: session.amount_total / 100,
amount_shipping: session.total_details.amount_shipping / 100,
images: JSON.parse(session.metadata.images),
timestamp: admin.firestore.FieldValue.serverTimestamp(),
})
.then(() => {
console.log(`SUCCESS : Order ${session.id} has been added to DB.`);
});
} catch (error) {
console.error(error);
}
};
export default async (req, res) => {
try {
} catch (error) {
console.error(error);
}
if (req.method === "POST") {
const requestBuffer = await buffer(req);
const payload = requestBuffer.toString();
const sig = req.headers["stripe-signature"];
let event;
//verify that event posted from stripe
try {
event = stripe.webhooks.constructEvent(payload, sig, endpointSecret);
} catch (err) {
return res.status(400).send(`Webhook error : ${err.message}`);
}
//Handle checkout session completed event
try {
if (event.type === "checkout.session.completed") {
const session = event.data.object;
return fullfillOrder(session)
.then(() => res.status(200))
.catch((err) =>
res.status(400).send(`Webhook Error :${err.message}`)
);
}
} catch (error) {
console.log("🚀 ~ file: webhook.js ~ line 56 ~ error", error);
}
}
};
export const config = {
api: {
bodyParser: false,
externalResolver: true,
},
};
1条答案
按热度按时间z9smfwbn1#
当您在控制台上发现此错误时[ERROR]无法开机自检:发布“http://localhost:3000/api/webhook”:已超过上下文截止时间(等待标头时已超过客户端超时)
因此,很明显,当你webhook API没有为以前的webhook调用发送200状态码时,strip将发送这个错误。
请确保在webhook API中正确发送响应,并使用200状态代码
你可以使用try/catch来更好的处理