#chalmagean - Checkout + updates

1 messages ยท Page 1 of 1 (latest)

native beaconBOT
warm moat
#

Hi ๐Ÿ‘‹
Can you share the request ID for an update request to make the changes you are describing?

near nest
#

Oh, I only have POST requests...

#

that might be the issue...

warm moat
#

You have to use POST requests to make any changes to your Stripe data so that's normal

near nest
#

ok, so here's my request req_OX1RGiKCWnmqB7

warm moat
#

Thank you

#

This is a subscription creation request. It will create a new subscription

near nest
#

right, so to update the subscription, I'd have to send a new price?

warm moat
#

You would need to specify the ID of the Subscription, and you would need to provide a new price for the Subscription Item

near nest
#

is there a way to retrieve the price from the checkout session?

#

in the callback I mean

warm moat
#

What kind of callback do you mean here?

near nest
#

so I set up a checkout session, enter the payment details (credit card) and then I pay

#

then I get a callback, where I want to update the subscription

warm moat
#

What callback? is this a webhook? what is the event type?

near nest
#

oh, sorry... yes it's a webhook

#

with the checkout session id

warm moat
#

Okay in that case then the price would be in the line_items.data property. There will be an object for each price with other associated information (e.g. quantity, description, etc.)

near nest
#

this is what the session object looks like

#

for some reason I'm not seeing line_items in there

warm moat
near nest
#

right

warm moat
#

Oh wait

#

You need to Expand the line_items

near nest
#

OH!

#

I see...

warm moat
#

That will return a Checkout Session object with the details of the line_items

near nest
#

Stripe::Checkout::Session.retrieve(params[:checkout_session_id], {expand:['line_items']})

#

!! #<NoMethodError: undefined method `strip' for ["line_items"]:Array>

warm moat
near nest
#

that's what I used

#

Stripe::Checkout::Session.retrieve(params[:checkout_session_id], expand: ['line_items'])

#

oh, wait

#

nvm

#

that worked, thank you

warm moat
#

๐ŸŽ‰ great, I'm glad we could help ๐Ÿ™‚

near nest
#

I may be missing something obvious here (sorry to bug you)

#

but I get Cannot add multiple subscription items with the same plan: price_1MgYCGCL3oovQ9hjja15ER4g

#

and I'm not sure why

#
::Stripe::Subscription.update(
          stripe_session.subscription,
          {
            proration_behavior: 'create_prorations',
            items: [price: stripe_price]
          }
warm moat
#

If you just provide a new item like this it will merge it with your existing subscription items. So you are added a second item. If you want to replace the existing item you need to include the Subscription Item ID.

near nest
#

oh, I see...

near nest
#

So the session object has this 1 item:

>> stripe_session.line_items
=> #<Stripe::ListObject:0x66bc0> JSON: {
  "object": "list",
  "data": [
    {"id":"li_1Mj3llCL3oovQ9hju8ALaJCA","object":"item","amount_discount":0,"amount_subtotal":19900,"amount_tax":0,"amount_total":19900,"currency":"usd","description":"Premium","price":{"id":"price_1MgYCGCL3oovQ9hjja15ER4g","object":"price","active":true,"billing_scheme":"per_unit","created":1677610004,"currency":"usd","custom_unit_amount":null,"livemode":false,"lookup_key":null,"metadata":{},"nickname":null,"product":"prod_NRR4PrvNK0yV6s","recurring":{"aggregate_usage":null,"interval":"year","interval_count":1,"trial_period_days":null,"usage_type":"licensed"},"tax_behavior":"unspecified","tiers_mode":null,"transform_quantity":null,"type":"recurring","unit_amount":19900,"unit_amount_decimal":"19900"},"quantity":1}
  ],
  "has_more": false,
  "url": "/v1/checkout/sessions/cs_test_a1JpoBHERwOUygYEVvrHwOvPEZ8RgDRbB9PdK4HSbLKk1oGBkdBG2wMxlf/line_items"
}
#

And when I try to update it:

::Stripe::Subscription.update(
          stripe_session.subscription,
          {
            proration_behavior: 'create_prorations',
            items: [id: stripe_line_item.id, price: stripe_line_item.price.id]
          }
#

I get No subscription item with this ID (li_1Mj3llCL3oovQ9hju8ALaJCA) on the subscription.

native ledge
#

So I think what is happening is that Checkout Session line items are different than subscription line items. If you retrieve the subscription that you are looking at and check its items list you will see that their id's start with si_ https://stripe.com/docs/api/subscriptions/object#subscription_object-items-data-id

#

Catching up on a couple other threads, just to clarify on this thread's topic, is this still about you creating a Subscription with Checkout and then later trying to upgrade or downgrade it?

near nest
#

yes it is

#

trying to upgrade and prorate

native ledge
#

Apologies for the delay, thanks for confirming. So I think what you need to do is make sure you are working directly with the subscription object and its items rather than the Checkout Session.

#

Is there a reason you were still trying to work with the checkout session there? Just for testing? Or do you have data there that you are trying to connect with the actual subscription object?

near nest
#

I'm getting the new (upgrade) price from the session

#

I'm currently testing an implementation for upgrading from one plan to another

#

so I start a checkout session, pay for the upgrade, and on the webhook, I want to update the existing subscription

#

to use the new price

native ledge
#

Gotcha. So in that case, I think you would need to list the subscriptions for that customer[1] (which you can find with the customer property on the checkout session), find the subscription and item you want to modify in that list, and then make the update call that you were making but with the subscription item ID (si_123) from the item on that existing subscription

[1] https://stripe.com/docs/api/subscriptions/list#list_subscriptions-customer

near nest
#

I don't want invoices (for now).

#

I'll look at the first option.

native ledge
#

chalmagean - Checkout + updates