【发布时间】:2023-03-23 20:13:01
【问题描述】:
我正在使用 Rails 创建一个应用程序,可以通过使用贝宝的硬币来使用。我正在使用在 Paypal 网站上创建的通用立即购买按钮。我将按钮设置为向我网站上的 URL 提供 IPN。 以前我在身份验证方面遇到问题,但使用下面的代码我已经解决了。现在的问题是,当 IPN 通知控制器但会话被破坏时,我无法使用硬币。所以我的问题是如何检索/保留会话或用户,以便我可以将硬币添加到正确的帐户?
用户模型(相关部分):
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Gets the amount of coins the user currently has.
# @return The amount of coins the user has.
def get_coins
self.coins
end
# Adds an amount of coins to the user and saves to the database.
# @param amount The amount of coins to add to the user.
def add_coins(amount)
full_amount = amount + self.coins
self.coins = full_amount
self.save
end
IPN 指向的控制器:
class PaymentNotificationsController < ApplicationController
before_action :authenticate_user!, :except => [:create]
# POST /payment_notifications
# POST /payment_notifications.json
def create
puts "made it here"
#PaymentNotification.create!(:params => params)
user =User.find(session[:user_id])
user.coins += 100
user.save!
render :nothing => true
end
end
目前上面的代码有一个错误提示“cant find user with id="
应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
skip_before_filter :verify_authenticity_token
end
【问题讨论】:
-
你好。请发布您的用户和 IPN 的型号代码。
-
我没有 IPN 代码。至于用户,可以。
-
为了澄清我没有对 IPN 进行编码,我只使用我在服务商帐户资料中设置的 paypal“默认”。据我所知,它设置了我不知道如何更改的参数。至于用户,添加了代码。也感谢您的帮助。
-
PayPal 拨打的电话会影响哪些模型/表格等?
-
您在上面的代码中看到了这一切。 Paypal 调用对应于 PaymentNotificationsController 的 URL。我试图在该模型中访问的用户是一个带下划线的表格,其中有一个称为硬币的整数。
标签: ruby-on-rails paypal devise paypal-ipn