简体   繁体   English

何时在条带式计费周期结束时按月使用量收费

[英]When to charge for monthly usage at end of billing cycle in stripe

I have a subscription plan which costs $10/month per user plan + data "overage" charges.我有一个订阅计划,每个用户计划每月花费 10 美元 + 数据“超额”费用。 In other words, it is similar to (not unlimited) cell phone data plan.换句话说,它类似于(不是无限的)手机数据计划。

When should I add in the monthly data usage at the end of the cycle?我应该在什么时候添加周期结束时的每月数据使用量?

According to Subscription lifestyle documentation here , it says that the invoice.created event occurs roughly an hour before the invoice.payment_succeeded or charge.succeeded .根据认购生活方式文档在这里,它说, invoice.created前的事件发生大约一个小时invoice.payment_succeededcharge.succeeded However, it seems almost impossible to test this, as whenever an invoice is first created for a subscription, all the webhook events seem to fire simultaneously.但是,似乎几乎不可能对此进行测试,因为每当首次为订阅创建发票时,所有 webhook 事件似乎都会同时触发。 How should this be dealt with?这应该如何处理? Note that I only want to update the metered usage amount one time, at the end of the cycle.请注意,我只想在周期结束时更新metered usage amount一次。

My code is currently something like this:我的代码目前是这样的:

def stripe_webhook(request):

    if event_type == 'invoice.created':

        subscription_item_id = [item['subscription_item'] for item in stripe_data_obj['lines']['data'] if item['plan']['usage_type'] == 'metered'][0]

        data_usage = user.get_data_usage(start_date, end_date)
        usage = stripe.UsageRecord.create(
          quantity=data_usage,
          timestamp=int(time.time()),
          subscription_item=subscription_item_id,
          action = 'set'
        )

If you want to manually add line items to the invoice, then you can definitely test this.如果您想手动将行项目添加到发票中,那么您绝对可以对此进行测试。 The idea would be to put your customer on a trial period for a few minutes.这个想法是让您的客户试用几分钟。 The first invoice created is for the trial and you can ignore that one and all related events.创建的第一张发票用于试用,您可以忽略该事件和所有相关事件。 A few minutes later, when the trial period ends, a new invoice will be created and an invoice.created event will be sent to your endpoint.几分钟后,当试用期结束时,将创建一个新发票,并且invoice.created事件将发送到您的端点。 That one will allow you to modify the new invoice and add the extra amount/fees to charge this month.这将允许您修改新发票并添加本月收取的额外金额/费用。

To do this, you would use the Create Subscription API and pass the trial_end parameter as a unix timestamp representing a few minutes in the future.为此,您将使用 Create Subscription API并将trial_end参数作为代表未来几分钟的 unix 时间戳传递。

Separately though, if you really want to report over-usage, you could use metered billing and usage records as documented here: https://stripe.com/docs/billing/subscriptions/metered-billing另外,如果您真的想报告过度使用,您可以使用此处记录的计量计费和使用记录: https : //stripe.com/docs/billing/subscriptions/metered-billing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM