#r-y-a-n_api
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1240038665376239688
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
This response looks correct for the other price id req_6XrRCq2aGhKNUF
Can you specify what behaivor are you expecting? An example with exact amounts would help
Sure, this is the test subscription:
sub_1PG7CdDfBjEyR7leAmqKfow3
It's currently got one price at $100/year. I created it today. I am trying to preview changing the price to one that is $250/year. When I call the upcoming invoice endpoint with that new price ID, the total amount of the invoice should be $250 - X, where X is the amount left on my $100 plan (i.e. $99.XX). So roughly $150 total.
Instead, the response has these three line items:
Unused time on Puck Member Annual after 14 May 2024 -9998
Remaining time on Puck Premium Subscription after 14 May 2024 24996
1 ร Puck Premium Subscription (at $250.00 / year) 25000
So the total is: int(42648)
The third line item should not be there, right? Adding those three up, plus tax, gets the total of 42648, but that's not what I'm actually charged if I go through with this change. I get charged the correct amount of roughly $150.
This is the call I'm making in PHP:
$items = [
[
'id' => $sub_item_id,
'price' => $price_id,
],
];
$invoice = $PuckStripe->invoices->upcoming([
'customer' => $customer_id,
'subscription' => $current_subscription->id,
'subscription_details' => [
'items' => $items,
'proration_date' => time(),
],
]);
๐ taking over for my teammate! Give me a few mins to catch up please
What value are you passing for subscription_details.proration_date?
time()
Ah, current time
same result if I omit that parameter
Can you share the full result of your request to the retrieve an upcoming invoice endpoint?
The line item for the 1 ร Puck Premium Subscription (at $250.00 / year) is for the billing period May 14, 2025 - May 14, 2026
["end"]=>
int(1778785919)
["start"]=>
int(1747249919)
}```
How can I avoid that being included here? I just want to know what the next payment for the user will be, which will be charged when the subscription is updated
I tried this but I get the same result as before:
'subscription_details' => [
'items' => $items,
'proration_date' => time(),
'prebilling' => [
'iterations' => 1,
],
],
Also tried passing 0 for iterations, but I got an error it must be >=1
Okay, I see what's going on here
The three lines are expected to be there: a credit for the unused time on the old price, a debit for the remaining time on the old price, and the new $250/year charge. In this case, the "upcoming" invoice is the one for the billing period starting in May 2025. The 42648 amount is the correct total for the three items. The reason the top level amount_due is lower (26655) is because this customer has a credit balance of $159.93 ["starting_balance"]=> int(-15993)
I removed the balance, and the result is the same as far as including the next term.
This still doesn't explain why it's including the next term for this one invoice.
How can I generate a preview of the invoice this user will receive next when they change their plan? It won't include the billing period for 2025-2026, that will be the following invoice after the one I want.
I went ahead and made the change for this user. You can see the invoice it generated here. Why doesn't the preview invoice match this amount? https://dashboard.stripe.com/test/invoices/in_1PGT8SDfBjEyR7lefFt9uuo5
You can use the same endpoint to retrieve an upcoming invoice but just look at the proration lines.
The upcoming invoice endpoint for a subscription that already exists will show you a preview of their next scheduled invoice (which is currently scheduled for May 14, 2025)
But when I make that same change, just updating the price id on the subscription, the result is the invoice I just showed that doesn't include year 2025-2026. Is there no way to preview that invoice before making the change?
The invoice for May 2025 isn't accurate anyway because they will have already paid for 2024 by then. That doesn't make sense with annual billing
Yeah, I see your point.
The only way to do this is to use the upcoming invoice API where subscription_details.proration_date is the date where you intend to make the price upgrade/downgrade and only pay attention to the proration lines ("unused time on..." and "remaining time on...")
What about tax - won't the tax applied include that amount, so I would need to do a separate tax calculation on just the total from the line items with proration=true?
The tax should be calculated for those proration lines
Right. But the tax amount is based on the total including the non-prorated lines as well right?
Ah I see, each line item has a separate tax amount already
OK, I have what I need now, thank you! I ended up having to add customer starting balance & each prorated line item amount+tax
$total = $invoice->starting_balance;
foreach($invoice->lines->data as $item) {
if ($item->proration) {
$total += $item->amount;
if (isset($item->tax_amounts[0]->amount)) {
$total += $item->tax_amounts[0]->amount;
}
}
}
return $total;