C# Paypal Ipn与asp.net MVC的集成
HomeControler/Index.cshtml页面如下所示C# Paypal Ipn与asp.net MVC的集成,c#,asp.net-mvc-4,paypal,paypal-ipn,C#,Asp.net Mvc 4,Paypal,Paypal Ipn,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
<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>
用户ID
用贝宝支付
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();
}
public ActionResult Ipn()
{
//从PayPal接收IPN请求并解析返回的所有变量
var formVals=新字典();
formVals.Add(“cmd”和“notify-validate”);
//如果您想使用PayPal沙盒,请将其从false更改为true
字符串响应=GetPayPalResponse(formVals,false);
如果(响应=“已验证”)
{
字符串transactionID=请求[“txn_id”];
字符串sAmountPaid=Request[“mc_gross”];
字符串deviceID=请求[“自定义”];
//确认订单
十进制支付金额=0;
十进制.特里帕尔斯(sAmountPaid,OutAmountPaid);
如果(sAmountPaid==“2.95”)
{
//获取返回的信息并将其存储到订阅表中
//在这里,您可以使用tran的详细信息更新数据库
返回视图();
}
其他的
{
//失败-这是IPN,因此没有查看器
//您可能需要在此处记录一些内容
}
}
返回视图();
}
我的问题是,即使在支付完成后,上面的Ipn操作方法也不会触发。无法调试。我该怎么做?如果我没记错的话,Ipn是一个异步调用,可以在交易后的任何时间进行(通常是“即时的”,有时不是那么多)。但这来自PayPal,他无法访问
http://localhost
。要测试IPN,您需要部署到任何人都可以访问的实际internet站点。我与IPN合作已经有几年了,但这是我的一般经验。在应用程序中设置一些日志记录,发布,然后执行测试事务
编辑:
此外-我认为您可以为它提供您的WAN IP地址(非本地),打开路由器中的端口,然后使用该IP地址(注意,您可能需要启用与IIS Express的远程连接-请参阅):
如果我没记错的话,IPN是一个异步调用,可以在事务完成后随时调用(通常是“即时的”,有时不是那么多)。但这来自PayPal,他无法访问
http://localhost
。要测试IPN,您需要部署到任何人都可以访问的实际internet站点。我与IPN合作已经有几年了,但这是我的一般经验。在应用程序中设置一些日志记录,发布,然后执行测试事务
编辑:
此外-我认为您可以为它提供您的WAN IP地址(非本地),打开路由器中的端口,然后使用该IP地址(注意,您可能需要启用与IIS Express的远程连接-请参阅):
您可以在本地主机上检查ipn。您需要设置路由器以接受并将传入呼叫重定向到本地主机。然后转到paypal.sandbox。使用他们的工具,您可以模拟对本地主机的不同IPN响应(当然,使用您的外部Ip地址) 通过这种方式,sandbox将tcp/Ip消息发送到您的机器,您的路由器将其重定向到托管测试网站的机器 最好不要尝试向sandbox发送消息,而期望接收并捕获响应。这并不是说沙箱工作不正常。是的 问题是,如果sandbox响应很快,那么您的测试机器(在调试模式下)可能不够快,无法捕获返回的tcp/ip数据包。您可以使用另一台计算机在本地主机网站上启动事务。i、 分离测试事务路径
希望这有帮助。您可以在本地主机上检查ipn。您需要设置路由器以接受并将传入呼叫重定向到本地主机。然后转到paypal.sandbox。使用他们的工具,您可以模拟对本地主机的不同IPN响应(当然,使用您的外部Ip地址) 通过这种方式,sandbox将tcp/Ip消息发送到您的机器,您的路由器将其重定向到托管测试网站的机器 最好不要尝试向sandbox发送消息,而期望接收并捕获响应。这并不是说沙箱工作不正常。是的 问题是,如果sandbox响应很快,那么您的测试机器(在调试模式下)可能不够快,无法捕获返回的tcp/ip数据包。您可以使用另一台计算机在本地主机网站上启动事务。i、 分离测试事务路径
希望这能有所帮助。非常好的建议。我今晚将尝试此解决方案,并会给您反馈。非常感谢。我已通过将我的应用程序托管在具有可信证书(https)的公共访问IIS上解决了我的问题。感谢您的想法。@Ostati是的,我已成功完成IPN集成。如果您需要任何帮助,请告诉我。另外,当您为我发表评论时,请使用“@sampath”,否则SOF不会通知我,或者直接放在我的问题区域,那么无需使用“@”标记。@sampath,你好,sampath,我是Kishore。我在ipn方面也面临同样的问题。我没有得到任何回复,因此,我无法找到解决方案。这是我的代码片段@Kishore我想与您分享我的工作代码。因此,如果您仍然有相同的问题,请让我知道。非常好的建议。我今晚将尝试此解决方案,并将给您反馈。非常感谢。我已通过使用可信认证(https)在公共访问IIS上托管我的应用解决了我的问题。谢谢您的想法。@Ostati是的,我有
<input type="hidden" name="notify_url" value="https://97.25.43.21:13769/Home/Ipn">