-
-
Notifications
You must be signed in to change notification settings - Fork 729
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
[Checkout] Shipping fees update, remove order callback #13023
Merged
filipefurtad0
merged 10 commits into
openfoodfoundation:master
from
rioug:12907-fix-checkout-shipping-fee
Jan 24, 2025
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
20df5c2
Remove before save callback to update shipping fees
rioug f9bd720
Add spec for #update_shipping_fees!
rioug 9e7e40a
Update spec to properly test shipping fee update
rioug a8d1d0c
Update spec to properly update line items on an order
rioug 1afa7fe
Per review, small improvment
rioug c71ae35
Fix payment_total calculation
rioug 17e7f7d
Small linting fix
rioug d7ae91c
Per review, specify the actual expected value
rioug 0ca164a
Fix spec
rioug fcd366c
Fix spec
rioug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,40 +280,31 @@ | |
end | ||
end | ||
|
||
context "#process_payments!" do | ||
describe "#process_payments!" do | ||
let(:payment) { build(:payment) } | ||
before { allow(order).to receive_messages pending_payments: [payment], total: 10 } | ||
|
||
it "should return false if no pending_payments available" do | ||
it "returns false if no pending_payments available" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for tidying these 👍 |
||
allow(order).to receive_messages pending_payments: [] | ||
expect(order.process_payments!).to be_falsy | ||
end | ||
|
||
context "when the processing is sucessful" do | ||
it "should process the payments" do | ||
it "processes the payments" do | ||
expect(payment).to receive(:process!) | ||
expect(order.process_payments!).to be_truthy | ||
end | ||
|
||
it "stores the payment total on the order" do | ||
allow(payment).to receive(:process!) | ||
allow(payment).to receive(:completed?).and_return(true) | ||
|
||
order.process_payments! | ||
|
||
expect(order.payment_total).to eq(payment.amount) | ||
end | ||
end | ||
|
||
context "when a payment raises a GatewayError" do | ||
before { expect(payment).to receive(:process!).and_raise(Spree::Core::GatewayError) } | ||
|
||
it "should return true when configured to allow checkout on gateway failures" do | ||
it "returns true when configured to allow checkout on gateway failures" do | ||
Spree::Config.set allow_checkout_on_gateway_error: true | ||
expect(order.process_payments!).to be_truthy | ||
end | ||
|
||
it "should return false when not configured to allow checkout on gateway failures" do | ||
it "returns false when not configured to allow checkout on gateway failures" do | ||
Spree::Config.set allow_checkout_on_gateway_error: false | ||
expect(order.process_payments!).to be_falsy | ||
end | ||
|
@@ -1247,8 +1238,7 @@ | |
|
||
order.update_shipping_fees! | ||
|
||
item_num = order.line_items.sum(&:quantity) | ||
expect(order.reload.adjustment_total).to eq(item_num * shipping_fee) | ||
expect(order.reload.adjustment_total).to eq(15) # 3 items * 5 | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -69,6 +69,7 @@ | |||||
|
||||||
click_on "Complete order" | ||||||
expect(page).to have_content "Your order has been processed successfully" | ||||||
expect(page.find("#amount-paid").text).to have_content "$19.99" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using an ID, can we test for the associated label instead? (also below)
Suggested change
|
||||||
|
||||||
expect(order.reload.state).to eq "complete" | ||||||
expect(order.payments.count).to eq 1 | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It's a bit weird to take a payment without updating the total. But I guess that we are just moving the responsibility of updating the total out of the callback. And at some point, we may not want payments in a callback either.
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.
It was cause for me to look it up to make sure I understood. I found that four controllers update the
order.payment_total
(viaOrderManagement::Order::Updater#update_totals_and_states
):checkout_controller.rb
,voucher_adjustments_controller.rb
,api/v0/shipments_controller.rb
,admin/adjustments_controller.rb
It is updated by counting up all payments. That seems like a much safer method than the incrementing above.
https://github.com/openfoodfoundation/openfoodnetwork/blob/master/engines/order_management/app/services/order_management/order/updater.rb#L60
Very good!
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.
The issue was actually caused by this :
openfoodnetwork/app/models/spree/payment.rb
Lines 235 to 237 in 31c7173
which is triggered by an
after_save
callback.So by the time the code reach these deleted lines the payment was already added to
order.payment_total
. It was hidden by the factOrderManagement::Order::Updater#update_totals_and_states
was triggered further down the line, which now does not always happen.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.
I see. I can see that this triggers the same code (
order.payment_total = payments.completed.sum(:amount)
)If we can be sure that
update_totals_and_states
is always triggered down the line (by a controller), then perhaps we can remove this model callback also! But I'm afraid to suggest it in this PR..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.
I think that would need more investigation. I believe the payment callback is there to ensure the
order.payment_total
is updated when a payment is completed outside the checkout. Ie, capturing a cash payment in the backoffice. There might be other scenarios as well, It's not something I would want to do here.