Class: StripeInvoice::Charge
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- StripeInvoice::Charge
- Defined in:
- app/models/stripe_invoice/charge.rb
Class Method Summary collapse
-
.create_from_stripe(stripe_charge) ⇒ Object
builds the invoice from the stripe CHARGE object OR updates the existing invoice if an invoice for that id exists.
Instance Method Summary collapse
- #datetime ⇒ Object
- #indifferent_json ⇒ Object
- #owner ⇒ Object
- #refunds ⇒ Object
- #total_refund ⇒ Object
Class Method Details
.create_from_stripe(stripe_charge) ⇒ Object
builds the invoice from the stripe CHARGE object OR updates the existing invoice if an invoice for that id exists
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/stripe_invoice/charge.rb', line 33 def self.create_from_stripe(stripe_charge) charge = Charge.find_by_stripe_id(stripe_charge[:id]) unless stripe_charge.paid puts "ignoring unpaid charges" return end # for existing invoices just update and be done if charge.present? charge.update_attribute(:json, stripe_charge) return charge end owner = get_subscription_owner stripe_charge return unless owner stripe_invoice = Stripe::Invoice.retrieve stripe_charge[:invoice] last_charge = Charge.last new_charge_number = (last_charge ? (last_charge.id * 7) : 1).to_s.rjust(5, '0') charge_date = Time.at(stripe_charge[:created]).utc.to_datetime charge = Charge.create({ stripe_id: stripe_charge[:id], owner_id: owner.id, date: stripe_charge[:created], amount: stripe_charge[:amount], subtotal: stripe_invoice[:subtotal], discount: stripe_invoice[:discount], total: stripe_invoice[:total], currency: stripe_invoice[:currency], period_start: stripe_invoice[:period_start], period_end: stripe_invoice[:period_end], invoice_number: "#{charge_date.year}-#{new_charge_number}", json: stripe_charge }) puts "Charge saved: #{charge.id}" end |
Instance Method Details
#datetime ⇒ Object
15 16 17 |
# File 'app/models/stripe_invoice/charge.rb', line 15 def datetime Time.at(date).to_datetime end |
#indifferent_json ⇒ Object
11 12 13 |
# File 'app/models/stripe_invoice/charge.rb', line 11 def indifferent_json @json ||= json.with_indifferent_access end |
#owner ⇒ Object
19 20 21 |
# File 'app/models/stripe_invoice/charge.rb', line 19 def owner @owner ||= Koudoku.owner_class.find(owner_id) end |
#refunds ⇒ Object
23 24 25 |
# File 'app/models/stripe_invoice/charge.rb', line 23 def refunds indifferent_json[:refunds] end |
#total_refund ⇒ Object
27 28 29 |
# File 'app/models/stripe_invoice/charge.rb', line 27 def total_refund refunds.inject(0){ |total,refund| total + refund[:amount]} end |