【发布时间】:2022-01-12 18:16:34
【问题描述】:
我正在使用带有节点 js 的条带进行付款,并且我正在创建一个会话并使用条带结帐界面。我正在使用 webhook 来监听事件,当支付创建或成功时,条纹发送 payment_intent.succeeded 所以我用它做一些事情。但问题是当我点击取消 url 取消付款时,stripe 不会取消付款或发送 payment_intent.canceled 这是一个问题,因为那时我不知道付款是否取消,我不能做我想做的事打算做。这是我的代码:
// This will share the stripe publickey with the frontend
const webhook = async (req, res) => {
// Signature verification
const playload = req.body;
const sig = req.headers["stripe-signature"];
const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;
let event;
try {
// Checking if the response is actually from stripe
event = stripe.webhooks.constructEvent(playload, sig, endpointSecret);
} catch (error) {
console.log(error.message);
res.status(400).json({ success: false });
return;
}
// Getting the ad id
const adID = event.data.object.metadata.id;
const userEmail = event.data.object.metadata.email;
// Checking if the payment did faile
if (
event.type === "payment_intent.canceled" ||
event.type === "charge.failed" ||
event.type === "charge.expired"
) {
const paymentIntent = event.data.object;
console.log(`PaymentIntent ${paymentIntent.status}`);
// Sending the deleting request
await axios.delete(`${process.env.SERVER_URL}/api/v1/single-ad`, {
data: { id: adID, email: userEmail },
});
return res.status(200).json({ message: "Ad is successfully deleted" });
}
if 语句永远不会执行,因为如果支付被取消,stripe 不会发回任何东西。
【问题讨论】:
标签: node.js stripe-payments stripes