Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ability to have limited payment options that will count down as…

… more people order them ("limited: x of y remaining"). When the limit is reached, the option becomes disabled
commit 75219fa03f448b2b337d6a5720ada450028c66f9 1 parent 451bca0
@jes5e jes5e authored
View
1  app/assets/javascripts/preorder.js.coffee
@@ -41,6 +41,7 @@ Selfstarter =
# the radio button selection should bring up the email field and button
$('.payment_options ol li').on "click", ->
+ return false if $(this).children(".payment_radio").attr("disabled") == "disabled"
$(".payment_radio").parents("ol>li").removeClass("checkout_option_selected")
$(this).addClass("checkout_option_selected")
$(this).children(".payment_radio").attr "checked", "checked"
View
6 app/assets/stylesheets/checkout.css.scss
@@ -150,6 +150,12 @@
line-height: 14px;
}
+ .sold-out
+ {
+ color: #ffffff;
+ background-color: black;
+ padding: 3px;
+ }
}
div.description
{
View
8 app/controllers/preorder_controller.rb
@@ -11,13 +11,15 @@ def prefill
@user = User.find_or_create_by_email!(params[:email])
if Settings.use_payment_options
- price = params['payment_option']
- raise Exception.new("No payment option was selected") if price.nil?
+ payment_option_id = params['payment_option']
+ raise Exception.new("No payment option was selected") if payment_option_id.nil?
+ payment_option = PaymentOption.find(payment_option_id)
+ price = payment_option.amount
else
price = Settings.price
end
- @order = Order.prefill!(:name => Settings.product_name, :price => price, :user_id => @user.id)
+ @order = Order.prefill!(:name => Settings.product_name, :price => price, :user_id => @user.id, :payment_option => payment_option)
# This is where all the magic happens. We create a multi-use token with Amazon, letting us charge the user's Amazon account
# Then, if they confirm the payment, Amazon POSTs us their shipping details and phone number
View
8 app/helpers/preorder_helper.rb
@@ -25,4 +25,12 @@ def video_url
def encoded_root_url
raw URI.encode "#{request.scheme}://#{request.host}/preorder"
end
+
+ def sold_out(payment_option)
+ payment_option.limit > -1 and order_count(payment_option) >= payment_option.limit
+ end
+
+ def order_count(payment_option)
+ Order.where(payment_option_id: payment_option).count(:token) # count of orders that have a token from amazon and are for this payment option
+ end
end
View
15 app/models/order.rb
@@ -1,17 +1,20 @@
class Order < ActiveRecord::Base
- attr_accessible :address_one, :address_two, :city, :country, :number, :state, :status, :token, :transaction_id, :zip, :shipping, :tracking_number, :name, :price, :phone, :expiration
+ attr_accessible :address_one, :address_two, :city, :country, :number, :state, :status, :token, :transaction_id, :zip,
+ :shipping, :tracking_number, :name, :price, :phone, :expiration, :payment_option
attr_readonly :uuid
before_validation :generate_uuid!, :on => :create
belongs_to :user
+ belongs_to :payment_option
self.primary_key = 'uuid'
# This is where we create our Caller Reference for Amazon Payments, and prefill some other information.
def self.prefill!(options = {})
- @order = Order.new
- @order.name = options[:name]
- @order.user_id = options[:user_id]
- @order.price = options[:price]
- @order.number = Order.next_order_number
+ @order = Order.new
+ @order.name = options[:name]
+ @order.user_id = options[:user_id]
+ @order.price = options[:price]
+ @order.number = Order.next_order_number
+ @order.payment_option = options[:payment_option] if !options[:payment_option].nil?
@order.save!
@order
View
2  app/models/payment_option.rb
@@ -1,3 +1,3 @@
class PaymentOption < ActiveRecord::Base
- attr_accessible :amount, :amount_display, :delivery_desc, :description, :limit, :purchased_count, :shipping_desc
+ attr_accessible :amount, :amount_display, :delivery_desc, :description, :limit, :shipping_desc
end
View
8 app/views/preorder/_payment_options.html.erb
@@ -4,13 +4,19 @@
<ol>
<% PaymentOption.all.each_with_index do |p, index| %>
<li>
- <%= radio_button_tag "payment_option", p.amount, false, class: 'payment_radio' %>
+ <%= radio_button_tag "payment_option", p.id, false, class: 'payment_radio', disabled: sold_out(p) %>
<%= label_tag("payment_option_#{index}", p.amount_display) %>
<div class="shipping">
+ <% if sold_out p %>
+ <span class="sold-out">Sold Out</span>
+ <% end %>
<span><%= p.shipping_desc %></span>
</div>
<div class="description">
<p class="full"><%= raw p.description %></p>
+ <% if p.limit > -1 %>
+ <p class="limited">Limited (<%= p.limit - order_count(p) %> of <%= p.limit %> remaining)</p>
+ <% end %>
<p class="delivery_date"><%= p.delivery_desc %></p>
</div>
</li>
View
1  db/migrate/20130106183354_create_payment_options.rb
@@ -7,7 +7,6 @@ def change
t.string :shipping_desc
t.string :delivery_desc
t.integer :limit
- t.integer :purchased_count
t.timestamps
end
View
5 db/migrate/20130107010733_add_payment_option_id_to_orders.rb
@@ -0,0 +1,5 @@
+class AddPaymentOptionIdToOrders < ActiveRecord::Migration
+ def change
+ add_column :orders, :payment_option_id, :integer
+ end
+end
View
8 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130106183354) do
+ActiveRecord::Schema.define(:version => 20130107010733) do
create_table "orders", :id => false, :force => true do |t|
t.string "token"
@@ -32,8 +32,9 @@
t.string "phone"
t.string "name"
t.date "expiration"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "payment_option_id"
end
create_table "payment_options", :force => true do |t|
@@ -43,7 +44,6 @@
t.string "shipping_desc"
t.string "delivery_desc"
t.integer "limit"
- t.integer "purchased_count"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
View
24 db/seeds.rb
@@ -13,8 +13,7 @@
description: '<strong>Basic level: </strong>You receive a great big thankyou from us! You Rock',
shipping_desc: '',
delivery_desc: '',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 100.00,
@@ -22,8 +21,7 @@
description: '<strong>Package 1: </strong>You receive our print edition',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: 250,
- purchased_count: 0
+ limit: 250
},
{
amount: 125.00,
@@ -31,8 +29,7 @@
description: '<strong>Package 2: </strong>You will receive both our print and digital edition',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 200.00,
@@ -40,8 +37,7 @@
description: '<strong>Package 3: </strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 250.00,
@@ -49,8 +45,7 @@
description: '<strong>Package 4: </strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 300.00,
@@ -58,8 +53,7 @@
description: '<strong>Package 5: </strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 500.00,
@@ -67,8 +61,7 @@
description: '<strong>Package 6: </strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 1000.00,
@@ -76,7 +69,6 @@
description: '<strong>Package 7: </strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
}
])
Please sign in to comment.
Something went wrong with that request. Please try again.