2020-06-10 11:33:03 +02:00
|
|
|
# frozen_string_literal: true
|
2016-04-11 18:37:06 +02:00
|
|
|
|
2020-06-10 11:33:03 +02:00
|
|
|
# validates the given card token through the Stripe API
|
2016-04-11 18:37:06 +02:00
|
|
|
class StripeCardTokenValidator
|
|
|
|
def validate(record)
|
2020-06-10 11:33:03 +02:00
|
|
|
return unless options[:token]
|
|
|
|
|
|
|
|
res = Stripe::Token.retrieve(options[:token], api_key: Setting.get('stripe_secret_key'))
|
|
|
|
if res[:id] != options[:token]
|
|
|
|
record.errors[:card_token] << "A problem occurred while retrieving the card with the specified token: #{res.id}"
|
2016-04-11 18:37:06 +02:00
|
|
|
end
|
2020-06-10 11:33:03 +02:00
|
|
|
rescue Stripe::InvalidRequestError => e
|
|
|
|
record.errors[:card_token] << e
|
2016-04-11 18:37:06 +02:00
|
|
|
end
|
2020-06-10 11:33:03 +02:00
|
|
|
end
|