我正在尝试将Stripe与Devise集成到我的rails应用程序中。当我尝试注册我的用户并包含信用卡信息时,我不断收到以下错误:条纹卡令牌不能为空。对于那些将Stripe与Devise集成在一起的人来说,对于我在这里做错了什么的想法。将包含一些基本代码,并乐意根据需要提供更多。谢谢。
用户模型
class User < ActiveRecord::Base
validates :name, :lastname, :industry, :company, :title, :address, :state, :city, :zip, :stripe_card_token, presence: true
validates :phone, presence: true, length: { maximum: 11 }
after_create :create_a_customer
def create_a_customer
token = self.stripe_card_token
customer = Stripe::Customer.create(
:card => token,
:plan => 2017,
:email => self.email
)
end
end
应用/的.js
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
user.setupForm()
user =
setupForm: ->
$('#new_user').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
user.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, user.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)
$('#stripe_error').show()
$('input[type=submit]').attr('disabled', false)
应用/查看/设计/注册/新
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name, 'First Name' %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :lastname, 'Last Name' %><br>
<%= f.text_field :lastname %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
.
.
.
<%= f.hidden_field :stripe_card_token %>
<% if @user.stripe_card_token.present? %>
Credit Card is on File
<% else %>
<div class="field">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil, class: 'form-control' %>
</div>
<div class="field">
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
<%= 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>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
应用/控制器
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?
def after_sign_in_path_for(resource_or_scope)
user_path(current_user)
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << [:name, :stripe_card_token, :email, :lastname, :title, :industry, :company, :address, :zip, :phone, :state, :city]
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :stripe_card_token, :email, :lastname, :title, :industry, :company, :address, :zip, :phone, :state, :city) }
end
end