如何使用Stripe API计算按比例分配的数量?

时间:2016-08-16 15:41:44

标签: ruby

我正在使用Stripe。我想知道如何计算按比例分配的天数

我希望显示类似的东西

  增加1个座位(每个月9美元 - 按比例分配26天)

在api中我没有看到任何项目prorate_day

博洛

1 个答案:

答案 0 :(得分:0)

subscription_proration_date您在寻找什么?然后它会为你计算它。

https://stripe.com/docs/subscriptions/guide

了解详情

ruby​​中按比例订阅的示例如下

# Set your secret key: remember to change this to your live secret key in production
# See your keys here https://dashboard.stripe.com/account/apikeys
Stripe.api_key = "sk_test_9OkpsFpKa1HDHaZa7e0BeGaO"

proration_date = Time.now.to_i
invoice = Stripe::Invoice.upcoming(:customer => "cus_3R1W8PG2DmsmM9", :subscription => "sub_3R3PlB2YlJe84a",
                                   :subscription_plan => "premium_monthly", :subscription_proration_date => proration_date)
current_prorations = invoice.lines.data.select { |ii| ii.period.start == proration_date }
cost = 0
current_prorations.each do |p|
  cost += p.amount
end

# Display the cost of these prorations invoice items to the end user,
# and actually do the update when they agree.
# To make sure that the proration is calculated the same as when it was previewed,
# you need to pass in the proration_date parameter

# later...

subscription = Stripe::Subscription.retrieve("sub_3R3PlB2YlJe84a")
subscription.plan = "premium_monthly"
subscription.proration_date = proration_date
subscription.save