本地使用Stripe CLI监听Webhooks时出现400错误求助
监听Stripe Webhooks时持续返回400错误,求排查
我在监听Stripe Webhooks时遇到了持续的400错误,之前曾出现404错误,现在变为400,想请教这一情况是否关键,以及问题出在哪里。
Stripe CLI命令
stripe listen --forward-to localhost:3000/stripe/webhooks
支付成功后的日志
2024-02-11 19:03:48 --> charge.succeeded [evt_3OihNVAHrCXdaN2D1FwDqPAg] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_3OihNVAHrCXdaN2D1FwDqPAg] 2024-02-11 19:03:48 --> customer.subscription.created [evt_1OihNXAHrCXdaN2DEwvHAQ9w] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNXAHrCXdaN2DEwvHAQ9w] 2024-02-11 19:03:48 --> customer.subscription.updated [evt_1OihNXAHrCXdaN2DUpdKFe5Y] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNXAHrCXdaN2DUpdKFe5Y] 2024-02-11 19:03:48 --> payment_intent.succeeded [evt_3OihNVAHrCXdaN2D19uFzM2H] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_3OihNVAHrCXdaN2D19uFzM2H] 2024-02-11 19:03:48 --> payment_intent.created [evt_3OihNVAHrCXdaN2D1klShlPc] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_3OihNVAHrCXdaN2D1klShlPc] 2024-02-11 19:03:48 --> invoice.created [evt_1OihNXAHrCXdaN2DXgLhnMb2] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNXAHrCXdaN2DXgLhnMb2] 2024-02-11 19:03:48 --> invoice.finalized [evt_1OihNYAHrCXdaN2DID3UutqE] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNYAHrCXdaN2DID3UutqE] 2024-02-11 19:03:48 --> invoice.updated [evt_1OihNYAHrCXdaN2DM0TgxtvW] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNYAHrCXdaN2DM0TgxtvW] 2024-02-11 19:03:48 --> invoice.paid [evt_1OihNYAHrCXdaN2D8kt2YgNg] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNYAHrCXdaN2D8kt2YgNg] 2024-02-11 19:03:48 --> invoice.payment_succeeded [evt_1OihNYAHrCXdaN2Dbdeq4Bn9] 2024-02-11 19:03:48 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNYAHrCXdaN2Dbdeq4Bn9] 2024-02-11 19:03:50 --> checkout.session.completed [evt_1OihNZAHrCXdaN2DLafn3NZO] 2024-02-11 19:03:50 <-- [400] POST http://localhost:3000/stripe/webhooks [evt_1OihNZAHrCXdaN2DLafn3NZO]
后端index.js代码
// Create app and the clients const app = express(); const PORT = 3000; // making sure requests from the client side work app.use(cors({ origin: "http://localhost:5173", credentials: true, })); // middleware to read body app.use(bodyParser.json()); app.use(bodyParser.raw({type: "application/json"})); app.use(bodyParser.urlencoded({extended: true})); // Create stripe const stripe = Stripe("stripe api key"); // Stripe webhook const webhookSecret = "webhook secret key"; // Create stripe customer for prepayment app.post("/user/register", async (req, res) => { const { email, name, data: reqData } = req.body; const userData = reqData; console.log("user before try:", userData); // Store user's stripe customer id try { if (await email && await name) { const customer = await createStripeCustomer({ email, name }); console.log("stripe cstomer was created:", customer); // Check if userData is defined if (userData) { const userId = userData; console.log("user after try:", userId); // inserting stripe customer id into supabase accounts table const { error: insertError } = await supabase .from('accounts') .update({ stripeid: customer.id }) .eq("id", userId) if (insertError) { console.log("Error inserting stripe id into accounts", insertError); } else { console.log("stripeID was inserted into user account:", name, customer.id); } } else { console.log("Userdata was not defined", userData); } } else { console.error("email and name not present stripe customer was not created.", email, name); } } catch (error) { console.log("Error creating stripe customer", error); console.log("email and name failed:", email, name ); } // Send response to the client res.status(200).send("request successful"); }); // Create stripe customer async function createStripeCustomer({ name, email }) { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve, reject) => { try { const Customer = await stripe.customers.create({ name: name, email: email, }); resolve(Customer); console.log("Stripe customer was created", Customer); } catch (err) { console.log(err); reject(err); } }); } app.post("/create-checkout-session", async (req, res) => { const { customerId, priceId } = req.body; console.log("recived customer and priceId, creating session", customerId, priceId); const session = await stripe.checkout.sessions.create({ customer: customerId, payment_method_types: [ "card", ], line_items: [ { price: priceId, quantity: 1, } ], mode: "subscription", success_url: 'https://localhost:5173/payment/success', // TODO: replace with real url cancel_url: 'https://localhost:5173/pricing', // TODO: replace with real url }) res.json({ id: session.id }) }); // Stripe webhooks! app.post('/stripe/webhooks', (req, res) => { const sig = req.headers['stripe-signature']; let event; try { event = stripe.webhooks.constructEvent(req.rawBody, sig, webhookSecret); } catch (error) { return res.status(400).send(`Webhook Error: ${error.message}`); } // Handle the event switch (event.type) { case 'payment_intent.succeeded': { const email = event['data']['object']['receipt_email'] // contains the email that will recive the recipt for the payment (users email usually) console.log(`PaymentIntent was successful for ${email}!`) console.log("updating user plan now!") break; } default: // Unexpected event type return res.status(400).end(); } // Return a 200 response to acknowledge receipt of the event res.json({received: true}); })
疑问
- 代码或配置中哪里操作出错导致400错误?
- 之前的404错误变为现在的400,这一变化是否是关键线索?
内容的提问来源于stack exchange,提问作者anon
相关产品推荐
相关产品推荐

