Skip to content

Instantly share code, notes, and snippets.

@matthewarkin
Last active March 3, 2017 19:16
Show Gist options
  • Save matthewarkin/27bcfdf622eb91d3508ea082d0ad50b8 to your computer and use it in GitHub Desktop.
Save matthewarkin/27bcfdf622eb91d3508ea082d0ad50b8 to your computer and use it in GitHub Desktop.
proration date provided = 1488568488
proration date is just time.now
Returned invoice object:
{
"object": "invoice",
"amount_due": 9636,
"application_fee": null,
"attempt_count": 0,
"attempted": false,
"charge": null,
"closed": false,
"currency": "usd",
"customer": "cus_A6jH2UVlVgF8VO",
"date": 1488568489,
"description": null,
"discount": null,
"ending_balance": null,
"forgiven": false,
"lines": {
"object": "list",
"data": [
{
"id": "ii_19tLrZGR6JAGMQRJCJ43kVtv",
"object": "line_item",
"amount": -364,
"currency": "usd",
"description": "Unused time on monthly widgets after 03 Mar 2017",
"discountable": false,
"livemode": false,
"metadata": {
},
"period": {
"start": 1488568488,
"end": 1489449600
},
"plan": {
"id": "Commence-monthly-widgets-lpZb",
"object": "plan",
"amount": 1000,
"created": 1472840401,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"name": "monthly widgets",
"statement_descriptor": null,
"trial_period_days": null
},
"proration": true,
"quantity": 1,
"subscription": "sub_A6jHj3yBcHY8VR",
"subscription_item": "si_19mVp0GR6JAGMQRJUnchvbun",
"type": "invoiceitem"
},
{
"id": "sub_A6jHj3yBcHY8VR",
"object": "line_item",
"amount": 10000,
"currency": "usd",
"description": null,
"discountable": true,
"livemode": false,
"metadata": {
},
"period": {
"start": 1488568489,
"end": 1520104489
},
"plan": {
"id": "testyear",
"object": "plan",
"amount": 10000,
"created": 1427340983,
"currency": "usd",
"interval": "year",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"name": "yearly",
"statement_descriptor": null,
"trial_period_days": null
},
"proration": false,
"quantity": 1,
"subscription": null,
"subscription_item": "si_19mVp0GR6JAGMQRJUnchvbun",
"type": "subscription"
}
],
"has_more": false,
"total_count": 2,
"url": "/v1/invoices/upcoming/lines?customer=cus_A6jH2UVlVgF8VO&subscription=sub_A6jHj3yBcHY8VR&subscription_plan=testyear&subscription_proration_date=1488568488"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": 1488572089,
"paid": false,
"period_end": 1488568489,
"period_start": 1488568489,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"subscription": "sub_A6jHj3yBcHY8VR",
"subscription_proration_date": 1488568488,
"subtotal": 9636,
"tax": null,
"tax_percent": null,
"total": 9636,
"webhooks_delivered_at": null
}
require 'stripe'
Stripe.api_key = "test key"
(1..100).each do |i|
puts i
proration_date = Time.now.to_i
# See what the next invoice would look like with a plan switch
# and proration set:
invoice = Stripe::Invoice.upcoming(
:customer => "cus_A6jH2UVlVgF8VO",
:subscription => "sub_A6jHj3yBcHY8VR",
:subscription_plan => "testyear", # Switch to new plan
:subscription_proration_date => proration_date)
# Calculate the proration cost:
lines = invoice.lines.data[0..-1]#invoice.lines.data.size-2]
current_prorations = lines.select do |ii|
if ii.period.start != proration_date
puts "ERROR"
puts "=========================="
puts proration_date
puts invoice
puts "=========================="
end
ii.period.start == proration_date
end
cost = 0
current_prorations.each do |p|
cost += p.amount
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment