2025-05-15 10:19:56 -04:00
|
|
|
module Family::PlaidConnectable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
has_many :plaid_items, dependent: :destroy
|
|
|
|
end
|
|
|
|
|
2025-05-25 08:12:54 -04:00
|
|
|
def can_connect_plaid_us?
|
|
|
|
plaid(:us).present?
|
|
|
|
end
|
|
|
|
|
|
|
|
# If Plaid provider is configured and user is in the EU region
|
|
|
|
def can_connect_plaid_eu?
|
|
|
|
plaid(:eu).present? && self.eu?
|
|
|
|
end
|
|
|
|
|
2025-05-15 10:19:56 -04:00
|
|
|
def create_plaid_item!(public_token:, item_name:, region:)
|
2025-05-23 18:58:22 -04:00
|
|
|
public_token_response = plaid(region).exchange_public_token(public_token)
|
2025-05-15 10:19:56 -04:00
|
|
|
|
|
|
|
plaid_item = plaid_items.create!(
|
|
|
|
name: item_name,
|
|
|
|
plaid_id: public_token_response.item_id,
|
|
|
|
access_token: public_token_response.access_token,
|
|
|
|
plaid_region: region
|
|
|
|
)
|
|
|
|
|
|
|
|
plaid_item.sync_later
|
|
|
|
|
|
|
|
plaid_item
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_link_token(webhooks_url:, redirect_url:, accountable_type: nil, region: :us, access_token: nil)
|
2025-05-23 18:58:22 -04:00
|
|
|
return nil unless plaid(region)
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
plaid(region).get_link_token(
|
2025-05-15 10:19:56 -04:00
|
|
|
user_id: self.id,
|
|
|
|
webhooks_url: webhooks_url,
|
|
|
|
redirect_url: redirect_url,
|
|
|
|
accountable_type: accountable_type,
|
|
|
|
access_token: access_token
|
|
|
|
).link_token
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2025-05-23 18:58:22 -04:00
|
|
|
def plaid(region)
|
2025-05-25 08:12:54 -04:00
|
|
|
Provider::Registry.plaid_provider_for_region(region)
|
2025-05-15 10:19:56 -04:00
|
|
|
end
|
|
|
|
end
|