From 0af8377844bce6decc1bdffc9526913deebabb22 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 18 May 2016 15:40:08 +1000 Subject: [PATCH] Add payment method fees to order during checkout The fee is displayed as "Transaction fee". --- .../darkswarm/services/checkout.js.coffee | 5 ++++- .../services/payment_methods.js.coffee | 1 + app/helpers/checkout_helper.rb | 9 +++++++++ app/models/spree/payment_decorator.rb | 19 +++++++++++++++++++ .../api/payment_method_serializer.rb | 7 ++++++- app/views/checkout/_payment.html.haml | 1 + app/views/checkout/_summary.html.haml | 5 +++++ config/locales/en.yml | 1 + spec/features/admin/adjustments_spec.rb | 4 ++-- 9 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/checkout.js.coffee b/app/assets/javascripts/darkswarm/services/checkout.js.coffee index 34cae22fcc..0ab94f61a4 100644 --- a/app/assets/javascripts/darkswarm/services/checkout.js.coffee +++ b/app/assets/javascripts/darkswarm/services/checkout.js.coffee @@ -62,8 +62,11 @@ Darkswarm.factory 'Checkout', (CurrentOrder, ShippingMethods, PaymentMethods, $h shippingPrice: -> @shippingMethod()?.price || 0.0 + paymentPrice: -> + @paymentMethod()?.price || 0.0 + paymentMethod: -> PaymentMethods.payment_methods_by_id[@order.payment_method_id] cartTotal: -> - @shippingPrice() + @order.display_total + @order.display_total + @shippingPrice() + @paymentPrice() diff --git a/app/assets/javascripts/darkswarm/services/payment_methods.js.coffee b/app/assets/javascripts/darkswarm/services/payment_methods.js.coffee index 8b44d3e80a..423ceb57e2 100644 --- a/app/assets/javascripts/darkswarm/services/payment_methods.js.coffee +++ b/app/assets/javascripts/darkswarm/services/payment_methods.js.coffee @@ -4,5 +4,6 @@ Darkswarm.factory "PaymentMethods", (paymentMethods)-> payment_methods_by_id: {} constructor: -> for method in @payment_methods + method.price = parseFloat(method.price) @payment_methods_by_id[method.id] = method diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index 080db9c0e8..66d98d7862 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -94,4 +94,13 @@ module CheckoutHelper current_order.tokenized_permission.save! session[:access_token] = token end + + def payment_method_price(method, order) + price = method.compute_amount(order) + if price == 0 + t('checkout_method_free') + else + "{{ #{price} | localizeCurrency }}" + end + end end diff --git a/app/models/spree/payment_decorator.rb b/app/models/spree/payment_decorator.rb index 1a80f0269f..fda67fedc8 100644 --- a/app/models/spree/payment_decorator.rb +++ b/app/models/spree/payment_decorator.rb @@ -1,5 +1,24 @@ module Spree Payment.class_eval do + has_one :adjustment, as: :source, dependent: :destroy + + after_save :ensure_correct_adjustment, :update_order + + def ensure_correct_adjustment + if adjustment + adjustment.originator = payment_method + adjustment.label = adjustment_label + adjustment.save + else + payment_method.create_adjustment(adjustment_label, order, self, true) + reload + end + end + + def adjustment_label + I18n.t('payment_method_fee') + end + # Pin payments lacks void and credit methods, but it does have refund # Here we swap credit out for refund and remove void as a possible action def actions_with_pin_payment_adaptations diff --git a/app/serializers/api/payment_method_serializer.rb b/app/serializers/api/payment_method_serializer.rb index d62bc18154..a135f5d792 100644 --- a/app/serializers/api/payment_method_serializer.rb +++ b/app/serializers/api/payment_method_serializer.rb @@ -1,3 +1,8 @@ class Api::PaymentMethodSerializer < ActiveModel::Serializer - attributes :name, :description, :id, :method_type + attributes :name, :description, :id, :method_type, + :price + + def price + object.compute_amount(options[:current_order]) + end end diff --git a/app/views/checkout/_payment.html.haml b/app/views/checkout/_payment.html.haml index c11d7cd45e..c0578a8a4c 100644 --- a/app/views/checkout/_payment.html.haml +++ b/app/views/checkout/_payment.html.haml @@ -26,6 +26,7 @@ name: "order.payment_method_id", "ng-model" => "order.payment_method_id" = method.name + = "(#{payment_method_price(method, @order)})" %small.error.medium.input-text{"ng-show" => "!fieldValid('order.payment_method_id')"} = "{{ fieldErrors('order.payment_method_id') }}" diff --git a/app/views/checkout/_summary.html.haml b/app/views/checkout/_summary.html.haml index 3a4f6a4ea1..a3b61c5ded 100644 --- a/app/views/checkout/_summary.html.haml +++ b/app/views/checkout/_summary.html.haml @@ -19,6 +19,11 @@ = t :checkout_shipping_price %td.shipping.text-right {{ Checkout.shippingPrice() | localizeCurrency }} + %tr + %th + = t :payment_method_fee + %td.text-right {{ Checkout.paymentPrice() | localizeCurrency }} + %tr %th = t :checkout_total_price diff --git a/config/locales/en.yml b/config/locales/en.yml index 3249f00d5a..649b7bcf74 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1038,6 +1038,7 @@ Please follow the instructions there to make your enterprise visible on the Open properties: "Properties" shipping_methods: "Shipping Methods" payment_methods: "Payment Methods" + payment_method_fee: "Transaction fee" enterprise_fees: "Enterprise Fees" inventory_settings: "Inventory Settings" tag_rules: "Tag Rules" diff --git a/spec/features/admin/adjustments_spec.rb b/spec/features/admin/adjustments_spec.rb index b9a6ee3465..a051d28b2b 100644 --- a/spec/features/admin/adjustments_spec.rb +++ b/spec/features/admin/adjustments_spec.rb @@ -48,7 +48,7 @@ feature %q{ visit spree.admin_orders_path page.find('td.actions a.icon-edit').click click_link 'Adjustments' - page.find('td.actions a.icon-edit').click + page.find('tr', text: 'Shipping').find('a.icon-edit').click # Then I should see the uneditable included tax and our tax rate as the default page.should have_field :adjustment_included_tax, with: '10.00', disabled: true @@ -72,7 +72,7 @@ feature %q{ visit spree.admin_orders_path page.find('td.actions a.icon-edit').click click_link 'Adjustments' - page.find('td.actions a.icon-edit').click + page.find('tr', text: 'Shipping').find('a.icon-edit').click # Then I should see the uneditable included tax and 'Remove tax' as the default tax rate page.should have_field :adjustment_included_tax, with: '0.00', disabled: true