Ruby on rails 为什么我的用户从Paypal付款返回时没有登录?

Ruby on rails 为什么我的用户从Paypal付款返回时没有登录?,ruby-on-rails,paypal,devise,Ruby On Rails,Paypal,Devise,用户注册帐户后,他们将自动登录 然后他们可以选择“激活他们的帐户”来解锁应用程序的功能。帐户激活将引导他们进入Paypal网关。成功完成付款后,它们将重定向回发票页面。但是,发票页面上没有当前用户会话。你知道我做错了什么吗?代码如下 我正在使用Desive管理身份验证 这是用户模型 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confir

用户注册帐户后,他们将自动登录

然后他们可以选择“激活他们的帐户”来解锁应用程序的功能。帐户激活将引导他们进入Paypal网关。成功完成付款后,它们将重定向回发票页面。但是,发票页面上没有当前用户会话。你知道我做错了什么吗?代码如下

我正在使用Desive管理身份验证

这是用户模型

    class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :first_name, :last_name, :email, :password, presence: true

  serialize :notification_params, Hash
  def paypal_url(return_path)
    values = {
        business: "seller@seller.com",
        cmd: "_xclick",
        upload: 1,
        return: "#{Rails.application.secrets.app_host}#{return_path}",
        invoice: id,
        amount: 20,
        item_name: "Online Store",
        item_number: 1,
        quantity: '1',
        notify_url: "#{Rails.application.secrets.app_host}/hook"
    }
    "#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
  end

end
这是管理支付/网络钩子的控制器

class MembershipsController < ApplicationController

  def show
    @user = current_user

  end

  protect_from_forgery except: [:hook]
  def hook
    params.permit! # Permit all Paypal input params
    status = params[:payment_status]
    if status == "Completed"
      @user = User.find params[:invoice]
      @user.update_attributes notification_params: params, status: status, transaction_id: params[:txn_id], purchased_at: Time.now
    end
    render nothing: true
  end
end

如果你还有什么需要看的,请告诉我。谢谢你

经过研究,我在谷歌论坛上找到了Stefan Wrobel的答案

“好的,我终于明白了。对于rails 3.0.4及以上版本,自动 如果有任何操作,表单上的CSRF保护将使会话无效 除GET之外的其他操作已完成,并且未提供真实性令牌。 由于无法让Paypal发送真伪令牌, 有两种解决方法:

  • 允许Paypal保留其默认设置 行为并发回您的站点。在这种情况下,您可以禁用 使用protect_from_伪造保护伪造:except=> [:controller\u action\u name]虽然这可能被视为 如果您接受Paypal的支付数据,则可能存在安全漏洞 传输信息而不进行验证,因为有人可能 只需发布虚假数据并完成事务

  • 强制贝宝发送 通过设置web支付标准rm,用户可以通过GET返回 强制获取的参数为1(rm表示返回方法,必须 与返回参数结合使用以指定URL)。 请看这里:

  • 祝你好运,希望这有帮助!花了太长的时间才弄明白这一点 出去……”

    class MembershipsController < ApplicationController
    
      def show
        @user = current_user
    
      end
    
      protect_from_forgery except: [:hook]
      def hook
        params.permit! # Permit all Paypal input params
        status = params[:payment_status]
        if status == "Completed"
          @user = User.find params[:invoice]
          @user.update_attributes notification_params: params, status: status, transaction_id: params[:txn_id], purchased_at: Time.now
        end
        render nothing: true
      end
    end
    
    %section#course-content
      %section#ruby
        %section.detailed-syllabus
          .wrapper-inside
            %h3.title Course invoice
          .columns.clearfix
            .left{style: "padding-left:100px;"}
              %p#notice= notice
              %p
                - if user_signed_in?
                  %strong Email:
                  = current_user.first_name
                - else
                  %strong Invalid User Session