Handle SCA payments that can't complete

This commit is contained in:
Pau Perez
2021-05-06 16:03:25 +02:00
parent 2147584daf
commit dea6a01e61
2 changed files with 32 additions and 5 deletions

View File

@@ -23,10 +23,10 @@ class ProcessPaymentIntent
end
end
def initialize(payment_intent, order)
def initialize(payment_intent, order, last_payment = nil)
@payment_intent = payment_intent
@order = order
@last_payment = OrderPaymentFinder.new(order).last_payment
@last_payment = last_payment.presence || OrderPaymentFinder.new(order).last_payment
end
def call!
@@ -35,10 +35,15 @@ class ProcessPaymentIntent
mark_as_processed
OrderWorkflow.new(@order).next
last_payment.complete! if last_payment.can_complete?
OrderWorkflow.new(order).next
if last_payment.can_complete?
last_payment.complete!
Result.new(ok: true)
else
Result.new(ok: false, error: "The payment could not be completed")
end
Result.new(ok: true)
rescue Stripe::StripeError => e
Result.new(ok: false, error: e.message)
end

View File

@@ -94,5 +94,27 @@ describe ProcessPaymentIntent do
expect(payment.reload.state).to eq("failed")
end
end
context "when the payment can't be completed" do
let(:intent) { "pi_123" }
let(:service) { ProcessPaymentIntent.new(intent, order, payment) }
before do
allow(payment).to receive(:can_complete?).and_return(false)
allow(validator).to receive(:call).with(intent, anything).and_return(intent)
end
it "returns a failed result" do
result = service.call!
expect(result.ok?).to eq(false)
expect(result.error).to eq("The payment could not be completed")
end
it "does not complete the payment" do
service.call!
expect(payment.reload.state).to eq("pending")
end
end
end
end