#msmello

1 messages · Page 1 of 1 (latest)

slender mortarBOT
zinc canyon
#

Hello, not sure I fully follow your use case. Is this essentially a referral program and users that get referred get a discount based on the person who referred them?>

#

And do you already have some of this working in Stripe or are you asking from the ground up?

ornate sequoia
#

No, I create a Saas, basically, you will be able to create your organization and add new members.
To use the software that I developed, the user needs to select a plan (Premium e.g.) that costs 29 dollars.

My desired behaviour is, if the owner of an organisation used some discount coupon, this same discount should be applied to all new members (each new member cost +29 dollars additionally).

#

I have two functions, the first one is the checkout and the second one is to update the subscription after adding a new member.

#
checkout.ts

const clientReferenceId = params.organizationId;

  const client = getSupabaseServerClient();

  const members = await getOrganizationMembers(client, clientReferenceId);
  console.log({ members, data: members.data })
  const numberOfMembers = members.count || 1;

  // we pass an optional customer ID, so we do not duplicate the Stripe
  // customers if an organization subscribes multiple times
  const customer = params.customerId || undefined;

  // if it's a one-time payment
  // you should change this to "payment"
  // docs: https://stripe.com/docs/billing/subscriptions/build-subscription
  const mode: Stripe.Checkout.SessionCreateParams.Mode = 'subscription';

  // get stripe instance
  const stripe = await getStripeInstance();

  const lineItem: Stripe.Checkout.SessionCreateParams.LineItem = {
    quantity: numberOfMembers,
    price: params.priceId,
  };

  return stripe.checkout.sessions.create({
    mode,
    customer,
    line_items: [lineItem],
    success_url: successUrl,
    cancel_url: cancelUrl,
    client_reference_id: clientReferenceId.toString(),
    allow_promotion_codes: true,
  });
#

To increase the subscription quantity:

export async function increaseSubscriptionQuantity(
  subscriptionId?: string
) {

  if (!subscriptionId) {
    throw new Error(
      `Subscription with ID does not exist.`
    );
  }

  const stripe = await getStripeInstance();
  const subscription = await stripe.subscriptions.retrieve(subscriptionId);

  // As we add only 1 line item during the subscription creation
  // we can safely select the first in the array
  //
  // NB: if you customize the line items, you have to find a different way
  // to select the correct line item!
  const lineItem = subscription.items.data[0];

  if (!lineItem) {
    throw new Error(
      `Subscription with ID ${subscriptionId} has no line items?`
    );
  }

  const currentQuantity = lineItem.quantity ?? 0;
  const quantity = currentQuantity + 1;
  const coupon = subscription.discount?.coupon.id;
  const isCouponValid = subscription.discount?.coupon.valid;

  const stripeParams: Stripe.SubscriptionUpdateParams = {
    items: [{ id: lineItem.id, quantity: quantity }],
  }

  if (coupon && isCouponValid) {
    stripeParams.coupon = coupon;
  }

  return stripe.subscriptions.update(subscriptionId, stripeParams);
}
#

But when I am checking the billing, it is a little bit strange:

#

I just tried the flow, I create an account, selected the premium plan, and after that I added one user:

#

But it seems that the invoice that was paid, is correct (50% of discount).
But the next one is not, because it is considering one fully and the other one with the coupon applied.

#

This is my subscription id: sub_1MsnatHZABaXHq0TYTLW6hKN

zinc canyon
#

Apologies the server is very busy so I'm just getting to this

ornate sequoia
#

Can you help me to understand, how this works and what I am doing wrong?

zinc canyon
#

What duration do you have on the coupon? It sounds like this may have been a one time coupon but you want it to apply many times (possibly forever)?

ornate sequoia
#

I created this one

#

I thought that I should be able to see something like this:
$14.50 (paid)
$14.50 (next month)

$29.00

Something like that, the problem that I am having now is that I don't know if what I did is correct but I don't know how to read it or if I am doing something wrong

zinc canyon
#

Can you explain exactly what you wanted to happen on this subscription?

#

I see it was 1@$29/month with a 50% off coupon and then got upgraded to 2@$29/month with that coupon so I would expect the subscription to be $29/month from that

ornate sequoia
#

In the next month or in the current one?

zinc canyon
#

I was looking at the upcoming invoice because your screenshot was of the pending invoice items

#

Can you tell me more about what update you were trying to make and what you wanted each of these invoices to look like?

ornate sequoia
#

I would like the user to apply the discount on his first subscription and then all new members that get added will have the coupon applied to their subscriptions.

So, If I subscribed to the plan today, and add a new member tomorrow (applying 50% discount - full price 29 Euros)

How it should be shown to me on the billing page?

zinc canyon
#

I'm a bit confused. The prices on that subscription are $29 a month, wouldn't applying the 50% discount mean that they pay $14.50 a month as happened with sub_1MsnatHZABaXHq0TYTLW6hKN?

ornate sequoia
#

Yes, exactly

#

So the sum of the next 2 subscriptions, would be $29 right?

zinc canyon
#

Correct

#

And on that subscription I do see the 50% off coupon being applied to all of the items on that subscription

#

Like right now in the upcoming invoice section, I see the remaining time and unused time both have 50% off. Then the next month's charges show 2x$29.00=$58 but just under that we show -$29.00 from the 50% discount so it does look like it is getting applied everywhere

ornate sequoia
#

But why I am receiving this price? US$43.50

#

I know that it is a dumb question but for me isn't clear still.

zinc canyon
#

So the top two lines come from the proration that happened when you updated your subscription. You updated the subscription halfway through the month, so we credited the user $14.50 (half of their 1x$29.00 plan) for the unused time and charged $29.00 (half of their 2x$29.00 plan)

#

And then the bottom shows how much we would charge for a full month at 2x$29.00, so $58.00. But you still have the 50% off coupon so we take $29.00 off of that.

#

The total is $29 (the cost for the full next month) plus $14.50 (the charge from your prorations midway through this month)

#

Does that help?

ornate sequoia
#

Yes, it does. Let me just think about this for a moment to see if I really understood.

#

I understood, and it makes sense but I have questions still.

#

Imagining that this came from a real user.
How much has the user already paid?
How much I will pay in the next billing?
And when this billing would happen?

zinc canyon
#

$43.50, the total, is how much they will be charged

ornate sequoia
#

It seems to me that if I was using my credit card, I would receive notifications like this:

  • $14.50 Subscription paid
  • $14.50 Subscription Refund
  • $29.00 Subscription paid

Next month (at someday)

  • 29.00 Subscription paid
zinc canyon
#

And the bill will be sent on 3 May, you can actually see that it says that just under the "upcoming invoice" line in the screenshot that you sent

#

No, they'll only see the actual charges. So $14.50 and then $43.50 in this case

#

The rest is just Stripe things for tracking the right amount to charge

ornate sequoia
#

So, as you said $43.50 in this case. But will this $43.50 be charged on the 3 of May or in the current month when the user was added?

zinc canyon
#

On 3 May

#

So to be clear on the timeline of things:

  1. Today, April 3rd, you created this subscription for 1@$29 with a 50% off coupon. So the user was charged $14.50.
  2. Then you updated the subscription to have a quantity of 2 with prorations on. That means Stripe said that the user should have paid $29 for the whole month. But you didn't tell us to charge immediately so that extra $14.50 is going on the next invoice (we can discuss how to charge them now for that if you want)
  3. The upcoming invoice section in our dashboard is showing how much we will charge the user for their full $29 next month as well as the $14.50 for their unpaid thing from this month
ornate sequoia
#

Thank you so much, it makes sense.
I would like to know how to charge them instantly if it is possible.

slender mortarBOT
zinc canyon
#

Awesome, so in future you can pass a proration_behavior of always_invoice when updating these subscriptions, that will tell Stripe to immediately charge the user if they owe extra money because of the update https://stripe.com/docs/api/subscriptions/update#update_subscription-proration_behavior

ornate sequoia
#

I will for sure

#

Thank you so much

#

You really helped me to understand

#

Can I give you 5 stars, or something like that?