由于条带卡令牌未定义,无法处理条带付款,尝试了https://stripe.com/docs/testing的有效卡号。仍然无效。
user.rb文件:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :plan
attr_accessor :stripe_card_token
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
end
end
routes.rb文件:
Rails.application.routes.draw do
devise_for :users, controllers: {registrations: 'users/registrations'}
resources :contacts
get '/about' => 'pages#about'
root 'pages#home'
end
users.js文件:
$(document).ready(function() {
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'));
$("#form-submit-btn").click(function(event) {
event.preventDefault();
$('input[type=submit]').prop('disabled', true);
var error = false;
var ccNum = $('#card_number').val();
var cvcNum = $('#card_code').val();
var expMonth = $('#card_month').val();
var expYear = $('#card_year').val();
if(!error) {
Stripe.createToken({
number: ccNum,
cvc: cvcNum,
exp_month: expMonth,
exp_year: expYear
}, stripeResponseHandler);
}
}); // form submission
function stripeResponseHandler(status, response) {
var f = $('#new_user');
var token = response.id;
f.append('<input type="hidden" name = "user[stripe_card_token]" value="' + token + '" />');
f.get(0).submit();
}
});
application.html.erb文件:
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag "https://js.stripe.com/v2/", type: 'text/javascript' %>
<%= javascript_include_tag 'application' %>
<%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>
<%= csrf_meta_tags %>