Stripe - 检索结帐会话的 line_items

Leh*_*afa 6 webhooks stripe-payments reactjs next.js

我正在尝试获取 Stripe 中结帐会话的 line_items,以便我可以发送一封包含产品下载链接的电子邮件,但不幸的是,当我尝试检索时,line_itemscheckout_session得到一个空值。

\n

前端:Next.js

\n

这是我的代码/pages/api/webhooks/index.ts

\n

检查线路:77

\n
import { buffer } from "micro";\nimport Cors from "micro-cors";\nimport { NextApiRequest, NextApiResponse } from "next";\n\nimport Stripe from "stripe";\nconst stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {\n  // https://github.com/stripe/stripe-node#configuration\n  apiVersion: "2020-08-27",\n});\n\nconst webhookSecret: string = process.env.STRIPE_WEBHOOK_SECRET!;\n\n// Stripe requires the raw body to construct the event.\nexport const config = {\n  api: {\n    bodyParser: false,\n  },\n};\n\nconst cors = Cors({\n  allowMethods: ["POST", "HEAD"],\n});\n\nconst webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {\n  if (req.method === "POST") {\n    const buf = await buffer(req);\n    const sig = req.headers["stripe-signature"]!;\n\n    let event: Stripe.Event;\n\n    try {\n      event = stripe.webhooks.constructEvent(\n        buf.toString(),\n        sig,\n        webhookSecret\n      );\n    } catch (err) {\n      // On error, log and return the error message.\n      console.log(`\xe2\x9d\x8c Error message: ${err.message}`);\n      res.status(400).send(`Webhook Error: ${err.message}`);\n      return;\n    }\n\n    // Successfully constructed event.\n    console.log(`\xe2\x9c\x85 Success: ${event.id}`);\n\n    // Cast event data to Stripe object.\n    if (event.type === "payment_intent.created") {\n      const paymentIntent = event.data.object as Stripe.PaymentIntent;\n      console.log(` PaymentIntent status: ${paymentIntent.status}`);\n      console.log('-----------------------------------------------------');\n    } else if (event.type === "payment_intent.payment_failed") {\n      const paymentIntent = event.data.object as Stripe.PaymentIntent;\n      console.log(\n        `\xe2\x9d\x8c Payment failed: ${paymentIntent.last_payment_error?.message}`\n      );\n      console.log('-----------------------------------------------------');\n    } else if (event.type === "customer.created") {\n      const customer = event.data.object as Stripe.Customer;\n      console.log(`Customer created: ${customer.id}`);\n      console.log('-----------------------------------------------------');\n    } else if(event.type === 'payment_intent.requires_action') {\n      const paymentIntent = event.data.object as Stripe.PaymentIntent;\n      console.log(`\xe2\x9d\x8c Payment requires action: ${paymentIntent.id}`);\n    } else if (event.type === "payment_intent.succeeded") {\n      const paymentIntent = event.data.object as Stripe.PaymentIntent;\n      console.log(` Payment succeeded: ${paymentIntent.id}`);\n      console.log('-----------------------------------------------------');\n    } else if (event.type === "charge.succeeded") {\n      const charge = event.data.object as Stripe.Charge;\n      console.log(` Charge id: ${charge.id}`);\n      console.log('-----------------------------------------------------');\n    } else if (event.type === "checkout.session.completed") {\n      const checkout_session = event.data.object as Stripe.Checkout.Session;\n      console.log(` Session id: ${checkout_session.id}`);\n      console.log(`Customer email: ${checkout_session.customer_details.email}`);\n      console.log(`Order products: ${checkout_session.line_items}`);\n      console.log('-----------------------------------------------------');\n    } else {\n      console.warn(`\xe2\x80\x8d\xe2\x99\x80\xef\xb8\x8f Unhandled event type: ${event.type}`);\n      console.log('-----------------------------------------------------');\n    }\n\n    // Return a response to acknowledge receipt of the event.\n    res.json({ received: true });\n  } else {\n    res.setHeader("Allow", "POST");\n    res.status(405).end("Method Not Allowed");\n  }\n};\n\nexport default cors(webhookHandler as any);\n
Run Code Online (Sandbox Code Playgroud)\n

当我尝试登录时,line_itemscheckout_session得到一个空值。

\n

Nol*_*n H 12

如API 参考中所述,会话line_items的 Session 是“可扩展的”并且默认情况下不包含在内。这意味着它们在 Webhook 事件中传递时不包含在对象中,并且您在检索对象时必须通过扩展显式请求该值。

要获取这些,您需要检索会话并请求line_items将其包含在响应中:

const session = await stripe.checkout.sessions.retrieve(
  'cs_test_123', {
  expand: ['line_items']
});
Run Code Online (Sandbox Code Playgroud)