Stripe retrieve the good price ID

Viewed 452

I setup my shop with Stripe. You can buy it with the good product and with different sizes. It works well until I have to sent an email to the seller with some informations like the name of the product bought, the size of the product (product.nickname), the customer information and so on. My problem is I can't get this 'nickname'.

As I understand in Stripe, you have some Products, which can have different Prices. For me, I have a product with 3 prices. Each price has his own ID and a 'label' (according to Stripe called 'nickname' as I saw in the request). The problem came when I want to retrieve this ID to get the specific nickname (the name if the size). For some reason, the ID returned for the selected product is not the same as the ID from Stripe Dashboard. And each time I got a new purchase, this returned ID change.

In my WebhooksController in def create:

case event.type
when 'checkout.session.completed'
              session = event.data.object
              @order = Stripe::Checkout::Session.retrieve({ id: session.id, expand: ["line_items", "customer"]})
    OrderMailer.order_mail(@order).deliver
end
render json: { message: 'success' }

In my order_mail.html.erb:

<% @order.line_items.data.each do |line| %>
<%= line %>
<% end %>

Can someone explain me why the line_item ID returned in the webhook is not the same as the Stripe dashboard ID? And how can I get and display the nickname of the price selected in my mail?

here is what I got for the <%= line %>

{ "id": "li_1K41JPFE4oPSz5YO98K2r2CW", "object": "item", "amount_subtotal": 4000, "amount_total": 4000, "currency": "eur", "description": "Soleil Levant", "price": { "id": "price_1K41JPFE4oPSz5YORWdXXGuT", "object": "price", "active": false, "billing_scheme": "per_unit", "created": 1638874459, "currency": "eur", "livemode": false, "lookup_key": null, "metadata": { }, "nickname": null, "product": "prod_KjUHP8uALE6aZI", "recurring": null, "tax_behavior": "unspecified", "tiers_mode": null, "transform_quantity": null, "type": "one_time", "unit_amount": 4000, "unit_amount_decimal": "4000" }, "quantity": 1 } 

If I cUrl the right price:

https://api.stripe.com/v1/prices/price_1JxC8kFE4oPSz5YOc391IZ9I\
  -u sk_test_51JbmGvFE4oPSz5YOsl0JTTEi0quapEwPBXQsQRbRXPdr38hSiyVDPs9LnkH4nHCGXqwoYjOTnHoH6FtIC4EP32pr00KHIxFlLp:

I got this:

{
  "id": "price_1JxC8kFE4oPSz5YOc391IZ9I",
  "object": "price",
  "active": true,
  "billing_scheme": "per_unit",
  "created": 1637247786,
  "currency": "eur",
  "livemode": false,
  "lookup_key": null,
  "metadata": {
    "nickname": "S"
  },
  "nickname": "S",
  "product": "prod_KcR0HgpfzldOj8",
  "recurring": null,
  "tax_behavior": "unspecified",
  "tiers_mode": null,
  "transform_quantity": null,
  "type": "one_time",
  "unit_amount": 4000,
  "unit_amount_decimal": "4000"
}
1 Answers

What you are doing should work:

  • First, create a Price with nickname: "foobar", let’s say its ID is "price_abc".
  • Then, create a Checkout Session where you set line_items[0].price: "price_abc".
  • Later, you retrieve the Checkout Session with expand: ["line_items"]. If you look into checkout.line_items.data[].price, you will find an object that contains what you are looking for: { id: "price_abc", nickname: "foobar", … }. Note that you can see the structure of the line_items.data object in the Stripe documentation here.

So you need to double check that you are using the correct price ID when creating the Checkout Session, and that you are looking at checkout.line_items.data[].price to find the id and nickname of the price.

Related