#MarkoBoras

1 messages · Page 1 of 1 (latest)

earnest anchorBOT
karmic frigate
#

👋 happy to help

#

you can upgrade the same subscription

#

instead of creating a new one

#

to give the customer the chance to manage their own subscription

zinc cradle
#

My user is subscribing from free to plus plan through striep checkout

#

And I am listening on checkout.session.completed on my webhook api endpoint

#

when I recieve event from stripe user already has 'Free' and 'Plus' plan subscribed

#

and I am currently doing this

#

export async function cancelFreePlanSubscription(customer: string) {
const subscriptionList = await getSubscriptionList(customer);
console.log('Subscription list', subscriptionList);

if (subscriptionList.data.length > 1) {
const oldestSubscription = subscriptionList.data.reduce((prev, curr) => {
return prev.current_period_start < curr.current_period_start
? prev
: curr;
});
console.log('Oldest subscription', oldestSubscription);
const oldSubscriptionPriceId = oldestSubscription.items.data[0].plan.id;
console.log('Old subscription price ID', oldSubscriptionPriceId);

if (oldSubscriptionPriceId === FREE_PRODUCT_PRICE_ID) {
  console.log('Deleting subscription', oldestSubscription.id);
  await stripe.subscriptions.del(oldestSubscription.id);
}

}
}

karmic frigate
#

that's why you're ending up with 2 subscriptions

#

have you checked the links that I've sent you?

zinc cradle
#

yeah

#

but I am solving everything through checkout and customer portal

karmic frigate
#

it's up to you then

zinc cradle
#

by default when user creates an account he is subscribed to free plan which is stripe product

when he wants to buy plus plan first time he is going on stripe checkout where I cancel free plan in webhook

when user wants to upgrade from monthly to yearly I am redirecting him to customer portal

#

I am not sure if that is the best flow

#

that's why I am asking

#

I want simplest and most safe solution

karmic frigate
#

that's not the best flow, I suggested another flow and you didn't agree

#

so I'm not really sure what I can do else here

zinc cradle
#

I am using stripe checkout for the first time payment because of this case

  • For EU users I need to set automatic_tax to true
  • For Non-EU users I need to charge them with '25%' tax of my own country

That are my country tax regulations. I am solving that with this

export const createCheckoutSession = region('europe-west2').https.onCall(
async ({ priceId, customerId, returnUrl }: CreateCheckoutSessionData) => {
try {
const currentCustomer = await findCustomerById(customerId);
const customerCountry = currentCustomer?.address?.country;
const isEuropeanUnionUser = EU_COUNTRIES.includes(customerCountry || '');

  const lineItem = !isEuropeanUnionUser
    ? {
        price: priceId,
        quantity: 1,
        tax_rates: [NON_EU_TAX_RATE],
      }
    : {
        price: priceId,
        quantity: 1,
      };

  const session: StripeCheckoutSession =
    await stripe.checkout.sessions.create({
      line_items: [lineItem],
      mode: 'subscription',
      customer: customerId,
      success_url: returnUrl,
      cancel_url: returnUrl,
      automatic_tax: { enabled: isEuropeanUnionUser ? true : false },
      tax_id_collection: {
        enabled: true,
      },
      customer_update: {
        name: 'auto',
      },
      phone_number_collection: {
        enabled: false,
      },
      locale: 'auto',
    });

  return onSuccess(session);
} catch (e) {
  return onError(JSON.stringify(e));
}

},
);

#

If I could achieve same with customer portal I would switch everything there

karmic frigate
#

you can create custom rates for each country, but you'd have to do one for each country

#

so I guess there's no easy way of doing it using the Customer Portal

#

but regardless

#

you can use the Subscription API to upgrade the subscription

#

without having to create another one