Files
openfoodnetwork/app/models/stripe_account.rb
Maikel Linke 1364b878fe Add ApplicationRecord for customisations
Rails 5 introduced this new class to confine application-specific monkey
patches to our models only, and not leak into other libraries using
ActiveRecord::Base.

https://bigbinary.com/blog/application-record-in-rails-5
2021-04-15 15:59:03 +10:00

22 lines
671 B
Ruby

class StripeAccount < ApplicationRecord
belongs_to :enterprise
validates :stripe_user_id, :stripe_publishable_key, presence: true
validates :enterprise_id, uniqueness: true
def deauthorize_and_destroy
accounts = StripeAccount.where(stripe_user_id: stripe_user_id)
# Only deauthorize the user if it is not linked to multiple accounts
return destroy if accounts.count > 1
destroy && Stripe::OAuth.deauthorize(stripe_user_id: stripe_user_id)
rescue Stripe::OAuth::OAuthError
Bugsnag.notify(
RuntimeError.new("StripeDeauthorizeFailure"),
stripe_account: stripe_user_id,
enterprise_id: enterprise_id
)
true
end
end