op_cart 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/op_cart/orders_controller.rb +1 -1
- data/app/models/op_cart/card.rb +34 -0
- data/app/models/op_cart/customer.rb +45 -0
- data/app/models/op_cart/order.rb +4 -5
- data/db/migrate/20140829200318_create_op_cart_orders.rb +0 -1
- data/db/migrate/20150131000000_create_op_cart_customers.rb +12 -0
- data/db/migrate/20150131000001_create_op_cart_cards.rb +15 -0
- data/lib/op_cart/engine.rb +0 -6
- data/lib/op_cart/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4784222186a87a804618584469de28a04472da24
|
4
|
+
data.tar.gz: c2f44cdac74ff12e11461da43d66d3d4aba580a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e9d15cde05165f7850269b4b05f5a2385b3e74de9981306f217359ceb9b8061712185dcdb6113a4a4c9483aa07d33070d6a601bc7302b52fcb6b8c851908178
|
7
|
+
data.tar.gz: 4368bd5bf33eb1373ea83fd50976f1a19ae54a409d0ff9e79074b992574c386c6640dd639a492ea4c69e094933cd2042e151093c6db9ceaab56a37b8bbfae968
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module OpCart
|
2
|
+
class Card < ActiveRecord::Base
|
3
|
+
belongs_to :customer
|
4
|
+
before_validation :create_processor_object, on: :create
|
5
|
+
before_validation :update_local_object, on: :update
|
6
|
+
|
7
|
+
validates :processor_token, uniqueness: true, presence: true
|
8
|
+
validates :fingerprint, :brand, :last4, :exp_month, :exp_year, presence: true
|
9
|
+
validates :customer, presence: true
|
10
|
+
|
11
|
+
def processor_object
|
12
|
+
@processor_object ||= customer.processor_object.cards.retrieve processor_token
|
13
|
+
rescue Stripe::InvalidRequestError => e
|
14
|
+
raise e unless e.message.include? 'does not have card'
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def create_processor_object
|
19
|
+
if processor_object
|
20
|
+
update_local_object
|
21
|
+
else
|
22
|
+
update_local_object customer.processor_object.cards.create(card: processor_token)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_local_object card=processor_object
|
27
|
+
self.fingerprint ||= card.fingerprint
|
28
|
+
self.brand ||= card.brand
|
29
|
+
self.last4 ||= card.last4
|
30
|
+
self.exp_month ||= card.exp_month
|
31
|
+
self.exp_year ||= card.exp_year
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module OpCart
|
2
|
+
class Customer < ActiveRecord::Base
|
3
|
+
has_many :cards
|
4
|
+
belongs_to :default_card, class_name: 'OpCart::Card'
|
5
|
+
belongs_to :user
|
6
|
+
|
7
|
+
before_validation :create_customer, on: :create
|
8
|
+
before_validation :update_customer, on: :update
|
9
|
+
|
10
|
+
validates :processor_token, uniqueness: true, presence: true
|
11
|
+
validates :user, presence: true
|
12
|
+
|
13
|
+
def processor_object
|
14
|
+
@processor_object ||= Stripe::Customer.retrieve processor_token
|
15
|
+
end
|
16
|
+
|
17
|
+
def update_card token
|
18
|
+
processor_object.card = token
|
19
|
+
processor_object.save
|
20
|
+
if card = cards.find_by(fingerprint: processor_object.cards.first.try(:fingerprint))
|
21
|
+
card.update_attribute :processor_token, processor_object.default_card
|
22
|
+
else
|
23
|
+
card = cards.create processor_token: processor_object.default_card
|
24
|
+
end
|
25
|
+
update_attribute :default_card, card
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def create_customer
|
31
|
+
if processor_token
|
32
|
+
update_customer
|
33
|
+
else
|
34
|
+
self.processor_token = Stripe::Customer.create(email: user.email).id
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def update_customer
|
39
|
+
self.delinquent = processor_object.delinquent
|
40
|
+
processor_object.save email: user.email,
|
41
|
+
default_card: default_card.try(:processor_token)
|
42
|
+
self.default_card ||= Card.find_by processor_token: processor_object.default_card
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/app/models/op_cart/order.rb
CHANGED
@@ -4,6 +4,7 @@ module OpCart
|
|
4
4
|
belongs_to :shipping_address
|
5
5
|
belongs_to :user
|
6
6
|
|
7
|
+
attr_accessor :processor_token
|
7
8
|
accepts_nested_attributes_for :line_items
|
8
9
|
|
9
10
|
validates :total, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
@@ -23,17 +24,15 @@ module OpCart
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def charge_customer
|
26
|
-
customer =
|
27
|
-
|
28
|
-
email: user.email
|
29
|
-
)
|
27
|
+
customer = Customer.find_or_create_by user: user
|
28
|
+
customer.update_card processor_token
|
30
29
|
|
31
30
|
# TODO Create shipping address
|
32
31
|
|
33
32
|
charge = Stripe::Charge.create(
|
34
33
|
amount: total,
|
35
34
|
currency: "usd",
|
36
|
-
customer: customer.
|
35
|
+
customer: customer.processor_token
|
37
36
|
)
|
38
37
|
#TODO: Mark order as charged
|
39
38
|
# update_attribute :charged, true if charge.captured
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class CreateOpCartCustomers < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :op_cart_customers do |t|
|
4
|
+
t.string :processor_token
|
5
|
+
t.boolean :delinquent, default: false
|
6
|
+
t.references :default_card, index: true
|
7
|
+
t.references :user, index: true
|
8
|
+
|
9
|
+
t.timestamps null: false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateOpCartCards < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :op_cart_cards do |t|
|
4
|
+
t.string :processor_token, index: true
|
5
|
+
t.string :fingerprint, index: true
|
6
|
+
t.string :last4
|
7
|
+
t.string :brand
|
8
|
+
t.string :exp_month
|
9
|
+
t.string :exp_year
|
10
|
+
t.references :customer, index: true
|
11
|
+
|
12
|
+
t.timestamps null: false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/op_cart/engine.rb
CHANGED
data/lib/op_cart/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: op_cart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Boehs
|
@@ -78,6 +78,8 @@ files:
|
|
78
78
|
- Rakefile
|
79
79
|
- app/controllers/op_cart/application_controller.rb
|
80
80
|
- app/controllers/op_cart/orders_controller.rb
|
81
|
+
- app/models/op_cart/card.rb
|
82
|
+
- app/models/op_cart/customer.rb
|
81
83
|
- app/models/op_cart/line_item.rb
|
82
84
|
- app/models/op_cart/order.rb
|
83
85
|
- app/models/op_cart/product.rb
|
@@ -89,6 +91,8 @@ files:
|
|
89
91
|
- db/migrate/20140829200318_create_op_cart_orders.rb
|
90
92
|
- db/migrate/20140829201756_create_op_cart_line_items.rb
|
91
93
|
- db/migrate/20150125000000_create_op_cart_shipping_addresses.rb
|
94
|
+
- db/migrate/20150131000000_create_op_cart_customers.rb
|
95
|
+
- db/migrate/20150131000001_create_op_cart_cards.rb
|
92
96
|
- db/seeds.rb
|
93
97
|
- lib/op_cart.rb
|
94
98
|
- lib/op_cart/engine.rb
|