条带标记未附加到Rails App的请求主体

我正在制作一个Rails应用程序,允许用户以$ X / yr成为订阅用户,我被困在订阅页面上,无论我做什么,stripeToken都没有附加到请求和CreateSubscription.rb说“这个客户没有附加付款来源“。 我的turbolinks被禁用了。

我的条带日志说它收到POST主体的请求:电子邮件:“raf6000@gmail.com”计划:“praducks-annual”但错误被抛出如下 – 因为没有源对象附加到请求 – 错误:键入:“invalid_request_error”消息:“此客户没有附加的付款来源”

CreateSubscription.rb

class CreateSubscription def self.call(plan, email_address, token) user, raw_token = CreateUser.call(email_address) subscription = Subscription.new( plan: plan, user: user ) begin stripe_sub = nil if user.stripe_customer_id.blank? puts "Customer is new" puts token.nil? customer = Stripe::Customer.create( source: token, email: user.email, plan: plan.stripe_id, ) user.stripe_customer_id = customer.id user.save! stripe_sub = customer.subscriptions.first else puts "Customer exists" customer = Stripe::Customer.retrieve(user.stripe_customer_id) # Check if the customer was a stripe customer, but had no source added # If no source then update customer here:- # if customer.sources.total_count == 0 # customer.source = token # customer.save # end stripe_sub = customer.subscriptions.create( plan: plan.stripe_id ) end subscription.stripe_id = stripe_sub.id subscription.save! rescue Stripe::StripeError => e subscription.errors[:base] << e.message end subscription end end 

CreateUser.rb

 class CreateUser def self.call(email_address) user = User.find_by(email: email_address) return user if user.present? raw_token, enc_token = Devise.token_generator.generate( User, :reset_password_token) password = SecureRandom.hex(32) user = User.create!( email: email_address, password: password, password_confirmation: password, reset_password_token: enc_token, reset_password_sent_at: Time.now ) return user, raw_token end end 

subscription.js

 jQuery(function($) { $('#payment-form').submit(function(event) { var $form = $(this); alert('CLICKED'); $form.find('button').prop('disabled', true); Stripe.card.createToken($form, stripeResponseHandler); return false; }); }); function stripeResponseHandler(status, response) { var $form = $('#payment-form'); if (response.error) { // Show the errors on the form $form.find('.payment-errors').text(response.error.message); $form.find('button').prop('disabled', false); } else { // response contains id and card, which contains additional card details var token = response.id; // Insert the token into the form so it gets submitted to the server $form.append($('').val(token)); // and submit $form.get(0).submit(); } }; 

SubscriptionsController

 class SubscriptionsController  "Please select a plan first." end end def create @plan = Plan.find(params[:plan_id]) puts params[:stripeToken] @subscription = CreateSubscription.call( @plan, params[:email_address], params[:stripeToken] ) if @subscription.errors.blank? flash[:notice] = 'Thank you for your upgrade! ' + 'Enjoy your premium membership at Praducks.com. ' + 'We also sent you an email with the details of your purchase.' redirect_to '/' else render :new end end def edit @subscription = current_user.subscription if @subscription.nil? redirect_to subscriptions_path, :notice => "Please join a membership before making changes" end end def update @subscription = current_user.subscription @subscription = ChangeSubscriptionCard.call( @subscription, params[:stripeToken] ) if @subscription.errors.blank? flash[:notice] = 'Account updated!' redirect_to '/' else render :edit end end protected def load_plans @plans = Plan.where(published: true).order('amount') end private # Never trust parameters from the scary internet, only allow the white list through. def subscriptions_params params.require(:subscription).permit(:email_address, :stripeToken) end end 

new.html.erb

 <!----> 

Joining

<input type="hidden" name="plan_id" value="" /> <input type="hidden" name="stripeEmail" value="" /> <input type="hidden" name="email_address" value="" /> :
*
*
*

application.html.erb

        <!---->         $(function(){ Stripe.setPublishableKey(''); });     

在本地运行此代码后,我看到了几件事:

我注意到使用rails helper方法javascript_include_tag没有包含subscriptions.js。 除非您将脚本放在app的public/js文件夹中,否则脚本可能不在页面上。

假设你确实在public/js有它并且它在页面上,我在提交表单时注意到这个错误:

 Uncaught TypeError: $form.find(...).prop is not a function 

由于此错误,您的提交处理程序永远不会return false以防止表单提交,因此它提交没有条带标记。

页面上的jquery版本是1.3.2,其中.prop不可用是导致此错误的原因。

如果您不想升级您的jquery版本,可以使用.attr替换.prop

 $form.find('button').prop('disabled', true); 

变为:

 $form.find('button').attr('disabled', true); 

此外,如果您想确保表单不提交(假设启用了javascript),我通常会在提交处理程序的最开头添加此行:

 event.preventDefault(); 

这应该确保您的表单不会提交。 您还可以删除处理程序末尾的return false ,因为不再需要它。