Paypal Ipn integration with asp.net MVC

2019-02-20 16:50发布

问题:

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 ?

回答1:

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">


回答2:

You can check ipn on your localhost. You need to set up your router to accept and redirect incoming calls to your localhost. Then go to paypal.sandbox. Using their tools you can mimic different IPN responses to your localhost(of course using your outside Ip address).

In this way, sandbox sends tcp/Ip messages to your machine, your router redirects it to your machine which is hosting the test website.

It is best not to try to send a message to sandbox and expect to receive and capture the response back. It is not that sandbox is not working correctly. It is.

The problem is if sandbox responds quickly then your test machine(whilst in debug mode) may not be fast enough to capture the return tcp/ip packet. You could use another machine to start a transaction on your localhost website. i.e. decoupling test transaction path.

Hope this helps.