ruby-on-rails - 将 stripe 与 Devise 集成

标签 ruby-on-rails ruby-on-rails-3 devise payment-processing stripe-payments

我正在关注 stripe 上的 railscast #288,但我在设置 stripe 隐藏 token 时遇到了一些困难。我正在使用带有简单表单集成的最新版本的设计。

这是我的注册 Controller :

class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource 
    if resource.save_with_payment
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

我的新用户注册表格:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'well'} ) do |f| %>
  <%= f.error_notification %>
  <%= f.error_messages %>
  <h3 class="center">Create your credentials for logging in:</h3><hr>
  <div class="inputs">
    <%= f.input :email, :required => true  %>
    <%= f.input :password, :required => true %>
    <%= f.input :password_confirmation, :required => true %>
  <h3 class="center">Are you a Shipper or a Carrier?</h3><hr>
    <%= f.input :role, :collection => User::ROLES, :as => :radio_buttons %>
    <%= f.input :stripe_card_token, :as => :hidden %>
  </div>
  <h3 class="center">Your Credit Card Information:</h3><hr>
  <fieldset>
    <div class="field">
      <%= label_tag :card_number, "Credit Card Number" %>
      <div class="input-prepend inline-block">
      <span class="add-on"><strong>#</strong></span>
      <%= text_field_tag :card_number, nil, name: nil %>
    </div>
    <div class="field">
      <%= label_tag :card_code, "Security Code on Back of Card (CVV)" %>
      <div class="input-prepend inline-block">
      <span class="add-on icon-credit-card"></span>
      <%= text_field_tag :card_code, nil, name: nil, :size => 4  %>
    </div>
    <div class="field">
      <%= label_tag :card_month, "Card Expiration" %>
      <%= select_month nil, { add_month_numbers: true }, { name: nil, id: "card_month" } %>
      <%= select_year nil, { start_year: Date.today.year, end_year: Date.today.year+15 },
  { name: nil, id: "card_year" } %>
    </div>
    <div id="stripe_error">
      <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
    </div>
  </fieldset>
  <hr>
  <%= f.input :terms, :as => :boolean, label: "* I have read and agreed to the terms of service" %>
  <div class="actions">
    <%= f.button :submit, "Sign up", class: "btn-large icon-thumbs-up" %>
  </div>
<% end %>

我有一个具有以下属性的用户模型:

attr_accessible :email, :password, :password_confirmation, :remember_me, :role, :terms, :stripe_token, :stripe_card_token

最后是让这一切正常工作的 CoffeeScript :

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

subscription = 
  setupForm: ->
    $('#new_user').submit ->
        $('input[type=submit]').attr('disabled', true)
        if $('#card_number').length
            subsciption.processCard()
            false
        else
            true

processCard: ->
    card =
        number: $('#card_number').val()
        cvc: $('#card_code').val()
        expMonth: $('#card_month').val()
        expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)

handleStripeResponse: (status, response) ->
    if status == 200
        $('#user_stripe_card_token').val(response.id)
        $('#new_user')[0].submit()
    else 
        $('#stripe_error').text(response.error.message)
        $('input[type=submit]').attr('disabled', false)

当我去创建一个新用户时,我的 Action Controller 给我以下错误信息:

undefined method `stripe_card_token' for #<User:0x00000103fe7b90>

但是我认为这不可能,因为我已经在 user.rb 模型的 attr_accessible 调用中定义了这个方法。任何解释都会非常有帮助,因为我认为我已经完成了 90% 的设置。

最佳答案

除非 stripe_card_token 是您的 Users 表中的一个字段,否则您需要将其设为 attr_accessor(以及 attr_accessible)。

关于ruby-on-rails - 将 stripe 与 Devise 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11068856/

相关文章:

ruby-on-rails - Rails - 3 列关联表?

ruby-on-rails - 用于设计注册 Controller 获取零资源的自定义操作

css - 如何使用 Assets 管道更改 Rails 4 中的 link_to 悬停图像

ruby-on-rails - Rails Mailer 上的 RSpec class_spy

ruby-on-rails - Rails 将包含日期时间的字符串转换为日期

ruby-on-rails - Rails simple_form - 隐藏字段 - 创建?

ruby-on-rails - 使用 map 方法格式化 JSON 输出 - Rails 3

ruby-on-rails - rails : how to extend a generator?

css - 基于媒体查询的 Controller 操作 (Rails)

ruby-on-rails - 设计/ cucumber - 添加确认用户存在的步骤