簡體   English   中英

與貝寶ipn混淆和迷失方向

[英]confused and disoriented with paypal ipn

我正在使用此gem在Paypal中付款https://github.com/tc/paypal_adaptive

我對這個寶石感到非常困惑和迷失。 它的文檔記錄不充分,讓我很難理解如何在ipn響應中從Paypal獲取數據。

我希望這個問題能幫助更多的人遇到同樣的問題。

我的步驟是:

我使用orders_controller.rb方法從我的orders_controller.rb向Paypal發送請求。

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在我的日志控制台中的請求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"}

我重定向到貝寶頁面,並成功在貝寶上付款:D。

付款成功完成后,我會轉到:

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

我在orders_controller.rbresponse_paypal動作。 這是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

在我的routes.rb中 ,添加:

match "/ipn_notification" => PaypalIpn

我的兩個問題是:

一)我不認為這使得支付該文件被解雇后,我不能在我的控制台數據看,我來自PayPal獲得。

b)我想發送給我的請求給paypal,對象@gift的ID,以便以后可以在paypal_ipn.rb恢復並保存我的數據庫。

我在做錯什么以及如何解決這些問題?

謝謝

我沒有使用過該寶石,但是我以前使用過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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM