From 0f12041d6412364767d9daec5d5a165f8633053e Mon Sep 17 00:00:00 2001 From: Sean Schofield Date: Thu, 18 Jun 2009 16:44:50 -0400 Subject: [PATCH] Replaced ship_amount and tax_amount with a flexible system of charges. [#499 state:resolved milestone:0.8.99] --- app/controllers/checkouts_controller.rb | 10 ++- app/helpers/spree/base_helper.rb | 4 +- app/models/charge.rb | 7 ++ app/models/checkout.rb | 25 ++++++- app/models/order.rb | 47 ++++++------ app/models/shipment.rb | 10 +-- app/models/shipping_charge.rb | 2 + app/models/tax_charge.rb | 2 + app/views/admin/orders/show.html.erb | 2 +- .../admin/shared/_order_details.html.erb | 36 --------- app/views/admin/tax_rates/index.html.erb | 2 +- app/views/order_mailer/cancel.html.erb | 5 +- app/views/order_mailer/confirm.html.erb | 5 +- app/views/orders/_google_order.html.erb | 4 +- app/views/shared/_order_details.html.erb | 74 ++++++++++--------- config/locales/en-US.yml | 2 - db/migrate/20090603153927_create_checkouts.rb | 12 ++- db/migrate/20090618203233_create_charges.rb | 36 +++++++++ db/sample/charges.yml | 14 ++++ db/sample/checkouts.yml | 6 +- db/sample/orders.yml | 2 - public/javascripts/checkout.js | 8 +- public/stylesheets/sass/_checkout.sass | 3 + test/factories.rb | 11 ++- test/fixtures/charges.yml | 7 ++ test/unit/charge_test.rb | 13 ++-- test/unit/checkout_test.rb | 56 ++++++++++++++ .../lib/spree/payment_gateway.rb | 4 +- 28 files changed, 269 insertions(+), 140 deletions(-) create mode 100644 app/models/charge.rb create mode 100644 app/models/shipping_charge.rb create mode 100644 app/models/tax_charge.rb delete mode 100644 app/views/admin/shared/_order_details.html.erb create mode 100644 db/migrate/20090618203233_create_charges.rb create mode 100644 db/sample/charges.yml create mode 100644 test/fixtures/charges.yml diff --git a/app/controllers/checkouts_controller.rb b/app/controllers/checkouts_controller.rb index 5cc89ca0275..c242578dd99 100644 --- a/app/controllers/checkouts_controller.rb +++ b/app/controllers/checkouts_controller.rb @@ -30,9 +30,9 @@ def update end success.wants.js do - render :json => { :order_total => number_to_currency(@order.total), - :ship_amount => number_to_currency(@order.ship_amount), - :tax_amount => number_to_currency(@order.tax_amount), + @order.reload + render :json => { :order_total => number_to_currency(@order.total), + :charges => charge_hash, :available_methods => rate_hash}.to_json, :layout => false end @@ -74,5 +74,9 @@ def rate_hash :name => ship_method.name, :rate => number_to_currency(ship_method.calculate_shipping(fake_shipment)) } end + end + + def charge_hash + Hash[*@order.charges.collect { |c| [c.description, number_to_currency(c.amount)] }.flatten] end end \ No newline at end of file diff --git a/app/helpers/spree/base_helper.rb b/app/helpers/spree/base_helper.rb index 451a65e1e01..69d94160ef5 100644 --- a/app/helpers/spree/base_helper.rb +++ b/app/helpers/spree/base_helper.rb @@ -31,10 +31,10 @@ def order_price(order, options={}) options.reverse_merge! :format_as_currency => true, :show_vat_text => true # overwrite show_vat_text if show_price_inc_vat is false - options[:show_vat_text] = Spree::Tax::Config[:show_price_inc_vat] + options[:show_vat_text] = Spree::Config[:show_price_inc_vat] amount = order.item_total - amount += Spree::VatCalculator.calculate_tax(order) if Spree::Tax::Config[:show_price_inc_vat] + amount += Spree::VatCalculator.calculate_tax(order) if Spree::Config[:show_price_inc_vat] options.delete(:format_as_currency) ? number_to_currency(amount) : amount end diff --git a/app/models/charge.rb b/app/models/charge.rb new file mode 100644 index 00000000000..7d07749e088 --- /dev/null +++ b/app/models/charge.rb @@ -0,0 +1,7 @@ +class Charge < ActiveRecord::Base + belongs_to :order + acts_as_list :scope => :order + + validates_presence_of :amount + validates_presence_of :description +end diff --git a/app/models/checkout.rb b/app/models/checkout.rb index 344cf05c2e5..11d02a164df 100644 --- a/app/models/checkout.rb +++ b/app/models/checkout.rb @@ -1,6 +1,6 @@ -class Checkout < ActiveRecord::Base - after_update :update_order_totals +class Checkout < ActiveRecord::Base before_save :authorize_creditcard + after_save :update_charges belongs_to :order belongs_to :shipping_method belongs_to :bill_address, :foreign_key => "bill_address_id", :class_name => "Address" @@ -17,7 +17,26 @@ def authorize_creditcard return unless cc.valid? and cc.authorize(order.total) order.complete end - def update_order_totals + def update_charges + # update shipping (if applicable) + if shipping_method + ship_charge = order.shipping_charges.first + ship_charge ||= order.shipping_charges.build + ship_charge.amount = shipping_method.calculate_shipping(Shipment.new(:order => order, :address => ship_address)) + ship_charge.description = "#{I18n.t(:shipping)} (#{shipping_method.name})" + ship_charge.save + end + # update tax (if applicable) + tax_amount = order.calculate_tax + if tax_amount > 0 + tax_charge = order.tax_charges.first + tax_charge ||= order.tax_charges.build(:description => I18n.t(:tax)) + tax_charge.amount = tax_amount + tax_charge.save + end + + order.reload order.update_totals + order.save end end diff --git a/app/models/order.rb b/app/models/order.rb index 3633f0a711c..50b569bb0a2 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -1,7 +1,7 @@ class Order < ActiveRecord::Base - before_save :update_line_items before_create :generate_token - before_create :create_checkout + before_save :update_line_items, :update_totals + after_create :create_checkout has_many :line_items, :dependent => :destroy, :attributes => true has_many :inventory_units @@ -13,14 +13,15 @@ class Order < ActiveRecord::Base has_one :checkout has_one :bill_address, :through => :checkout has_one :ship_address, :through => :checkout - + has_many :charges, :order => :position + has_many :shipping_charges + has_many :tax_charges + delegate :email, :to => :checkout delegate :ip_address, :to => :checkout delegate :special_instructions, :to => :checkout validates_associated :line_items, :message => "are not valid" - validates_numericality_of :tax_amount - validates_numericality_of :ship_amount validates_numericality_of :item_total validates_numericality_of :total @@ -32,7 +33,7 @@ class Order < ActiveRecord::Base make_permalink :field => :number # attr_accessible is a nightmare with attachment_fu, so use attr_protected instead. - attr_protected :ship_amount, :tax_amount, :item_total, :total, :user, :number, :ip_address, :checkout_complete, :state, :token + attr_protected :charge_total, :item_total, :total, :user, :number, :state, :token def to_param self.number if self.number @@ -138,10 +139,14 @@ def item_total self.item_total = tot end - def total - self.total = self.item_total + self.ship_amount + self.tax_amount - end - + def ship_total + shipping_charges.inject(0) { |sum, charge| charge.amount + 0 } + end + + def tax_total + tax_charges.inject(0) { |sum, charge| charge.amount + 0 } + end + # convenience method since many stores will not allow user to create multiple shipments def shipment shipments.last @@ -172,16 +177,9 @@ def shipping_methods ShippingMethod.all.select { |method| method.zone.include?(ship_address) && method.available?(self) } end - def update_totals - # finalize order totals - tmp_shipment = shipment || Shipment.new(:order => self, :address => checkout.ship_address) - if checkout.shipping_method - self.ship_amount = checkout.shipping_method.calculate_shipping(tmp_shipment) if checkout.ship_address - else - self.ship_amount = 0 - end - self.tax_amount = calculate_tax - save! + def update_totals + self.charge_total = self.charges.inject(0) { |sum, charge| charge.amount + sum } + self.total = self.item_total + self.charge_total end def calculate_tax @@ -204,14 +202,13 @@ def complete_order shipments.build(:address => ship_address, :shipping_method => checkout.shipping_method) checkout.update_attribute(:completed_at, Time.now) InventoryUnit.sell_units(self) - #update_totals save_result = save! if email OrderMailer.deliver_confirm(self) end save_result - end - + end + def cancel_order restock_inventory OrderMailer.deliver_cancel(self) @@ -234,6 +231,6 @@ def generate_token end def create_checkout - self.checkout = Checkout.create unless self.checkout - end + self.checkout = Checkout.create(:order => self) unless self.checkout + end end diff --git a/app/models/shipment.rb b/app/models/shipment.rb index 255b7207025..7b31bbc9bad 100644 --- a/app/models/shipment.rb +++ b/app/models/shipment.rb @@ -4,7 +4,7 @@ class Shipment < ActiveRecord::Base belongs_to :address before_create :generate_shipment_number - after_save :recalculate_tax +# after_save :recalculate_tax after_save :transition_order attr_accessor :special_instructions @@ -41,8 +41,8 @@ def transition_order order.state_events.create(:name => I18n.t('ship'), :user => current_user, :previous_state => order.state_was) end - def recalculate_tax - return unless order && order.respond_to?(:calculate_tax) - order.update_attribute("tax_amount", order.calculate_tax) - end +# def recalculate_tax +# return unless order && order.respond_to?(:calculate_tax) +# order.update_attribute("tax_amount", order.calculate_tax) +# end end diff --git a/app/models/shipping_charge.rb b/app/models/shipping_charge.rb new file mode 100644 index 00000000000..4a559fb63b9 --- /dev/null +++ b/app/models/shipping_charge.rb @@ -0,0 +1,2 @@ +class ShippingCharge < Charge +end \ No newline at end of file diff --git a/app/models/tax_charge.rb b/app/models/tax_charge.rb new file mode 100644 index 00000000000..9b253494218 --- /dev/null +++ b/app/models/tax_charge.rb @@ -0,0 +1,2 @@ +class TaxCharge < Charge +end \ No newline at end of file diff --git a/app/views/admin/orders/show.html.erb b/app/views/admin/orders/show.html.erb index 4943d991aaa..5057336ea64 100644 --- a/app/views/admin/orders/show.html.erb +++ b/app/views/admin/orders/show.html.erb @@ -5,7 +5,7 @@ <%= render :partial => 'admin/shared/order_tabs', :locals => {:current => "Order Details"} %> -<%= render :partial => 'admin/shared/order_details', :locals => {:order => @order} -%> +<%= render :partial => 'shared/order_details', :locals => {:order => @order} -%> <% if @order.bill_address %>
diff --git a/app/views/admin/shared/_order_details.html.erb b/app/views/admin/shared/_order_details.html.erb deleted file mode 100644 index d626127d26f..00000000000 --- a/app/views/admin/shared/_order_details.html.erb +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - <% @order.line_items.each do |item| %> - - - - - - - <% end %> - - - - - - - - - - - - - - - - -
<%= t('item_description') %><%= t('price') %><%= t('qty') %><%= t('total') %>
<%=item.variant.product.name-%> <%= "(" + variant_options(item.variant) + ")" unless item.variant .option_values.empty? %><%= number_to_currency item.price -%><%=item.quantity-%><%= number_to_currency (item.price * item.quantity)-%>
<%= t('subtotal') %>:<%= number_to_currency @order.item_total -%>
<%= t('tax') %>:<%= number_to_currency @order.tax_amount -%>
<%= t('shipping') %>: - <% if @order.shipment %> - (<%= @order.shipment.shipping_method.name %>) - <% end %> - <%= number_to_currency @order.ship_amount -%>
<%= t('order_total') %>:<%= number_to_currency @order.total -%>
diff --git a/app/views/admin/tax_rates/index.html.erb b/app/views/admin/tax_rates/index.html.erb index 45fd3d15932..782a713893f 100644 --- a/app/views/admin/tax_rates/index.html.erb +++ b/app/views/admin/tax_rates/index.html.erb @@ -6,7 +6,7 @@
-

<%=t("ext_tax_calculator_tax_rates") %>

+

<%=t("tax_rates") %>

diff --git a/app/views/order_mailer/cancel.html.erb b/app/views/order_mailer/cancel.html.erb index 6c192f6802e..d0f6d6ea556 100644 --- a/app/views/order_mailer/cancel.html.erb +++ b/app/views/order_mailer/cancel.html.erb @@ -10,6 +10,7 @@ Order Summary [CANCELED] <% end -%> ============================================================ Subtotal: <%= number_to_currency @order.item_total %> -Tax: <%= number_to_currency @order.tax_amount %> -Shipping: <%= number_to_currency @order.ship_amount %> +<% @order.charges.each do |charge| %> + <%= "#{charge.description}: #{number_to_currency charge.amount}"%> +<% end %> Order Total: <%= number_to_currency @order.total %> diff --git a/app/views/order_mailer/confirm.html.erb b/app/views/order_mailer/confirm.html.erb index b7b95ea8533..a541b576beb 100644 --- a/app/views/order_mailer/confirm.html.erb +++ b/app/views/order_mailer/confirm.html.erb @@ -10,8 +10,9 @@ Order Summary <% end -%> ============================================================ Subtotal: <%= number_to_currency @order.item_total %> -Tax: <%= number_to_currency @order.tax_amount %> -Shipping: <%= number_to_currency @order.ship_amount %> +<% @order.charges.each do |charge| %> + <%= "#{charge.description}: #{number_to_currency charge.amount}"%> +<% end %> Order Total: <%= number_to_currency @order.total %> diff --git a/app/views/orders/_google_order.html.erb b/app/views/orders/_google_order.html.erb index 0da6f7d8b38..b3ac2a62b89 100644 --- a/app/views/orders/_google_order.html.erb +++ b/app/views/orders/_google_order.html.erb @@ -10,8 +10,8 @@ "<%= order.number %>", //Order Number "", //Affiliation "<%= order.total %>", //Order total - "<%= order.tax_amount %>", //Tax Amount - "<%= order.ship_amount %>", //Ship Amount + "<%= order.tax_charges.sum(:amount).to_s %>", //Tax Amount + "<%= order.shipping_charges.sum(:amount).to_s %>", //Ship Amount "", //City "", //State "" //Country diff --git a/app/views/shared/_order_details.html.erb b/app/views/shared/_order_details.html.erb index 9fa7318a96f..440d9b771b6 100644 --- a/app/views/shared/_order_details.html.erb +++ b/app/views/shared/_order_details.html.erb @@ -1,36 +1,38 @@ -
<%=t("zone")%>
- - - - - - - <% @order.line_items.each do |item| %> - - - - - - - <% end %> - - - - - - - - - - - - - - - - -
<%= t('item_description') %><%= t('price') %><%= t('qty') %><%= t('total') %>
<%=item.variant.product.name-%> <%= "(" + variant_options(item.variant) + ")" unless item.variant .option_values.empty? %><%= number_to_currency item.price -%><%=item.quantity-%><%= number_to_currency (item.price * item.quantity)-%>
<%= t('subtotal') %>:<%= number_to_currency @order.item_total -%>
<%= t('tax') %>:<%= number_to_currency @order.tax_amount -%>
<%= t('shipping') %>: - <% if @order.shipment && @order.shipment.shipping_method %> - (<%= @order.shipment.shipping_method.name %>) - <% end %> - <%= number_to_currency @order.ship_amount -%>
<%= t('order_total') %>:<%= number_to_currency @order.total -%>
+ + + + + + + + + <% @order.line_items.each do |item| %> + + + + + + + <% end %> + + + + + + + + + <% order.charges.each do |charge| %> + + + + + <% end %> + + + + + + + +
<%= t('item_description') %><%= t('price') %><%= t('qty') %><%= t('total') %>
<%=item.variant.product.name-%> <%= "(" + variant_options(item.variant) + ")" unless item.variant .option_values.empty? %><%= number_to_currency item.price -%><%=item.quantity-%><%= number_to_currency (item.price * item.quantity)-%>
<%= t('subtotal') %>:<%= number_to_currency @order.item_total -%>
<%= charge.description %><%= number_to_currency charge.amount -%>
<%= t('order_total') %>:<%= number_to_currency @order.total -%>
\ No newline at end of file diff --git a/config/locales/en-US.yml b/config/locales/en-US.yml index e1eceb82525..5289330d8d3 100644 --- a/config/locales/en-US.yml +++ b/config/locales/en-US.yml @@ -46,10 +46,8 @@ en-US: ip_address: "IP Address" item_total: "Item Total" number: Number - ship_amount: "Ship Amount" special_instructions: "Special Instructions" state: State - tax_amount: "Tax Amount" total: Total product: available_on: "Available On" diff --git a/db/migrate/20090603153927_create_checkouts.rb b/db/migrate/20090603153927_create_checkouts.rb index 0cfef24a157..e26f59f311e 100644 --- a/db/migrate/20090603153927_create_checkouts.rb +++ b/db/migrate/20090603153927_create_checkouts.rb @@ -1,6 +1,12 @@ class Order < ActiveRecord::Base belongs_to :bill_address, :foreign_key => "bill_address_id", :class_name => "Address" - belongs_to :ship_address, :foreign_key => "ship_address_id", :class_name => "Address" + belongs_to :ship_address, :foreign_key => "ship_address_id", :class_name => "Address" + has_many :shipments +end +Checkout.class_eval do + # temporarily disable the charge stuff since its interfering with this migration + def update_charges + end end class CreateCheckouts < ActiveRecord::Migration @@ -19,13 +25,13 @@ def self.up change_table :creditcards do |t| t.references :checkout end - + Checkout.reset_column_information Creditcard.reset_column_information # move address, etc. from order to checkout Order.all.each do |order| - shipping_method = order.shipment.shipping_method if order.shipment + shipping_method = order.shipments.last.shipping_method if order.shipments.present? completed_at = order.attributes["created_at"] if order.attributes["checkout_complete"] creditcard = Creditcard.find_by_order_id(order.id) checkout = Checkout.create(:order => order, diff --git a/db/migrate/20090618203233_create_charges.rb b/db/migrate/20090618203233_create_charges.rb new file mode 100644 index 00000000000..d30fc90f1fd --- /dev/null +++ b/db/migrate/20090618203233_create_charges.rb @@ -0,0 +1,36 @@ +class CreateCharges < ActiveRecord::Migration + def self.up + create_table :charges do |t| + t.references :order + t.string :type + t.decimal :amount, :precision => 8, :scale => 2, :default => 0.0, :null => false + t.string :description + t.integer :position + t.timestamps + end + + change_table :orders do |t| + t.decimal :charge_total, :precision => 8, :scale => 2, :default => 0.0, :null => false + end + + # create shipping and taxation charges for order, then drop columns + Order.reset_column_information + Order.all.each do |order| + order.attributes + ship_total = order.attributes["ship_amount"] || 0 + tax_total = order.attributes["tax_amount"] || 0 + order.shipping_charges.create(:amount => ship_total, :description => "Shipping") if ship_total > 0 + order.tax_charges.create(:amount => tax_total, :description => "Tax") if tax_total > 0 + order.update_attribute("charge_total", ship_total + tax_total) + end + + change_table :orders do |t| + t.remove :ship_amount + t.remove :tax_amount + end + end + + def self.down + drop_table :charges + end +end diff --git a/db/sample/charges.yml b/db/sample/charges.yml new file mode 100644 index 00000000000..18fbc8d4a20 --- /dev/null +++ b/db/sample/charges.yml @@ -0,0 +1,14 @@ +<% 1.upto(30) do |i| %> +tax_<%= i %>: + order: order_<%= i %> + amount: 2.78 + type: TaxCharge + description: Tax +<% end %> +<% 1.upto(30) do |i| %> +ship_<%= i %>: + order: order_<%= i %> + amount: 5 + type: ShippingCharge + description: Shipping +<% end %> \ No newline at end of file diff --git a/db/sample/checkouts.yml b/db/sample/checkouts.yml index bc2dcd66b5d..850a6d5b639 100644 --- a/db/sample/checkouts.yml +++ b/db/sample/checkouts.yml @@ -3,7 +3,7 @@ checkout_<%= i %>: ship_address: frank_ship_address_<%= i %> bill_address: frank_bill_address_<%= i %> email: frank.foo@example.com - order: order_<%= i %> - ip_address: 127.0.0.1 - completed_at: <%= Time.now %> + order: order_<%= i %> + completed_at: <%=Time.now%> + ip_address: 127.0.0.1 <% end %> \ No newline at end of file diff --git a/db/sample/orders.yml b/db/sample/orders.yml index 3c1dae66f65..32810f59a06 100644 --- a/db/sample/orders.yml +++ b/db/sample/orders.yml @@ -3,8 +3,6 @@ order_<%= i %>: user: frank number: <%= "R#{Array.new(9){rand(9)}.join}" %> state: new - ship_amount: 5.00 - tax_amount: 2.78 item_total: 37.97 total: 45.75 <% end %> \ No newline at end of file diff --git a/public/javascripts/checkout.js b/public/javascripts/checkout.js index 81e206b4204..928a7385854 100644 --- a/public/javascripts/checkout.js +++ b/public/javascripts/checkout.js @@ -301,10 +301,12 @@ var update_shipping_methods = function(methods) { }; var update_confirmation = function(order) { + var textToInsert = ''; + for (var key in order.charges) { + textToInsert += '' + key + '' + order.charges[key] + ''; + } + $('tbody#order-charges').html(textToInsert); $('span#order_total').html(order.order_total); - $('span#ship_amount').html(order.ship_amount); - $('span#tax_amount').html(order.tax_amount); - $('span#ship_method').html(order.ship_method); }; var submit_registration = function() { diff --git a/public/stylesheets/sass/_checkout.sass b/public/stylesheets/sass/_checkout.sass index 10bf9286f96..7f9c024a0a0 100644 --- a/public/stylesheets/sass/_checkout.sass +++ b/public/stylesheets/sass/_checkout.sass @@ -147,3 +147,6 @@ div#order :display block :text-align right :padding-right 150px + +tbody#subtotal + border-bottom:1px solid #DDDDDD; \ No newline at end of file diff --git a/test/factories.rb b/test/factories.rb index eaa9870c7f6..2f7f8dce4ff 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -1,5 +1,4 @@ Factory.define :order do |f| - f.line_items { [Factory(:line_item)] } f.charges { [Factory(:ship_charge), Factory(:tax_charge)] } end @@ -116,4 +115,14 @@ Factory.define :creditcard_txn do |f| f.amount 45.75 f.response_code 12345 +end + +Factory.define :ship_charge, :class => ShippingCharge do |f| + f.amount 8.99 + f.description "Shipping" +end + +Factory.define :tax_charge, :class => TaxCharge do |f| + f.amount 3.17 + f.description "Sales Tax" end \ No newline at end of file diff --git a/test/fixtures/charges.yml b/test/fixtures/charges.yml new file mode 100644 index 00000000000..5bf02933a3a --- /dev/null +++ b/test/fixtures/charges.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +# one: +# column: value +# +# two: +# column: value diff --git a/test/unit/charge_test.rb b/test/unit/charge_test.rb index f74d4b63bb6..83f804b5b14 100644 --- a/test/unit/charge_test.rb +++ b/test/unit/charge_test.rb @@ -5,16 +5,17 @@ class ChargeTest < ActiveSupport::TestCase should_validate_presence_of :description context "order instance with charges" do setup do - @charge = Factory(:tax_charge, :amount => 3.00) + @charge = Factory(:tax_charge, :amount => 3.17) @order = Factory(:order, :charges => [@charge]) end context "when adding another charge" do setup do @order.charges << Factory(:ship_charge, :amount => 8.99) @order.save - end - should_change "@order.total", :by => 8.99 - should_change "@order.charge_total", :by => 8.99 + end + + should_change "@order.total.to_f", :by => 8.99 + should_change "@order.charge_total.to_f", :by => 8.99 should_not_change "@order.item_total" end context "when destroying a charge" do @@ -22,8 +23,8 @@ class ChargeTest < ActiveSupport::TestCase @order.charges.clear @order.save end - should_change "@order.total.to_f", :by => -3.00 - should_change "@order.charge_total", :by => -3.00 + should_change "@order.total.to_f", :by => -3.17 + should_change "@order.charge_total.to_f", :by => -3.17 should_not_change "@order.item_total" end end diff --git a/test/unit/checkout_test.rb b/test/unit/checkout_test.rb index 366d41f29d5..e5257c4b526 100644 --- a/test/unit/checkout_test.rb +++ b/test/unit/checkout_test.rb @@ -34,6 +34,62 @@ class CheckoutTest < ActiveSupport::TestCase should_not_change "Creditcard.count" should_not_change "CreditcardPayment.count" should_not_change "CreditcardTxn.count" + end + end + + context "belonging to an order" do + setup do + @checkout = Checkout.new(:order => Order.create, :ship_address => Factory(:address), :bill_address => Factory(:address)) + end + context "with no shipping method" do + setup { @checkout.save } + should_not_change "@checkout.order.shipping_charges" end + context "with shipping method" do + setup do + @shipping_method = ShippingMethod.new + @checkout.shipping_method = @shipping_method + @shipping_method.stub!(:available?, :return => true) + @shipping_method.stub!(:calculate_shipping, :return => 10) + @checkout.save + end + should "increase shipping_charges by 1" do + assert_equal 1, @checkout.order.shipping_charges.size + end + should "have the correct value for the new shipping charge" do + assert_equal 10, @checkout.order.shipping_charges.first.amount + end + should_change "@checkout.order.total", :by => 10 + context "and shipping amount changes" do + setup do + @shipping_method.stub!(:calculate_shipping, :return => 20) + @checkout.save + end + should_not_change "@checkout.order.shipping_charges.count" + should_change "@checkout.order.shipping_charges.first.amount", :from => 10, :to => 20 + end + end + context "with taxable items" do + setup do + @checkout.order.stub!(:calculate_tax, :return => 15) + @checkout.save + end + should "increase tax_charges by 1" do + assert_equal 1, @checkout.order.tax_charges.size + end + should "have the correct value for the newly created tax charge" do + assert_equal 15, @checkout.order.tax_charges.first.amount + end + should_change "@checkout.order.total", :by => 15 + context "and tax amount changes" do + setup do + @checkout.order.stub!(:calculate_tax, :return => 8) + @checkout.save + end + should_not_change "@checkout.order.tax_charges.count" + should_change "@checkout.order.tax_charges.first.amount", :from => 15, :to => 8 + end + end end + end diff --git a/vendor/extensions/payment_gateway/lib/spree/payment_gateway.rb b/vendor/extensions/payment_gateway/lib/spree/payment_gateway.rb index 030699be1eb..fe2bb371f3b 100644 --- a/vendor/extensions/payment_gateway/lib/spree/payment_gateway.rb +++ b/vendor/extensions/payment_gateway/lib/spree/payment_gateway.rb @@ -85,8 +85,8 @@ def minimal_gateway_options :customer => checkout.email, :ip => checkout.ip_address, :order_id => checkout.order.number, - :shipping => checkout.order.ship_amount * 100, - :tax => checkout.order.tax_amount * 100, + :shipping => checkout.order.ship_total * 100, + :tax => checkout.order.tax_total * 100, :subtotal => checkout.order.item_total * 100} end