Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Plaid::ApiError (MAYBE-RAILS-78) #1759

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions app/models/provider/plaid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

MAYBE_SUPPORTED_PLAID_PRODUCTS = %w[transactions investments liabilities].freeze
MAX_HISTORY_DAYS = Rails.env.development? ? 90 : 730
SUPPORTED_COUNTRY_CODES = %w[US CA GB FR ES IE NL DE IT PL DK NO SE EE LT LV PT BE].freeze

class << self
def process_webhook(webhook_body)
Expand Down Expand Up @@ -68,20 +69,26 @@
@client = self.class.client
end

def get_link_token(user_id:, webhooks_url:, redirect_url:, accountable_type: nil, eu: false)
def get_link_token(user_id:, webhooks_url:, redirect_url:, accountable_type: nil, country_code: "US")
country_code = country_code.to_s.upcase
country_code = "US" unless SUPPORTED_COUNTRY_CODES.include?(country_code)

request = Plaid::LinkTokenCreateRequest.new({
user: { client_user_id: user_id },
user: { client_user_id: user_id.to_s },
client_name: "Maybe Finance",
products: [ get_primary_product(accountable_type) ],
additional_consented_products: get_additional_consented_products(accountable_type),
country_codes: get_country_codes(eu),
country_codes: [country_code],

Check failure on line 81 in app/models/provider/plaid.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 81 in app/models/provider/plaid.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
language: "en",
webhook: webhooks_url,
redirect_uri: redirect_url,
transactions: { days_requested: MAX_HISTORY_DAYS }
})

client.link_token_create(request)
rescue Plaid::ApiError => e
Rails.logger.error("Plaid link token creation failed: #{e.message}")
raise
end

def exchange_public_token(token)
Expand Down Expand Up @@ -198,12 +205,4 @@
def get_additional_consented_products(accountable_type)
MAYBE_SUPPORTED_PLAID_PRODUCTS - [ get_primary_product(accountable_type) ]
end

def get_country_codes(eu)
if eu
[ "ES", "NL", "FR", "IE", "DE", "IT", "PL", "DK", "NO", "SE", "EE", "LT", "LV", "PT", "BE" ] # EU supported countries
else
[ "US", "CA" ] # US + CA only
end
end
end
77 changes: 77 additions & 0 deletions test/models/provider/plaid_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require "test_helper"

class Provider::PlaidTest < ActiveSupport::TestCase
setup do
@provider = Provider::Plaid.new
end

test "get_link_token uses supported country code" do
mock_client = mock()
@provider.stubs(:client).returns(mock_client)

mock_client.expects(:link_token_create).with(
has_entries(
country_codes: ["CA"],

Check failure on line 14 in test/models/provider/plaid_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 14 in test/models/provider/plaid_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
client_name: "Maybe Finance",
language: "en"
)
).returns(OpenStruct.new(link_token: "test_token"))

response = @provider.get_link_token(
user_id: "123",
webhooks_url: "https://example.com/webhook",
redirect_url: "https://example.com/redirect",
country_code: "CA"
)

assert_equal "test_token", response.link_token
end

test "get_link_token defaults to US for unsupported country code" do
mock_client = mock()
@provider.stubs(:client).returns(mock_client)

mock_client.expects(:link_token_create).with(
has_entries(
country_codes: ["US"]

Check failure on line 36 in test/models/provider/plaid_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 36 in test/models/provider/plaid_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
)
).returns(OpenStruct.new(link_token: "test_token"))

response = @provider.get_link_token(
user_id: "123",
webhooks_url: "https://example.com/webhook",
redirect_url: "https://example.com/redirect",
country_code: "XX"
)

assert_equal "test_token", response.link_token
end

test "get_link_token handles nil country code" do
mock_client = mock()
@provider.stubs(:client).returns(mock_client)

mock_client.expects(:link_token_create).with(
has_entries(
country_codes: ["US"]

Check failure on line 56 in test/models/provider/plaid_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 56 in test/models/provider/plaid_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
)
).returns(OpenStruct.new(link_token: "test_token"))

response = @provider.get_link_token(
user_id: "123",
webhooks_url: "https://example.com/webhook",
redirect_url: "https://example.com/redirect",
country_code: nil
)

assert_equal "test_token", response.link_token
end

test "get_primary_product returns correct product for accountable type" do
assert_equal "investments", @provider.send(:get_primary_product, "Investment")
assert_equal "liabilities", @provider.send(:get_primary_product, "CreditCard")
assert_equal "liabilities", @provider.send(:get_primary_product, "Loan")
assert_equal "transactions", @provider.send(:get_primary_product, "Depository")
assert_equal "transactions", @provider.send(:get_primary_product, nil)
end
end
Loading