pythonpython-3.xstripe-paymentsstripe-taxstripe-sca

Stripe Checkout - Create Session - Apply Tax Rates on subscriptions


I'm trying to set up the new Stripe Checkout Create session. I'm not able to set the tax rate on the subscription during the session creation as the subscription is automatically created by Stripe.

I have set up a Tax rate on the dashboard with the default 20% VAT Rate. I want this applied automatically to all subscriptions. Can anybody guide me through this?

stripe.checkout.Session.create(
    payment_method_types=['card'],
    subscription_data={
        'items': [{
            'plan': plan.stripe_plan_name,
            'quantity': 1
        }],
    },
    customer_email = user.email,
    success_url='https://www.jetpackdata.com/success',
    cancel_url='https://www.jetpackdata.com/cancel'
)

And Picked by stripe.redirectToCheckout on the client-side.

I'm listening on the webhooks for 'checkout.session.completed' to upgrade the account on my backend.

I'm listening to 'invoice.created' and when the status=draft, I set the default tax rate (As we have an hour during which it can be modified after creation).

Should I listen to instead 'customer.subscription.created' and set it directly on the subscription instead of setting it on every invoice?

The first client subscription purchase doesn't seem to apply the tax rate as the status doesn't remain in draft for an hour as it does during the subscription cycle. Is it because I'm in Test mode?

Any help would be appreciated.


Solution

  • Reaching out to Stripe Technical support, i got this:

    "At the present moment, we don't currently have the ability to set a tax rate through Checkout, but it is a feature that is on our roadmap to be added in the future."

    So here's a workaround in the meanwhile for those who need to set taxes on Subscription with the new Stripe Checkout Session. The following outline will help you add a tax on your subscription right from the first invoice and the subsequent subscription invoices !

    1. Create a new customer and store the customer id on your backend:
    new_customer = stripe.Customer.create(
        email = user.email
    )
    
    1. Create an invoice items for your Tax on the subscription plan: (This will be automatically pulled into the first subscription plan)
    new_tax_invoice = stripe.InvoiceItem.create(
        customer=new_customer['id'],
        amount=int(plan.price*20),
        currency="eur",
        description="VAT"
    )
    
    1. Create a Stripe Session checkout and handover the stripe_session.id to stripe.redirectToCheckout on the client side
    stripe_session = stripe.checkout.Session.create(
        payment_method_types=['card'],
        subscription_data={
            'items': [{
            'plan': plan.stripe_plan_name,
            'quantity': 1
            }],
        },
        customer = new_customer['id'],
        success_url=app.config['STRIPE_SUCCESS_URL'],
        cancel_url=app.config['STRIPE_CANCEL_URL'],
    )
    
    1. Create a tax object on your Stripe Dashboard with your tax rate

    2. Listen to the Stripe Webhook for customer.subscription.created and update the subscription object with the default tax rates id that you got from step 4

    if stripe_webhook['type'] == 'customer.subscription.created':
        stripe.Subscription.modify(
            stripe_webhook['data']['object']['id'],
            default_tax_rates = [app.config['STRIPE_TAX_RATE']]
        )
    
    1. Listen to the Stripe Webhook for checkout.session.completed and do the necessary housekeeping on your backend with the stripe_subscription_id and stripe_customer_id