gpt4 book ai didi

ruby-on-rails - Stripe token 未附加到 Rails 应用程序的请求正文

转载 作者:行者123 更新时间:2023-12-05 01:27:22 24 4
gpt4 key购买 nike

我正在制作一个 Rails 应用程序,允许用户成为订阅用户,费用为 X 美元/年,但我被困在订阅页面上,无论我做什么,stripeToken 都不会附加到请求中,并且 CreateSubscription.rb 说“这个客户没有附加付款来源”。我的 Turbolinks 被禁用。

我的 Stripe 日志显示它正在收到带有 POST 正文的请求:电子邮件:“[email protected]”计划:“praducks-year”但错误抛出如下 - 因为没有附加到请求的源对象 -错误:类型:“无效请求错误”消息:“该客户没有附加付款来源”

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

订阅.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($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};

订阅 Controller

class SubscriptionsController < ApplicationController
before_filter :authenticate_user!
before_filter :load_plans

def index
# if !current_user.subscription.nil?
# redirect_to edit_subscription_path
# end
end

def new
@subscription = Subscription.new
@plan = Plan.find_by_id(params[:plan_id])
if @plan.nil?
redirect_to subscriptions_path, :notice => "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

<!--<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">-->

<div class="col2">
<div class="shoppingcart">
<% unless @subscription.errors.blank? %>
<%= @subscription.errors.full_messages.to_sentence %>
<% end %>
<h4 class="heading colr">Joining <%= @plan.name %></h4>
<div class="clear"></div>
<div class="cart_form">
<div class="form_cont">
<%= form_for @subscription, url: subscription_path, html: { id: 'payment-form' } do |f| %>
<input type="hidden" name="plan_id" value="<%= @plan.id %>" />
<input type="hidden" name="stripeEmail" value="<%= current_user.email %>" />
<input type="hidden" name="email_address" value="<%= current_user.email %>" />
<span class="payment-errors"></span>

<%= f.label :Email_Address %>: <%= current_user.email %>
<div class="clear"></div>

<%= f.label :Card_Number %>
<div class="clear"></div>
<input type="text" size="20" data-stripe="number"/>*
<div class="clear"></div>
<%= f.label :CVC %>
<div class="clear"></div>
<input type="text" size="4" data-stripe="cvc"/>*
<div class="clear"></div>
<%= f.label :"Expiration (MM/YYYY)" %>
<div class="clear"></div>
<input type="text" data-stripe="exp-month" placeholder="mm" id="exp" limit="2"/>
<input type="text" data-stripe="exp-year" placeholder="yyyy" id="exp" limit="4"/>*
<div class="clear"></div>
<button type="submit">Join</button>
<% end %>
</div>
</div>
</div>
</div>

application.html.erb

  <title><%=  full_title(yield(:title)) %></title>
<% if params[:beta] == "1" %>
<% session[:beta] = "1" %>
<% end %>
<!--CSS-->
<%= stylesheet_link_tag 'application', media: 'all' %>

<!--<%= javascript_include_tag 'application', type: 'text/javascript'%>-->

<script type="text/javascript" src="https://s3-us-west-2.amazonaws.com/praducks-uploads/jquery.min.js"></script>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1"/><!-- Prelang: Analytics / Google Analytics -->
<%= analytics_init if Rails.env.production? or Rails.env.development? %>

<!--Javascript-->
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
$(function(){
Stripe.setPublishableKey('<%= Rails.configuration.stripe[:publishable_key] %>');
});
</script>
<script type="text/javascript" src="/js/subscriptions.js"></script>

</head>
<body data-no-turbolink>
<div id ="wrapper_sec">

<% if Rails.env.production? %>
<% if session[:beta] == "1" %>
<%= render partial: "elements/navbar" %>
<%= bootstrap_flash %>
<!-- Bread Crumb Section -->
<%= render partial: "elements/crumbs" unless current_page?(root_url) %>
<div class="clear"></div>
<%= yield %>
<%= render partial: "elements/footer" %>
<% else %>
<%= render "layouts/comingsoon" %>
<% end %>
<% else %>
<%= render partial: "elements/navbar" %>
<%= bootstrap_flash %>
<!-- Banner -->
<%= render "elements/banner" if current_page?(root_url) %>
<!-- Bread Crumb Section -->
<%= render partial: "elements/crumbs" %>
<div class="clear"></div>
<div id="content_sec">
<%= render "elements/leftnav" %>
<%= yield %>
</div>
<%= render partial: "elements/footer" %>
<% end %>

</div>
</body>

最佳答案

在本地运行此代码后,我看到了一些事情:

我注意到,subscriptions.js 并未使用rails 帮助器方法javascript_include_tag 包含在内。除非您已将脚本放入应用的 public/js 文件夹中,否则您的脚本可能不会出现在页面上。

假设您确实在 public/js 中拥有它并且它位于页面上,我在提交表单时注意到此错误:

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

由于此错误,您的提交处理程序永远不会达到 return false 以阻止表单提交,因此它在没有 Stripe token 的情况下提交。

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

如果您不想升级 jquery 版本,可以将 .prop 替换为 .attr:

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

变成:

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

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

event.preventDefault();

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

关于ruby-on-rails - Stripe token 未附加到 Rails 应用程序的请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com