stripe_rails 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +1 -1
- data/app/models/stripe_rails/stripe_customer.rb +7 -0
- data/db/migrate/20120822045452_create_stripe_rails_stripe_customers.rb +7 -0
- data/lib/stripe_rails.rb +3 -3
- data/lib/stripe_rails/acts_as_customer.rb +22 -5
- data/lib/stripe_rails/cache.rb +25 -0
- data/lib/stripe_rails/engine.rb +0 -5
- data/lib/stripe_rails/helpers.rb +15 -0
- data/lib/stripe_rails/version.rb +1 -1
- metadata +13 -32
- data/app/models/stripe_rails/charge.rb +0 -27
- data/app/models/stripe_rails/customer.rb +0 -135
- data/app/models/stripe_rails/invoice.rb +0 -5
- data/db/migrate/20120608044024_create_stripe_rails_customers.rb +0 -10
- data/lib/stripe_rails/errors.rb +0 -45
- data/lib/stripe_rails/pci_compliance.rb +0 -5
data/README.textile
CHANGED
@@ -12,7 +12,7 @@ gem 'stripe_rails'
|
|
12
12
|
|
13
13
|
h3. Compatibility
|
14
14
|
|
15
|
-
The gem works with rails 3.2.x and mongoid
|
15
|
+
The gem works with rails 3.2.x and mongoid and active:record when using model relationships. It uses rails cache to cache the stripe object for 1 hour on subsequent get requests.
|
16
16
|
|
17
17
|
h2. Usage
|
18
18
|
|
data/lib/stripe_rails.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require "stripe_rails/
|
2
|
-
require "stripe_rails/
|
3
|
-
require "stripe_rails/errors"
|
1
|
+
require "stripe_rails/cache"
|
2
|
+
require "stripe_rails/helpers"
|
4
3
|
require "stripe_rails/autoload_const_defined"
|
4
|
+
require "stripe_rails/engine"
|
5
5
|
|
6
6
|
module StripeRails
|
7
7
|
end
|
@@ -2,11 +2,21 @@ module StripeRails::ActsAsCustomer
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
has_one :stripe, class_name: 'StripeRails::Customer', autobuild: true, dependent: :destroy, as: :stripe_customer
|
6
|
-
before_create :create_stripe_customer
|
7
5
|
|
8
|
-
|
9
|
-
|
6
|
+
# Mongoid
|
7
|
+
if self.respond_to?(:field)
|
8
|
+
field :stripe_customer_id
|
9
|
+
elsif self.kind_of(ActiveRecord::Base)
|
10
|
+
StripeRails::StripeCustomer.send :belongs_to, :stripe_customerable, class_name: self.to_s, polymorphic: true, dependent: :destroy
|
11
|
+
has_one :stripe_customer, class_name: 'StripeRails::StripeCustomer', as: :stripe_customerable
|
12
|
+
delegate :stripe_customer_id, :stripe_customer
|
13
|
+
else
|
14
|
+
raise "ORM is not supported, use ActiveRecord or Mongoid"
|
15
|
+
end
|
16
|
+
|
17
|
+
def stripe
|
18
|
+
Stripe::Customer.retrieve(stripe_customer_id) if stripe_customer_id
|
19
|
+
end
|
10
20
|
|
11
21
|
def self.stripe_description(method)
|
12
22
|
send :define_method, :stripe_description do
|
@@ -37,7 +47,14 @@ module StripeRails::ActsAsCustomer
|
|
37
47
|
end
|
38
48
|
|
39
49
|
def create_stripe_customer
|
40
|
-
|
50
|
+
return false if stripe
|
51
|
+
params = {}
|
52
|
+
params['description'] = stripe_description if respond_to? :stripe_description
|
53
|
+
self.stripe_customer_id = Stripe::Customer.create(params).id
|
54
|
+
|
55
|
+
#stripe.update_subscription(plan: stripe_subscription_plan }) if respond_to? :stripe_subscription_plan
|
56
|
+
|
57
|
+
save!
|
41
58
|
end
|
42
59
|
|
43
60
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Stripe
|
2
|
+
class Customer < APIResource
|
3
|
+
|
4
|
+
def refresh
|
5
|
+
|
6
|
+
key = Digest::MD5.hexdigest "#{url}_#{@api_key}_stripe"
|
7
|
+
|
8
|
+
value = Rails.cache.fetch key, expires_in: 1.hour, force: @force, compress: true do
|
9
|
+
@force = false
|
10
|
+
super.to_json
|
11
|
+
end
|
12
|
+
|
13
|
+
response = Util.symbolize_names Stripe::JSON.load value
|
14
|
+
|
15
|
+
refresh_from(response, @api_key)
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def refresh!
|
20
|
+
@force = true
|
21
|
+
refresh
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/stripe_rails/engine.rb
CHANGED
@@ -7,10 +7,5 @@ module StripeRails
|
|
7
7
|
config.autoload_paths += Dir["#{config.root}/lib**/"]
|
8
8
|
config.autoload_paths += Dir["#{config.root}/stripe_callbacks/**"]
|
9
9
|
|
10
|
-
initializer "Pci Compliance" do
|
11
|
-
::Stripe::Token.send(:include, Stripe::PciCompliance) if Rails.application.config.respond_to?(:stripe_pci_compliance) && Rails.application.config.stripe_pci_compliance
|
12
|
-
Rails.application.config.autoload_paths += Dir["#{Rails.root.to_s}/app/stripe_callbacks/**"]
|
13
|
-
end
|
14
|
-
|
15
10
|
end
|
16
11
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Stripe
|
2
|
+
class Customer < APIResource
|
3
|
+
|
4
|
+
def days_remaining_in_trial
|
5
|
+
if subscription && subscription.status == 'trialing'
|
6
|
+
((Time.at(subscription.trial_end) - Time.now) / 24 / 60 / 60).ceil
|
7
|
+
elsif subscription
|
8
|
+
0
|
9
|
+
else
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/lib/stripe_rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stripe_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: jquery-rails
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,22 +59,6 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: mongoid
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ~>
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: 3.0.0.rc
|
70
|
-
type: :runtime
|
71
|
-
prerelease: false
|
72
|
-
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
|
-
requirements:
|
75
|
-
- - ~>
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version: 3.0.0.rc
|
78
62
|
- !ruby/object:Gem::Dependency
|
79
63
|
name: rspec-rails
|
80
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,8 +76,8 @@ dependencies:
|
|
92
76
|
- !ruby/object:Gem::Version
|
93
77
|
version: '0'
|
94
78
|
description: Stripe Rails was built on top of the official stripe gem to bring ease
|
95
|
-
of use of stripe in your models. The gem also
|
96
|
-
so that
|
79
|
+
of use of stripe in your models. The gem also uses your applications cache to store
|
80
|
+
objects locally so that you dont have to continuously hit Stripes API, it wont have
|
97
81
|
to reach the stripe API on every page load. Lastly, this gem has built in responses
|
98
82
|
for stripe webhooks/callbacks.
|
99
83
|
email:
|
@@ -110,21 +94,19 @@ files:
|
|
110
94
|
- app/controllers/stripe_rails/callbacks_controller.rb
|
111
95
|
- app/helpers/stripe_rails/application_helper.rb
|
112
96
|
- app/helpers/stripe_rails/callbacks_helper.rb
|
113
|
-
- app/models/stripe_rails/
|
114
|
-
- app/models/stripe_rails/customer.rb
|
115
|
-
- app/models/stripe_rails/invoice.rb
|
97
|
+
- app/models/stripe_rails/stripe_customer.rb
|
116
98
|
- app/stripe_callbacks/ping_callback.rb
|
117
99
|
- app/views/layouts/stripe_rails/application.html.erb
|
118
100
|
- config/routes.rb
|
119
|
-
- db/migrate/
|
101
|
+
- db/migrate/20120822045452_create_stripe_rails_stripe_customers.rb
|
120
102
|
- lib/generators/stripe_callback/stripe_callback_generator.rb
|
121
103
|
- lib/generators/stripe_callback/USAGE
|
122
104
|
- lib/stripe_rails/acts_as_customer.rb
|
123
105
|
- lib/stripe_rails/autoload_const_defined.rb
|
106
|
+
- lib/stripe_rails/cache.rb
|
124
107
|
- lib/stripe_rails/callback.rb
|
125
108
|
- lib/stripe_rails/engine.rb
|
126
|
-
- lib/stripe_rails/
|
127
|
-
- lib/stripe_rails/pci_compliance.rb
|
109
|
+
- lib/stripe_rails/helpers.rb
|
128
110
|
- lib/stripe_rails/version.rb
|
129
111
|
- lib/stripe_rails.rb
|
130
112
|
- lib/tasks/stripe_rails_tasks.rake
|
@@ -151,10 +133,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
133
|
version: '0'
|
152
134
|
requirements: []
|
153
135
|
rubyforge_project:
|
154
|
-
rubygems_version: 1.8.
|
136
|
+
rubygems_version: 1.8.23
|
155
137
|
signing_key:
|
156
138
|
specification_version: 3
|
157
139
|
summary: Stripe Rails was built on top of the official stripe gem for easier integration
|
158
140
|
into a rails project.
|
159
141
|
test_files: []
|
160
|
-
has_rdoc:
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module StripeRails
|
2
|
-
class Charge
|
3
|
-
|
4
|
-
def initialize(customer)
|
5
|
-
@customer = customer
|
6
|
-
raise Stripe::InvalidObjectError, 'Object is not a stripe customer!' unless @customer.kind_of? Stripe::Customer
|
7
|
-
end
|
8
|
-
|
9
|
-
def all(options = {})
|
10
|
-
Stripe::Charge.all(options.merge({ customer: @customer.id }))
|
11
|
-
end
|
12
|
-
|
13
|
-
def create(amount, options = {})
|
14
|
-
Stripe::Charge.create(options.merge({ amount: amount, customer: @customer.id }))
|
15
|
-
end
|
16
|
-
|
17
|
-
def retrieve(id)
|
18
|
-
charge = Stripe::Charge.retrieve(id)
|
19
|
-
raise Stripe::ChargeAssociationError, 'The charge does not belong to this customer!' unless charge.customer == @customer.id
|
20
|
-
end
|
21
|
-
|
22
|
-
def refund(id)
|
23
|
-
retrieve(id).refund
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
@@ -1,135 +0,0 @@
|
|
1
|
-
module StripeRails
|
2
|
-
class Customer
|
3
|
-
|
4
|
-
# Support for mongoid
|
5
|
-
include Mongoid::Document
|
6
|
-
include Mongoid::Timestamps
|
7
|
-
|
8
|
-
store_in collection: "stripe_customers"
|
9
|
-
|
10
|
-
field :data, type: String, default: -> { nil }
|
11
|
-
field :unit_price, type: Float
|
12
|
-
field :_id, type: String, default: -> { nil }
|
13
|
-
|
14
|
-
attr_readonly :_id
|
15
|
-
|
16
|
-
def charges
|
17
|
-
StripeRails::Charge.new(@customer)
|
18
|
-
end
|
19
|
-
|
20
|
-
ActionDispatch::Session::CookieStore
|
21
|
-
|
22
|
-
def invoices
|
23
|
-
StripeRails::Invoice.new(@customer)
|
24
|
-
end
|
25
|
-
|
26
|
-
def initialize(attributes={}, options={})
|
27
|
-
attributes = { data: attributes.to_json.to_a.pack('m') } if self.new_record?
|
28
|
-
super(attributes,options)
|
29
|
-
end
|
30
|
-
|
31
|
-
# All returns items from the stripe API
|
32
|
-
def self.all(*args)
|
33
|
-
return super(nil) if args.first && args.first[:local] == true
|
34
|
-
Stripe::Customer.all(*args)
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.find(id)
|
38
|
-
customer = self.find_or_initialize_by(_id: id)
|
39
|
-
customer.save if customer.new_record?
|
40
|
-
customer
|
41
|
-
end
|
42
|
-
|
43
|
-
# Privatize methods that can potential break caching and updating
|
44
|
-
class << self
|
45
|
-
alias :retrieve :find
|
46
|
-
private :find_by
|
47
|
-
end
|
48
|
-
|
49
|
-
private :attributes=, :update_attributes, :update_attributes!
|
50
|
-
|
51
|
-
def method_missing(meth, *args, &block)
|
52
|
-
if @customer.respond_to?(meth.to_sym)
|
53
|
-
value = @customer.send meth, *args
|
54
|
-
refresh_from @customer
|
55
|
-
value
|
56
|
-
elsif meth =~ /=$/ && %w(card= description= coupon= email=).include?(meth.to_s)
|
57
|
-
value = @customer.send meth, *args
|
58
|
-
refresh_from @customer
|
59
|
-
value
|
60
|
-
else
|
61
|
-
super
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# Callbacks
|
66
|
-
after_initialize :stripe_read
|
67
|
-
before_update :stripe_update
|
68
|
-
before_destroy :stripe_destroy
|
69
|
-
|
70
|
-
def delete(options={})
|
71
|
-
@customer.delete
|
72
|
-
super(options)
|
73
|
-
end
|
74
|
-
|
75
|
-
def refresh!
|
76
|
-
@customer = Stripe::Customer.retrieve(id)
|
77
|
-
send :data=, @customer.to_json.to_a.pack('m')
|
78
|
-
save
|
79
|
-
self
|
80
|
-
end
|
81
|
-
|
82
|
-
def days_remaining_in_trial
|
83
|
-
if subscription && subscription.status == 'trialing'
|
84
|
-
((Time.at(subscription.trial_end) - Time.now) / 24 / 60 / 60).ceil
|
85
|
-
elsif subscription
|
86
|
-
0
|
87
|
-
else
|
88
|
-
nil
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
# Using a callback so that we can call create and save on a new record
|
93
|
-
def create_customer
|
94
|
-
params = {}
|
95
|
-
params['description'] = stripe_customer.stripe_description if stripe_customer.respond_to? :stripe_description
|
96
|
-
refresh_from Stripe::Customer.create(params)
|
97
|
-
|
98
|
-
if stripe_customer.respond_to? :stripe_subscription_plan
|
99
|
-
@customer.update_subscription({ plan: stripe_customer.stripe_subscription_plan })
|
100
|
-
refresh_from @customer
|
101
|
-
end
|
102
|
-
|
103
|
-
self._id = @customer.id
|
104
|
-
self.unit_price = stripe_customer.stripe_unit_price
|
105
|
-
|
106
|
-
save
|
107
|
-
end
|
108
|
-
|
109
|
-
private
|
110
|
-
|
111
|
-
def refresh_from(stripe_object)
|
112
|
-
raise Stripe::InvalidObjectError, "Is not a stripe customer!" unless stripe_object.kind_of? Stripe::Customer
|
113
|
-
@customer = stripe_object
|
114
|
-
send :data=, @customer.to_json.to_a.pack('m')
|
115
|
-
self
|
116
|
-
end
|
117
|
-
|
118
|
-
def stripe_read
|
119
|
-
if !new_record? && updated_at && updated_at < 1.day.ago
|
120
|
-
self.refresh!
|
121
|
-
else
|
122
|
-
@customer = Stripe::Customer.construct_from(JSON.parse data.unpack('m').first ) if data && data.unpack('m').first.present?
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def stripe_update
|
127
|
-
refresh_from @customer.save
|
128
|
-
end
|
129
|
-
|
130
|
-
def stripe_destroy
|
131
|
-
@customer.delete
|
132
|
-
end
|
133
|
-
|
134
|
-
end
|
135
|
-
end
|
data/lib/stripe_rails/errors.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
module Stripe
|
2
|
-
|
3
|
-
class PciComplianceError < StandardError
|
4
|
-
def initialize(msg)
|
5
|
-
super
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class InvalidObjectError < StandardError
|
10
|
-
def initialize(msg)
|
11
|
-
super
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class CollectionInvalidError < StandardError
|
16
|
-
def initialize(msg)
|
17
|
-
super
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class PriceInvalidError < StandardError
|
22
|
-
def initialize(msg)
|
23
|
-
super
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class ChargeAssociationError < StandardError
|
28
|
-
def initialize(msg)
|
29
|
-
super
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class InvalidEventError < StandardError
|
34
|
-
def initialize(msg='Event is invalid!')
|
35
|
-
super
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
class InvalidCallbackResponseError < StandardError
|
40
|
-
def initialize(msg='Response must be a hash!')
|
41
|
-
super
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|