Paypal Ipn 与 asp.net MVC 的集成 [英] Paypal Ipn integration with asp.net MVC

查看:19
本文介绍了Paypal Ipn 与 asp.net MVC 的集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HomeControler/Index.cshtml 页面如下

<form id="paypalForm" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"><字段集><p class="inline-small-label" style="padding-top: 7px;"><label for="device-id"><span>用户 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">使用PayPal支付</button></fieldset></表单>

HomeControler/Ipn Action方法如下

public ActionResult Ipn(){//接收来自PayPal的IPN请求并解析所有返回的变量var formVals = new Dictionary();formVals.Add("cmd", "_notify-validate");//如果您想使用 PayPal 沙箱,请将其从 false 更改为 true字符串响应 = GetPayPalResponse(formVals, false);如果(响应==已验证"){string transactionID = Request["txn_id"];字符串 sAmountPaid = Request["mc_gross"];string deviceID = Request["custom"];//验证订单十进制金额支付 = 0;Decimal.TryParse(sAmountPaid, out amountPaid);if (sAmountPaid == "2.95"){//获取返回的信息并将其存储到订阅表中//这是您将使用 tran 的详细信息更新数据库的地方返回视图();}别的{//让失败 - 这是 IPN,所以没有查看器//你可能想在这里记录一些东西}}返回视图();}

我的问题是即使付款完成后,上面的 Ipn 操作方法也没有触发.无法调试.我怎么能这样做?

解决方案

如果我没记错的话,IPN 是一个异步调用,可以在事务之后的任何时间进行(它通常是即时的",有时不是那么多).但这来自无法访问 http://localhost 的 PayPal.要测试 IPN,您需要部署到任何人都可以访问的实际 Internet 站点.我在 IPN 工作已经有几年了 - 但那是我的一般经验.在您的应用程序中设置一些日志记录,发布,然后执行您的测试事务.

另外 - 我认为你可以给它你的 WAN IP 地址(不是本地的),打开你路由器中的端口,而是使用那个 IP 地址(注意你可能需要启用与 IIS Express 的远程连接 - 请参阅IIS Express 启用外部请求):

HomeControler/Index.cshtml page is as below

<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 Action method is as below

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();
}

My question is even after payment has been done, above Ipn action method is not firing.Cannot debug.How could I do that ?

解决方案

If I remember correctly, the IPN is an asynchronous call that can come at anytime after the transaction (its generally "instant", sometimes not so much). But this comes from PayPal, who cannot access http://localhost. To test IPN you need to deploy to an actual internet site that anyone can access. It's been a few years since I worked with IPN - but that was my general experience. Setup some logging in your application, publish, then do your test transactions.

EDIT:

Also - I think you can give it your WAN IP address (not local), open up the ports in your router, and instead use that IP address (note you may need to enable remote connections with IIS Express - see IIS Express enable external request):

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

这篇关于Paypal Ipn 与 asp.net MVC 的集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆