我遇到了这个错误,但是我不知道为什么。我明确地排除了CSRF检查。即使在生产环境中,#webhook
方法也能正常工作。其他类似的问题都是关于Devise的,但是我在这个控制器中没有使用Devise。
stripes_controller.rb
class StripesController < ApplicationController
Stripe.api_key = ENV['STRIPE_PRIVATE']
protect_from_forgery :except => [:webhook, :create]
def show
end
def create
# Amount in cents
@amount = params[:amount]
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Membership Fee',
:currency => 'usd'
)
render :show
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to stripe_path
end
routes.rb
resource :stripe do
日志
Started POST "/stripe/" for 127.0.0.1 at 2018-01-03 11:58:17 -0500
Processing by StripesController#create as HTML
Parameters: {"amount"=>"100", "stripeToken"=>"tok_1BgFZ2IYOmXNPhc121HxNtw0", "stripeEmail"=>"test@example.com"}
Rendering stripes/show.haml within layouts/application
Rendered stripes/show.haml within layouts/application (2.0ms)
User Load (3.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(2.0ms) BEGIN
(1.0ms) COMMIT
Completed 401 Unauthorized in 3533ms (ActiveRecord: 6.0ms)
Started GET "/" for 127.0.0.1 at 2018-01-03 11:58:21 -0500
Rails默认使用
:debug
日志级别。 http://guides.rubyonrails.org/v5.0/debugging_rails_applications.html#log-levels
我可以通过设置devise.rb
来重现此问题。config.timeout_in = 1.minute # 8.hours
如果我已登录并活跃,它可以正常工作,但是如果会话超时,那么就会导致401问题。
这是浏览器的请求/响应。它显示成功和302响应,而不是401。虽然服务器控制台日志中显示为401。
POST /stripe/ HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 82
Cache-Control: max-age=0
Origin: http://localhost:3000
Content-Type: application/x-www-form-urlencoded
...
HTTP/1.1 302 Found
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Location: http://localhost:3000/
Content-Type: text/html; charset=utf-8
即使按状态排序,即使使用保留日志功能,在浏览器网络日志中也没有单个401响应。
Rails 5.0.2
StripesController#create
的 _全部_内容,包括任何before_action
/after_action
回调以及影响它/受其影响的ApplicationController
操作。 - ArtOfCode#create
,但它无法访问用户。没有before_action
或after_action
回调。 - Chloestripes/show
被重定向了,事实上似乎在任何before_action
上都没有停止。您能否也显示stripes/show.html.haml
? - Marek Lipka