Class: StripeInvoice::Charge

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/stripe_invoice/charge.rb

Class Method Summary collapse

Instance Method Summary collapse

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



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/stripe_invoice/charge.rb', line 46

def self.create_from_stripe(stripe_charge)
  
  unless stripe_charge.paid
    return puts "[#{self.class.name}##{__method__.to_s}] ignoring unpaid charge #{stripe_charge.id}"
  end
   
  charge = Charge.find_by_stripe_id(stripe_charge[:id])
  
  # for existing invoices just update and be done
  if charge.present?
    puts "[#{self.class.name}##{__method__.to_s}] updating data for #{stripe_charge.id}"
    charge.update_attribute(:json, stripe_charge)
    return charge 
  end
  
  owner = get_subscription_owner stripe_charge
  unless owner
    puts "[#{self.class.name}##{__method__.to_s}] didn't find owner for #{stripe_charge.id}"
    return nil 
  end
  
  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

#billing_addressObject



23
24
25
# File 'app/models/stripe_invoice/charge.rb', line 23

def billing_address
  indifferent_json[:metadata][:billing_address] || owner.try(:billing_address)
end

#countryObject



32
33
34
# File 'app/models/stripe_invoice/charge.rb', line 32

def country
  indifferent_json[:metadata][:country] || owner.try(:country)
end

#datetimeObject



15
16
17
# File 'app/models/stripe_invoice/charge.rb', line 15

def datetime
  Time.at(date).to_datetime
end

#indifferent_jsonObject



11
12
13
# File 'app/models/stripe_invoice/charge.rb', line 11

def indifferent_json
 @json ||= json.with_indifferent_access 
end

#ownerObject



19
20
21
# File 'app/models/stripe_invoice/charge.rb', line 19

def owner
  @owner ||= Koudoku.owner_class.find(owner_id)
end

#refundsObject



36
37
38
# File 'app/models/stripe_invoice/charge.rb', line 36

def refunds
  indifferent_json[:refunds]
end

#tax_numberObject



27
28
29
# File 'app/models/stripe_invoice/charge.rb', line 27

def tax_number
  indifferent_json[:metadata][:tax_number] || owner.try(:tax_number)
end

#total_refundObject



40
41
42
# File 'app/models/stripe_invoice/charge.rb', line 40

def total_refund
  refunds.inject(0){ |total,refund| total + refund[:amount]}
end