我正在将Paypal IPN集成到我的市场应用程序中。我一直在与MVC架构进行斗争,试图让它发挥作用。
我希望在收到" VERIFIED"的IPN响应后更新订单数据库中的a:created_at列。然后Order模型将使用回调来保存创建操作后的顺序:purchase_at列不是nil。
我的想法是使用IPN响应将订单保存到数据库,但我无法弄清楚如何使控制器/模型正常通信。
class PaymentNotificationsController < ApplicationController
protect_from_forgery :except => [:create] #Otherwise the request from PayPal wouldn't make it to the controller
def create
response = validate_IPN_notification(request.raw_post)
case response
when "VERIFIED"
order.update_attribute(:purchased_at, Time.now)
when "INVALID"
else
end
render :nothing => true
end
protected
def validate_IPN_notification(raw)
uri = URI.parse('https://ipnpb.sandbox.paypal.com/cgi-bin/webscr?cmd=_notify-validate')
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 60
http.read_timeout = 60
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
response = http.post(uri.request_uri, raw,
'Content-Length' => "#{raw.size}",
'User-Agent' => "My custom user agent"
).body
end
end
class PaymentNotification < ApplicationRecord
belongs_to :order
end
class Order < ApplicationRecord
belongs_to :item
belongs_to :buyer, class_name: "User"
belongs_to :seller, class_name: "User"
after_create :purchased?
private
def purchased?
if :purchased_at != nil
order.save
end
end
end