bodega 0.4.9 → 0.4.12

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.9
1
+ 0.4.12
@@ -20,7 +20,7 @@ class Bodega::OrdersController < ApplicationController
20
20
  end
21
21
 
22
22
  def create
23
- if current_order.update_attributes(params[:order])
23
+ if current_order.update_attributes(params[:order].merge(checking_out: params[:checkout].present?))
24
24
  if !current_order.new_shipping_rates? && params[:checkout]
25
25
  redirect_to current_order.payment_method.checkout_url(complete_order_url, new_order_url, params)
26
26
  else
@@ -4,7 +4,11 @@ module Bodega
4
4
  class Order < ActiveRecord::Base
5
5
  self.table_name = :bodega_orders
6
6
 
7
+ attr_accessor :checking_out
8
+
9
+ attr_accessible :email, :checking_out
7
10
  attr_accessible :order_products_attributes, :postal_code, :shipping_rate_code
11
+ attr_accessible :street_1, :street_2, :city, :state
8
12
 
9
13
  before_create :set_identifier
10
14
  before_save :set_shipping_rates, if: :set_shipping_rates?
@@ -30,13 +34,15 @@ module Bodega
30
34
 
31
35
  serialize :shipping_rates
32
36
 
37
+ validates_presence_of :email, if: :require_email?
38
+
33
39
  def finalize!(options)
34
40
  self.class.transaction do
35
41
  self.status = :complete
36
42
  self.save!
37
43
  begin
38
44
  self.payment_id = payment_method.complete!(options)
39
- self.save
45
+ self.save!
40
46
  rescue Exception
41
47
  raise ActiveRecord::Rollback
42
48
  end
@@ -85,6 +91,10 @@ module Bodega
85
91
  end
86
92
  end
87
93
 
94
+ def state_options
95
+ US_STATES.values
96
+ end
97
+
88
98
  def subtotal
89
99
  order_products.inject(Money.new(0)) {|sum, order_product| sum += order_product.subtotal }
90
100
  end
@@ -138,6 +148,10 @@ module Bodega
138
148
  end
139
149
  end
140
150
 
151
+ def require_email?
152
+ Bodega.config.collect_email && persisted? && (postal_code_changed? || checking_out)
153
+ end
154
+
141
155
  def set_identifier
142
156
  self.identifier = self.class.count.succ.to_s(36)
143
157
  end
@@ -161,4 +175,61 @@ module Bodega
161
175
  self.total = subtotal + tax + shipping
162
176
  end
163
177
  end
178
+
179
+
180
+ US_STATES = ActiveSupport::OrderedHash.new
181
+ Hash['Alabama', 'AL',
182
+ 'Alaska', 'AK',
183
+ 'Arizona', 'AZ',
184
+ 'Arkansas', 'AR',
185
+ 'California', 'CA',
186
+ 'Colorado', 'CO',
187
+ 'Connecticut', 'CT',
188
+ 'Delaware', 'DE',
189
+ 'District Of Columbia', 'DC',
190
+ 'Florida', 'FL',
191
+ 'Georgia', 'GA',
192
+ 'Hawaii', 'HI',
193
+ 'Idaho', 'ID',
194
+ 'Illinois', 'IL',
195
+ 'Indiana', 'IN',
196
+ 'Iowa', 'IA',
197
+ 'Kansas', 'KS',
198
+ 'Kentucky', 'KY',
199
+ 'Louisiana', 'LA',
200
+ 'Maine', 'ME',
201
+ 'Maryland', 'MD',
202
+ 'Massachusetts', 'MA',
203
+ 'Michigan', 'MI',
204
+ 'Minnesota', 'MN',
205
+ 'Mississippi', 'MS',
206
+ 'Missouri', 'MO',
207
+ 'Montana', 'MT',
208
+ 'Nebraska', 'NE',
209
+ 'Nevada', 'NV',
210
+ 'New Hampshire', 'NH',
211
+ 'New Jersey', 'NJ',
212
+ 'New Mexico', 'NM',
213
+ 'New York', 'NY',
214
+ 'North Carolina', 'NC',
215
+ 'North Dakota', 'ND',
216
+ 'Ohio', 'OH',
217
+ 'Oklahoma', 'OK',
218
+ 'Oregon', 'OR',
219
+ 'Pennsylvania', 'PA',
220
+ 'Rhode Island', 'RI',
221
+ 'South Carolina', 'SC',
222
+ 'South Dakota', 'SD',
223
+ 'Tennessee', 'TN',
224
+ 'Texas', 'TX',
225
+ 'Utah', 'UT',
226
+ 'Vermont', 'VT',
227
+ 'Virginia', 'VA',
228
+ 'Washington', 'WA',
229
+ 'West Virginia', 'WV',
230
+ 'Wisconsin', 'WI',
231
+ 'Wyoming', 'WY'
232
+ ].sort.each do |key, value|
233
+ US_STATES[key] = value
234
+ end
164
235
  end
@@ -7,9 +7,10 @@
7
7
  <%= form.fields_for(:order_products) do |order_product_form| -%>
8
8
  <%= render partial: 'cart_row', locals: {form: order_product_form, order_product: order_product_form.object} %>
9
9
  <% end =%>
10
+ <%= render partial: 'email_row', locals: {form: form} if Bodega.config.collect_email %>
11
+ <%= render partial: 'shipping_row', locals: {form: form} if current_order.shipping_method %>
10
12
  </tbody>
11
13
  <tfoot>
12
- <%= render partial: 'shipping_row', locals: {form: form} if current_order.shipping_method %>
13
14
  <% if current_order.ready? -%>
14
15
  <tr>
15
16
  <td class="product-total-label" colspan="3"><%= t 'bodega.total' %></td>
@@ -0,0 +1,5 @@
1
+ <tr id="bodega-email">
2
+ <td colspan="5">
3
+ <%= form.email_field :email, placeholder: t('bodega.email') %>
4
+ </td>
5
+ </tr>
@@ -1,11 +1,24 @@
1
+ <% unless current_order.payment_method.shipping? -%>
2
+ <tr id="bodega-shipping-address">
3
+ <td colspan="5">
4
+ <%= form.text_field :street_1, placeholder: t('bodega.street_1') %>
5
+ <%= form.text_field :street_2, placeholder: t('bodega.street_2') %>
6
+ <%= form.text_field :city, placeholder: t('bodega.city') %>
7
+ <%= form.select :state, current_order.state_options, {}, prompt: t('bodega.state') %>
8
+ <%= form.text_field :postal_code, placeholder: t('bodega.postal_code') %>
9
+ </td>
10
+ </tr>
11
+ <% end -%>
1
12
  <tr>
2
13
  <td class="shipping-postal-code" colspan="3">
14
+ <% if current_order.payment_method.shipping? -%>
3
15
  <%= form.text_field :postal_code, placeholder: t('bodega.postal_code') %>
16
+ <% end -%>
4
17
  <% if current_order.postal_code.present? -%>
5
18
  <%= form.select :shipping_rate_code, current_order.shipping_rate_options %>
6
19
  <% end -%>
7
20
  </td>
8
21
  <td class="shipping-total" colspan="2">
9
- <%= humanized_money_with_symbol current_order.shipping %>
22
+ <%= humanized_money_with_symbol current_order.shipping if current_order.ready? %>
10
23
  </td>
11
24
  </tr>
@@ -1,24 +1,21 @@
1
+ <% if params[:stripe] == 'yes' -%>
1
2
  <%= javascript_include_tag 'https://checkout.stripe.com/v2/checkout.js' %>
2
3
  <script type="text/javascript">
3
- $('#bodega-checkout').click(function(event) {
4
- event.preventDefault();
5
- var button = $(this);
6
- var form = button.parents('form');
4
+ var form = $('#bodega-cart').parents('form');
7
5
 
8
- var token = function(response) {
9
- button.attr('name', '');
10
- input = $('<input type="hidden" name="stripe" />').val(response.id);
11
- form.append('<input type="hidden" name="checkout" value="1" />');
12
- form.append(input).submit();
13
- };
6
+ var token = function(response) {
7
+ input = $('<input type="hidden" name="stripe" />').val(response.id);
8
+ form.append('<input type="hidden" name="checkout" value="1" />');
9
+ form.append(input).submit();
10
+ };
14
11
 
15
- StripeCheckout.open({
16
- key: <%= Bodega.config.stripe.publishable_key.inspect.html_safe %>,
17
- amount: <%= current_order.total_cents %>,
18
- name: <%= Bodega.config.store_name.inspect.html_safe %>,
19
- description: <%= current_order.summary.inspect.html_safe %>,
20
- panelLabel: 'Confirm:',
21
- token: token
22
- });
12
+ StripeCheckout.open({
13
+ key: <%= Bodega.config.stripe.publishable_key.inspect.html_safe %>,
14
+ amount: <%= current_order.total_cents %>,
15
+ name: <%= Bodega.config.store_name.inspect.html_safe %>,
16
+ description: <%= current_order.summary.inspect.html_safe %>,
17
+ panelLabel: 'Confirm:',
18
+ token: token
23
19
  });
24
20
  </script>
21
+ <% end -%>
@@ -18,6 +18,19 @@
18
18
  </td>
19
19
  </tr>
20
20
  <% end -%>
21
+
22
+ <tr>
23
+ <td class="product-name">
24
+ <%= @order.shipping_rate_name %>
25
+ </td>
26
+ <td class="price">
27
+ <%= humanized_money_with_symbol @order.shipping %>
28
+ </td>
29
+ <td class="subtotal">
30
+ <%= humanized_money_with_symbol @order.shipping %>
31
+ </td>
32
+ </tr>
33
+
21
34
  <tr>
22
35
  <td colspan="2"></td>
23
36
  <td><%= humanized_money_with_symbol @order.total %></td>
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "bodega"
8
- s.version = "0.4.8"
8
+ s.version = "0.4.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Flip Sasser"]
12
- s.date = "2013-03-31"
12
+ s.date = "2013-04-24"
13
13
  s.description = "Bodega adds checkout logic to any model in your app!"
14
14
  s.email = "flip@x451.com"
15
15
  s.extra_rdoc_files = [
@@ -33,7 +33,10 @@ Gem::Specification.new do |s|
33
33
  "app/models/bodega/product.rb",
34
34
  "app/views/bodega/orders/_cart.html.erb",
35
35
  "app/views/bodega/orders/_cart_row.html.erb",
36
+ "app/views/bodega/orders/_email_row.html.erb",
37
+ "app/views/bodega/orders/_paypal.html.erb",
36
38
  "app/views/bodega/orders/_shipping_row.html.erb",
39
+ "app/views/bodega/orders/_stripe.html.erb",
37
40
  "app/views/bodega/orders/edit.html.erb",
38
41
  "app/views/bodega/orders/new.html.erb",
39
42
  "app/views/bodega/orders/show.html.erb",
@@ -42,12 +45,14 @@ Gem::Specification.new do |s|
42
45
  "config/routes.rb",
43
46
  "db/migrate/20121111170337_create_bodega_orders.rb",
44
47
  "db/migrate/20121111170420_create_bodega_order_products.rb",
48
+ "db/migrate/20130415214518_add_email_to_orders.rb",
45
49
  "lib/bodega.rb",
46
50
  "lib/bodega/engine.rb",
47
51
  "lib/bodega/optional.rb",
48
52
  "lib/bodega/payment_method.rb",
49
53
  "lib/bodega/payment_method/base.rb",
50
54
  "lib/bodega/payment_method/paypal.rb",
55
+ "lib/bodega/payment_method/stripe.rb",
51
56
  "lib/bodega/shipping_method.rb",
52
57
  "lib/bodega/shipping_method/base.rb",
53
58
  "lib/bodega/shipping_method/ups.rb",
@@ -13,3 +13,8 @@ en:
13
13
  order_failed: "There was a problem processing this order. Your account has not been charged."
14
14
  calculate_shipping: "Calculate Shipping"
15
15
  remove_link: "Remove"
16
+ email: "Email address"
17
+ street_1: "Shipping address"
18
+ street_2: "Shipping address cont'd"
19
+ city: "City"
20
+ state: "State"
@@ -11,6 +11,10 @@ class CreateBodegaOrders < ActiveRecord::Migration
11
11
  t.string :shipping_rate_name, limit: 50
12
12
  t.text :shipping_rates
13
13
  t.string :tracking_number
14
+ t.string :street_1, limit: 60
15
+ t.string :street_2, limit: 60
16
+ t.string :city, limit: 60
17
+ t.string :state, limit: 3
14
18
  t.string :postal_code, limit: 11
15
19
  t.money :shipping
16
20
  t.money :tax
@@ -0,0 +1,5 @@
1
+ class AddEmailToOrders < ActiveRecord::Migration
2
+ def change
3
+ add_column :bodega_orders, :email, :string
4
+ end
5
+ end
@@ -12,6 +12,7 @@ module Bodega
12
12
  option :max_quantity, 1000
13
13
 
14
14
  option :store_name, "Store name (override in Bodega.config.store_name)"
15
+ option :collect_email, false
15
16
 
16
17
  # Auto-detect payment method. If a user has the Paypal gem installed,
17
18
  # it'll use that. If a user has the Plinq gem installed, it'll use that.
@@ -18,6 +18,12 @@ module Bodega
18
18
  def initialize(order)
19
19
  self.order = order
20
20
  end
21
+
22
+ # Does the payment method provide shipping details? If not, they'll
23
+ # be editable at checkout.
24
+ def shipping?
25
+ true
26
+ end
21
27
  end
22
28
  end
23
29
  end
@@ -8,19 +8,29 @@ module Bodega
8
8
 
9
9
  # Redirect to /cart/complete?stripe=tokenToVerifyPayment
10
10
  def checkout_url(success_url, cancel_url, params = {})
11
- uri = Addressable::URI.heuristic_parse(success_url)
12
- uri.query_hash[:stripe] = params[:stripe]
11
+ if params[:stripe]
12
+ uri = Addressable::URI.heuristic_parse(success_url)
13
+ uri.query_hash[:stripe] = params[:stripe]
14
+ else
15
+ uri = Addressable::URI.heuristic_parse(cancel_url)
16
+ uri.query_hash[:stripe] = 'yes'
17
+ end
13
18
  uri.to_s
14
19
  end
15
20
 
16
21
  def complete!(options = {})
17
22
  ::Stripe.api_key = Bodega.config.stripe.secret_key
18
- ::Stripe::Charge.create(
23
+ charge = ::Stripe::Charge.create(
19
24
  amount: order.total_cents,
20
25
  currency: 'usd',
21
26
  card: options[:stripe],
22
27
  description: order.summary
23
- ).id
28
+ )
29
+ charge.id
30
+ end
31
+
32
+ def shipping?
33
+ false
24
34
  end
25
35
 
26
36
  protected
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bodega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.4.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-31 00:00:00.000000000 Z
12
+ date: 2013-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -131,6 +131,7 @@ files:
131
131
  - app/models/bodega/product.rb
132
132
  - app/views/bodega/orders/_cart.html.erb
133
133
  - app/views/bodega/orders/_cart_row.html.erb
134
+ - app/views/bodega/orders/_email_row.html.erb
134
135
  - app/views/bodega/orders/_paypal.html.erb
135
136
  - app/views/bodega/orders/_shipping_row.html.erb
136
137
  - app/views/bodega/orders/_stripe.html.erb
@@ -142,6 +143,7 @@ files:
142
143
  - config/routes.rb
143
144
  - db/migrate/20121111170337_create_bodega_orders.rb
144
145
  - db/migrate/20121111170420_create_bodega_order_products.rb
146
+ - db/migrate/20130415214518_add_email_to_orders.rb
145
147
  - lib/bodega.rb
146
148
  - lib/bodega/engine.rb
147
149
  - lib/bodega/optional.rb