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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79460d9add671f55638c9210167b3b10e899a37d
4
- data.tar.gz: f8146f320bf1c2a125b5e2096537caf35f4d6e90
3
+ metadata.gz: 4784222186a87a804618584469de28a04472da24
4
+ data.tar.gz: c2f44cdac74ff12e11461da43d66d3d4aba580a3
5
5
  SHA512:
6
- metadata.gz: a39e432d8b9022466da41141e96dfb0927cebe52a0311c6af20e29890a272b9fa2b51d2c9c7b8fe49a699bea8663fa6882524d80afea2a9e426024ec351bcbc9
7
- data.tar.gz: 9c7d2785dad800e8f0eedd88d4ff0ba8f6f252c95b2eab81faa7d082daf14c894a2b131c2bc602fbe325fcecc3f2832b1b9a113b59998415b63ed3c5fcf8fb3d
6
+ metadata.gz: 3e9d15cde05165f7850269b4b05f5a2385b3e74de9981306f217359ceb9b8061712185dcdb6113a4a4c9483aa07d33070d6a601bc7302b52fcb6b8c851908178
7
+ data.tar.gz: 4368bd5bf33eb1373ea83fd50976f1a19ae54a409d0ff9e79074b992574c386c6640dd639a492ea4c69e094933cd2042e151093c6db9ceaab56a37b8bbfae968
@@ -35,7 +35,7 @@ module OpCart
35
35
  end
36
36
 
37
37
  def order_params
38
- params.require(:order).permit :email, :password, :card_token
38
+ params.require(:order).permit :email, :password, :processor_token
39
39
  end
40
40
 
41
41
  def line_items_params
@@ -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
@@ -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 = Stripe::Customer.create(
27
- card: card_token,
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.id
35
+ customer: customer.processor_token
37
36
  )
38
37
  #TODO: Mark order as charged
39
38
  # update_attribute :charged, true if charge.captured
@@ -1,7 +1,6 @@
1
1
  class CreateOpCartOrders < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :op_cart_orders do |t|
4
- t.string :card_token, null: false
5
4
  t.integer :total, null: false
6
5
  t.integer :tax_amount, default: 0
7
6
  t.string :status, null: false
@@ -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
@@ -1,11 +1,5 @@
1
1
  module OpCart
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace OpCart
4
-
5
- initializer "op_cart.url_helpers" do
6
- ActiveSupport.on_load(:action_controller) do
7
- helper Rails.application.routes.url_helpers
8
- end
9
- end
10
4
  end
11
5
  end
@@ -1,3 +1,3 @@
1
1
  module OpCart
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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