#msmello
1 messages · Page 1 of 1 (latest)
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?
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
Apologies the server is very busy so I'm just getting to this
Can you help me to understand, how this works and what I am doing wrong?
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)?
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
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
In the next month or in the current one?
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?
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?
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?
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
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.
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?
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?
So I misstated that a bit. It looks like this user paid $14.50 already, you can see the other invoice on that customer https://dashboard.stripe.com/test/invoices/in_1MsnatHZABaXHq0TNz0WLPp1
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
$43.50, the total, is how much they will be charged
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
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
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?
On 3 May
So to be clear on the timeline of things:
- Today, April 3rd, you created this subscription for 1@$29 with a 50% off coupon. So the user was charged $14.50.
- 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)
- 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
Thank you so much, it makes sense.
I would like to know how to charge them instantly if it is possible.
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
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
For that specific subscription, you can create an invoice for that customer via the API. The invoice item for that $14.50 will get added to the invoice and you can charge it by finalizing that invoice https://stripe.com/docs/api/invoices/create
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I would recommend reading our docs on prorations and subscription upgrade/downgrades. They will help you understand more of what is going on here
https://stripe.com/docs/billing/subscriptions/prorations
https://stripe.com/docs/billing/subscriptions/upgrade-downgrade