Skip to content

Commit

Permalink
Merge pull request #12880 from rioug/5574-fix-checkout-order-total-calc
Browse files Browse the repository at this point in the history
Fix checkout order total and payment fees calculation
  • Loading branch information
drummer83 authored Oct 16, 2024
2 parents ed668de + aa5feb6 commit a023443
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
1 change: 0 additions & 1 deletion app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ def ensure_correct_adjustment
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,30 @@ def update_pending_payment
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module Order
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
Expand Down Expand Up @@ -183,6 +183,19 @@ module Order

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
Expand Down
19 changes: 11 additions & 8 deletions spec/system/admin/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,22 @@ def new_order_with_distribution(distributor, order_cycle)
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

Expand Down
2 changes: 1 addition & 1 deletion spec/system/consumer/checkout/summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
include StripeStubs
include PaypalHelper
include AuthenticationHelper
include UIComponentHelper

let!(:zone) { create(:zone_with_member) }
let(:supplier) { create(:supplier_enterprise) }
Expand Down Expand Up @@ -48,7 +49,6 @@

before do
login_as(user)
visit checkout_path
end

context "summary step" do
Expand Down

0 comments on commit a023443

Please sign in to comment.