Merge pull request #3563 from kristinalim/fix/3562-fix_payment_fee_for_order_based_calculator

3562 Fix error calculating payment fee using order-based calculator
This commit is contained in:
Pau Pérez Fabregat
2019-03-06 19:31:49 +01:00
committed by GitHub
2 changed files with 24 additions and 9 deletions

View File

@@ -4,6 +4,8 @@ module Spree
Payment.class_eval do
extend Spree::LocalizedNumber
delegate :line_items, to: :order
has_one :adjustment, as: :source, dependent: :destroy
after_save :ensure_correct_adjustment, :update_order
@@ -30,15 +32,6 @@ module Spree
I18n.t('payment_method_fee')
end
# This is called by the calculator of a payment method
def line_items
if order.complete? && Spree::Config[:track_inventory_levels]
order.line_items.select { |li| inventory_units.pluck(:variant_id).include?(li.variant_id) }
else
order.line_items
end
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

View File

@@ -133,6 +133,28 @@ module Spree
order.reload.update!
end
context "when order-based calculator" do
let!(:shop) { create(:enterprise) }
let!(:payment_method) { create(:payment_method, calculator: calculator) }
let!(:calculator) do
Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10)
end
context "when order complete and inventory tracking enabled" do
let!(:order) { create(:completed_order_with_totals, distributor: shop) }
let!(:variant) { order.line_items.first.variant }
let!(:inventory_item) { create(:inventory_item, enterprise: shop, variant: variant) }
it "creates adjustment" do
payment = create(:payment, order: order, payment_method: payment_method,
amount: order.total)
expect(payment.adjustment).to be_present
expect(payment.adjustment.amount).not_to eq(0)
end
end
end
context "to Stripe payments" do
let(:shop) { create(:enterprise) }
let(:payment_method) { create(:stripe_payment_method, distributor_ids: [create(:distributor_enterprise).id], preferred_enterprise_id: shop.id) }