Skip to content
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

Merged

Conversation

rioug
Copy link
Collaborator

@rioug rioug commented Oct 1, 2024

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 :

  • Set up a Cash payment method with a Flat Percent calculator
  • Assign it to your a shop

In the front end

  • Start placing an order, selecting the Cash payment method and stop at Confirmation step
  • Continue shopping by clicking on the shop name next the cart
  • Add or update the quantity of an item
  • Continue checkout, you should land back on the Confirmation step
  • -> check the order total and payment fees are correct
  • Confirm the order
  • Confirmation page and email should have the same order total and payment fee as the confirmation step

With a Stripe payment method:

  • Set up a Stripe connect with a Flat Percent calculator

Use the same scenario as above

Testing #12512

In the back office:

  • search for a completed order with a percentage fee payment method
  • add a product or update the quantity of one of the product
  • check that order total and payment fee are updated accodingly

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

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
@rioug rioug added the user facing changes Thes pull requests affect the user experience label Oct 1, 2024
The future is now ! :D
Copy link
Member

@mkllnk mkllnk left a 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.

Comment on lines 314 to 354
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

Copy link
Collaborator Author

@rioug rioug Oct 1, 2024

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

Copy link
Member

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
@dacook dacook added the bug-s2 The bug is affecting any of the non-critical features described in S1 and there is no workaround. label Oct 3, 2024
Copy link
Member

@dacook dacook left a 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!

@drummer83 drummer83 self-assigned this Oct 11, 2024
@drummer83 drummer83 added the pr-staged-au staging.openfoodnetwork.org.au label Oct 11, 2024
@drummer83
Copy link
Contributor

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 master

After editing the order and returning to checkout:
image

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:
image

The calculation is correct, but the shipping calculation is based on the old cart, before adding another 20 $ product. ❌

After staging

After editing the order and returning to checkout:
image

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:
image

The calculation is correct, but the shipping calculation is based on the old cart, before adding another 20 $ product. ❌

Conclusion

The 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!
We don't have a full fix of the issue with different order totals on the order summary screen and the order confirmation screen. However, at least the calculation on the order summary page is correct now - but still the order confirmation is based on incorrect values due to the not updated shipping fees.

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!

@drummer83 drummer83 removed their assignment Oct 11, 2024
@drummer83 drummer83 added feedback-needed and removed pr-staged-au staging.openfoodnetwork.org.au labels Oct 11, 2024
@rioug
Copy link
Collaborator Author

rioug commented Oct 14, 2024

@konrad I just tried on master, and the issue is still there.

@drummer83
Copy link
Contributor

drummer83 commented Oct 14, 2024

@konrad I just tried on master, and the issue is still there.

What exactly do you mean by "the issue", @rioug?
Shopfront or backoffice?
Payment fee or shipping fee?

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.

@rioug
Copy link
Collaborator Author

rioug commented Oct 14, 2024

What exactly do you mean by "the issue", @rioug?

I meant the payment fee not being correctly calculated in the front end, you should be able to reproduce it in master with the testing notes for #5574 (see PR description)

@drummer83 drummer83 self-assigned this Oct 16, 2024
@drummer83 drummer83 added the pr-staged-au staging.openfoodnetwork.org.au label Oct 16, 2024
@drummer83
Copy link
Contributor

Hmm, interesting! It's a bit confusing.

You're right, in master the Order Summary page still uses the old transaction fee.
But, the Order Confirmation page is using the old shipping fee.
The issue around the transaction fee is solved with your PR. 💪

Before staging

Transaction fee only

Before editing the order

image

After editing the order

image

Order confirmation page

image

Transaction fee and shipping fee

Before editing the order

image

After editing the order

image

Order confirmation page

image

After staging

Transaction fee only

Before editing the order

image

After editing the order

image

Order confirmation page

image

Transaction fee and shipping fee

Before editing the order

image

After editing the order

image

Order confirmation page

image

Conclusion

The issue around the payment fee is solved by your PR. 💪
The issue around the shipping fee (#12907) is still open. This should be handled separately.
Merging this one then! 🥳
Thanks again, @rioug! 🙏

@drummer83 drummer83 merged commit a023443 into openfoodfoundation:master Oct 16, 2024
59 checks passed
@drummer83 drummer83 removed the pr-staged-au staging.openfoodnetwork.org.au label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-s2 The bug is affecting any of the non-critical features described in S1 and there is no workaround. user facing changes Thes pull requests affect the user experience
Projects
Status: Done
4 participants