2013-07-28 17 views
1

HomeControler/Index.cshtml頁好PayPal IPN一體化是如下用asp.net MVC

<div id="paypalDiv"> 

     <form id="paypalForm" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> 
      <fieldset> 
       <p class="inline-small-label" style="padding-top: 7px;"> 
        <label for="device-id"><span>User ID</span></label></p> 

       <input id="custom" class="full-width" type="text" name="custom" value=""> 
       <input class="full-width" type="hidden" name="business" value="[email protected]"> 
       <input type="hidden" name="cmd" value="_xclick"> 
       <input type="hidden" name="item_name" value="credits"> 
       <input type="hidden" name="item_number" value="40"> 
       <input type="hidden" name="amount" value="2.95"> 
       <input type="hidden" name="no_shipping" value="1"> 
       <input type="hidden" name="return" value="http://localhost:13769"> 
       <input type="hidden" name="notify_url" value="https://localhost:13769/Home/Ipn"> 
       <button class="btn btn-primary submit-button" type="submit">Pay with PayPal</button> 
      </fieldset> 

     </form> 
    </div> 

HomeControler/Ipn的操作方法如下

public ActionResult Ipn() 
{ 
    // Receive IPN request from PayPal and parse all the variables returned 
    var formVals = new Dictionary<string, string>(); 

    formVals.Add("cmd", "_notify-validate"); 

    // if you want to use the PayPal sandbox change this from false to true 
    string response = GetPayPalResponse(formVals, false); 

    if (response == "VERIFIED") 
    { 
     string transactionID = Request["txn_id"]; 
     string sAmountPaid = Request["mc_gross"]; 
     string deviceID = Request["custom"]; 

     //validate the order 
     Decimal amountPaid = 0; 
     Decimal.TryParse(sAmountPaid, out amountPaid); 

     if (sAmountPaid == "2.95") 
     { 
      // take the information returned and store this into a subscription table 
      // this is where you would update your database with the details of the tran 

      return View(); 

     } 
     else 
     { 
      // let fail - this is the IPN so there is no viewer 
      // you may want to log something here 
     } 
    } 

    return View(); 
} 

我的問題是,即使付款完成後,上面的Ipn操作方法不會解僱。不能調試。我該怎麼做?

+0

嗨,我面臨同樣的問題。如何在本地服務器上測試它。並可以請您出示您的GetPayPalResponse方法代碼? –

回答

6

如果我沒有記錯的話,IPN是一個異步調用,它可以在事務之後的任何時候發生(它通常是「即時」,有時不是那麼多)。但是,這來自PayPal,他無法訪問http://localhost。要測試IPN,您需要部署到任何人都可以訪問的實際Internet站點。自從我與IPN合作已有幾年了 - 但那是我的一般經驗。在應用程序中設置一些日誌記錄,發佈,然後進行測試事務。

編輯:

而且 - 我認爲你可以給它你的廣域網IP地址(非本地),在您的路由器打開的端口,並改用IP地址(注意,您可能需要啓用遠程連接使用IIS Express - 請參閱IIS Express enable external request):

<input type="hidden" name="notify_url" value="https://97.25.43.21:13769/Home/Ipn"> 
+0

優秀的建議。我會在今晚嘗試這個解決方案,並會給你一個反饋。謝謝很多。 – Sampath

+0

Sampath,有關您如何解決問題的任何反饋?謝謝.. – ostati

+0

我已通過託管我的應用程序在公共訪問IIS上使用可信任認證(https)解決了我的問題。感謝您的想法。 – Sampath

0

您可以在本地主機上檢查ipn。您需要將您的路由器設置爲接受並將來電重定向到本地主機。然後去paypal.sandbox。使用他們的工具,您可以模擬對本地主機的不同IPN響應(當然使用您的外部IP地址)。

通過這種方式,沙箱將tcp/ip消息發送到您的機器,您的路由器將它重定向到您的機器,該機器託管測試網站。

這是最好不要試圖發送消息到沙箱,並期望收到並捕獲迴應。這並不是說沙盒工作不正常。它是。

問題如果沙箱響應很快,那麼您的測試機器(在調試模式下)可能不足以捕獲返回的tcp/ip數據包。您可以使用其他機器在本地主機網站上啓動交易。即解耦測試事務路徑。

希望這會有所幫助。

+0

讓ngrok橋接互聯網和本地主機之間的通道,它是可以在所有平臺上運行的好工具,包括Windows。 – Adamy