troubleshooting Question

Difficulty confirming Instant Payment Notification (IPN) from Paypal within an MVC3 Application

Avatar of Ecnalyr
Ecnalyr asked on
ASP.NETC#.NET ProgrammingPayPal
11 Comments1 Solution1407 ViewsLast Modified:
I am trying to get Paypal's IPN service working within my app.

When I use the Paypal Sandbox IPN Simulator set to the transaction type of, "Web Accept," Paypal says the message went through just fine  (and if I mess up the code in my Action that handles the IPN, Paypal says there was a server error, so this seems to be communicating correctly).

However, it doesn't appear to actually be doing anything.  If I navigate to my IPN action in a browser `myapp.com/Paypal/IPN`, I receive a response from paypal that says `INVALID` (as expected) and this is written to my output via `Debug.Write`.  When I click "Send IPN" in Paypal's simulator, I get no debug messages at all, although my IPN action is full of `Debug.Write` lines.  (Do calls made from outside your local environment simply not allow `Debug.Write` output?)

For reference, here is the majority of my IPN Action (I've removed various logic for clarity's sake):

    public ActionResult IPN()
    {
        Debug.Write("entering ipn action ");
        var formVals = new Dictionary<string, string>();
        formVals.Add("cmd", "_notify-validate");

        string response = GetPayPalResponse(formVals, true);
        Debug.Write("IPN Response received: " + response + " <-- That was response. . . ");

        if (response == "VALID")
        {
            Debug.Write("Response Was Verified");
        }

        else
        {
            Debug.Write("RESPONSE WAS NOT VERIFIED");
        }

        return this.View();
    }


    string GetPayPalResponse(Dictionary<string, string> formVals, bool useSandbox)
    {
        string paypalUrl = useSandbox
                                ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
                                : "https://www.paypal.com/cgi-bin/webscr";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl);
            
        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";

        byte[] param = Request.BinaryRead(Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);

        StringBuilder sb = new StringBuilder();
        sb.Append(strRequest);

        foreach (string key in formVals.Keys)
        {
            sb.AppendFormat("&{0}={1}", key, formVals[key]);
        }
        strRequest += sb.ToString();
        req.ContentLength = strRequest.Length;

        string response = "";
        using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
        {
            streamOut.Write(strRequest);
            streamOut.Close();
            using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
            {
                response = streamIn.ReadToEnd();
            }
        }
        return response;
    }

Am I correct in understanding that if Paypal is actually submitting a request to my IPN action, I should receive the `Debug.Write` messages the same as when I visit the IPN action within my browser?

It does not appear to me that anything actually happens when Paypal sends the IPN simulated message to my web application, but Paypal says things are ok and Paypal somehow knows if I intentionally make the IPN action have an error when it is caused (so it appears to actually be calling the action somehow).

Can anyone help me understand what I am not understanding here?

I just want my user's to be able to pay using Paypal's standard payment method with a 'buy now' button and be able to change a database value from `false` to 'true' when the payment is confirmed to have been received.

Thank you for your time.

Note: Another way I tested this was to have the action change something in my database if it was called (I simply did something like `MyEntity.Value = new value` then `db.SaveAll();`.  This change to my database was made if I navigated directly to the action within my browser, but no change occurs when I had the paypal IPN simulator 'ping' the action.
ASKER CERTIFIED SOLUTION
Ecnalyr

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros