#Kobweb
1 messages · Page 1 of 1 (latest)
Yes, when a user singup by an affiliate link, we create a coupon and attribute it to the new customer
The easiest way is to create a new coupon.
Hum okay, so i can just check the last subscription coupon and if not "used", delete the subscription and recreate the same coupon to the customer ?
Yes.
Ok i will try that
Ok i have a little weird thing
When i create my customer and create a coupon for him, there is the stripe dashboard :
We an see on the left, the coupon
But when i create an empty subscription (for the user fill it with his card on my website), the coupon disapear from the left and in the subscription (on the dashboard)
So i can't get the coupon back from the subscription
(to recreate it)
How do you add a Coupon to a Customer in the first place?
When my user signup, there is the code :
const coupon = await this.stripeService.getClient().coupons.create({
applies_to: {
products: [productIds.basic, productIds.team, productIds.pro],
},
duration: "once",
max_redemptions: 1,
name: "Réduction d'affilié",
percent_off: 20,
});
(products is an array of product ids)
And when you try to re-create it you run the same code?
There is my code when a user click to a new subscription :
const stripeClient = this.stripeService.getClient();
// get last subscription of the user
const subscription = await this.stripeService.getClient().subscriptions.list({customer: customerId, limit: 1, expand: ['data.default_tax_rates']}) as Stripe.ApiList<Stripe.Subscription>;
if (subscription.data.length === 0) {
return;
}
console.log({subscription: subscription.data[0]})
// if the subscription is not active and never used, get the coupon, delete the subscription and recreate the coupon
if (subscription.data[0].status !== 'active' && subscription.data[0].default_tax_rates.length === 0 && subscription.data[0].discount?.coupon?.id) {
const coupon = await stripeClient.coupons.retrieve(subscription.data[0].discount.coupon.id);
await stripeClient.subscriptions.del(subscription.data[0].id);
await stripeClient.coupons.del(coupon.id);
await stripeClient.coupons.create({...coupon, id: undefined});
}
And the subscription.data[0].discount is null
Sorry for the wait.
No problems
Okay, I'm a bit confused.
You can create a new subscription with the same coupon ID. You don't actually need to delete it or re-create it.
If a coupon has disappeared from a customer (e.g. used), you can add it again by updating the Customer: https://stripe.com/docs/api/customers/update#update_customer-coupon
Yes but at the moment when my customer create an empty subscription (created and pass to my stripe elements to my customer pay) the coupon just "disappears" from everywhere, not found by the customer and not found by the subscription
And very strange thing, it is still applied
If my customer pay, the discount will be applied
You should see all you coupons here:
https://dashboard.stripe.com/test/coupons
https://dashboard.stripe.com/coupons
You can check in your first Subscription Invoice if the Coupon was used.
Are you using a one time coupon or a coupon that will still be applied on future cycles?
I am not immediately sure when that field is unset but can look in to it
Okay, that should work somewhere. Where are you not seeing this coupon that you expect to see it?
When my customer is signed up, a coupon is created for him, i can see it in the stripe dashboard, when my customer click on a subscription plan, i create an empty subscription and the coupon disappears from the stripe dashboard (left of my customer page) and the coupon is applied to the subscription (but not return by the stripe api (if I retrieve the subscription discount is null)) if my customer go back and click, i create a new empty subscription and the coupon is lost
So ? You know how to fix my problem ?
Do you have the ID of one of the subscriptions that you created this way (sub_123)?
Here is one : sub_1MVxaWFpZdlKgOMylWKex9Np
Hum wait
Probably not created with the coupon, here is one more sure : sub_1MVxFaFpZdlKgOMyg4hGfEs2
Thanks for the IDs, checking in to them
I actually still see a discount on the customer object on that second subscription. If you retrieve the customer via the API that should show up in their discount field https://stripe.com/docs/api/customers/object#customer_object-discount
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Checking in to that with the subcription...
It looks like that subscription is for the right produt
I think the discount should be applied once that subscription's first invoice is finalized
Have you tried finalizing these invoices to see if they show up then?
Yes if the user pay the discount will be applied, but it's not my problem
When my user is new, he got a coupon
When he will click on BASIC PLAN on my website
An empty subscription for the basic plan linked to my user exist, okay cool
BUT now the user say "oh i think it's not enought for me, let's see other plan" he go back
And now he click on PREMIUM PLAN on my website
I create a new empty subscription but the coupon not come on this new subscription
While the first empty subscription is not paid and so i don't want to "apply" the coupon on a none used subscription
In that case, you may want to delete the original subscription before creating your new premium one
Unless the first payment has already been made, in which case you may want to do more of an upgrade. But that is up to you, you could also refund the original and do a new charge
And just delete the old incompleted subscription will "give back" the used coupon to the user ?
As far as I can see, the incomplete subscription that you provided didn't use up that customer's discount yet. It looks like it is still on the customer from the first time it was applied
i just recreated a customer with a coupon if you want we do step by step to see what happen
That could help. Can you go far enough to where you don't see the discount on them anymore and send me the ID?
Here is the id of my new customer : cus_NGW2dnuhfLG7Ke
I can see in the stripe dashboard his coupon on left
And for the moment, this code :
const customer = await this.stripeService.getClient().customers.retrieve(qualiobee.stripeCustomerId, {expand: ['discount']});
Return me a customer object with a discount :
discount: {
id: 'di_1MVz0jFpZdlKgOMyPR2FlWfF',
object: 'discount',
checkout_session: null,
coupon: [Object],
customer: 'cus_NGW2dnuhfLG7Ke',
end: null,
invoice: null,
invoice_item: null,
promotion_code: null,
start: 1675091469,
subscription: null
},
So i cant print to my user "hey you have a discount"
I let you do your checks, tell me when i can create the subscription
Yes I see the discount on that customer object
Ah I see, so it still lists a discount on the customer object on my side but I do see that it isn't in the dashboard anymore.
I think that would be the case, can you try deleting the subscription?
I delete it programmaticaly or by the dashboard ?
(at the end i will need be done by code)
Either should work here, dashboard should be fine for this test if that is faster
Ok i delete the subscription by code (that was fast to adapt my code)
And now
const customer = await this.stripeService.getClient().customers.retrieve(qualiobee.stripeCustomerId, {expand: ['discount']});
doesn't return me a discount
And the coupon is "lost"
Do you see it on your side ?
Still shows it on our side. I am trying this in my own code to see if the discount is truly gone or not, thanks for recreating that again
No, I think I get the steps well enough. I am doing this on my side
Thank you for the patience in clarifying this with me
Ah, my apologies, I was not looking at the data on my side correctly when I looked at this before. I do see that the coupon is considered to be used up once that subscription is created.
So because this is a one time coupon, it will show up on the subscription's first invoice here https://stripe.com/docs/api/invoices/object#invoice_object-discounts
Okay, so i just need when a user create a subscription, check if there is already a subscription with just a create invoiced (not finalized/paid) and get the coupon and create a copy
So you can check that to see if you need to apply the coupon again to the user when creating the other subscription
Exactly
Okay
That should make sure the coupon is applied there
I will try this