#ninjajinja_code

1 messages ยท Page 1 of 1 (latest)

dull brambleBOT
#

๐Ÿ‘‹ 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.

fallen ether
#
 * 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

ruby rampart
#

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?

dull brambleBOT
fallen ether
#

sure

cedar lintel
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

#

would you mind sharing the IDs instead of screenshots?

fallen ether
#

sure

#

in test mode
sub_1QVY7jGA5TYWMni85bh2Lzvx

#

the attached schedule
sub_sched_1QVY7wGA5TYWMni8AEyBvrY2

cedar lintel
#

taking a look

#

give me a couple of minutes please

fallen ether
#

subscription that was working as expected
sub_1QHJr2GA5TYWMni8vGlXHk70

cedar lintel
#

let's forget a bit about these subscriptions for a second

fallen ether
#

ok

cedar lintel
#

would you mind explaining how you would expect the subscriptions to work with an example

fallen ether
#

lets day we have product that offers seats

cedar lintel
#

regardless of the schedule maybe there's another way to achieve this

fallen ether
#

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

cedar lintel
#

ok

fallen ether
#

their pahse end should concide with next phase

cedar lintel
#

ok

fallen ether
#

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

cedar lintel
#

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?

fallen ether
#

no

#

we are not giving trial

cedar lintel
#

what should the customer pay for those 15 days?

#

could you give me an example with prices and dates?

fallen ether
#

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

cedar lintel
#

I'm not sure I follow

#

let us try it this way

fallen ether
#

ok

#

please go ahead

cedar lintel
#

on Dec 16th a customer comes and tries to subscribe, what happens on that day?

#

do they pay anything?

fallen ether
#

yes

#

they are chanrged

cedar lintel
#

the full price?

#

for 3months

fallen ether
#

yes but their cycle ends on 31 mar

cedar lintel
#

ok

fallen ether
#

instead of 15

cedar lintel
#

perfect

#

that's what I was talking about when I said you need to add trial period

#

to change the billing cycle anchor

fallen ether
#
  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?

cedar lintel
#

no

#

wait....

#

let me ask another question

#

before we get into the coding part

fallen ether
#

ok sure

cedar lintel
#

so let's say on Jan 15th, another customer subscribes

fallen ether
#

yes

cedar lintel
#

what should they pay?

fallen ether
#

90 dollars and they wold again be charged on 31 mar /1 apr

cedar lintel
#

so no proration either?

fallen ether
#

yes

#

no prorations

cedar lintel
#

ok perfect

#

then you need to do the following

#

regardless of when the customer starts the subscription

#

use

    month: <next quarter month>,
    day_of_month: 1,
fallen ether
#

that would auto propogate?

cedar lintel
#

yes

#

but you would do that as an update

fallen ether
#

1 thing is still unclear why schedule

cedar lintel
#

after the first invoice

fallen ether
#

is not working as expected

cedar lintel
#

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

fallen ether
#

I hope you had a look at both the subs

cedar lintel
#

what did you see that changed?

#

would you mind pointing the differences out?

fallen ether
#

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

cedar lintel
#

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

fallen ether
#

would they be chareged immideately

#

if we add trial?

cedar lintel
#

I'm trying to reproduce something to get you a full on working flow

#

please give me a couple of minutes

fallen ether
#

sure

#

thanks for your patientce with me

dull brambleBOT
cedar lintel
#

sorry for keeping you waiting @fallen ether

fallen ether
#

no worries

cedar lintel
#

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

fallen ether
#

sure thanks

#

i have a small query what if i add the trial at start

lyric forum
#

Hi ๐Ÿ‘‹ yup, happy to help!

fallen ether
#

Hi @lyric forum thanks for helping me out

lyric forum
#

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.

fallen ether
#

yes

#

so what @cedar lintel suggested is that I would charge first invoice

#

and then set trial end date according to me?

lyric forum
#

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.

Learn how to set the billing date for subscriptions.

fallen ether
#

ok

lyric forum
#

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.

fallen ether
#

ahh

#

that would be horrible UX

lyric forum
#

I think Subscription Schedules are likely the clenaer approach here, though they have a bit more upfront work to get going.

fallen ether
#

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

lyric forum
#

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.

fallen ether
#

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

lyric forum
#

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_anchor to phase_start for 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.