Python changin/更新条带订阅(django)的问题
目前,我在我的网站上有一个可用的订阅的代码。代码检查用户是否已经有计划,如果没有,则运行else语句(工作正常),如果有,则用新订阅替换用于更新其当前订阅的代码。(不工作) 当我尝试将用户订阅更新为新订阅时,出现以下运行时错误:Python changin/更新条带订阅(django)的问题,python,django,stripe-payments,stripe-api,Python,Django,Stripe Payments,Stripe Api,目前,我在我的网站上有一个可用的订阅的代码。代码检查用户是否已经有计划,如果没有,则运行else语句(工作正常),如果有,则用新订阅替换用于更新其当前订阅的代码。(不工作) 当我尝试将用户订阅更新为新订阅时,出现以下运行时错误: Request req_kK2v51jnhuuKsW: Cannot add multiple subscription items with the same plan: price_HHU1Y81pU1wrNp 我正在测试的帐户的计划与我尝试更新的计划不同。(此代
Request req_kK2v51jnhuuKsW: Cannot add multiple subscription items with the same plan: price_HHU1Y81pU1wrNp
我正在测试的帐户的计划与我尝试更新的计划不同。(此代码用于基本计划,该帐户已启用标准计划)
非常感谢您的帮助
编辑:这是模型数据,我尝试将所有“无”值更改为其他值,以查看是否会更改错误,但没有
SUB_PLANS = [
('None','None'),
('B','Basic Plan'),
('S', 'Standard Plan'),
('P', 'Premium Plan'),
]
GENRE_CHOICES = [
('1','None'),
('2','Adventure'),
('3','Action'),
('4','Puzzle'),
('5','Story Based'),
]
# Create your models here.
class Profile(models.Model):
User = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
username = models.CharField(max_length=30)
#where games are sent
inbox = models.EmailField(max_length = 50)
current_plan = models.CharField(max_length = 4, choices = SUB_PLANS, default='None')
#genres they like
genre_one = models.CharField(max_length = 20, choices = GENRE_CHOICES, default = 'None')
genre_two = models.CharField(max_length = 20, choices = GENRE_CHOICES, default = 'None')
genre_three = models.CharField(max_length = 20, choices = GENRE_CHOICES, default = 'None')
subscription_id = models.CharField(max_length = 40, default="None")
customer_id = models.CharField(max_length = 40, default = "None")
'''on account creation plan == null, then once they buy one,
plan is added as a dropdown that they can edit easy'''
def __str__(self):
return self.username
这里的诀窍是,如果要更新已经有单个项的现有订阅,则需要在更新时传递该SubscriptionItem的ID,以便API知道您没有尝试使用相同的计划添加第二个SubscriptionItem 根据该错误消息,该计划似乎已在该订阅上更新,但可能不是您期望的方式。如果订阅是从“标准”计划开始的,那么执行了上面的代码,它很可能在现有标准之外添加了“基本”计划。我打赌他们现在订阅了
basic
和standard
。为了达到预期效果,您需要删除标准SubscriptionItem并添加基本SubscriptionItem,我将在下面展示其代码。或者,您可以直接交换计划
请注意,如果每个订阅有多个计划,则需要修改此示例以找到正确的SubscriptionItem ID。以下是一种方法:
current_subscription = stripe.Subscription.retrieve(user_info.subscription_id)
new_plan = stripe.Subscription.modify(
user_info.subscription_id,
cancel_at_period_end=True,
proration_behavior='create_prorations',
#the new subscription
items=[{
'id': current_subscription['items'].data[0].id, # note if you have more than one Plan per Subscription, you'll need to improve this. This assumes one plan per sub.
'deleted': True,
}, {
'plan': 'price_HHU1Y81pU1wrNp'
}]
)
感谢您的详细回复!我现在明白我错在哪里了:)
current_subscription = stripe.Subscription.retrieve(user_info.subscription_id)
new_plan = stripe.Subscription.modify(
user_info.subscription_id,
cancel_at_period_end=True,
proration_behavior='create_prorations',
#the new subscription
items=[{
'id': current_subscription['items'].data[0].id, # note if you have more than one Plan per Subscription, you'll need to improve this. This assumes one plan per sub.
'deleted': True,
}, {
'plan': 'price_HHU1Y81pU1wrNp'
}]
)