diff --git a/core/app/models/spree/payment/cancellation.rb b/core/app/models/spree/payment/cancellation.rb index 0360b313033..7beeaf5e8ae 100644 --- a/core/app/models/spree/payment/cancellation.rb +++ b/core/app/models/spree/payment/cancellation.rb @@ -26,16 +26,10 @@ def initialize(reason: DEFAULT_REASON) # @param payment [Spree::Payment] - the payment that should be canceled # def cancel(payment) - # For payment methods already implemeting `try_void` - if try_void_available?(payment.payment_method) - if response = payment.payment_method.try_void(payment) - payment.handle_void_response(response) - else - payment.refunds.create!(amount: payment.credit_allowed, reason: refund_reason, perform_after_create: false).perform! - end + if response = payment.payment_method.try_void(payment) + payment.handle_void_response(response) else - # For payment methods not yet implemeting `try_void` - deprecated_behavior(payment) + payment.refunds.create!(amount: payment.credit_allowed, reason: refund_reason, perform_after_create: false).perform! end end @@ -44,19 +38,6 @@ def cancel(payment) def refund_reason Spree::RefundReason.where(name: reason).first_or_create end - - def try_void_available?(payment_method) - payment_method.respond_to?(:try_void) && - payment_method.method(:try_void).owner != Spree::PaymentMethod - end - - def deprecated_behavior(payment) - Spree::Deprecation.warn "#{payment.payment_method.class.name}#cancel is deprecated and will be removed. " \ - 'Please implement a `try_void` method instead that returns a response object if void succeeds ' \ - 'or `false|nil` if not. Solidus will refund the payment then.' - response = payment.payment_method.cancel(payment.response_code) - payment.handle_void_response(response) - end end end end diff --git a/core/spec/models/spree/payment/cancellation_spec.rb b/core/spec/models/spree/payment/cancellation_spec.rb index b1223f63463..83fdd300ac1 100644 --- a/core/spec/models/spree/payment/cancellation_spec.rb +++ b/core/spec/models/spree/payment/cancellation_spec.rb @@ -23,59 +23,42 @@ let(:payment_method) { create(:payment_method) } let(:payment) { create(:payment, payment_method: payment_method, amount: 10) } - context 'if the payment_method responds to `try_void`' do - context 'if payment method returns void response' do - before do - expect(payment_method).to receive(:try_void).with(payment) { double } - end - - it 'handles the void' do - expect(payment).to receive(:handle_void_response) - subject - end + context 'if payment method returns void response' do + before do + expect(payment_method).to receive(:try_void).with(payment) { double } end - context 'if payment method rejects the void' do - before do - expect(payment_method).to receive(:try_void).with(payment) { false } - end - - it 'refunds the payment' do - expect { subject }.to change { payment.refunds.count }.from(0).to(1) - end - - context 'if payment has partial refunds' do - let(:credit_amount) { payment.amount / 2 } - - before do - payment.refunds.create!( - amount: credit_amount, - reason: Spree::RefundReason.where(name: 'test').first_or_create, - perform_after_create: false - ).perform! - end - - it 'only refunds the allowed credit amount' do - subject - refund = payment.refunds.last - expect(refund.amount).to eq(credit_amount) - end - end + it 'handles the void' do + expect(payment).to receive(:handle_void_response) + subject end end - context 'if the payment_method does not respond to `try_void`', partial_double_verification: false do + context 'if payment method rejects the void' do before do - allow(payment_method).to receive(:respond_to?) { false } - allow(payment_method).to receive(:cancel) { double } - allow(payment).to receive(:handle_void_response) - expect(Spree::Deprecation).to receive(:warn). - with(/^Spree::PaymentMethod::.*#cancel is deprecated and will be removed/, any_args) + expect(payment_method).to receive(:try_void).with(payment) { false } end - it 'calls cancel instead' do - expect(payment_method).to receive(:cancel) - subject + it 'refunds the payment' do + expect { subject }.to change { payment.refunds.count }.from(0).to(1) + end + + context 'if payment has partial refunds' do + let(:credit_amount) { payment.amount / 2 } + + before do + payment.refunds.create!( + amount: credit_amount, + reason: Spree::RefundReason.where(name: 'test').first_or_create, + perform_after_create: false + ).perform! + end + + it 'only refunds the allowed credit amount' do + subject + refund = payment.refunds.last + expect(refund.amount).to eq(credit_amount) + end end end end