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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4784222186a87a804618584469de28a04472da24
4
- data.tar.gz: c2f44cdac74ff12e11461da43d66d3d4aba580a3
3
+ metadata.gz: e69269d8c9a1152b5369129c8c9d50713d4aabb7
4
+ data.tar.gz: 9dd208f2ca331e1e8ed47abdc4fec93c02849782
5
5
  SHA512:
6
- metadata.gz: 3e9d15cde05165f7850269b4b05f5a2385b3e74de9981306f217359ceb9b8061712185dcdb6113a4a4c9483aa07d33070d6a601bc7302b52fcb6b8c851908178
7
- data.tar.gz: 4368bd5bf33eb1373ea83fd50976f1a19ae54a409d0ff9e79074b992574c386c6640dd639a492ea4c69e094933cd2042e151093c6db9ceaab56a37b8bbfae968
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 #TODO: Move to find_products
7
+ @products = Product.all
8
8
  @order = Order.new
9
- if signed_in?
10
- @shipping_address = current_user.shipping_addresses.first
11
- end
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
- @products = Product.all
16
- @order = Order.new order_params #TODO: Move to set_order
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 signed_in? && @order.save
21
+ if @order.save
22
+ sign_in @order.user
22
23
  redirect_to @order, notice: 'Thank you for your purchase'
23
24
  else
24
- # TODO Set flash w/ error
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
- li_quantities_json = JSON.parse @line_items.quantities_json
53
- li_quantities_json.each do |product_id, quantity|
54
- @order.line_items <<
55
- LineItem.new(sellable: Product.find(product_id), quantity: quantity)
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 current_user.blank? && user = User.find_by(email: params[:user][:email])
61
- if user.valid_password? params[:user][:password]
62
- sign_in user
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
- flash[:alert] = 'A user with that email already exists. Please sign in or pick another email.'
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
- unless signed_in?
70
- sign_in User.create name: params[:shipping_address][:full_name],
71
- email: params[:user][:email], password: params[:user][:password],
72
- password_confirmation: params[:user][:password]
73
- end
74
- @order.user = current_user
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 ||= ShippingAddress.create(
79
- shipping_address_params.merge user_id: current_user.try(: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
@@ -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 = :new }, unless: :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
- customer.update_card processor_token
30
+ if processor_token.present?
31
+ customer.update_card processor_token
32
+ processor_token = nil
33
+ end
29
34
 
30
- # TODO Create shipping address
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
- #TODO: Mark order as charged
38
- # update_attribute :charged, true if charge.captured
39
- rescue Stripe::CardError => e
40
- # The card has been declined or some other error has occurred
41
- self.errors.add e
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 @@ module OpCart
4
4
  belongs_to :user
5
5
 
6
6
  validates :full_name, :street, :locality, :region, :postal_code, presence: true
7
+ validates :postal_code, format: { with: /\A\d{5}\z/ }
7
8
  validates :user, presence: true
8
9
  end
9
10
  end
@@ -13,3 +13,11 @@ en:
13
13
  locality: "City"
14
14
  region: "State"
15
15
  postal_code: "Zip Code"
16
+
17
+ activerecord:
18
+ errors:
19
+ models:
20
+ op_cart/order:
21
+ attributes:
22
+ line_items:
23
+ blank: "You must add at least one item to your order."
@@ -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
 
@@ -1,3 +1,3 @@
1
1
  module OpCart
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
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.0
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-31 00:00:00.000000000 Z
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails