首页 文章

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

提问于
浏览
0

我有一个现有的Rails 4,Ruby 2项目,Stripe与仅包含3个字段的表单集成 - CVC,卡号和到期日期 . 我的问题是我可以在卡片上找到名称' field inside the form. I saw that all tutorials have examples with a custom form and some JavaScript but my form is not inside the project and I can't,找到填充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

    我找不到CVC,卡号等字段在哪里填充...

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

    尽管内置选项有限,checkout.js的美丽使得支付变得容易 . 见configuration options here

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

    FWIW,我'm doing something similar to what you'要求(在PHP环境中)通过常规表单收集用户名,然后将其附加到传递给checkout.js的描述中
    例如

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

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

    祝好运!

相关问题