#chalmagean - Checkout + updates
1 messages ยท Page 1 of 1 (latest)
Hi ๐
Can you share the request ID for an update request to make the changes you are describing?
You have to use POST requests to make any changes to your Stripe data so that's normal
ok, so here's my request req_OX1RGiKCWnmqB7
right, so to update the subscription, I'd have to send a new price?
You would need to specify the ID of the Subscription, and you would need to provide a new price for the Subscription Item
is there a way to retrieve the price from the checkout session?
in the callback I mean
What kind of callback do you mean here?
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
What callback? is this a webhook? what is the event type?
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.)
this is what the session object looks like
for some reason I'm not seeing line_items in there
This is what the Checkout Session object is defined as in our API: https://stripe.com/docs/api/checkout/sessions/object
right
That will return a Checkout Session object with the details of the line_items
Stripe::Checkout::Session.retrieve(params[:checkout_session_id], {expand:['line_items']})
!! #<NoMethodError: undefined method `strip' for ["line_items"]:Array>
You can see the Ruby syntax for expanding responses here: https://stripe.com/docs/api/expanding_objects?lang=ruby
that's what I used
Stripe::Checkout::Session.retrieve(params[:checkout_session_id], expand: ['line_items'])
oh, wait
nvm
that worked, thank you
๐ great, I'm glad we could help ๐
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]
}
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.
oh, I see...
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.
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
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
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?
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?
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
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
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
That being said, a simpler way to do this could be to use our pending updates flow and then send the user to the Stripe hosted invoice page. The UI would be different but this could definitely cut down on a bunch of logic on your side https://stripe.com/docs/billing/subscriptions/pending-updates
chalmagean - Checkout + updates