Get upcoming Stripe invoice for prorated change in quantity

Viewed 507

I want to get the cost of changing the quantity of a subscription using Stripe. I found the Upcoming API, which looks like it should work, but I've not been able to work out the right combination of arguments to pass to it.

This returns the error You must pass one of plan, price, or price_data.:

$this->stripe->invoices->upcoming([
    'customer' => $this->stripe_id,
    'subscription' => $this->subscription_id,
    'subscription_items' => [[
      'quantity' => $quantity
    ]]
 ]

But when I add the price id, as below, I get the error Cannot add multiple subscription items with the same plan: price_1H...

$this->stripe->invoices->upcoming([
    'customer' => $this->stripe_id,
    'subscription' => $this->subscription_id,
    'subscription_items' => [[
        'price' => $this->price_id,
        'quantity' => $quantity
    ]]
]));

Removing the subscription id then only returns the cost of a brand new subscription, not the prorated invoice for the existing one.

1 Answers

As always, two seconds after posting the question I worked it out - as well as passing the subscription id, the subscription item's id needs to be passed as well, which I did by this snippet:

$subscription = $this->stripe->subscriptions->retrieve($this->subscription_id);
$this->stripe->invoices->upcoming([
    'customer' => $this->stripe_id,
    'subscription' => $this->subscription_id,
    'subscription_items' => [[
        'id' => $subscription->items->data[0]->id,
        'quantity' => $quantity
    ]]
]);
Related