use capture! if payment is already authorized

This commit is contained in:
Andy Brett
2021-04-27 17:59:03 -07:00
parent 1d29e277f2
commit 9906afa1a6
3 changed files with 35 additions and 3 deletions

View File

@@ -6,14 +6,22 @@ module Spree
def process!
return unless validate!
purchase!
if response_code
capture!
else
purchase!
end
end
def process_offline!
return unless validate!
return if authorization_action_required?
charge_offline!
if response_code
capture!
else
charge_offline!
end
end
def authorize!(return_url = nil)

View File

@@ -14,7 +14,7 @@ FactoryBot.define do
association(:source, factory: :credit_card)
order
state { 'checkout' }
response_code { '12345' }
response_code { nil }
payment_method { FactoryBot.create(:payment_method, distributors: [distributor]) }
end

View File

@@ -97,6 +97,30 @@ describe Spree::Payment do
expect { payment.process! }.to raise_error(Spree::Core::GatewayError)
expect(payment.state).to eq('invalid')
end
context "the payment is already authorized" do
before do
allow(payment).to receive(:response_code) { "pi_123" }
end
it "should call capture instead of purchase" do
expect(payment).to receive(:capture!)
expect(payment).to_not receive(:purchase!)
payment.process!
end
end
end
context "#process_offline when payment is already authorized" do
before do
allow(payment).to receive(:response_code) { "pi_123" }
end
it "should call capture if the payment is already authorized" do
expect(payment).to receive(:capture!)
expect(payment).to_not receive(:purchase!)
payment.process!
end
end
context "#authorize" do