React JS后端如何从Stripe.Checkout.session.line_items获取Price_data信息并将数据推入Firestore

发布于 2025-02-10 08:48:40 字数 1636 浏览 1 评论 0 原文

我正在尝试通过使用React Node JS创建Webhook函数来将Stripe Checkout Line_items价格数据推向Firestore,如下所示代码。


exports.stripeWebhook=functions.https.onRequest(async (req, res)=>{
  const stripe = require("stripe")(functions.config().stripe.token);
  let event;
try {
  const whSec=functions.config().stripe.payments_webhook_secret;
  event =stripe.webhooks.constructEvent(
     req.rawBody,
     req.headers["stripe-signature"],
     whSec,
  );
} catch (err) {
  console.error("Webhook signature verification failed.");
  return res.sendStatus(400);
}
 const dataObject=event.data.object;
 const session = await stripe.checkout.sessions.retrieve(dataObject.id, {
  expand: ['line_items']
});
 await admin.firestore().collection("customer").doc(String(dataObject.customer_email)).collection("order").doc().set({
  checkoutSessionId:dataObject.id,
  paymentStatus: dataObject.payment_status,
  shppingInfo:dataObject.shipping,
  amountTotal:dataObject.amount_total/100,
  customerId:dataObject.customer,
  CustomerEmail:dataObject.customer_email,
  orderItems:session.line_items.price_data,
 });
 return res.sendStatus(200);
});

该函数正常工作,除了 orderItems:session.line_items.price_data ,它显示出一个错误作为未定义的firestore值。如果我更改为orderItems:session.line_items,它没有错误,但是firestore中显示的line_items信息(下面的屏幕截图不是我想要的,我只想要line_items.price_data,其中包括项目标题,价格和图像。

<<<<<<<<<<<<<<<<<<<< a href =“ https://i.sstatic.net/7hyel.png” rel =“ nofollow noreferrer”>

我的问题是如何从line_items获取每个项目price_data?

I am trying to push the stripe checkout line_items price data to firestore by creating a webhook functions using react node js as the below code shown.


exports.stripeWebhook=functions.https.onRequest(async (req, res)=>{
  const stripe = require("stripe")(functions.config().stripe.token);
  let event;
try {
  const whSec=functions.config().stripe.payments_webhook_secret;
  event =stripe.webhooks.constructEvent(
     req.rawBody,
     req.headers["stripe-signature"],
     whSec,
  );
} catch (err) {
  console.error("Webhook signature verification failed.");
  return res.sendStatus(400);
}
 const dataObject=event.data.object;
 const session = await stripe.checkout.sessions.retrieve(dataObject.id, {
  expand: ['line_items']
});
 await admin.firestore().collection("customer").doc(String(dataObject.customer_email)).collection("order").doc().set({
  checkoutSessionId:dataObject.id,
  paymentStatus: dataObject.payment_status,
  shppingInfo:dataObject.shipping,
  amountTotal:dataObject.amount_total/100,
  customerId:dataObject.customer,
  CustomerEmail:dataObject.customer_email,
  orderItems:session.line_items.price_data,
 });
 return res.sendStatus(200);
});

The function works fine except orderItems:session.line_items.price_data, it shows an error as an undefined firestore value. if I change to orderItems:session.line_items, it has no error, but the line_items info shown in firestore (screen shot below is not what I want, I just want the line_items.price_data which include just item title, price and images.

enter image description here

My question is how to get each item price_data from line_items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2025-02-17 08:48:40

您可以尝试使用 list> listlineItems method 而不是并在 data.price.product 中扩展产品对象(根据在列表中扩展嵌套对象)。

const lineItems = await stripe.checkout.sessions.listLineItems(id, {expand: ['data.price.product']})

当您当前正在使用扩展的 line_items 数据检索会话对象时,此数据不包括产品的名称,图像,描述等。看来问题是由这些API调用引起的,如反对您的火箱功能或凡士馆中的某些东西。

我们可以看到产品数据来自 list_items.data.price.product 显示为可扩展,确认它需要扩展。似乎也没有 PRICE_DATA 字段(但仅 Price )或 product_data (而是 product> product )在响应中。这些特定字段显然只有在

您最终会得到类似的内容:

await admin.firestore().collection("customer").doc(String(dataObject.customer_email)).collection("order").doc().set({
    //...
    orderItems: lineItems.data[0].price //includes product details from nested expand
});

由于行项目 data 是一个包含每个项目的数组,因此您需要构建数组以在用例所需的情况下保存在firestore中。让我知道这是否有帮助。

You could try using the listLineItems method instead, and expand the product object inside data.price.product (according to the guide for expanding nested objects in lists).

const lineItems = await stripe.checkout.sessions.listLineItems(id, {expand: ['data.price.product']})

As you are currently retrieving the Session object with the expanded line_items data, this data won't include the product's name, images, description, etc. It looks like the problem is caused by these API calls, as opposed to something in your Firebase function or Firestore.

We can see that the product data from list_items.data.price.product is shown as expandable, which confirms it needs to be expanded. It also seems that there is no price_data field (but instead only price) nor product_data (but instead product) within the responses. These specific fields are apparently available only when creating Sessions.

You would end up with something like:

await admin.firestore().collection("customer").doc(String(dataObject.customer_email)).collection("order").doc().set({
    //...
    orderItems: lineItems.data[0].price //includes product details from nested expand
});

Since line items data is an array containing each item, you would need to build the array to save in Firestore as your use case requires. Let me know if this was helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文