op_cart 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 141aa04382aba1feb816cd854b830ceab70fe33a
4
- data.tar.gz: 7d44636bcb5cf528c512a0b1db8a3237c2ce0ffe
3
+ metadata.gz: 5eebd165fcfc2393d11a5bc38187675f501cb540
4
+ data.tar.gz: ae0ba7bc1ce2ee742a76b6c6fc4b7e4084262c8b
5
5
  SHA512:
6
- metadata.gz: 806d6828dcd0254f33933bd73517a2d4de92332acbad5d914f796cfd623f7e5fbfd4291e89a76dcd79b1a18ae12d8f6176c4e16dd52ed34052789664b1121575
7
- data.tar.gz: fa3e315d8b181bd9ed741737357e359b70fbcdd344791e72596d2834e2e92e6d0943c647f28e35822f141080d518a67ae35c039c524ef4f0194aff5ec328a3c0
6
+ metadata.gz: 8219b5278281607723727a06fd338a0d52c77e605ba6459cb3ba72d468eaf4ce2ef3521db6229f576cec3206246dea632a85819b3571d6125525dfa0ae25631a
7
+ data.tar.gz: 8e945ab2f42a3e2d5d363046aaea27d3ee79bd46fee4328a66138ec124963b7919c3da548541b6aa49c40db3f2fabf7b7120cbd67345631237dedaf4aeee873b
@@ -1,12 +1,12 @@
1
- OpCart =
1
+ @OpCart =
2
2
  ready: ->
3
3
  if $('body').is '.op_cart-orders-new'
4
- $number = $ '#order_credit_card_number'
5
- $expiry = $ '#order_credit_card_expiry'
6
- $cvc = $ '#order_credit_card_cvc'
7
- $city = $ '#order_shipping_address_city'
8
- $state = $ '#order_shipping_address_state'
9
- $zip = $ '#order_shipping_address_zip_code'
4
+ $number = $ '#order_credit_cards_number'
5
+ $expiry = $ '#order_credit_cards_expiry'
6
+ $cvc = $ '#order_credit_cards_cvc'
7
+ $city = $ '#order_shipping_addresses_city'
8
+ $state = $ '#order_shipping_addresses_state'
9
+ $zip = $ '#order_shipping_addresses_zip_code'
10
10
 
11
11
  $number.payment 'formatCardNumber'
12
12
  $expiry.payment 'formatCardExpiry'
@@ -29,11 +29,13 @@ OpCart =
29
29
  $form = $(this)
30
30
  $form.find("button").prop "disabled", true
31
31
 
32
- expiration = $("#order_credit_card_expiry").payment "cardExpiryVal"
32
+ Stripe.setPublishableKey $form.data("stripe-key")
33
+
34
+ expiration = $("#order_credit_cards_expiry").payment "cardExpiryVal"
33
35
 
34
36
  Stripe.card.createToken
35
- number: $("#order_credit_card_number").val()
36
- cvc: $("#order_credit_card_cvc").val()
37
+ number: $("#order_credit_cards_number").val()
38
+ cvc: $("#order_credit_cards_cvc").val()
37
39
  exp_month: expiration.month || 0
38
40
  exp_year: expiration.year || 0
39
41
  , OpCart.stripeResponseHandler
@@ -43,7 +45,6 @@ OpCart =
43
45
  stripeResponseHandler: (status, response) ->
44
46
  $form = $("#new_order")
45
47
 
46
- debugger
47
48
  if response.error || !$form.get(0).checkValidity()
48
49
  if response.error
49
50
  errorMessage = response.error.message
@@ -52,10 +53,32 @@ OpCart =
52
53
  $form.find(".payment-errors").text errorMessage
53
54
  $form.find("button").prop "disabled", false
54
55
  else
55
- $form.append $("<input type=\"hidden\" name=\"stripeToken\" />").val(response.id)
56
+ $('#order_card_token').val response.id
56
57
  $form.get(0).submit()
57
58
 
59
+ addItemToOrder: (productId, quantity) ->
60
+ currentQuantity = @lineItemQuantity productId
61
+ @lineItemQuantity productId, currentQuantity + 1
62
+ @updateDisplayedQuantity productId
63
+
64
+ removeItemFromOrder: (productId) ->
65
+ @lineItemQuantity productId, 0
66
+ @updateDisplayedQuantity productId
67
+
68
+ updateDisplayedQuantity: (productId) ->
69
+ $quantity = $ "#line_item_product_#{productId} .quantity .value"
70
+ $quantity.html @lineItemQuantity(productId)
71
+
72
+ lineItemQuantity: (productId, quantity) ->
73
+ $liQuantities = $ '#line_items_quantities'
74
+ liQuantities = JSON.parse $liQuantities.val() || "{}"
75
+
76
+ if quantity >= 0
77
+ liQuantities[productId] = quantity
78
+ $liQuantities.val JSON.stringify(liQuantities)
79
+ else
80
+ liQuantities[productId] || 0
58
81
 
59
82
  $ -> OpCart.ready()
60
- $(document).on 'page:load', -> OpCart.load()
83
+ $(document).on 'page:load', OpCart.load
61
84
 
@@ -1,32 +1,50 @@
1
1
  module OpCart
2
2
  class OrdersController < ApplicationController
3
+ # before_action :authenticate_user!, only: [:show, :edit, :update, :destroy]
4
+ before_action :set_order, only: [:show, :edit, :update, :destroy]
5
+
3
6
  def new
4
- @products = [Product.first] # Temp hack to display first product as order
7
+ @products = Product.all
5
8
  @order = Order.new
6
9
  end
7
10
 
8
11
  def create
9
- product = Product.find params[:product_id]
12
+ @order = Order.new order_params
13
+ @order.status = :new
14
+ @order.user = current_user || User.first #TODO: Create a user from email
15
+ add_line_items
10
16
 
11
- customer = Stripe::Customer.create(
12
- card: params[:stripeToken],
13
- email: params[:order][:email]
14
- )
17
+ if @order.save
18
+ redirect_to @order, notice: 'Thank you for your purchase'
19
+ else
20
+ render :new
21
+ end
22
+ end
23
+
24
+ def show
25
+ end
15
26
 
16
- # TODO Create Order w/ Line Items
17
- # TODO Create shipping address
27
+ private
18
28
 
19
- Stripe::Charge.create(
20
- amount: product.price,
21
- currency: "usd",
22
- customer: customer.id
29
+ def set_order
30
+ # TODO prevent viewing of others' orders
31
+ # @order = current_user.orders.find params[:id]
32
+ @order = Order.find params[:id]
33
+ end
34
+
35
+ def order_params
36
+ params.require(:order).permit(:email, :card_token,
37
+ { shipping_address: [ :full_name, :address, :zip_code, :city, :state ] }
23
38
  )
39
+ end
24
40
 
25
- redirect_to new_order_path, notice: 'Thank you for your purchase'
26
- rescue Stripe::CardError => e
27
- # The card has been declined or some other error has occurred
28
- @error = e
29
- render :new
41
+ def add_line_items
42
+ li_quantities_json = JSON.parse params[:line_items][:quantities]
43
+ li_quantities_json.each do |product_id, quantity|
44
+ @order.line_items <<
45
+ LineItem.new(sellable: Product.find(product_id), quantity: quantity)
46
+ end
30
47
  end
48
+
31
49
  end
32
50
  end
@@ -7,6 +7,22 @@ module OpCart
7
7
  validates :quantity, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
8
8
  validates :sellable_snapshot, presence: true
9
9
  validates :sellable, presence: true
10
- validates :order, presence: true
10
+ # validates :order, presence: true
11
+
12
+ before_validation :set_unit_price
13
+ before_validation :snapshot_sellable
14
+
15
+ def total
16
+ (unit_price || sellable.price) * quantity
17
+ end
18
+
19
+ private
20
+ def set_unit_price
21
+ self.unit_price = sellable.price
22
+ end
23
+
24
+ def snapshot_sellable
25
+ self.sellable_snapshot = sellable.attributes
26
+ end
11
27
  end
12
28
  end
@@ -1,7 +1,10 @@
1
1
  module OpCart
2
2
  class Order < ActiveRecord::Base
3
+ has_many :line_items
3
4
  belongs_to :user
4
5
 
6
+ accepts_nested_attributes_for :line_items
7
+
5
8
  validates :email, presence: true, format: { with: /@/ }
6
9
  validates :total, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
7
10
  validates :tax_amount, numericality:
@@ -9,12 +12,31 @@ module OpCart
9
12
  validates :status, presence: true
10
13
  validates :user, presence: true
11
14
 
15
+ before_validation :set_total
12
16
  before_save :charge_customer
13
17
 
14
18
  private
15
19
 
20
+ def set_total
21
+ self.total = line_items.reduce(0) { |total, line_item| total += line_item.total }
22
+ end
23
+
16
24
  def charge_customer
17
- # TODO Create and charge customer
25
+ customer = Stripe::Customer.create(
26
+ card: card_token,
27
+ email: email
28
+ )
29
+
30
+ # TODO Create shipping address
31
+
32
+ Stripe::Charge.create(
33
+ amount: total,
34
+ currency: "usd",
35
+ customer: customer.id
36
+ )
37
+ rescue Stripe::CardError => e
38
+ # The card has been declined or some other error has occurred
39
+ self.errors.add e
18
40
  end
19
41
  end
20
42
  end
@@ -1,21 +1,30 @@
1
- script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.payment/1.0.2/jquery.payment.min.js" type="text/javascript"
2
- script src="https://js.stripe.com/v2/" type="text/javascript"
3
-
4
- javascript:
5
- Stripe.setPublishableKey('#{Rails.configuration.stripe[:publishable_key]}')
6
-
7
- - if @products.any?
8
- -# Hack to display products temporarily
1
+ - if @products.present?
9
2
  .new_order
10
- h2 Items in Order
3
+ h2 Purchasble Items
11
4
  ul style="list-style-type: none"
12
5
  - @products.each do |product|
13
6
  li
14
7
  p = product.name
15
8
  p = product.description
16
9
  p = number_to_currency product.price / 100
10
+ p
11
+ a onclick="OpCart.addItemToOrder(#{product.id}, 1)" style="cursor:pointer"
12
+ | Add to Order
13
+
14
+ h2 Items in Order
15
+ ul style="list-style-type: none"
16
+ - @products.each do |product|
17
+ li id="#{dom_id product, 'line_item'}"
18
+ p = product.name
19
+ p = number_to_currency product.price / 100
20
+ p.quantity
21
+ span.name Quantity:&nbsp;
22
+ span.value 0
23
+ p
24
+ a onclick="OpCart.removeItemFromOrder(#{product.id})" style="cursor:pointer"
25
+ | Remove
17
26
 
18
- = simple_form_for @order do |f|
27
+ = simple_form_for @order, data: { stripe_key: Rails.configuration.stripe[:publishable_key] } do |f|
19
28
  span.payment-errors
20
29
  p
21
30
  h2 Your Information
@@ -23,7 +32,7 @@ javascript:
23
32
 
24
33
  p
25
34
  h2 Shipping Address
26
- = f.simple_fields_for :shipping_address do |sa|
35
+ = f.simple_fields_for :shipping_addresses do |sa|
27
36
  = sa.input :full_name, input_html: { autocomplete: 'name' }
28
37
  = sa.input :address, input_html: { autocomplete: 'street-address' }
29
38
  = sa.input :zip_code, input_html: { autocomplete: 'postal-code' }
@@ -32,14 +41,15 @@ javascript:
32
41
 
33
42
  p
34
43
  h2 Card Details
35
- = f.simple_fields_for :credit_card do |c|
44
+ = f.simple_fields_for :credit_cards do |c|
36
45
  = c.input :number, input_html: { autocomplete: 'cc-number' }
37
46
  = c.input :expiry, input_html: { autocomplete: 'cc-exp' }
38
47
  = c.input :cvc, input_html: { autocomplete: 'cc-csc' }
39
48
 
40
- = hidden_field_tag :product_id, @products.first.id
49
+ = f.input :card_token, as: :hidden
50
+ = hidden_field :line_items, :quantities, value: "{}"
41
51
 
42
52
  .form-actions
43
53
  = f.button :button, "Submit Payment"
44
54
  - else
45
- p Add an item to your order.
55
+ p Add an item to your order to begin.
@@ -0,0 +1,22 @@
1
+ .new_order
2
+ h2 Items in Order
3
+ ul
4
+ - @order.line_items.each do |line_item|
5
+ li id="#{dom_id line_item}"
6
+ p = line_item.sellable.name
7
+ p.quantity
8
+ = line_item.quantity
9
+ | &nbsp;@&nbsp;
10
+ = number_to_currency line_item.unit_price / 100
11
+ | &nbsp;=&nbsp;
12
+ = number_to_currency line_item.total / 100
13
+ li
14
+ p Order Total: #{number_to_currency @order.total / 100}
15
+
16
+ p
17
+ h2 Your Information
18
+ = @order.email
19
+ p
20
+ h2 Shipping Address
21
+ p 123 N Todo Street
22
+ p Edmond, Oklahoma 73013
@@ -3,13 +3,13 @@ en:
3
3
  placeholders:
4
4
  order:
5
5
  email: "Email"
6
- shipping_address:
6
+ shipping_addresses:
7
7
  full_name: "Full Name"
8
8
  address: "Street Address"
9
9
  zip_code: "Zip Code"
10
10
  city: "City"
11
11
  state: "State"
12
- credit_card:
12
+ credit_cards:
13
13
  number: "Card Number"
14
14
  expiry: 'MM/YY'
15
15
  cvc: 'CVC'
@@ -2,6 +2,7 @@ class CreateOpCartOrders < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :op_cart_orders do |t|
4
4
  t.string :email, null: false
5
+ t.string :card_token, null: false
5
6
  t.integer :total, null: false
6
7
  t.integer :tax_amount, default: 0
7
8
  t.string :status, null: false
data/db/seeds.rb CHANGED
@@ -9,3 +9,15 @@ product1_attrs = {
9
9
  }
10
10
 
11
11
  product1 = OpCart::Product.where(sku: product1_attrs[:sku]).first_or_create product1_attrs
12
+
13
+ product2_attrs = {
14
+ name: 'Device 9000',
15
+ description: 'The much improved Device 9000',
16
+ sku: 'A2000',
17
+ price: 25000,
18
+ allow_purchases: true,
19
+ charge_taxes: true,
20
+ user: User.first,
21
+ }
22
+
23
+ product2 = OpCart::Product.where(sku: product2_attrs[:sku]).first_or_create product2_attrs
@@ -1,3 +1,3 @@
1
1
  module OpCart
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
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.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Boehs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-10 00:00:00.000000000 Z
11
+ date: 2014-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -87,6 +87,7 @@ files:
87
87
  - app/models/op_cart/order.rb
88
88
  - app/models/op_cart/product.rb
89
89
  - app/views/op_cart/orders/new.html.slim
90
+ - app/views/op_cart/orders/show.html.slim
90
91
  - config/initializers/stripe.rb
91
92
  - config/locales/op_cart.en.yml
92
93
  - config/routes.rb