From 5934b100e9256f3a86c005278bbcf2ea2b7cc2e0 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 27 Apr 2021 13:14:02 +0100 Subject: [PATCH 1/3] Test payment and shipment states in checkout spec If these aren't behaving correctly we need to know about it... --- .../consumer/shopping/checkout_spec.rb | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 14e6d151d0..d0f4c03241 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -255,6 +255,10 @@ feature "As a consumer I want to check out my cart", js: true do place_order expect(page).to have_content "Your order has been processed successfully" end.to enqueue_job ConfirmOrderJob + + order = Spree::Order.complete.last + expect(order.payment_state).to eq "balance_due" + expect(order.shipment_state).to eq "pending" end end end @@ -389,7 +393,7 @@ feature "As a consumer I want to check out my cart", js: true do end.to enqueue_job ConfirmOrderJob # And the order's special instructions should be set - order = Spree::Order.complete.first + order = Spree::Order.complete.last expect(order.special_instructions).to eq "SpEcIaL NoTeS" # Shipment and payments states should be set @@ -421,6 +425,10 @@ feature "As a consumer I want to check out my cart", js: true do it "takes us to the order confirmation page when submitted with 'same as billing address' checked" do place_order expect(page).to have_content "Your order has been processed successfully" + + order = Spree::Order.complete.last + expect(order.payment_state).to eq "balance_due" + expect(order.shipment_state).to eq "pending" end it "takes us to the cart page with an error when a product becomes out of stock just before we purchase", js: true do @@ -443,9 +451,11 @@ feature "As a consumer I want to check out my cart", js: true do expect(page).to have_content "Your order has been processed successfully" # There are two orders - our order and our new cart - o = Spree::Order.complete.first - expect(o.shipment_adjustments.first.amount).to eq(4.56) - expect(o.payments.first.amount).to eq(10 + 1.23 + 4.56) # items + fees + shipping + order = Spree::Order.complete.last + expect(order.shipment_adjustments.first.amount).to eq(4.56) + expect(order.payments.first.amount).to eq(10 + 1.23 + 4.56) # items + fees + shipping + expect(order.payment_state).to eq "balance_due" + expect(order.shipment_state).to eq "pending" end end @@ -464,9 +474,11 @@ feature "As a consumer I want to check out my cart", js: true do expect(page).to have_content "Your order has been processed successfully" # There are two orders - our order and our new cart - o = Spree::Order.complete.first - expect(o.all_adjustments.payment_fee.first.amount).to eq 5.67 - expect(o.payments.first.amount).to eq(10 + 1.23 + 5.67) # items + fees + transaction + order = Spree::Order.complete.last + expect(order.all_adjustments.payment_fee.first.amount).to eq 5.67 + expect(order.payments.first.amount).to eq(10 + 1.23 + 5.67) # items + fees + transaction + expect(order.payment_state).to eq "balance_due" + expect(order.shipment_state).to eq "pending" end end @@ -485,8 +497,10 @@ feature "As a consumer I want to check out my cart", js: true do expect(page).to have_content "Your order has been processed successfully" # Order should have a payment with the correct amount - o = Spree::Order.complete.first - expect(o.payments.first.amount).to eq(11.23) + order = Spree::Order.complete.last + expect(order.payments.first.amount).to eq(11.23) + expect(order.payment_state).to eq "paid" + expect(order.shipment_state).to eq "ready" end it "shows the payment processing failed message when submitted with an invalid credit card" do From a5a324fd6f803cf4762c32c238b13628c2111930 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 27 Apr 2021 13:15:44 +0100 Subject: [PATCH 2/3] Add failing spec for payment and shipment states bug The order was being left in an unexpected condition here, it's states are not set and there seem to be some weird issues. --- spec/features/admin/payments_spec.rb | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spec/features/admin/payments_spec.rb b/spec/features/admin/payments_spec.rb index d6c5f38d66..75b1de38c2 100644 --- a/spec/features/admin/payments_spec.rb +++ b/spec/features/admin/payments_spec.rb @@ -35,4 +35,33 @@ feature ' expect(page).to have_content I18n.t(:new_payment) end end + + context "creating an order's first payment via admin", js: true do + before do + order.update_columns( + state: "payment", + payment_state: nil, + shipment_state: nil, + completed_at: nil + ) + end + + xit "creates the payment, completes the order, and updates payment and shipping states" do + login_as_admin_and_visit spree.new_admin_order_payment_path order + + expect(page).to have_content "New Payment" + + within "#new_payment" do + find('input[type="radio"]').click + end + + click_button "Update" + expect(page).to have_content "Payments" + + order.reload + expect(order.state).to eq "complete" + expect(order.payment_state).to eq "balance_due" + expect(order.shipment_state).to eq "pending" + end + end end From 22f79fe5405b6dbe8a986074c52644ef6db88f24 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 27 Apr 2021 11:49:09 +0100 Subject: [PATCH 3/3] Reload order before updating in Payment callback --- app/models/spree/payment.rb | 2 +- spec/features/admin/payments_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/payment.rb b/app/models/spree/payment.rb index d16e428922..e9173d59a1 100644 --- a/app/models/spree/payment.rb +++ b/app/models/spree/payment.rb @@ -201,7 +201,7 @@ module Spree end def update_order - order.update! + order.reload.update! end # Necessary because some payment gateways will refuse payments with diff --git a/spec/features/admin/payments_spec.rb b/spec/features/admin/payments_spec.rb index 75b1de38c2..23baabd4ed 100644 --- a/spec/features/admin/payments_spec.rb +++ b/spec/features/admin/payments_spec.rb @@ -46,7 +46,7 @@ feature ' ) end - xit "creates the payment, completes the order, and updates payment and shipping states" do + it "creates the payment, completes the order, and updates payment and shipping states" do login_as_admin_and_visit spree.new_admin_order_payment_path order expect(page).to have_content "New Payment"