如何处理基于货币的 Stripe 订阅?
Posted
技术标签:
【中文标题】如何处理基于货币的 Stripe 订阅?【英文标题】:How to handle Stripe subscriptions based on Currency? 【发布时间】: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
我看过他们的文档,它不允许我更改客户的货币。
在订阅之前我可以将美元兑换成客户的货币吗?
【问题讨论】:
【参考方案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
,
]
)
【讨论】:
以上是关于如何处理基于货币的 Stripe 订阅?的主要内容,如果未能解决你的问题,请参考以下文章