Skip to content

Commit

Permalink
Merge pull request #3844 from nebulab/kennyadsl/use-try-void-in-payme…
Browse files Browse the repository at this point in the history
…nt-cancel

Raise canceling a payment when try_void is not implemented
  • Loading branch information
kennyadsl authored Nov 18, 2020
2 parents 43486c0 + 8cc77f4 commit f37386e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 67 deletions.
25 changes: 3 additions & 22 deletions core/app/models/spree/payment/cancellation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
73 changes: 28 additions & 45 deletions core/spec/models/spree/payment/cancellation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f37386e

Please sign in to comment.