React JS后端如何从Stripe.Checkout.session.line_items获取Price_data信息并将数据推入Firestore
我正在尝试通过使用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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用
list> listlineItems
method 而不是并在data.price.product
中扩展产品对象(根据在列表中扩展嵌套对象)。当您当前正在使用扩展的
line_items
数据检索会话对象时,此数据不包括产品的名称,图像,描述等。看来问题是由这些API调用引起的,如反对您的火箱功能或凡士馆中的某些东西。我们可以看到产品数据来自 list_items.data.price.product 显示为可扩展,确认它需要扩展。似乎也没有
PRICE_DATA
字段(但仅Price
)或product_data
(而是product> product
)在响应中。这些特定字段显然只有在。您最终会得到类似的内容:
由于行项目
data
是一个包含每个项目的数组,因此您需要构建数组以在用例所需的情况下保存在firestore中。让我知道这是否有帮助。You could try using the
listLineItems
method instead, and expand the product object insidedata.price.product
(according to the guide for expanding nested objects in lists).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 noprice_data
field (but instead onlyprice
) norproduct_data
(but insteadproduct
) within the responses. These specific fields are apparently available only when creating Sessions.You would end up with something like:
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.