Files
openfoodnetwork/lib/stripe/payment_intent_validator.rb
Matt-Yorkley e686a4f627 Move raising of errors when payment intent state != "requires_capture" out of PaymentIntentValidator service
When we're fetching the payment intent via PaymentIntentValidator in StripeSCA#purhcase (to capture it), we want it to fail loudly if it's not in "requires_capture" state. We're now also re-using the same PaymentIntentValidator service to check if payment processing was *successful*, in which case we need it *not* to fail loudly if the state == "succeeded", eg != "requires_capture".
2021-05-16 12:19:04 +01:00

25 lines
823 B
Ruby

# frozen_string_literal: true
# This class validates if a given payment intent ID is valid in Stripe
module Stripe
class PaymentIntentValidator
def call(payment_intent_id, stripe_account_id)
payment_intent_response = Stripe::PaymentIntent.retrieve(payment_intent_id,
stripe_account: stripe_account_id)
raise_if_last_payment_error_present(payment_intent_response)
payment_intent_response
end
private
def raise_if_last_payment_error_present(payment_intent_response)
return unless payment_intent_response.respond_to?(:last_payment_error) &&
payment_intent_response.last_payment_error.present?
raise Stripe::StripeError, payment_intent_response.last_payment_error.message
end
end
end