Anirudh
Anirudh

Reputation: 3358

How to handle Stripe subscriptions based on Currency?

I'm using the Stripe payment gateway for my SAAS app.

I have created a Product and multiple Plans are linked to it. Plans price is in USD.

I have created a new customer. Then I'm trying to subscribe to a Plan, but I'm getting the below error

You cannot combine currencies on a single customer. This customer has had a subscription, coupon, or invoice item with currency inr

I have seen their documentation, it is not allowing me to change the Currency of the customer.

Is there anyway I can convert USD to the currency of the customer before subscribing?

Upvotes: 11

Views: 3949

Answers (1)

korben
korben

Reputation: 1146

My recommendation would be to, prior to attaching the subscription, retrieve the customer object for which you'd like to have a subscription attached, determine the currency of the customer, and create a new plan (in the currency) if necessary. To do this though, you'd probably want to use a currency conversion API from a third-party, as Stripe does not support that.

Example in Python:

plan_id = ...  # This would have been retrieved from your form, most likely  (eg. 'basic-plan')

usd_plan = stripe.Plan.retrieve(plan_id)

cus = stripe.Customer.retrieve()

if cus.currency is not 'usd':  # if the currency of the customer is not "usd"

  # create a new plan id for currency (eg. 'basic-plan-cad')
  plan_id = plan_id + '-' + cus.currency  # check if there is a 
  
  # use a 3rd party to get the currency
  amount_in_currency = amount * <API_CONVERSION_RATE>

  # check that the plan doesn't already exist and create it otherwise
  try:
    stripe.Plan.create(
      amount=amount_in_currency,
      interval=usd_plan.interval,
      product={
        "name": usd_plan.product
      },
      currency=cus.currency,
      id=plan_id
    )
  except Exception as e:  # this may fail if the plan already exists
    break

# create the subscription
sub = stripe.Subscription.create(
  customer="cus_xxx",
  items=[
    {
      "plan": plan_id,  # this will either be the `basic-plan` or `basic-plan-{currency}
    },
  ]
)

Upvotes: 1

Related Questions