mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
We observed invalid payment states in Bugsnag but we don't actually know in which state the payment intent was in. From the context we can guess that it was "succeeded" but it would be good to validate this. And in the future it would be good to know if there are other invalid states we can end up in. The notification to Bugsnag happens in another part of the code.
33 lines
1.1 KiB
Ruby
33 lines
1.1 KiB
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)
|
|
raise_if_not_in_capture_state(payment_intent_response)
|
|
|
|
payment_intent_id
|
|
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
|
|
|
|
def raise_if_not_in_capture_state(payment_intent_response)
|
|
state = payment_intent_response.status
|
|
return unless state != 'requires_capture'
|
|
|
|
raise Stripe::StripeError, I18n.t(:invalid_payment_state, state: state)
|
|
end
|
|
end
|
|
end
|