Moosa
Moosa

Reputation: 3216

Rails 4 Form Syntax for Stripe Bank Token -

I'm building a marketplace and have a form to collect bank account details from sellers so we can transfer payments.

One of the stripe parameters is the sellers name, which is an input on the form. When I submit the form, the controller reads the name only if it is stored in the database already (i.e. if I manually enter name in db, then run form). If a name doesn't already exist, it gives me an error saying I need to enter a name to create the stripe token.

How do I change the form/method such that the name is available for stripe when the user hits submit?

Here is my update method: the error is in the line with :name => current_user.bankaccname. This is an input in the form pasted below.

def update

    Stripe.api_key = ENV["STRIPE_API_KEY"]
      token = params[:stripeToken]

      recipient = Stripe::Recipient.create(
        :name => current_user.bankaccname,
        :type => "individual",
        :bank_account => token
        )

      current_user.recipient = recipient.id
      current_user.save

    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to edit_user_url, notice: 'Your account was successfully updated.' }
      else
        format.html { render action: 'edit' }
      end
    end
  end

my form:

<%= form_for @user, url: user_path, html: { method: :put } do |f| %>
     <div class="form-group">
      <%= f.label :name %><i> (as it appears in your bank account)</i>
      <%= f.text_field :bankaccname, class:"form-control" %>
     </div>

 <div class="form-group">
      <%= label_tag :country %>
      <%= text_field_tag :country, nil, { :name => nil, :'data-stripe' => "country", class: "form-control" } %>
    </div>
    <div class="form-group">
      <%= label_tag :routing_number %>
      <%= text_field_tag :routing_number, nil, { :name => nil, :'data-stripe' => "routingNumber", class: "form-control" } %>
    </div>
    <div class="form-group">
      <%= label_tag :account_number %>
      <%= text_field_tag :account_number, nil, { :name => nil, :'data-stripe' => "accountNumber", class: "form-control" } %>
    </div>

     <div class="form-group">
        <%= f.submit "Submit", class:"btn btn-primary" %>
     </div>

<% end %>

Upvotes: 0

Views: 148

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 3427

check this, it is more optimized, it makes only one call to db (i guess). and it will solve your above problem too.

def update
    @user.attributes = user_params
    Stripe.api_key = ENV["STRIPE_API_KEY"]
      token = params[:stripeToken]

      recipient = Stripe::Recipient.create(
        :name => @user.bankaccname,
        :type => "individual",
        :bank_account => token
        )

     @user.recipient = recipient.id
     respond_to do |format|
      if @user.save
        format.html { redirect_to edit_user_url, notice: 'Your account was successfully updated.' }
      else
        format.html { render action: 'edit' }
      end
    end
  end

Upvotes: 1

Related Questions