Search code examples
phplaravelstripe-paymentsstripe-connect

Why is stripe making two accounts?


I'm creating a managed account in Stripe and for some reason, when I create an account it creates two in my dashboard.

This is in Laravel. Here is my code:

$user = Auth::user();

    //create them a connect account
    $account = \Stripe\Account::create(
        array(
            "country" => "GB",
            "managed" => true,
            "external_account" => request('stripeToken'),
            "legal_entity[type]" => $user->legal_entity_type,
            "legal_entity[first_name]" => $user->name,
            "legal_entity[last_name]" => $user->last_name,
            "tos_acceptance[date]" => $user->tos_acceptance_date,
            "tos_acceptance[ip]" => $user->tos_acceptance_ip,
            "legal_entity[dob][day]" => $user->dob_day,
            "legal_entity[dob][month]" => $user->dob_month,
            "legal_entity[dob][year]" => $user->dob_year,
            "legal_entity[address][city]" => $user->address_city,
            "legal_entity[address][line1]" => $user->address_line1,
            "legal_entity[address][postal_code]" => $user->address_postal_code,
        )
    );
    //grab the stripe users ID, secret key and publishable key 
    $acc_id = $account->id;
    $secret_key = $account->keys->secret;
    $publishable = $account->keys->publishable;

    //update the users table to reflect the changes
    $user->stripe_id=$acc_id;
    $user->stripe_secret=$secret_key;
    $user->stripe_key=$publishable;
    $user->save();

    return redirect()->action('HomeController@index');

I create the managed account with all the required information (including bank token created from previous form and submitted) then update my users tables with the stripe id etc and save it.

However when I go to my Stripe dashboard I have two entries with different account ID's but all the same details.

Any advice where I'm going wrong would be great

When I die and dump the account ID it's always the second account ID.

Edit: The first account never receives the External Account attribute, but the second account in the dashboard does, and that's the one that ends up being attributed in the users table as the stripe_id

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script>
  Stripe.setPublishableKey('{{ config('services.stripe.key') }}');

  function stripeResponseHandler(status, response) {

    // Grab the form:
    var $form = $('#bank-account-form');

    if (response.error) { // Problem!

      // Show the errors on the form:
      $form.find('.bank-errors').text(response.error.message);
      $form.find('button').prop('disabled', false); // Re-enable submission

    } else { // Token created!

      // Get the token ID:
      var token = response.id;

      // Insert the token into the form so it gets submitted to the server:
      $form.append($('<input type="hidden" name="stripeToken" />').val(token));

      // Submit the form:
      $form.get(0).submit();

    }
  }
$(document).ready(function () {
$("#bank-account-form").submit(function (event) {
    // disable the submit button to prevent repeated clicks
    $('.submit-button').attr("disabled", "disabled");
    // bank account parameters
    var bankAccountParams = {
        country: $('.country').val(),
        currency: $('.currency').val(),
        //routing_number: $('.routing-number').val(),
        account_number: $('.account-number').val(),
        account_holder_name: $('.account-holder-name').val(),
        account_holder_type: $('.account-holder-type').val()
    }
    if ($('.routing-number').val() != '') {
      bankAccountParams['routing_number'] = $('.routing-number').val();
    }
    // createToken returns immediately - the supplied callback submits the form if there are no errors
    Stripe.bankAccount.createToken(bankAccountParams, stripeResponseHandler);
});
});
</script>

Thanks,


Solution

  • Your backend code for creating the managed account is being called twice, once without the stripeToken POST parameter and once with it.

    My guess is that you have a client-side form using Stripe.js to collect the user's bank account information, and there is an issue with the form where the browser submits it twice, once without the token and once with it.

    In your form's event handler, make sure that you disable the default submission by calling preventDefault(); on the submit event and/or returning false in the handler.

    The form should only be submitted by Stripe.js' own callback, when the token has been created.