#Kobweb

1 messages · Page 1 of 1 (latest)

inner ironBOT
proud geyser
#

Hi! Let me help you with this.

#

Is this a one-time coupon?

hasty flare
#

Yes, when a user singup by an affiliate link, we create a coupon and attribute it to the new customer

proud geyser
#

The easiest way is to create a new coupon.

hasty flare
#

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 ?

proud geyser
#

Yes.

hasty flare
#

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)

proud geyser
#

How do you add a Coupon to a Customer in the first place?

hasty flare
#

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)

proud geyser
#

And when you try to re-create it you run the same code?

hasty flare
#

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

proud geyser
#

Sorry for the wait.

hasty flare
#

No problems

proud geyser
#

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.

hasty flare
#

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

proud geyser
#

You can check in your first Subscription Invoice if the Coupon was used.

hasty flare
#

Yes coupon are "used"

#

But why stripe api return no "discount" ?

hot pelican
#

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

hasty flare
#

there is one of my coupon

hot pelican
#

Okay, that should work somewhere. Where are you not seeing this coupon that you expect to see it?

hasty flare
#

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 ?

hot pelican
#

Do you have the ID of one of the subscriptions that you created this way (sub_123)?

hasty flare
#

Here is one : sub_1MVxaWFpZdlKgOMylWKex9Np

#

Hum wait

#

Probably not created with the coupon, here is one more sure : sub_1MVxFaFpZdlKgOMyg4hGfEs2

hot pelican
#

Thanks for the IDs, checking in to them

#

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?

hasty flare
#

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

hot pelican
#

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

hasty flare
hot pelican
#

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

hasty flare
#

i just recreated a customer with a coupon if you want we do step by step to see what happen

hot pelican
#

That could help. Can you go far enough to where you don't see the discount on them anymore and send me the ID?

hasty flare
#

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

hot pelican
#

Yes I see the discount on that customer object

hasty flare
#

Okay i will create a new empty subscription

#

Done

hot pelican
#

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?

hasty flare
#

I delete it programmaticaly or by the dashboard ?

#

(at the end i will need be done by code)

hot pelican
#

Either should work here, dashboard should be fine for this test if that is faster

hasty flare
#

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 ?

hot pelican
#

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

hasty flare
#

You want i re-do all the process ?

#

Recreate new user, etc.. ?

hot pelican
#

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

hasty flare
#

No problem

#

I remain at your disposal

hot pelican
#

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.

hasty flare
#

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

hot pelican
#

So you can check that to see if you need to apply the coupon again to the user when creating the other subscription

#

Exactly

hasty flare
#

Okay

hot pelican
#

That should make sure the coupon is applied there

hasty flare
#

I will try this