-
-
Notifications
You must be signed in to change notification settings - Fork 724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix checkout order total and payment fees calculation #12880
Fix checkout order total and payment fees calculation #12880
Conversation
Although the change fix the issue in the back office scenario, it has the side effect of getting the order total out of sync. Updating a payment adjustment need to be followed by udpating the order total and payment amount to keep everything in sync.
The order updater did not take into account payment fees on pending payment.
Previous iteration did not actually check the payment fee had been updated. It also checks the order total get correctly updated. Spec is passing, so fixing the order updater also fix this bug : openfoodfoundation#12512
Check if payment actually have an adjustment before trying to update it
The future is now ! :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done for finding that! Great work.
context "when updating cart after summary step" do | ||
let(:order) { | ||
create(:order_ready_for_payment, distributor:) | ||
} | ||
let!(:payment_with_fee) { | ||
create( | ||
:payment_method, | ||
distributors: [distributor], | ||
name: "Payment with Fee", description: "Payment with fee", | ||
calculator: Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) | ||
) | ||
} | ||
|
||
it "calculated the correct order total" do | ||
visit checkout_step_path(:payment) | ||
expect(page).to have_checked_field "Payment with Fee" | ||
|
||
click_button "Next - Order summary" | ||
expect(page).to have_title "Checkout Summary - Open Food Network" | ||
|
||
# Back to the shop | ||
click_link "Shopping @ #{distributor.name}" | ||
expect(page).to have_content(distributor.name) | ||
|
||
# Add item to cart | ||
within_variant(order.line_items.first.variant) do | ||
click_button increase_quantity_symbol | ||
end | ||
wait_for_cart | ||
|
||
# Checkout | ||
toggle_cart | ||
click_link "Checkout" | ||
|
||
# Check summary page total | ||
expect(page).to have_title "Checkout Summary - Open Food Network" | ||
expect(page).to have_selector("#order_total", text: 22.00) | ||
expect(order.reload.payments.first.amount).to eql(22.00) | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkllnk I was wondering if we actually need to keep this spec ? this is covered by the unit test on the order updater
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Let's remove it then.
It's covered by unit test of order updater
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, well done on fixing the whole bug!
Hi @rioug, Thanks for working on this! Unfortunately I couldn't reproduce this bug in current master. I can observe the described behaviour, but it's for the shipping fees, not for the payment fees. And this actually is still the case after staging your PR. Before staging = current masterAfter editing the order and returning to checkout: The calculation makes no sense at all: 50 + 5 + 5 = 60 != 58 --> order total is incorrect ❌ Order confirmation shows the old shipping fee, the payment fee is updated correctly: The calculation is correct, but the shipping calculation is based on the old cart, before adding another 20 $ product. ❌ After stagingAfter editing the order and returning to checkout: The calculation makes sense now: 50 + 5 + 5 = 60 --> order total is correct ✔️ Order confirmation still shows the old shipping fee, the payment fee is updated correctly: The calculation is correct, but the shipping calculation is based on the old cart, before adding another 20 $ product. ❌ ConclusionThe original issue mentioned in the opening post was fixed in some earlier PR already. This PR here is about something else - but it's definitely great! I'm tempted to merge this one, because it's fixing the incorrect calculation on the order summary page. However, maybe fixing the bit with the incorrect shipping fee will make this unnecessary? I'm not sure. Adding the feedback needed label for our devs to look at it and decide. I will also open an issue on the missing update of shipping fees. ➡️ #12907 Thanks again! |
@konrad I just tried on master, and the issue is still there. |
What exactly do you mean by "the issue", @rioug? The backoffice issue regarding payment fee (opening post in #5574) I could not reproduce. That's why I closed it. The shopfront issue regarding shipping fee is still there, but has no issue. That's why I opened #12907. |
Hmm, interesting! It's a bit confusing. You're right, in master the Order Summary page still uses the old transaction fee. Before stagingTransaction fee onlyBefore editing the orderAfter editing the orderOrder confirmation pageTransaction fee and shipping feeBefore editing the orderAfter editing the orderOrder confirmation pageAfter stagingTransaction fee onlyBefore editing the orderAfter editing the orderOrder confirmation pageTransaction fee and shipping feeBefore editing the orderAfter editing the orderOrder confirmation pageConclusionThe issue around the payment fee is solved by your PR. 💪 |
What? Why?
The order updater wasn't updating payment fee when an order was updated after payment had already been added.This resulted in the order total being wrong on the checkout summary page. The fee was getting updated once the order was finalised but it wasn't added to the payment amount, this resulted in customer owing money when they paid with Stripe, the owed money being the missing payment fee.
Because of the fix introduced by fixing #12512, the payment fees was actually correct on the summary page.
To fix this, I made sure that payment fees are recalculated if the order has a pending payment, and then update the order total and payment total accordingly.
What should we test?
Testing #5574 :
In the back end :
In the front end
With a Stripe payment method:
Use the same scenario as above
Testing #12512
In the back office:
Release notes
Changelog Category (reviewers may add a label for the release notes):
The title of the pull request will be included in the release notes.
Dependencies
Documentation updates