From 8bd890f31187c87259a42258ae6ce19cf167df13 Mon Sep 17 00:00:00 2001 From: Ana Nunes da Silva Date: Sun, 5 Feb 2023 17:04:05 +0000 Subject: [PATCH 1/5] Add :checkout scope in Spree::Payment --- app/models/spree/payment.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/spree/payment.rb b/app/models/spree/payment.rb index 4d61396129..3d6e59ea36 100644 --- a/app/models/spree/payment.rb +++ b/app/models/spree/payment.rb @@ -47,6 +47,7 @@ module Spree scope :with_state, ->(s) { where(state: s.to_s) } scope :completed, -> { with_state('completed') } scope :incomplete, -> { where(state: %w(checkout pending requires_authorization)) } + scope :checkout, -> { with_state('checkout') } scope :pending, -> { with_state('pending') } scope :failed, -> { with_state('failed') } scope :valid, -> { where.not(state: %w(failed invalid)) } From b48b3ad42bd26e7752c6e1dddfe7915eb0155611 Mon Sep 17 00:00:00 2001 From: Ana Nunes da Silva Date: Mon, 6 Feb 2023 11:28:24 +0000 Subject: [PATCH 2/5] Update: Untaken cash payments to void on cancelled order --- app/models/spree/order.rb | 1 + .../order_management/order/updater_spec.rb | 2 +- spec/models/spree/order_spec.rb | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 802992a73c..8a4401ff3e 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -665,6 +665,7 @@ module Spree def after_cancel shipments.each(&:cancel!) + payments.checkout.each(&:void!) OrderMailer.cancel_email(id).deliver_later if send_cancellation_email update(payment_state: updater.update_payment_state) diff --git a/engines/order_management/spec/services/order_management/order/updater_spec.rb b/engines/order_management/spec/services/order_management/order/updater_spec.rb index 23827e1c85..718cdc662b 100644 --- a/engines/order_management/spec/services/order_management/order/updater_spec.rb +++ b/engines/order_management/spec/services/order_management/order/updater_spec.rb @@ -61,7 +61,7 @@ module OrderManagement expect(order.shipment_state).to be_nil end - ["shipped", "ready", "pending"].each do |state| + ["shipped", "ready", "pending", "canceled"].each do |state| it "is #{state}" do allow(shipment).to receive(:state).and_return(state) updater.update_shipment_state diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 8c1adba511..51f164a13b 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -303,6 +303,29 @@ describe Spree::Order do end end + describe "#cancel" do + let(:order) { create(:order_with_totals_and_distribution, :completed) } + + before { order.cancel! } + + it "should cancel the order" do + expect(order.state).to eq 'canceled' + end + + it "should cancel the shipments" do + expect(order.shipments.pluck(:state)).to eq ['canceled'] + end + + context "when payment has not been taken" do + context "and payment is in checkout state" do + it "should change the state of the payment to void" do + order.payments.reload + expect(order.payments.pluck(:state)).to eq ['void'] + end + end + end + end + context "insufficient_stock_lines" do let(:line_item) { build(:line_item) } From f2b407c194245cc59e17aa0a3ebf86d2198d4368 Mon Sep 17 00:00:00 2001 From: Ana Nunes da Silva Date: Mon, 6 Feb 2023 11:32:43 +0000 Subject: [PATCH 3/5] Add :void scope in Spree::Payment --- app/models/spree/payment.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/spree/payment.rb b/app/models/spree/payment.rb index 3d6e59ea36..33aac13d66 100644 --- a/app/models/spree/payment.rb +++ b/app/models/spree/payment.rb @@ -51,6 +51,7 @@ module Spree scope :pending, -> { with_state('pending') } scope :failed, -> { with_state('failed') } scope :valid, -> { where.not(state: %w(failed invalid)) } + scope :void, -> { with_state('void') } scope :authorization_action_required, -> { where.not(cvv_response_message: nil) } scope :requires_authorization, -> { with_state("requires_authorization") } scope :with_payment_intent, ->(code) { where(response_code: code) } From c9e6d24eb0ba06ec9d7f5ff0e9bd6310c521b3a9 Mon Sep 17 00:00:00 2001 From: Ana Nunes da Silva Date: Mon, 6 Feb 2023 11:33:47 +0000 Subject: [PATCH 4/5] Add :resume event in Spree::Payment --- app/models/spree/payment.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/spree/payment.rb b/app/models/spree/payment.rb index 33aac13d66..13a546990d 100644 --- a/app/models/spree/payment.rb +++ b/app/models/spree/payment.rb @@ -91,6 +91,10 @@ module Spree event :complete_authorization do transition from: [:requires_authorization], to: :completed end + event :resume do + transition from: [:void], to: :checkout + end + after_transition to: :completed, do: :set_captured_at end From f4980fa84ded6728218bf954948d524fa327e5ee Mon Sep 17 00:00:00 2001 From: Ana Nunes da Silva Date: Mon, 6 Feb 2023 11:37:30 +0000 Subject: [PATCH 5/5] Update void payments to checkout on resumed order --- app/models/spree/order.rb | 2 ++ spec/models/spree/order_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 8a4401ff3e..e696b872e9 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -673,6 +673,8 @@ module Spree def after_resume shipments.each(&:resume!) + payments.void.each(&:resume!) + update(payment_state: updater.update_payment_state) end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 51f164a13b..400c02fe86 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -326,6 +326,30 @@ describe Spree::Order do end end + describe "#resume" do + let(:order) { create(:order_with_totals_and_distribution, :completed) } + + before do + order.cancel! + order.resume! + end + + it "should resume the order" do + expect(order.state).to eq 'resumed' + end + + it "should resume the shipments" do + expect(order.shipments.pluck(:state)).to eq ['pending'] + end + + context "when payment is in void state" do + it "should change the state of the payment to checkout" do + order.payments.reload + expect(order.payments.pluck(:state)).to eq ['checkout'] + end + end + end + context "insufficient_stock_lines" do let(:line_item) { build(:line_item) }