#ninjajinja_code
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/1317115484591624225
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
* Updates a Stripe subscription schedule with quarterly billing cycles.
* If within the last 7 days of the current quarter, it schedules through the end of the next quarter.
*
* @param scheduleId - The ID of the subscription schedule to update
* @param scheduleStartDate - The start date for the schedule (Unix timestamp)
* @param price - The Stripe price ID
* @param quantity - The quantity for the subscription
* @returns Promise resolving to the updated SubscriptionSchedule
*/
export async function updateStripeSubscriptionSchedule(
scheduleId: string,
scheduleStartDate: number,
price: string,
quantity: number
): Promise<Stripe.SubscriptionSchedule> {
const endDateInSeconds = getQuarterEndTimestamp();
return stripe.subscriptionSchedules.update(scheduleId, {
phases: [
{
items: [
{
price,
quantity,
},
],
proration_behavior: "none",
start_date: scheduleStartDate,
end_date: endDateInSeconds,
},
{
items: [
{
price,
quantity,
},
],
proration_behavior: "none",
billing_cycle_anchor: "phase_start",
},
],
});
}
complete code for refrence
hi! what does " but it does not move invocies date" mean?
do you have an example sub_sched_xxx ID of a schedule that shows the issue? Can you explain what specific API field you expect to have a certain value, and what value you see instead?
๐ taking over for my colleague. Let me catch up.
would you mind sharing the IDs instead of screenshots?
sure
in test mode
sub_1QVY7jGA5TYWMni85bh2Lzvx
the attached schedule
sub_sched_1QVY7wGA5TYWMni8AEyBvrY2
subscription that was working as expected
sub_1QHJr2GA5TYWMni8vGlXHk70
let's forget a bit about these subscriptions for a second
ok
would you mind explaining how you would expect the subscriptions to work with an example
lets day we have product that offers seats
regardless of the schedule maybe there's another way to achieve this
now if you take subscribe in middle of quarter your phase end should concide with yearly quarter cycle
now we have this in palce
buisess wnats that if a user susbsctibes during last 15 days of subscription
ok
their pahse end should concide with next phase
ok
ie we incur cost for last 15 days of quarter
the scheduleing was owrking proery and invocies were getting creted on time
but now the invoice dates are not moving with schedule
while creating scheule the startdate snchoer is not chagned
but end date is
so basically, if I understood correctly, if the subscription starts at any point in time before 15 days of the end of the quarter then you would charge for a whole quarter but if you start in the last 15 days of the quarter then those should be a trial period.
is that your use case?
what should the customer pay for those 15 days?
could you give me an example with prices and dates?
sure
they pay 90$ per seat
now if they register within last 15 days of quarter they can register their seats and wait for rewards their next charge would be on last day of next qarter
other wise they will paty for current quarter and cycle should sync with yearly quarters
on Dec 16th a customer comes and tries to subscribe, what happens on that day?
do they pay anything?
yes but their cycle ends on 31 mar
ok
instead of 15
perfect
that's what I was talking about when I said you need to add trial period
to change the billing cycle anchor
stripeCustomerId: string,
priceId: string,
nfts: number[],
dbSubscriptionId: string,
dbUserId: string
) {
return stripe.subscriptions.create({
customer: stripeCustomerId,
items: [{ price: priceId, quantity: nfts.length }],
payment_settings: {
payment_method_types: ["card", "paypal"],
save_default_payment_method: "on_subscription",
},
payment_behavior: "default_incomplete",
expand: ["latest_invoice.payment_intent", "schedule"],
metadata: {
userId: dbUserId,
subscriptionId: dbSubscriptionId,
},
proration_behavior: "none",
collection_method: "charge_automatically",
});
}
current implementation
so i would add a anchor?
ok sure
so let's say on Jan 15th, another customer subscribes
yes
what should they pay?
90 dollars and they wold again be charged on 31 mar /1 apr
so no proration either?
ok perfect
then you need to do the following
use the billing cycle anchor config https://docs.stripe.com/billing/subscriptions/billing-cycle#use-billing_cycle_anchor_config
regardless of when the customer starts the subscription
use
month: <next quarter month>,
day_of_month: 1,
that would auto propogate?
yes
and when its the last 15 days you just add the number of days as https://docs.stripe.com/api/subscriptions/create#create_subscription-trial_period_days
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
but you would do that as an update
1 thing is still unclear why schedule
after the first invoice
is not working as expected
I'm not sure what do you mean by not working as expected
but using schedules is a bit of a stretch for your use case
like there's no need for that
I hope you had a look at both the subs
1st one the next invoice was at correct time i.e 1st jan as they subbed in middle of quarter
now the newer one sub_1QVY7jGA5TYWMni85bh2Lzvx the inovoices are not changing
as expected
they are following the billing cycle not the one we are setting up
because you can't have a billing cycle period bigger than the price interval
that's why I explained that you would need to use trial period
I'm trying to reproduce something to get you a full on working flow
please give me a couple of minutes
sorry for keeping you waiting @fallen ether
no worries
so to go back to our example
if the subscription starts on Dec 15th you would start by creating the subscription as you would do normally for the other scenario
billing_cycle_anchor_config: {
day_of_month: 1,
hour: 0,
minute: 0,
second: 0,
},
items: [
{
price: 'price_xxxx',
},
],
customer: 'cus_xxxx',
proration_behavior: 'none',
});
and then once the invoice is paid you update the trial_end to the date of the end of the next quarter
'sub_xxx',
{
trial_end: xxxxx,
}
);```
the only downside for this is that you would have the first invoice that would have a different billing period technically
@lyric forum is here to help you with any follow-ups
Hi ๐ yup, happy to help!
Hi @lyric forum thanks for helping me out
If the Subscription starts with a trial period then the customer isn't charged up front, which doesn't sound like it's what you want.
yes
so what @cedar lintel suggested is that I would charge first invoice
and then set trial end date according to me?
Yup, the gist here is that you want to create the Subscription normally (no trial period or other discounts in place) so the customer is charged up front and their Subscription starts. Then you want to adjust the behavior of the Subscription so that it's billing cycle anchor is moved to the align with the quarter while also avoiding charging the customer when the Subscription moves to that next billing period.
Using a trial period is the easier way to adjust the billing cycle anchor for an existing Subscription:
https://docs.stripe.com/billing/subscriptions/billing-cycle#add-a-trial-to-change-the-billing-cycle
and also allows you to avoid charging for a period of time.
ok
If you already use trialing Subscriptions in other parts of your flows, or if you rely on webhook event handlers that don't currently handle Subscriptions in a trial status, then there could be some additional logic that you need to plumb up. Also if you leverage some of our hosted UIs, then it could be apparent to the customer that they have a trial period, which they may find unexpected.
I think Subscription Schedules are likely the clenaer approach here, though they have a bit more upfront work to get going.
we have schedules in palce
but whats happening is
if we are settting schedule for more than price cycle
its not able to create the same
i can share some ecaple subs in test modes
sub_1QHJr2GA5TYWMni8vGlXHk70
works as ecpected
I'd be happy to take a closer look at that. I saw you discussing something similar in the thread earlier, but I don't have a good grasp on what the concern there was yet.
sub_1QVY7jGA5TYWMni85bh2Lzvx
we are creating a scheule and attachin it to sub if a sub is between the quarter i.e 15th of nov then the next cycle starts form 1st of jan as in case of sub_1QHJr2GA5TYWMni8vGlXHk70
but when a user subscribes within last 15 days of quarter thier sub starts as soon as they apy but it should end on last day of upcoming quarter which is not happening sub_1QVY7jGA5TYWMni85bh2Lzvx sub is example of that
hope I am able to make it clear
The problem with the approach you're using, is that you're still using a Price that charges each quarter. So even though your phase lasts half a year, the Price is still configured to charge the Customer in that time.
What I think you need to do instead is:
- create the Subscription
- create the Subscription Schedule
- update the Subscription Schedule so that it's first phase ends at the end of the current quarter AND set
billing_cycle_anchortophase_startfor the second phase AND use a mechanism to discount that billing period so the Customer isn't charged for it (like using a Coupon)
A potential downside here, is that an Invoice is still generated even if it's for $0, not sure whether that's problematic for your flow.