mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #7497 from andrewpbrett/fix-double-stripe-payments
Use capture! if payment is already authorized
This commit is contained in:
@@ -13,7 +13,11 @@ module Spree
|
||||
return unless validate!
|
||||
return if authorization_action_required?
|
||||
|
||||
charge_offline!
|
||||
if preauthorized?
|
||||
capture!
|
||||
else
|
||||
charge_offline!
|
||||
end
|
||||
end
|
||||
|
||||
def authorize!(return_url = nil)
|
||||
@@ -184,6 +188,10 @@ module Spree
|
||||
|
||||
private
|
||||
|
||||
def preauthorized?
|
||||
response_code.presence&.match("pi_")
|
||||
end
|
||||
|
||||
def validate!
|
||||
return false unless payment_method&.source_required?
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -158,12 +158,19 @@ describe SubscriptionConfirmJob do
|
||||
allow(order).to receive(:pending_payments) { [stripe_sca_payment] }
|
||||
allow(stripe_sca_payment_method).to receive(:provider) { provider }
|
||||
allow(stripe_sca_payment_method.provider).to receive(:purchase) { true }
|
||||
allow(stripe_sca_payment_method.provider).to receive(:capture) { true }
|
||||
end
|
||||
|
||||
it "runs the charges in offline mode" do
|
||||
job.send(:confirm_order!, order)
|
||||
expect(stripe_sca_payment_method.provider).to have_received(:purchase)
|
||||
end
|
||||
|
||||
it "uses #capture if the payment is already authorized" do
|
||||
allow(stripe_sca_payment).to receive(:preauthorized?) { true }
|
||||
expect(stripe_sca_payment_method.provider).to receive(:capture)
|
||||
job.send(:confirm_order!, order)
|
||||
end
|
||||
end
|
||||
|
||||
context "Stripe Connect" do
|
||||
|
||||
@@ -16,6 +16,7 @@ describe Spree::Gateway::StripeSCA, type: :model do
|
||||
amount: order.total,
|
||||
payment_method: subject,
|
||||
source: credit_card,
|
||||
response_code: "12345"
|
||||
)
|
||||
}
|
||||
let(:gateway_options) {
|
||||
|
||||
@@ -97,6 +97,29 @@ 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 purchase" do
|
||||
expect(payment).to 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_offline!
|
||||
end
|
||||
end
|
||||
|
||||
context "#authorize" do
|
||||
|
||||
Reference in New Issue
Block a user