我正在尝试使用Stripe创建订阅包。 这是我到目前为止所拥有的
我的控制器方法。
def subscription_one
session[:tab] = "$1.99/Month"
@subscription = Subscription.where(user_id:current_user.id)
end
def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
subscription_one.html.erb
<% if @subscription.present? %>
CREDIT CARD DETAILS PRESENT
<% else %>
<form action="/membership/apply" method="POST" id="payment-form">
<article>
<label class="amount"> <span>Amount: $5.00</span> </label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"></script>
<% end %>
在我提供出现的字段中的所有值后,当我提交时出现错误
ActionController::InvalidAuthenticityToken in MembershipController#create
有什么想法吗?
答案 0 :(得分:4)
几个问题:
-
<强>表格强>
您提供的form
是硬编码的
如Philidor Green
所述,您遇到的问题是,此表单不会由Rails提供正确的authenticity token
。根据经验,Rails为大多数HTML元素提供帮助,允许您为应用程序创建一致的代码:
<%= form_tag path do %>
<% end %>
您应该使用form_tag
来实现此目标
-
<强>订阅强>
Subscription.new(params[:subscription])
应该是:
def subscribed_one
Subscription.new(subscription_params)
end
private
def subscription_params
params.require(:subscription).permit(:params, :attributes)
end
-
<强>更新强>
为了解决这个问题,我要这样做:
#view
<% if @subscription.present? %>
Credit Card Details Present
<% else %>
<%= form_tag membership_apply_path, id: "payment-form" do %>
<%= content_tag :article do %>
<%= label_tag class: "amount" %>
<%= content_tag :span, "Amount: $5.00" %>
<% end %>
<% submit_tag "Susbcribe" %>
<% end %>
<% end %>
#app/views/layouts/application.html.erb
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500">
</script>
#app/controllers/subscriptions_controller.rb
def subscription_one
session[:tab] = "$1.99/Month"
@subscription = Subscription.where(user_id:current_user.id)
end
def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
答案 1 :(得分:2)
我找到了一个解决方法,也许是为了说实话,它是标准的条带检查,用for_path替换标准的Form元素。它似乎有效:
<%= form_tag stripe_user_standart_charge_checkout_path do %>
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="..."
data-amount="999"
data-name="Grégoire Mulliez"
data-description="Timee"
data-zip-code="true"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="eur">
</script>
<% end %>
stripe_user_standart_charge_checkout_path是您想要的任何路线,不要忘记在路线中将其定义为POST(不是get).rb
编辑:你可以使用这种方法检索小时交易令牌,我也添加了一些html隐藏字段来复活,同时我的交易细节,它的工作就像一个魅力