I'd like to show a customer a preview (or a quote) of what their subscription charges will be if they make changes to their subscription. For this, I'm using the "upcoming invoice" API endpoint:
https://stripe.com/docs/api/invoices/upcoming
From that link, they state that "You can preview the effects of updating a subscription, including a preview of what proration will take place.".
here is the exapmle
def retrieveUpcomingInvoice(subscriptionId,customerId,nextPriceId):
try:
# Retrieve the subscription
subscription = stripe.Subscription.retrieve(subscriptionId)
# Retrieve the invoice
invoice = stripe.Invoice.upcoming(
customer=customerId,
subscription=subscriptionId,
subscription_items=[
{
'id': subscription['items']['data'][0].id,
'deleted': True,
},
{
'price': nextPriceId,
'deleted': False,
}
],
)
return invoice
except Exception as e:
return str(e)
i got the amount_due for upgrad subscription amount user has to pay but only for different billing cycle
Monthly to monthly subscription "or" yearly to yearly subscription(same billing cycle):
like if i am on monthly basic subscription for $10 and i upgrade the subscription for advance subscription for $30 than invoice in return i got amount_due for $40 which is merged amount of plus and advanced, which is wrong , because i had already paid for 10$, so amount pay should be 30-10 = 20$ (tested: if i pay then 20$ is cut from account) (and invoice show $40 which is wrong)
Monthly subscription to yearly subscription or Vice-versa (this is ok)
if currently i am on monthly basic subscription for 10$ and i upgrade to yearly plus subscription for 100$ then upcoming invoice in amount_due show me correct information which is 90$ (and also if i pay it cut 90$ from account)
so how to get coorect amount in same billing cycle in stripe
You have a subscription for $10 per month, and after 15 days (half of the month) you want to change it to $30. With the upcoming invoice, you get the invoice the customer should pay in the next billing cycle which includes 3 line items:
Unused time: -$5 (that's the initial price divided by two)
Remaining time: +$15 (that's the new price divided by two)
Next invoice: +$30 (the new price for next month)
Here the total is: -$5 + $15 + $30 = $40 to be paid in 15 days. If that's not what you want you could:
Remove the $30 of the next invoice by passing subscription_proration_behavior: "always_invoice".
Or remove the $15 of the remaining time by passing subscription_billing_cycle_anchor: "now"
Note that changing the billing cycle of a subscription from monthly to yearly works differently, as explained here.
reset your billing cycle for get the exact amount
add subscription_billing_cycle_anchor = "now" at getting upcoming invoice
def retrieveUpcomingInvoice(subscriptionId,customerId,nextPriceId):
try:
# Retrieve the subscription
subscription = stripe.Subscription.retrieve(subscriptionId)
# Retrieve the invoice
invoice = stripe.Invoice.upcoming(
customer=customerId,
subscription=subscriptionId,
subscription_items=[
{
'id': subscription['items']['data'][0].id,
'deleted': True,
},
{
'price': nextPriceId,
'deleted': False,
}
],
subscription_billing_cycle_anchor = "now", # add
)
return invoice
except Exception as e:
return str(e)
Related
I have a requirement to bill for the isoweeks of occupation in the previous month. The weeks are tiered such that...
(tier1) week1 = 23
(tier2) week2 = 28
(tier3) week3 = 35
(tier4) week4+ = 46
I have provided a sample of the table below:
The Weeks column counts the number of weeks in the previous month from TODAY() [2022-11-29]. Also note that 1 isoweek is subtracted from the total should occupation rollover from month to month to avoid duplicated billing from before the previous month-this has already been accounted for in the Weeks column, I also account for the current tier in the Tier column (I'm hoping that these helpers assist with final result)
If we look at Id 1, this entity has a total occupation of 4 isoweeks (2022-09-21 to 2022-10-21) however the intention is to bill 2 isoweeks in month 10. This entity is currently in tier 4. The result should be 81 (35+46).
If we look at Id 4, this entity has a total occupation of 13 isoweeks (2022-08-09 to 2022-10-31... it's still in occupation as at the end of month 10) however the intention is to bill 5 isoweeks in month 10. This entity is currently in tier 4. The result should be 230 (46+46+46+46+46).
If we look at Id 6, this entity has a total occupation of 3 isoweeks (2022-10-05 to 2022-10-19) the intention here is to bill 3 isoweeks in month 10. This entity is currently in tier 3. The result should be 86 (23+28+35).
I hope this makes sense. I greatly appreciate any and all help!
EDIT: I have attempted SUMPRODUCT using an array but either I am doing it incorrectly or the function isn't designed to work for my issue.
I have one product and 8 prices (4 monthly and 4 annual)
When a user updates a subscription from one number to two, stripe creates an invoice and waits for the user to pay for it
And here are my two situations when I do not understand what to do
The user bought a subscription to one number - the user subscribes to the second number, but leaves at the time of payment - the subscription has been updated and switched to the past_due status and is waiting for payment (the invoice has been created)
Now there are two options - the user again goes to the subscription update and wants to add another number -> adds a number
-> an invoice is being created -> waiting for payment -> the user pays only for the third number and his subscription becomes active for 3 numbers, but there is an unpaid invoice for the second number (this is the second screen)
The user did everything the same, but after he left the payment in the second step, he still had an open invoice, when the user removes the number from the subscription (lowers the price back) - the stripe puts money on the balance for this number (which he did not pay ) - but at the same time he has an open invoice for the added number (second)
https://ibb.co/vkMZ1JX
https://ibb.co/zh7KVtq
This is how I create/update a subscription - https://gist.github.com/0ceb4ec03f9e7818a140b6a60e20b238
async createSubscription(stripeCustomerId: string, subscriptionInfo: SubscriptionInfoT) {
return this.stripe.subscriptions.create({
customer: stripeCustomerId,
items: [
{
price: subscriptionInfo.priceId,
quantity: subscriptionInfo.quantity,
},
],
description: Object.values(subscriptionInfo.numbers)
.filter(n => n)
.join(', '),
metadata: subscriptionInfo.numbers,
payment_behavior: 'default_incomplete',
payment_settings: { save_default_payment_method: 'on_subscription' },
expand: ['latest_invoice.payment_intent'],
});
}
async updateCustomerSubscription(subscriptionId: string, subscriptionInfo: SubscriptionInfoT) {
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
return this.stripe.subscriptions.update(subscriptionId, {
payment_behavior: 'default_incomplete',
proration_behavior: 'always_invoice',
description: Object.values(subscriptionInfo.numbers)
.filter(n => n)
.join(', '),
items: [
{
id: subscription.items.data[0].id,
price: subscriptionInfo.priceId,
quantity: subscriptionInfo.quantity,
},
],
metadata: subscriptionInfo.numbers,
expand: ['latest_invoice.payment_intent'],
});
}
To fully understand what happened, you need to keep in mind that proration_behavior: 'always_invoice' will create a Proration.
For example, if a customer upgrades from a 10 USD per month subscription to a 20 USD option, they’re charged prorated amounts for the time spent on each option. Assuming the change occurred halfway through the billing period, the customer is billed an additional 5 USD: -5 USD for unused time on the initial price, and 10 USD for the remaining time on the new price.
As you see, Proration calculation contains 2 parts: the credit being given back for unused time of old Price, and the amount of new Price.
Now looking closer to your 2 situation:
At the time your customer changed from 1 number to 2 numbers, the $7.99 Invoice was already created with some Proration. When your customer changed again from 2 numbers to 3 numbers, the $9.99 was (probably) also created with some Proration. To correct all the flow, you would need to make sure your Customer pays for the $7.99 Invoice, since they already have got the credit part of it.
Same situation that you would want to make your Customer pay for the second Invoice, since they already have got the credit part of it.
There are a few other alternatives, most feasible one is checking for unpaid Invoice before allow to make the Subscription Update API. This will prevent both situations above from happening in the first place.
Other alternatives is to disable proration by setting proration_behavior to none to save all the headaches, then calculate any price gap manually on your own, if needed.
I'm trying to calculate the remaining balance of a home loan at any point in time for multiple home loans.
Its looks like it is not possible to find the home loan balance w/ out creating one of those long tables (example). Finding the future balance for multiple home loans would require setting up a table for ea. home (in this case, 25).
With a table, when you want to look at the balance after a certain amount of payments have been made for the home loan, you would just visually scan the table for that period...
But is there any single formula which shows the remaining loan balance by just changing the "time" variable? (# of years/mths in the future)...
An example of the information I'm trying to find is "what would be the remaining balance on a home loan with the following criteria after 10 years":
original loan amt: $100K
term: 30-yr
rate: 5%
mthly pmts: $536.82
pmts per yr: 12
I'd hate to have to create 25 different amortization schedules - a lot of copy-paste-dragging...
Thanks in advance!
You're looking for =FV(), or "future value).
The function needs 5 inputs, as follows:
=FV(rate, nper, pmt, pv, type)
Where:
rate = interest rate for the period of interest. In this case, you are making payments and compounding interest monthly, so your interest rate would be 0.05/12 = 0.00417
nper = the number of periods elapsed. This is your 'time' variable, in this case, number of months elapsed.
pmt = the payment in each period. in your case $536.82.
pv = the 'present value', in this case the principle of the loan at the start, or -100,000. Note that for a debt example, you can use a negative value here.
type = Whether payments are made at the beginning (1) or end (0) of the period.
In your example, to calculate the principle after 10 years, you could use:
=FV(0.05/12,10*12,536.82,-100000,0)
Which produces:
=81,342.32
For a loan this size, you would have $81,342.32 left to pay off after 10 years.
I don't like to post answer when there already exist a brilliant answer, but I want to give some views. Understanding why the formula works and why you should use FV as P.J correctly states!
They use PV in the example and you can always double-check Present Value (PV) vs Future Value (FV), why?
Because they are linked to each other.
FV is the compounded value of PV.
PV is the discounted value at interest rate of FV.
Which can be illustrated in this graph, source link:
In the example below, where I replicated the way the example calculate PV (Column E the example from excel-easy, Loan Amortization Schedule) and in Column F we use Excel's build in function PV. You want to know the other way... therefore FV Column J.
Since they are linked they need to give the same Cash Flows over time (bit more tricky if the period/interest rate is not constant over time)!!
And they indeed do:
Payment number is the number of periods you want to look at (10 year * 12 payments per year = 120, yellow cells).
PV function is composed by:
rate: discount rate per period
nper: total amount of periods left. (total periods - current period), (12*30-120)
pmt: the fixed amount paid every month
FV: is the value of the loan in the future at end after 360 periods (after 30 year * 12 payments per year). A future value of a loan at the end is always 0.
Type: when payments occur in the year, usually calculated at the end.
PV: 0.05/12, (12*30)-120, 536.82 ,0 , 0 = 81 342.06
=
FV: 0.05/12, 120, 536.82 , 100 000.00 , 0 = -81 342.06
I am using billing_cycle_anchor when creating a subscription. I have a requirement where I need to charge from the day of payment to last of the month. And then from 1st of every month.
I am using code shown below:
var date = new Date();
var lastDay = Date.UTC(date.getFullYear(), date.getMonth() + 1, 0);
var monthLastDay = Math.floor(lastDay/1000);
let subscription = {
metadata: metadata,
billing_cycle_anchor: monthLastDay
};
Using above below is what is happening:
Prorated amount is charged from May 16 to May 31st
Subscription is created from May 31 – Jun 30.
But I want subscription to be created from June 1.
EDIT:
Right now time which is being calculated is:
1527724800
which is equivalent to:
05/31/2018 # 12:00am (UTC)
Please let me know possible solution.
I have a data sheet that col A is an account number, col F is a payment amount, col I is a user, and col H is a payment date....I am trying to sum payment amounts that fall in the same month for a particular user for a average monthly payment amount. It is a list of future payments that can have weekly or biweekly payments. I am trying to find which payments fall within the same month and sum those amounts and then average those payment amounts??? Any clues? This formula gets me a unique value for each user but cannot seem to get what I am looking for:
=SUM(--(FREQUENCY(IF('RD-CRC Payments'!I:I <> " ",IF('RD-CRC Payments'!I:I=C5,'RD-CRC Payments'!A:A)),'RD-CRC Payments'!A:A)>0))
This maybe what you are looking for. Enter cell with Name and cell with Date/Month you want to search for where it says CELLWITHDATE and CELLWITHNAME.
This will get you the sum.
=SUMIFS('RD-CRC Payments'!F:F,'RD-CRC Payments'!H:H,">="&EOMONTH(CELLWITHDATE,-1)+1,'RD-CRC Payments'!H:H,"<="&EOMONTH(CELLWITHDATE,0),'RD-CRC Payments'!I:I,CELLWITHNAME)
This will get you the average payment for that month.
=SUMIFS('RD-CRC Payments'!F:F,'RD-CRC Payments'!H:H,">="&EOMONTH(CELLWITHDATE,-1)+1,'RD-CRC Payments'!H:H,"<="&EOMONTH(CELLWITHDATE,0),'RD-CRC Payments'!I:I,CELLWITHNAME)/COUNTIFS('RD-CRC Payments'!H:H,">="&EOMONTH(CELLWITHDATE,-1)+1,'RD-CRC Payments'!H:H,"<="&EOMONTH(CELLWITHDATE,0),'RD-CRC Payments'!I:I,CELLWITHNAME)
I decided to add this if you need to use a Month instead of an Actual Date. This can be adjusted to use over years as well.
=SUMIFS($F:$F,$H:$H,">="&DATEVALUE(CellWithMonth&" 1"),$H:$H,"<="&EOMONTH(DATEVALUE(CellWithMonth&" 1"),0),$I:$I,CellWithName)
=SUMIFS($F:$F,$H:$H,">="&EOMONTH(DATEVALUE(CellWithMonth&" 1"),-1)+1,$H:$H,"<="&EOMONTH(DATEVALUE(CellWithMonth&" 1"),0),$I:$I,CellWithName)/COUNTIFS($H:$H,">="&EOMONTH(DATEVALUE(CellWithMonth&" 1"),-1)+1,$H:$H,"<="&EOMONTH(DATEVALUE(CellWithMonth&" 1"),0),$I:$I,CellWithName)