我正在使用这个 gem 通过贝宝付款 https://github.com/tc/paypal_adaptive

我对这颗宝石感到非常困惑和迷失方向。它的记录很差,我很难理解如何从 PayPal 的 ipn 响应中获取数据。

我希望这个问题能够帮助更多有同样问题的人。

我的步骤是:

我从我的 PayPal 发送请求 orders_controller.rb 使用 preaproval_ payment 方法。

def preapproval_payment
preapproval_request = PaypalAdaptive::Request.new
data = {
  "returnUrl" => response_paypal_user_orders_url(current_user),
  "cancelUrl"=>  cancel_payment_gift_url(@gift),
  "requestEnvelope" => {"errorLanguage" => "en_US"},
  "senderEmail" => "gift_1342711309_per@gmail.com",
  "startingDate" => Time.now,
  "endingDate" => Time.now + (60*60*24) * 30,
  "currencyCode"=>"USD",
  "maxAmountPerPayment" => "@gift.price",
  "ipnNotificationUrl" => ipn_notification_url,
  "ip" => request.remote_ip
  }
    preapproval_response = preapproval_request.preapproval(data)
    puts data
  if preapproval_response.success?
    redirect_to preapproval_response.preapproval_paypal_payment_url
  else
    redirect_to gift_url(@gift), alert: t(".something_was_wrong")
  end
end

这些是我在命令日志控制台中请求的数据 puts data :

{"returnUrl"=>"http://localhost:3000/en/u/maserranocaceres/orders/response_paypal", "cancelUrl"=>"http://localhost:3000/en/gifts/gift-1/cancel_payment", "requestEnvelope"=>{"errorLanguage"=>"en_US"}, "senderEmail"=>"gift_1342711309_per@gmail.com", "startingDate"=>2012-07-29 13:05:49 +0200, "endingDate"=>2012-08-28 13:05:49 +0200, "currencyCode"=>"USD", "maxAmountPerPayment"=>9, "ipnNotificationUrl"=>"http://localhost:3000/ipn_notification?locale=en", "ip"=>"127.0.0.1"}

我重定向到 paypal 页面,并成功在 paypal 上付款:D。

付款成功完成后,我会被引导至:

http://localhost:3000/en/u/maserranocaceres/orders/response_paypal

我有 response_paypal 行动于 orders_controller.rb. 。这是 GET 操作,我的此操作代码是:

def response_paypal
  respond_to do |format|
       format.html { redirect_to user_orders_url(current_user), :alert => "works fine return url"}
    end
 end

到目前为止,一切正常。

现在我需要的是获取从 PayPal 收到的数据,并在付款成功处理后将新订单保存在我的数据库中。

为此,我在中创建了一个文件 lib/paypal_ipn.rb 我将以下内容添加到该文件中 https://github.com/tc/paypal_adaptive/blob/master/templates/paypal_ipn.rb

# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)

class PaypalIpn
  def self.call(env)
    if env["PATH_INFO"] =~ /^\/paypal_ipn/
      request = Rack::Request.new(env)
      params = request.params
      ipn = PaypalAdaptive::IpnNotification.new
      ipn.send_back(env['rack.request.form_vars'])
      if ipn.verified?
        #mark transaction as completed in your DB
        output = "Verified."
      else
        output = "Not Verified."
      end

      [200, {"Content-Type" => "text/html"}, [output]]
    else
      [404, {"Content-Type" => "text/html"}, ["Not Found"]]
    end
  end

end

在我的 路线.rb 我加:

match "/ipn_notification" => PaypalIpn

我的两个问题是:

A) 我没有看到付款后该文件被解雇,并且我在控制台中看不到从贝宝获得的数据。

b) 我想在我的请求中发送对象的 id 到 paypal @gift 为了以后能够恢复 paypal_ipn.rb 并保存我的数据库。

我做错了什么以及如何解决这些问题?

谢谢

有帮助吗?

解决方案

我没用过那个 gem,但我以前用过 PayPal IPN。以下是您应该检查的一些事项:

  1. 您的 PayPal 帐户是否设置为使用 IPN?您必须在帐户上启用此设置才能使其发挥作用。

  2. 您是否已验证在付款过程中传递 ipn_notification_url 时是否与您的“/ipn_notification”路由匹配?

  3. 为此,PayPal 必须能够直接与运行此应用程序的服务器进行通信。这意味着,通常,除非您在本地计算机上使用动态 DNS 等进行了自定义设置,否则您需要将此代码实际部署到服务器,以便 PayPal 能够与您的应用程序进行通信。换句话说,如果这是运行在 http://localhost:3000, ,这是行不通的。

为了回答你的第二个问题,如何恢复@gift以便在你的数据库中记录它已支付的事实,我不完全确定如何使用这个gem来做到这一点,但我会告诉你我如何使用ActiveMerchant来做到这一点- 可能非常相似。

  1. 在向 PayPal 发出的付款请求中,您可以传入发票号码。我相信该字段被称为“发票”。在这里您将传递礼物的 ID。

  2. 当 PayPal 通过 IPN 通知您的应用程序订单已付款时,它会将发票号码传回给您。使用此发票号码检索@gift,然后您可以用它执行您需要的操作。

以下是我使用 ActiveMerchant gem 的 PayPal 代码的相关部分: https://gist.github.com/3198178

祝你好运!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top