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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

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!

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: Test Ready 🧪
3 participants