op_cart 0.3.0 → 0.3.1
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 +49 -25
- data/app/models/op_cart/order.rb +26 -12
- data/app/models/op_cart/shipping_address.rb +1 -0
- data/config/locales/op_cart.en.yml +8 -0
- data/db/migrate/20140829200318_create_op_cart_orders.rb +1 -0
- data/lib/op_cart/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e69269d8c9a1152b5369129c8c9d50713d4aabb7
|
4
|
+
data.tar.gz: 9dd208f2ca331e1e8ed47abdc4fec93c02849782
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53cd8e80758bfc5eec306927376e07f885eaec0cba12c648befeaf47bac85f6a64d251b109da038875006e66a3e9287933d50472a224581c33c1a40da0f1a45b
|
7
|
+
data.tar.gz: d3bcec702719717a4fd3859543b223af70d3dbfa95db1fae5fbcb6331124342cace0a285a20324ae43e007e49fd6d1c45362589d05670fd558a45a248f7b85ac
|
@@ -4,24 +4,27 @@ module OpCart
|
|
4
4
|
before_action :set_order, only: [:show, :edit, :update, :destroy]
|
5
5
|
|
6
6
|
def new
|
7
|
-
@products = Product.all
|
7
|
+
@products = Product.all
|
8
8
|
@order = Order.new
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
@user = current_user || User.new
|
10
|
+
@card = current_user.try(:customer).try :default_card
|
11
|
+
@shipping_address = @user.shipping_addresses.first || @user.shipping_addresses.new
|
12
12
|
end
|
13
13
|
|
14
14
|
def create
|
15
|
-
@
|
16
|
-
@
|
15
|
+
@order = Order.new order_params
|
16
|
+
@user = current_user || @order.user
|
17
17
|
add_user
|
18
18
|
add_line_items
|
19
19
|
add_shipping_address
|
20
20
|
|
21
|
-
if
|
21
|
+
if @order.save
|
22
|
+
sign_in @order.user
|
22
23
|
redirect_to @order, notice: 'Thank you for your purchase'
|
23
24
|
else
|
24
|
-
|
25
|
+
@products = Product.all
|
26
|
+
@user = User.new user_params unless signed_in?
|
27
|
+
@card = current_user.try(:customer).try :default_card
|
25
28
|
render :new
|
26
29
|
end
|
27
30
|
end
|
@@ -38,6 +41,7 @@ module OpCart
|
|
38
41
|
params.require(:order).permit :email, :password, :processor_token
|
39
42
|
end
|
40
43
|
|
44
|
+
|
41
45
|
def line_items_params
|
42
46
|
params.require(:line_items).permit :quantities_json
|
43
47
|
end
|
@@ -47,36 +51,56 @@ module OpCart
|
|
47
51
|
:locality, :region
|
48
52
|
end
|
49
53
|
|
54
|
+
def user_params
|
55
|
+
params.require(:user).permit :email, :password
|
56
|
+
end
|
57
|
+
|
50
58
|
def add_line_items
|
51
59
|
@line_items = OpenStruct.new(quantities_json: line_items_params[:quantities_json] || {})
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
60
|
+
if @line_items.quantities_json.present?
|
61
|
+
li_quantities_json = JSON.parse @line_items.quantities_json
|
62
|
+
li_quantities_json.each do |product_id, quantity|
|
63
|
+
@order.line_items <<
|
64
|
+
LineItem.new(sellable: Product.find(product_id), quantity: quantity)
|
65
|
+
end
|
56
66
|
end
|
57
67
|
end
|
58
68
|
|
59
69
|
def add_user
|
60
|
-
if
|
61
|
-
if user
|
62
|
-
|
70
|
+
if !signed_in? && !@order.user
|
71
|
+
if user = find_user
|
72
|
+
if valid_password? user
|
73
|
+
@order.user = @user = user
|
74
|
+
else
|
75
|
+
flash.now[:alert] = 'A user with that email already exists. Please sign in or pick another email.'
|
76
|
+
end
|
63
77
|
else
|
64
|
-
|
65
|
-
return false
|
78
|
+
@order.user = @user = new_user
|
66
79
|
end
|
80
|
+
else
|
81
|
+
@order.user = current_user
|
67
82
|
end
|
83
|
+
end
|
68
84
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
85
|
+
def find_user
|
86
|
+
User.find_by email: params[:user][:email]
|
87
|
+
end
|
88
|
+
|
89
|
+
def valid_password? user
|
90
|
+
user.valid_password? params[:user][:password]
|
91
|
+
end
|
92
|
+
|
93
|
+
def new_user
|
94
|
+
user = User.new(user_params.merge(
|
95
|
+
name: shipping_address_params[:full_name],
|
96
|
+
password_confirmation: user_params[:password]
|
97
|
+
))
|
98
|
+
user.valid? && user
|
75
99
|
end
|
76
100
|
|
77
101
|
def add_shipping_address
|
78
|
-
@order.shipping_address = @shipping_address
|
79
|
-
shipping_address_params.merge user_id:
|
102
|
+
@order.shipping_address = @shipping_address = ShippingAddress.new(
|
103
|
+
shipping_address_params.merge user_id: @user.try(:id)
|
80
104
|
)
|
81
105
|
end
|
82
106
|
end
|
data/app/models/op_cart/order.rb
CHANGED
@@ -11,11 +11,13 @@ module OpCart
|
|
11
11
|
validates :tax_amount, numericality:
|
12
12
|
{ only_integer: true, greater_than_or_equal_to: 0 }, if: :tax_amount?
|
13
13
|
validates :status, presence: true
|
14
|
+
validates :line_items, presence: true
|
15
|
+
validates :shipping_address, associated: true
|
14
16
|
validates :user, presence: true
|
15
17
|
|
16
18
|
before_validation :set_total
|
17
|
-
before_validation -> { self.status = :
|
18
|
-
before_save :charge_customer
|
19
|
+
before_validation -> { self.status = :pending }, unless: :status?
|
20
|
+
before_save :charge_customer, if: -> { self.status == 'pending' }
|
19
21
|
|
20
22
|
private
|
21
23
|
|
@@ -25,20 +27,32 @@ module OpCart
|
|
25
27
|
|
26
28
|
def charge_customer
|
27
29
|
customer = Customer.find_or_create_by user: user
|
28
|
-
|
30
|
+
if processor_token.present?
|
31
|
+
customer.update_card processor_token
|
32
|
+
processor_token = nil
|
33
|
+
end
|
29
34
|
|
30
|
-
|
31
|
-
|
32
|
-
charge = Stripe::Charge.create(
|
35
|
+
self.processor_response = Stripe::Charge.create(
|
33
36
|
amount: total,
|
34
37
|
currency: "usd",
|
35
38
|
customer: customer.processor_token
|
36
|
-
)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
).to_hash
|
40
|
+
self.status = :paid if processor_response['captured']
|
41
|
+
rescue Stripe::InvalidRequestError, Stripe::CardError => e
|
42
|
+
if e.try(:code) == 'card_declined'
|
43
|
+
self.status = :payment_declined
|
44
|
+
end
|
45
|
+
|
46
|
+
self.processor_response = {
|
47
|
+
customer_token: customer.processor_token, error: e.json_body
|
48
|
+
}
|
49
|
+
|
50
|
+
if e.message.include? "Stripe token more than once"
|
51
|
+
message = 'There was a problem with the request. Sorry about that. Try entering your card again.'
|
52
|
+
end
|
53
|
+
|
54
|
+
self.errors.add :card_error, (message || e.message)
|
55
|
+
false
|
42
56
|
end
|
43
57
|
end
|
44
58
|
end
|
@@ -4,6 +4,7 @@ class CreateOpCartOrders < ActiveRecord::Migration
|
|
4
4
|
t.integer :total, null: false
|
5
5
|
t.integer :tax_amount, default: 0
|
6
6
|
t.string :status, null: false
|
7
|
+
t.json :processor_response, null: false
|
7
8
|
t.references :shipping_address, index: true, null: false
|
8
9
|
t.references :user, index: true, null: false
|
9
10
|
|
data/lib/op_cart/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: op_cart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Boehs
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01
|
11
|
+
date: 2015-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|