Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/decorators/models/spree/refund_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Spree
module RefundDecorator
attr_reader :response

Spree::Refund.prepend(self)
end
end
15 changes: 15 additions & 0 deletions app/models/spree/payment_method/stripe_credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ def void(response_code, _creditcard, _transaction_options)
gateway.void(response_code, {})
end

def payment_intents_refund_reason
Spree::RefundReason.where(name: Spree::Payment::Cancellation::DEFAULT_REASON).first_or_create
end

def try_void(payment)
if v3_intents? && payment.completed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you just return false here? It seems like solidus already handles the refund here 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChristianRimondi that way the payment would remain to its original state, instead of becomingvoid, as this line would not run. That line executes this method.

payment.refunds.create!(
amount: payment.credit_allowed,
reason: payment_intents_refund_reason
).response
else
payment.void_transaction!
end
end

def cancel(response_code)
gateway.void(response_code, {})
end
Expand Down
45 changes: 44 additions & 1 deletion spec/models/spree/payment_method/stripe_credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
let(:payment) {
double('Spree::Payment',
source: source,
order: order
order: order,
amount: order.total
)
}

Expand Down Expand Up @@ -241,4 +242,46 @@
expect(gateway).to receive(:capture).with(9855, '12345', anything).and_return(success_response)
end
end

describe '#try_void' do
let(:payment) { create :payment, amount: order.total }

shared_examples 'voids the payment transaction' do
it 'voids the payment transaction' do
expect(payment).to receive(:void_transaction!)

subject.try_void(payment)
end
end

context 'when using Payment Intents' do
before { subject.preferred_v3_intents = true }

context 'when the payment is completed' do
before do
allow(payment).to receive(:completed?) { true }
end

it 'creates a refund' do
expect { subject.try_void(payment) }.to change { Spree::Refund.count }.by(1)
end
end

context 'when the payment is not completed' do
it_behaves_like 'voids the payment transaction'
end
end

context 'when not using Payment Intents' do
before { subject.preferred_v3_intents = false }

context 'when the payment is completed' do
it_behaves_like 'voids the payment transaction'
end

context 'when the payment is not completed' do
it_behaves_like 'voids the payment transaction'
end
end
end
end