如何处理基于货币的条纹订阅?

时间:2019-04-10 17:47:04

标签: stripe-payments

我正在为我的SAAS应用程序使用Stripe付款网关。

我已经创建了一个产品,并且有多个计划链接到该产品。套餐价格以美元为单位。

我已经创建了一个新客户。然后,我尝试订阅计划,但出现以下错误

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

我看过他们的文档,不允许我更改客户的币种。

无论如何,我可以在订阅之前将USD转换为客户的货币吗?

1 个答案:

答案 0 :(得分:0)

我的建议是,在附加订阅之前,检索要为其附加订阅的客户对象,确定客户的币种,并在必要时创建一个新计划(使用币种) 。但是,要执行此操作,您可能要使用第三方提供的货币换算API,因为Stripe不支持。

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}
    },
  ]
)