如果 Stripe 中的收费日期相同,则所有金额的总和 [英] Sum of all amount if charge dates are the same in Stripe
问题描述
我正在迭代 stripe charge object 我想求和每天增加金额
.
I'm iterating over a stripe charge object and I want to sum up the amount
total for each day.
# Returns an array object of charges for a customer
@customer_charges = Stripe::Charge.all(:customer => current_user.stripeid)
观看次数:
<% @customer_charges.map do |c| %>
On Monday, you were charged a total of <%= c.amount %>
<% end %>
当然以上只是输出每个费用的行,而不是当天的总和.我面临的困难是汇总每天的所有费用.有人能指出我正确的方向吗?
Surely the above does nothing more than output lines of each charges but not the sum for the day. The difficulty I face is to sum up all charges for each day. Could someone point me in the right direction?
输出如下:
"On Monday, you were charged a total of 200000"
"On Tueesday, you were charged a total of 500000"
etc...
代替:
On Monday, you were charged a total of 100000"
On Monday, you were charged a total of 100000"
etc...
我的 view
看起来很乱,有几行 if 语句
来比较日期,这看起来不正确.
My view
looks messy with lines of if statements
to compare dates and that does not look right.
推荐答案
您需要遍历 Stripe 中的每个费用对象,存储每次费用的金额和解析日期:
You'll need to iterate through each charge object in Stripe, storing the amount and the parsed date for each charge:
# Fetch charges in batches of 100 records from Stripe API, yield each individual charge to a block.
def each_stripe_charge_for_customer(customer_id)
starting_after = nil
loop do
customer_charges = Stripe::Charge.all(customer: customer_id, limit: 100, starting_after: starting_after)
break if customer_charges.none?
charges.each do |charge|
yield charge
end
starting_after = charges.data.last.id
end
end
charges_by_date = Hash.new(0)
# For each Stripe charge, store the date and amount into a hash.
each_stripe_charge_for_customer(current_user.stripeid) do |stripe_charge|
# Parses Stripe's timestamp to a Ruby date object. `to_date` converts a DateTime object to a date (daily resolution).
charge_date = Time.at(stripe_charge.created).to_date
charge_amount = stripe_charge.amount
charges_by_date[charge_date] += charge_amount
end
这篇关于如果 Stripe 中的收费日期相同,则所有金额的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!