在 Stripe 中被 webhooks 困住了

问题描述 投票:0回答:1

我正在使用 Stripe webhook 通过我的页面接受付款。我收到一个奇怪的错误。当我将货币添加为美元时,它要求我提供印度境外的地址。没关系,但是当我提供货币为 INR 时,地址字段不会显示在我的屏幕上。然而,它要求我在提交时提供地址。

webhook.ts

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY ?? '', {
  apiVersion: '2023-10-16',
});

const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;

export const config = {
  api: {
    bodyParser: false,
  },
};

async function handleWebhookEvent(req, res) {
  const sig = req.headers['stripe-signature'];
  const buf = await buffer(req);

  let event;

  try {
    event = stripe.webhooks.constructEvent(buf.toString(), sig, webhookSecret);
  } catch (err) {
    console.error(`Webhook signature verification failed: ${err.message}`);
    return res.status(400).send(`Webhook signature verification failed: ${err.message}`);
  }

  switch (event.type) {
    case 'checkout.session.completed':
      const session = event.data.object;
      console.log(`Payment successful for session ID: ${session.id}`);


  res.status(200).end();
}

export default handleWebhookEvent;

subscribe.ts

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY ?? '');

export default async function handler(req:any, res:any) {
  if (req.method === "POST") {
    try {
      const session = await stripe.checkout.sessions.create({
        payment_method_types: ["card"],
        line_items: [
          {
            price_data: {
              currency: "usd",
              product_data: {
                name: "Sample Product",
              },
              unit_amount: 1000,
            },
            quantity: 1,
          },
        ],
        mode: "payment",
      });

      res.status(200).json({ sessionId: session.id });
    } catch (err) {
      res.status(500).json({ error: "Error creating checkout session" });
    }
  } else {
    res.setHeader("Allow", "POST");
    res.status(405).end("Method Not Allowed");
  }
}

可能是什么问题?

typescript next.js stripe-payments webhooks
1个回答
0
投票

对于正在阅读本文的其他人来说,下面的代码可能不是问题的答案,但经过讨论,我知道当前的问题是结账会话创建。 所以在上面讨论的背景下理解下面的块

const session = await stripe.checkout.sessions.create({
  customer:stripe_customID,
  billing_address_collection: 'required',
  success_url: 'url to pass the customer on sucess page for example https://exam.....com/success?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: "url to pass the customer to payment failed page if payment cancel due to any reason",
 
 
  line_items: [
    {price: item_Price, quantity: 1},
  ],
  mode: 'payment', //'subscription'// or whatever it is
});

返场

© www.soinside.com 2019 - 2024. All rights reserved.