将客户的卡名称字段添加到现有Rails项目的Stripe表单中

时间:2015-01-29 16:27:09

标签: ruby-on-rails checkout stripe-payments

我有一个现有的Rails 4,Ruby 2项目,Stripe与仅包含3个字段的表单集成 - CVC,卡号和到期日期。我的问题是我无法在表单中添加“名片上的卡片”字段。我看到所有教程都有自定义表单和一些JavaScript的示例,但我的表单不在项目中,我找不到填充CVC,卡号等字段的位置。此外,在本教程中没有任何JavaScript - https://stripe.com/docs/tutorials/forms但Stripe / Checkout仍然有效。

在users_controller.rb中有订阅方法:

def subscribe
  token = params[:stripeToken]
  subscription = params[:subscription]
  customer = current_user.stripe_customer_data

  subs = customer.subscriptions.all
  subs[:data].each do |sub|
    customer.subscriptions.retrieve(sub[:id]).delete
  end

  customer.subscriptions.create(
    plan: subscription,
    card: token,
    trial_end: current_user.subscription_expires.to_time.to_i
  )
  current_user.update_attribute(:subscription, subscription)
  current_user.update_attribute(:subscription_state, 'selected')

  @data = current_user.stripe_customer_data

  redirect_to edit_user_path(current_user)
end

在views / user / _subscrition.html.haml中:(这是关于Stripe / Checkout存在的视图和表单中的所有内容 - 没有以某种方式由Stripe / Checkout生成的自定义表单):

.col-md-3
    = form_tag user_subscribe_path do
      = hidden_field_tag :subscription, 'monthly',
      = javascript_include_tag "https://checkout.stripe.com/v2/checkout.js",
         :class => "stripe-button",
         :"data-key" => "#{STRIPE_PUBLIC_KEY}",
         :"data-label" => "Monthly Payment",
         :"data-panel-label" => "Pay {{amount}} monthly.",
         :"data-name" => Name",
         :"data-email" => "#{current_user.email}",
         :"data-description" => "Monthly",
         :"data-amount" => "29",
         :"data-image" => "#{image_path('image.png')}",
         :"data-allow-remember-me" => false

在与Stripe相关的用户模型中也有一些方法,但我没有看到它们与在表单中添加用户输入字段相关,因此除非需要,否则我不会添加它们。我在这里缺少什么,以及在CVC,到期日期和卡号旁边的表格中添加持卡人姓名输入字段的方法是什么?感谢。

1 个答案:

答案 0 :(得分:0)

  

我无法找到CVC,卡号等字段的填充位置......

这来自https://checkout.stripe.com/v2/checkout.js。您通过数据属性(如数据名称和数据描述)传递一些选项,然后checkout.js执行其操作并创建最终形式。

checkout.js之美让人很容易接受付款,尽管内置选项有限。见configuration options here

你可以对用户ZIP进行验证,虽然我不相信你可以使用Stripe来验证卡上的名字,如果我错了,请有人纠正我;)所以我猜你想要这个用于内部使用。

FWIW,我通过常规表单收集用户名,然后将其附加到传递给checkout.js的描述中,我正在做类似于你所要求的(在PHP环境中)。 例如

data-description=" <?php echo $payment_description .$customerName; ?>"

如果您使用订阅者信息维护内部数据库,您还可以将名称保存在那里并将其与cusotmer令牌配对。

祝你好运!