【问题标题】:How to handle Stripe subscriptions based on Currency?如何处理基于货币的 Stripe 订阅?
【发布时间】:2019-09-01 06:23:07
【问题描述】:

我正在为我的 SAAS 应用使用 Stripe 支付网关。

我创建了一个产品,并且多个计划链接到它。计划价格以美元为单位。

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

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

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

在订阅之前我可以将美元兑换成客户的货币吗?

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    我的建议是,在附加订阅之前,检索您希望为其附加订阅的客户对象,确定客户的货币,并在必要时创建一个新计划(以货币计) .但要做到这一点,您可能需要使用第三方的货币转换 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}
        },
      ]
    )
    

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多