Update bank account info - Stripe

Hey, I'm working on the Etsydemo course. Does Stripe allow sellers to update their bank account info? I want to create a form on the edit profile page for the seller to add and/or update their bank account and routing number. I've got the form functioning, but I'm not getting a token from Stripe when I hit "update." I edited the coffeescript code to change it from #new_listing to the edit profile page. Is there something else I need to do to get it working? Coffeescript: jQuery -> Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) listing.setupForm() listing = setupForm: -> $('#edit_user_registration').submit -> $('input[type=submit]').attr('disabled', true) Stripe.bankAccount.createToken($('#edit_user_registration'), listing.handleStripeResponse) false handleStripeResponse: (status, response) -> if status == 200 $('#edit_user_registration').append($('<input type="hidden" name="stripeToken" />').val(response.id)) $('#edit_user_registration')[0].submit() else $('#stripe_error').text(response.error.message).show() $('input[type=submit]').attr('disabled', false) form: <h2>Edit <%= resource_name.to_s.humanize %></h2> <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> <%= devise_error_messages! %> <div id="stripe_error" class="alert alert-danger" style="display:none"> <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript> </div> <div class="form-group"> <%= f.label :name %> <%= f.text_field :name, :autofocus => true, class: "form-control" %> </div> <div class="form-group"> <%= f.label :email %><br /> <%= f.email_field :email, class: "form-control" %> </div> <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> <div class="form-group"> Currently waiting confirmation for: <%= resource.unconfirmed_email %> </div> <% end %> <div class="form-group"> <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> <%= f.password_field :password, :autocomplete => "off", class: "form-control" %> </div> <div class="form-group"> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, class: "form-control" %> </div> <br> <h1>Bank Account Information</h1> <h3>Add/ Update Bank Account Information</h3> <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.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> <%= f.password_field :current_password, class: "form-control" %> </div> <div class="form-group"> <%= f.submit class: "btn btn-primary" %> </div> <% end %> <h3>Cancel my account</h3> <p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete, class: "btn btn-danger" %></p> <%= link_to "Back", :back %> Thank you!

Posted about 4 years ago by ERIN
Posted about 4 years ago by ERIN

I'm reposting the code because the format isn't very readable. Hopefully html works on this form

<p>
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
listing.setupForm()

listing =
setupForm: ->
$('#edit_user_registration').submit ->
$('input[type=submit]').attr('disabled', true)
Stripe.bankAccount.createToken($('#edit_user_registration'), listing.handleStripeResponse)
false

handleStripeResponse: (status, response) ->
if status == 200
$('#edit_user_registration').append($('<input type="hidden" name="stripeToken" />').val(response.id))
$('#edit_user_registration')[0].submit()
else
$('#stripe_error').text(response.error.message).show()
$('input[type=submit]').attr('disabled', false)
</p>

<p>
<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>

<div id="stripe_error" class="alert alert-danger" style="display:none">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>

<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, :autofocus => true, class: "form-control" %>
</div>

<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, class: "form-control" %>
</div>

<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div class="form-group">
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
</div>
<% end %>

<div class="form-group">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off", class: "form-control" %>
</div>

<div class="form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, class: "form-control" %>
</div>

<br>
<h1>Bank Account Information</h1>
<h3>Add/ Update Bank Account Information</h3>

<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.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, class: "form-control" %>
</div>

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

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete, class: "btn btn-danger" %></p>

<%= link_to "Back", :back %>
</p>

1
Posted about 4 years ago by Alex Yang

If you're trying to troubleshoot the code you've pasted, here are a few suggestions that might help:

- Check that '#edit_user_registration' is the right selector to use. Did you confirm that the code inside the block is being run? You can test this out by putting something like 'alert "Success!"' right after the "$('#edit_user_registration').submit ->" line.

- Go through the Etsydemo chapter where we create this CoffeeScript file and build it up gradually as shown in the video. In other words, start with:

if status == 200
alert(response.id)
...

before moving on the final version where we append the hidden form field. Building up the code gradually will make sure that each step works and help you in identifying any points when things break down. Hope that helps!

1
Posted about 4 years ago by ERIN

Thank you, Alex. I just changed #edit_user_registration to #edit_user and I was able to get a Stripe token.
In the "Transfer Funds" video, this code was added to the listings_controller.rb file.
def create
@listing = Listing.new(listing_params)
@listing.user_id = current_user.id

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

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

current_user.recipient = recipient.id
current_user.save

But since the Edit Profile page is handled by Devise::RegistrationsController#update instead of the listings_controller.rb file, could you offer some guidance on where to add this code and/or how to edit the Devise registrations controller?

1
Posted about 4 years ago by Alex Yang

You'll need to override the 'update' action in the Devise registrations controller. You can read more about that in the Devise documentation:
https://github.com/plataformatec/devise#configuring-controllers

It may help to also take a look at the original Devise registrations_controller.rb code here (the 'update' action in particular):
https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

Make sure that you set up your routes.rb file to use your custom registrations_controller.rb file instead of the default Devise one. There should an article on how to do that in the Devise wiki - if not, it shouldn't be too difficult to find some info with a Google search or two.

1
Posted about 4 years ago by ERIN

Thank you! That was very helpful. I was able to get it working.

1
Posted about 4 years ago by Alex Yang

Great job!

1