Merge pull request #12880 from rioug/5574-fix-checkout-order-total-calc

Fix checkout order total and payment fees calculation
This commit is contained in:
Konrad
2024-10-16 21:16:34 +02:00
committed by GitHub
5 changed files with 50 additions and 12 deletions

View File

@@ -155,7 +155,6 @@ module Spree
if adjustment
adjustment.originator = payment_method
adjustment.label = adjustment_label
adjustment.amount = payment_method.compute_amount(self)
adjustment.save
elsif !processing_refund? && payment_method.present?
payment_method.create_adjustment(adjustment_label, self, true)

View File

@@ -240,7 +240,30 @@ module OrderManagement
return unless order.state.in? ["payment", "confirmation", "complete"]
return unless order.pending_payments.any?
order.pending_payments.first.update_attribute :amount, order.total
@payment = order.pending_payments.first
return update_payment if @payment.adjustment.nil?
# Update payment tax fees if needed
new_amount = @payment.payment_method.compute_amount(@payment)
if new_amount != @payment.adjustment.amount
update_payment_adjustment(new_amount)
end
update_payment
end
def update_payment
# Update payment with correct amount
@payment.update_attribute :amount, order.total
end
def update_payment_adjustment(amount)
@payment.adjustment.update_attribute(:amount, amount)
# Update order total to take into account updated payment fees
update_adjustment_total
update_order_total
persist_totals
end
end
end

View File

@@ -121,7 +121,7 @@ module OrderManagement
updater.update
end
context "whith pending payments" do
context "with pending payments" do
let(:order) { create(:completed_order_with_totals) }
it "updates pending payments" do
@@ -183,6 +183,19 @@ module OrderManagement
expect { updater.update }.to change { payment.reload.amount }.from(10).to(20)
end
it "updates pending payments fees" do
calculator = build(:calculator_flat_percent_per_item, preferred_flat_percent: 10)
payment_method = create(:payment_method, name: "Percentage cash", calculator:)
payment = create(:payment, payment_method:, order:, amount: order.total)
# update order so the order total will change
update_order_quantity(order)
order.payments.reload
expect { updater.update }.to change { payment.reload.amount }.from(10).to(22)
.and change { payment.reload.adjustment.amount }.from(1).to(2)
end
end
context "with order in cart" do

View File

@@ -179,19 +179,22 @@ RSpec.describe '
context "When adding a product on an order with transaction fee" do
let(:order_with_fees) { create(:completed_order_with_fees, user:, distributor:, order_cycle: ) }
it 'recalculates transaction fee' do
it "recalculates transaction fee and order total" do
login_as_admin
visit spree.edit_admin_order_path(order_with_fees)
adjustment_for_transaction_fee = order_with_fees.all_adjustments.payment_fee.eligible.first
transaction_fee = adjustment_for_transaction_fee.amount
# Fee is $5 per item and we have two line items
expect(page).to have_css("#order_adjustments", text: 10.00)
expect(page).to have_css(".order-total", text: 36.00)
expect(page.find("#order_adjustments").text).to have_content(transaction_fee)
expect {
select2_select product.name, from: 'add_variant_id', search: true
find('button.add_variant').click
}.to change { order_with_fees.payments.first.adjustment.amount }.from(10.00).to(15.00)
.and change { order_with_fees.reload.total }.from(36.00).to(63.99)
select2_select product.name, from: 'add_variant_id', search: true
find('button.add_variant').click
expect(page).to have_css("#order_adjustments",
text: adjustment_for_transaction_fee.reload.amount)
expect(page).to have_css("#order_adjustments", text: 15.00)
expect(page).to have_css(".order-total", text: 63.99)
end
end

View File

@@ -10,6 +10,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do
include StripeStubs
include PaypalHelper
include AuthenticationHelper
include UIComponentHelper
let!(:zone) { create(:zone_with_member) }
let(:supplier) { create(:supplier_enterprise) }
@@ -48,7 +49,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do
before do
login_as(user)
visit checkout_path
end
context "summary step" do