Class: StripeRails::Customer
- Inherits:
-
Object
- Object
- StripeRails::Customer
show all
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- app/models/stripe_rails/customer.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}, options = {}) ⇒ Customer
Returns a new instance of Customer.
26
27
28
29
|
# File 'app/models/stripe_rails/customer.rb', line 26
def initialize(attributes={}, options={})
attributes = { data: attributes.to_json.to_a.pack('m') } if self.new_record?
super(attributes,options)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'app/models/stripe_rails/customer.rb', line 51
def method_missing(meth, *args, &block)
if @customer.respond_to?(meth.to_sym)
value = @customer.send meth, *args
refresh_from @customer
value
elsif meth =~ /=$/ && %w(card= description= coupon= email=).include?(meth.to_s)
value = @customer.send meth, *args
refresh_from @customer
value
else
super
end
end
|
Class Method Details
.all(*args) ⇒ Object
All returns items from the stripe API
32
33
34
35
|
# File 'app/models/stripe_rails/customer.rb', line 32
def self.all(*args)
return super(nil) if args.first && args.first[:local] == true
Stripe::Customer.all(*args)
end
|
.find(id) ⇒ Object
Also known as:
retrieve
37
38
39
40
41
|
# File 'app/models/stripe_rails/customer.rb', line 37
def self.find(id)
customer = self.find_or_initialize_by(_id: id)
customer.save if customer.new_record?
customer
end
|
Instance Method Details
#charges ⇒ Object
16
17
18
|
# File 'app/models/stripe_rails/customer.rb', line 16
def charges
StripeRails::Charge.new(@customer)
end
|
#create_customer ⇒ Object
Using a callback so that we can call create and save on a new record
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'app/models/stripe_rails/customer.rb', line 93
def create_customer
params = {}
params['description'] = stripe_customer.stripe_description if stripe_customer.respond_to? :stripe_description
refresh_from Stripe::Customer.create(params)
if stripe_customer.respond_to? :stripe_subscription_plan
@customer.update_subscription({ plan: stripe_customer.stripe_subscription_plan })
refresh_from @customer
end
self._id = @customer.id
self.unit_price = stripe_customer.stripe_unit_price
save
end
|
#days_remaining_in_trial ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'app/models/stripe_rails/customer.rb', line 82
def days_remaining_in_trial
if subscription && subscription.status == 'trialing'
((Time.at(subscription.trial_end) - Time.now) / 24 / 60 / 60).ceil
elsif subscription
0
else
nil
end
end
|
#delete(options = {}) ⇒ Object
70
71
72
73
|
# File 'app/models/stripe_rails/customer.rb', line 70
def delete(options={})
@customer.delete
super(options)
end
|
#invoices ⇒ Object
22
23
24
|
# File 'app/models/stripe_rails/customer.rb', line 22
def invoices
StripeRails::Invoice.new(@customer)
end
|
#refresh! ⇒ Object
75
76
77
78
79
80
|
# File 'app/models/stripe_rails/customer.rb', line 75
def refresh!
@customer = Stripe::Customer.retrieve(id)
send :data=, @customer.to_json.to_a.pack('m')
save
self
end
|