[英]Paypal Ipn integration with asp.net MVC
HomeControler / Index.cshtml頁面如下
<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="sampath-facilitator@inexisconsulting.com">
<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操作方法也不會觸發。無法調試。我該怎么辦?
如果我沒記錯的話,IPN是一個異步調用,可以在事務處理后的任何時候進行(它通常是“即時”的,有時不會那么多)。 但這來自PayPal,后者無法訪問http://localhost
。 要測試IPN,您需要部署到任何人都可以訪問的實際Internet站點。 自從我與IPN合作以來已經有幾年了-但這是我的一般經驗。 在應用程序中設置一些日志記錄,發布,然后進行測試事務。
編輯:
另外-我想您可以給它提供您的WAN IP地址(不是本地的),打開路由器中的端口,然后使用該IP地址(請注意,您可能需要啟用IIS Express的遠程連接-請參閱IIS Express啟用外部請求 ):
<input type="hidden" name="notify_url" value="https://97.25.43.21:13769/Home/Ipn">
您可以在本地主機上檢查ipn。 您需要設置路由器以接受傳入呼叫並將其重定向到本地主機。 然后轉到paypal.sandbox。 使用他們的工具,您可以模擬對本地主機的不同IPN響應(當然使用外部IP地址)。
這樣,沙盒會將tcp / Ip消息發送到您的計算機,路由器將其重定向到托管測試網站的計算機。
最好不要嘗試將消息發送到沙箱並期望收到並捕獲響應。 並非沙箱無法正常運行。 它是。
問題是,如果沙盒響應迅速,則您的測試計算機(處於調試模式)可能不夠快,無法捕獲返回的tcp / ip數據包。 您可以使用另一台計算機在本地主機網站上啟動事務。 即去耦測試事務路徑。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.