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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/stripe_invoice/charge.rb', line 86

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

#balance_transaction_currencyObject



63
64
65
# File 'app/models/stripe_invoice/charge.rb', line 63

def balance_transaction_currency
  self.balance_transaction_object[:currency]
end

#balance_transaction_objectObject



55
56
57
# File 'app/models/stripe_invoice/charge.rb', line 55

def balance_transaction_object
  @btobj ||= Stripe::BalanceTransaction.retrieve balance_transaction_sid
end

#balance_transaction_sidObject



59
60
61
# File 'app/models/stripe_invoice/charge.rb', line 59

def balance_transaction_sid
  self.indifferent_json[:balance_transaction]
end

#balance_transaction_total_less_refundsObject



67
68
69
70
71
72
# File 'app/models/stripe_invoice/charge.rb', line 67

def balance_transaction_total_less_refunds
  result = (balance_transaction_object[:amount] - self.total_refund_transaction)
  puts "[#{self.class.name}##{__method__.to_s}] result for #{id}-#{tax_number}: #{result}"
  
  result
end

#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

#source_countryObject

the country the source is registered in



75
76
77
78
79
80
81
82
# File 'app/models/stripe_invoice/charge.rb', line 75

def source_country
  # source can only be accessed via Customer
  cus = Stripe::Customer.retrieve self.indifferent_json[:customer]
  # TODO this is wrong, because there might be multiple sources and I 
  # just randomly select the first one - also some source types might NOT even
  # have a country information
  cus.sources.data[0].country
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_less_refundsObject



51
52
53
# File 'app/models/stripe_invoice/charge.rb', line 51

def total_less_refunds
  total - total_refund
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

#total_refund_transactionObject



44
45
46
47
48
49
# File 'app/models/stripe_invoice/charge.rb', line 44

def total_refund_transaction
  refunds.inject(0) do |total, refund|
    bt = Stripe::BalanceTransaction.retrieve(refund[:balance_transaction])
    total + bt[:amount]
  end
end