条纹 - 为客户创建新卡

时间:2014-02-10 15:32:18

标签: ruby-on-rails stripe-payments

我正在努力实现客户创建新卡的能力。

我创建了一个客户创建新卡的表单:

<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>

然后在控制器中我获取信息并创建新卡:

  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

当我提交表单时,我收到来自Stripe的以下错误: “收到未知参数:number,exp_month,exp_year,cvc,name,address_city,address_zip,address_state,address_country”。

所以它说的是我发送了错误的参数。但是如果你检查here那些参数是否存在(子参数)。

关于我做错了什么的任何线索?有关如何使用Stripe API创建新卡的任何指导都非常有用。谢谢。

1 个答案:

答案 0 :(得分:1)

使用条纹添加卡有两种方法。

1)使用卡片令牌。如果你有一个令牌,就像你一样,你不需要任何其他东西,所以你可以做到

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

2)你也可以这样做:

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]
  }
})

正如你在这里看到的那样,重要的是细节是嵌套到卡片属性中。

显然,你已经完成了两者的混合,你最好选择一个(令牌更好)并坚持下去。那是清楚的吗?