0

I have two plans, pro and basic.

When I registering on my app, it creates pro(trial) plan on chrgebee by default. When it expires, plan changes to basic, but I have button to change plan to pro, and when it calls function upgrade to pro, it creates pro(trial) plan again. But I need to have pro(active) plan.

Please, help me

export const updateSubscription = (app: app.App) => async (
    data: { priceId: string; planId: string; currencyCode: string; paymentIntent?: PaymentIntent },
    context: Partial<CallableContext>
) => {
    if (!context.auth || context.auth.token.phone_number === undefined || context.auth.token.phone_number === null) {
        throw new https.HttpsError('failed-precondition', 'The function must be called while authenticated.')
    }

    const phoneNumber = context.auth.token.phone_number
    const planSnap = await app.firestore().collection('plans').doc(data.planId).get()
    const plan = planSnap.data()
    const { subscription, organization } = await getSubscription(app, phoneNumber)
    const trialEndMonths = data.priceId.includes('basic') ? 150 : 1
    const endOfTerm = data.priceId.includes('basic') ? true : false
    const metadata = {
        features: plan?.features || [],
        organization: organization.key,
        subscription: subscription
    }

    return await accountingApi['subscription.update']({
        subscription,
        item: { priceId: data.priceId, quantity: organization.areasSyncedTotal || 1 },
        paymentIntent: data.paymentIntent,
        metadata,
        trialEnd: moment().startOf('day').add(trialEndMonths, 'month').unix(),
        endOfTerm
    })
}


'subscription.update': async ({
        subscription,
        item,
        metadata,
        trialEnd,
        paymentIntent,
        endOfTerm
    }: {
        subscription: string
        item: { priceId: string; quantity: number }
        metadata?: object
        trialEnd?: number
        paymentIntent?: PaymentIntent
        endOfTerm?: boolean
    }) => {
        const data = {
            subscription_items: [{ item_price_id: item.priceId, quantity: item.quantity }],
            payment_intent: { id: paymentIntent?.id },
            trial_end: trialEnd,
            end_of_term: endOfTerm
        } as _subscription.update_for_items_params
        console.log(data, 'data')

        if (metadata) {
            data.meta_data = metadata
        }
        return api['subscription.update'](subscription, data)
    },

I'have tried everything

Florent M.
  • 1,107
  • 1
  • 6
  • 14
  • I don't know chargebee, but you can refer to Stripe guide for upgrading/downgrading Subscription, if that could help you: https://stripe.com/docs/billing/subscriptions/upgrade-downgrade – os4m37 Jul 05 '23 at 09:26

0 Answers0