We help IT Professionals succeed at work.
Get Started

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

Ecnalyr
Ecnalyr asked
on
1,406 Views
Last Modified: 2016-03-24
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;
    }

Open in new window


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.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE