期末如何取消客户的条纹订阅?

时间:2020-02-06 08:54:02

标签: python python-3.x stripe-payments

我想要的功能是,如果有订阅的客户取消了他们的订阅,我想让他们使用我的服务,直到他们支付的本期末为止。

我要实现的计划是,当他们取消订阅时,将其订阅更新为在当前期间结束时取消。我目前正在尝试在python中实现代码,该代码将更新客户订阅的cancel_at_period_end属性,但似乎无法正常工作。这就是我所拥有的:

NaN

当我通过以下命令打印出cancel_at_period_end属性时,此方法运行良好:

stripe_customer_obj = stripe.Customer.retrieve(current_user.stripe_customer_id)
stripe_customer_subscription = stripe_customer_obj.subscriptions['data'][0]

stripe.Subscription.modify(
        stripe_customer_subscription['id'],
        metadata={"cancel_at_period_end": True},
    )

在修改订阅的代码前后,它为False。就像我修改订阅的那一行实际上并没有更新或保存,即使它正在运行并且没有崩溃。

如果相关(我认为不相关)-我正在尝试将cancel_at_period_end属性更新为True,以便在该期限结束时取消订阅时会触发一个Webhook,我可以使用它更新我的数据库中的客户条目,以便从应用程序中切断他们的访问权限。

1 个答案:

答案 0 :(得分:0)

代码的问题在于,您已将cancel_at_period_end包括在Subscription对象的metadata中。那只是一个简单的键->用于存储任意数据的值存储。

您只需要将cancel_at_period_end作为常规参数传递即可。像这样:

stripe.Subscription.modify(
  "sub_xxx",
  cancel_at_period_end=True  
)

希望这会有所帮助!