从会话对象中条带获取product_id / price_id

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

我目前正在使用Stripe Webhooks来在用户购买产品时得到通知。一切正常。从付款意图中,我可以获取SeesionCustomer对象。但是我找不到一种方法来获取用户支付的product_idprice_id

有人知道获取product_idprice_id的方法吗?

node.js stripe-payments backend payment
1个回答
0
投票

感谢您的提问。如您所见,checkout.session.completed事件中包含的会话数据不包括将价格ID与结帐会话相关联的line_items

line_items是可扩展属性之一,因此要检索价格ID,您要检索Checkout会话并使用expand在响应中包括订单项。无法配置Webhook,使发送给您的数据包括此数据。

有两种方法将客户的购买与Checkout会话相关联。首先,您可以将结帐会话的ID与客户购买的购物车或商品清单一起存储在数据库中。这样,如果结帐会话成功,则可以按ID查找购物车并知道购买了哪些物品。

或者,您可以侦听checkout.session.completed webhook事件,然后在收到新的成功结帐通知时,将retrieve the Sessionexpand一起使用,然后使用相关的价格数据。

使用如下所示的条带节点:

const session = await stripe.checkout.sessions.retrieve(
  'cs_test_xxx', {
    expand: ['line_items'],
  },
);
// note there may be more than one line item, but this is how you access the price ID.
console.log(session.line_items.data[0].price.id);
// the product ID is accessible on the Price object.
console.log(session.line_items.data[0].price.product);

要更进一步,如果您想要的不仅仅是产品ID,还可以通过传递line_items.data.price.product来扩展它,其中包括行项目及其相关价格以及该价格的完整产品对象。] >

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