5

While working on an app that connects with Stripe Accounts, I ran into an issue. If an account already exists for a given email, I can't create a new one. Stripe throws an error.

But how do I connect to that existing account? I can't find anything anywhere to do so.

For clarity, the failing code is:

Stripe::Acount.create(managed: false, email: 'Foo@UserEmail.address')

I'm creating a Stripe::Account object, not a Stripe::Customer object. The above code throws an exception if the email already exists on Stripe's API, but does not provide me with any information to connect with it.

My concern is if a user who already has a Stripe account attempts to join the site, how do I link them up.

RonLugge
  • 5,086
  • 5
  • 33
  • 61
  • Does this helps: https://stripe.com/docs/api/ruby#retrieve_account – Abhinay Mar 25 '16 at 18:01
  • Except the solution of digging through all accounts via the api, it doesn't seem possible to retrieve a customer from just the email (this would be a nice feature). If you can alter the database I would suggest that you store the customer_id somewhere on your side to ease the retrieve process. – coding addicted Mar 25 '16 at 18:22
  • Here is an answer that have few suggestions: http://stackoverflow.com/questions/26767150/stripe-is-it-possible-to-search-a-customer-by-their-email I have update my answer with a suggestion that can be helpfull – coding addicted Mar 25 '16 at 18:26
  • @codingaddicted that's for customer objects, not account objects. Thanks though. – RonLugge Mar 25 '16 at 18:29
  • Yes but you can do something quite the same with the `Stripe::Account.all`. – coding addicted Mar 25 '16 at 18:31
  • Your question raise my curiosity and I really want to know how you will solve this! – coding addicted Mar 25 '16 at 18:32

2 Answers2

2

Since you're using standalone accounts, you will not be able to create the account if the user already has a Stripe account with that email address.

In that case, you must use the OAuth flow to connect the existing account to your platform.

Note that you can use the OAuth flow to handle both cases. If the user already has a Stripe account, they can log into their existing account to connect it, and if they don't, they will be able to create a new account and connect it at the same time.

Ywain
  • 16,854
  • 4
  • 51
  • 67
0

With the Stripe Gem, I use something like this if an account already exists:

Stripe::Customer.retrieve(@user.stripe_id)

You can do something like this:

customer = if @user.stripe_id?
              Stripe::Customer.retrieve(@user.stripe_id)
            else
              Stripe::Customer.create(email: @user.email)
            end

I usually store the stripe_id with the associated model but you can use Stripe API to retrieve it.

UPDATE

I miss read your question. If your are talking about different accounts (and not customers). You can use the Stripe API to retrieve the list of all accounts and then select the one you need based on the email.

List all accounts with Stripe API

Then retrieve the account with the corresponding email:

Stripe::Account.retrieve("account_id")

Suggestions from this answer converted for the accounts:

accounts = Stripe::Account.all

account_i_need = accounts.select do |c|
  c.email == "foo@bar.com"
end
Community
  • 1
  • 1
coding addicted
  • 3,422
  • 2
  • 36
  • 47
  • I'm using Stripe Accounts, not Stripe Customers. The stripe-connect platform lets me create a Stripe Account for users. Very different set of code. – RonLugge Mar 25 '16 at 18:09
  • 1
    Yes I just see it, I read your question too fast. I update my answer to give you an alternative. – coding addicted Mar 25 '16 at 18:11