From ddb54d1924369fba41568fce3489cd993168d211 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 15 Jul 2016 17:43:26 +1000 Subject: [PATCH] Delete fee on payment method if payment invalid PayPalExpress is always creating two payments. The first one is invalidated and the second one succeeds. Without deleting the old fee on the invalidated payment, the order lists the fee twice. --- app/helpers/checkout_helper.rb | 1 + app/models/spree/payment_decorator.rb | 7 ++++++- app/views/checkout/_summary.html.haml | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index 66d98d7862..6359cb01bd 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -6,6 +6,7 @@ module CheckoutHelper # Remove empty tax adjustments and (optionally) shipping fees adjustments.reject! { |a| a.originator_type == 'Spree::TaxRate' && a.amount == 0 } adjustments.reject! { |a| a.originator_type == 'Spree::ShippingMethod' } if exclude.include? :shipping + adjustments.reject! { |a| a.originator_type == 'Spree::PaymentMethod' } if exclude.include? :payment adjustments.reject! { |a| a.source_type == 'Spree::LineItem' } if exclude.include? :line_item enterprise_fee_adjustments = adjustments.select { |a| a.originator_type == 'EnterpriseFee' && a.source_type != 'Spree::LineItem' } diff --git a/app/models/spree/payment_decorator.rb b/app/models/spree/payment_decorator.rb index d178e1de54..dbee3cfd0f 100644 --- a/app/models/spree/payment_decorator.rb +++ b/app/models/spree/payment_decorator.rb @@ -5,7 +5,12 @@ module Spree after_save :ensure_correct_adjustment, :update_order def ensure_correct_adjustment - if adjustment + # Don't charge for invalid payments. + # PayPalExpress always creates a payment that is invalidated later. + # Unknown: What about failed payments? + if state == "invalid" + adjustment.andand.destroy + elsif adjustment adjustment.originator = payment_method adjustment.label = adjustment_label adjustment.save diff --git a/app/views/checkout/_summary.html.haml b/app/views/checkout/_summary.html.haml index a3b61c5ded..0fec419556 100644 --- a/app/views/checkout/_summary.html.haml +++ b/app/views/checkout/_summary.html.haml @@ -9,7 +9,7 @@ = t :checkout_cart_total %td.cart-total.text-right= display_checkout_subtotal(@order) - - checkout_adjustments_for(current_order, exclude: [:shipping, :line_item]).reject{ |a| a.amount == 0 }.each do |adjustment| + - checkout_adjustments_for(current_order, exclude: [:shipping, :payment, :line_item]).reject{ |a| a.amount == 0 }.each do |adjustment| %tr %th= adjustment.label %td.text-right= adjustment.display_amount.to_html