mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
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".
25 lines
823 B
Ruby
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
|