简体   繁体   English

条带从 session object 获取 product_id / price_id

[英]Stripe get product_id / price_id from session object

I'm currently using Stripe Webhooks to get notified when user pays for a product.我目前正在使用 Stripe Webhooks 在用户为产品付款时收到通知。 This is working fine.这工作正常。 From the payment intent I can get the Seesion and the Customer Object.从付款意图中,我可以获得Seesion客户Object。 But I don't find a way to get the product_id or price_id for what the user paid.但是我找不到获取用户支付的product_idprice_id的方法。

Does someone know a way to get product_id or price_id ?有人知道获取product_idprice_id的方法吗?

Thanks for the question.谢谢你的问题。 As you noticed the Session data included in the checkout.session.completed event does not include the line_items where the Price ID is associated to the Checkout Session.正如您注意到的 Session 数据包含在checkout.session.completedline_items事件不包括价格 ID 与结帐 Z71C7AE294B7ABD866B3FB295B3B9E4A 关联的 line_items

line_items is one of the expandable properties, so to retrieve the Price ID you'd retrieve the Checkout Session and use expand to include the line items in the response. line_items是可扩展属性之一,因此要检索价格 ID,您需要检索 Checkout Session 并使用expand将订单项包含在响应中。 There is not a way to configure your webhook to have the data sent to you include this data.无法配置您的 webhook 以使发送给您的数据包含此数据。

There are two approaches to associating a customer's purchase with a Checkout Session.有两种方法可以将客户的购买与 Checkout Session 相关联。 First, you could store the ID of the Checkout Session in your database alongside the cart or list of items purchased by the customer.首先,您可以将 Checkout Session 的 ID 与购物车或客户购买的商品列表一起存储在数据库中。 That way you if a checkout session is successful, you can look up the cart by ID and know which items were purchased.这样,如果结帐 session 成功,您可以通过 ID 查找购物车并知道购买了哪些商品。

Alternatively you could listen for the checkout.session.completed webhook event, then when you receive a new notification for a successful checkout, retrieve the Session with expand then use the related price data.或者,您可以侦听checkout.session.completed webhook 事件,然后当您收到成功结帐的新通知时,检索 Session展开,然后使用相关价格数据。

Using stripe-node that would look like the following:使用如下所示的条带节点:

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);

To take this a step further, if you wanted more than just the ID of the product, you could also expand that by passing line_items.data.price.product which would include the line items their related prices and the full product objects for those prices.为了更进一步,如果您想要的不仅仅是产品的 ID,您还可以通过传递line_items.data.price.product来扩展它,其中将包括行项目的相关价格以及这些价格的完整产品对象.

While creating the Payment Intent, you can store additional information about the object in the metadata field.创建支付意图时,您可以在元数据字段中存储有关 object 的其他信息。

const paymentIntent = await stripe.paymentIntents.create({
    amount: 1099,
    currency: 'usd',
    payment_method_types: ['card'],
    metadata: {
        product_id: '123',
        price_id: '20',
    },
});

Once the payment is done, you can retrieve this information from the metadata field.付款完成后,您可以从元数据字段中检索此信息。

You can do the same for the Session Object as well.您也可以对 Session Object 执行相同的操作。

cjav_dev answer is great. cjav_dev 答案很棒。 Here is the PHP code for the same.这是相同的 PHP 代码。

$event = \Stripe\Event::constructFrom(
    json_decode($payload, true), $sig_header, $endpoint_secret
);
$eventObject = $event->data->object;

$stripe = new StripeClient('testsk_ssdfd...sdfs');
$csr = $stripe->checkout->sessions->retrieve($eventObject->id,
  ['expand' => ['line_items']]
);
$priceid = $csr->line_items['data'][0]['price']['id'];

Note the above is only retrieving the 1st line item.请注意,以上只是检索第一个行项目。 You may have to do a loop for all items.您可能必须对所有项目进行循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM