0
votes

I am trying to implement the ability for a customer to create a new card.

I have created a form in which the customer creates a new card:

<table class="table">
<%= simple_form_for(@user, :url => billing_path, html: {class: 'form-horizontal' }) do |f| %>
  <tr>
    <td>Country</td>
    <td><%= f.text_field :saddress_country, value: card1.address_country %> </td>
  </tr>
  <tr>
    <td>Billing Address</td>
    <td><%= f.text_field :sbilling_address1, value: card1.address_line1 %> </td>
    <td><%= f.text_field :sbilling_address2, value: card1.address_line2 %> </td>
  </tr>
  <tr>
    <td>City / State / Zip</td>
    <td><%= f.text_field :saddress_city, value: card1.address_city %> </td>
    <td><%= f.text_field :saddress_state, value: card1.address_state %> </td>
    <td><%= f.text_field :saddress_zip, value: card1.address_zip %> </td>
  </tr>
  <tr>
    <td>Credit Card Number </td>
    <td><%= f.text_field :scardnumber, placeholder: card1.last4, value: cardnumberempty %></td>
    <td><%= f.text_field :scvc, placeholder: "CVC", value: cvcempty %></td>
  </tr>
  <tr>
    <td>Card's Info</td>
    <td><%= f.text_field :sname, value: card1.name %> </td>
    <td><%= f.text_field :sexp_month, value: card1.exp_month %> </td>
    <td><%= f.text_field :sexp_year, value: card1.exp_year %> </td>
  </tr>
  <tr><td><%= f.submit %></td></tr>
<% end %>
</table>

And then in the controller I grab the information and create the new card:

  def create_card_stripe
    @user = current_user
    customer = Stripe::Customer.retrieve(current_user.stripe_customer_token)

    customer.cards.create({
      :card => current_user.stripe_card_token,
      :number => params[:user][:scardnumber],
      :exp_month => params[:user][:sexp_month],
      :exp_year => params[:user][:sexp_year],
      :cvc => params[:user][:scvc],
      :name => params[:user][:sname],
      :address_line1 => params[:user][:saddress_line1],
      :address_line2 => params[:user][:saddress_line2],
      :address_city => params[:user][:saddress_city],
      :address_zip => params[:user][:saddress_zip],
      :address_state => params[:user][:saddress_state],
      :address_country => params[:user][:saddress_country]
      })

    @user.update_attribute(:stripe_card_id, customer.active_card.id)
    if customer.save
      flash.alert = "Billing information updated successfully!"
      redirect_to edit_user_registration_path
    else
      flash.alert = "Stripe error"
      redirect_to edit_user_registration_path
    end
  end

When I submit the form I get the following error that comes from Stripe: "Received unknown parameters: number, exp_month, exp_year, cvc, name, address_city, address_zip, address_state, address_country".

So what it says is that I am sending wrong parameters. But if you check here those parameters are there (Child parameters).

Any clue on what I am doing wrong? Any guidance at all on how to create a new card using the Stripe API will be really helpful. Thank you.

1

1 Answers

1
votes

There is two ways of adding a card using Stripe.

1) Using the card token. If you have a token, like you do, you don't need anything else, so you can just do

customer.cards.create({
  :card => current_user.stripe_card_token
})

2) You could also do this:

customer.cards.create({
  :card => {
     :number => params[:user][:scardnumber],
     :exp_month => params[:user][:sexp_month],
     :exp_year => params[:user][:sexp_year],
     :cvc => params[:user][:scvc],
     :name => params[:user][:sname],
     :address_line1 => params[:user][:saddress_line1],
     :address_line2 => params[:user][:saddress_line2],
     :address_city => params[:user][:saddress_city],
     :address_zip => params[:user][:saddress_zip],
     :address_state => params[:user][:saddress_state],
     :address_country => params[:user][:saddress_country]
  }
})

So as you can see here, the important thing is that the details are nested into the card attribute.

Evidently, you've done a mix of the two, you are best off choosing one (token is better) and sticking with it. Is that clear?