Link to home
Start Free TrialLog in
Avatar of mmalik15
mmalik15

asked on

IPN delivery failed. Unable to connect to the specified URL. Please verify the URL and try again.

hi experts,

I have written my IPN LIstene in c# as attached in the code and trying to test it via paypal
Instant Payment Notification (IPN) simulator but getting the following error

IPN delivery failed. Unable to connect to the specified URL. Please verify the URL and try again.

Any ideas what i m doing wrong
thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Collections.Specialized;

public partial class IPNListener : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
        string strLive = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);
        string strResponse_copy = strRequest;  //Save a copy of the initial info sent by PayPal
        strRequest += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;

        //for proxy
        //WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
        //req.Proxy = proxy;
        //Send the request to PayPal and get the response
        StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
        streamOut.Write(strRequest);
        streamOut.Close();
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        string strResponse = streamIn.ReadToEnd();
        streamIn.Close();

        if (strResponse == "VERIFIED")
        {
            //check the payment_status is Completed
            //check that txn_id has not been previously processed
            //check that receiver_email is your Primary PayPal email
            //check that payment_amount/payment_currency are correct
            //process payment

            // pull the values passed on the initial message from PayPal

            NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
            string user_email = these_argies["payer_email"];
            string pay_stat = these_argies["payment_status"];
            //.
            //.  more args as needed look at the list from paypal IPN doc
            //.


            if (pay_stat.Equals("Completed"))
            {
                SendEmail(user_email);
            }


            // more checks needed here specially your account number and related stuff
        }
        else if (strResponse == "INVALID")
        {
            //log for manual investigation
        }
        else
        {
            //log response/ipn data for manual investigation
        }
    }

    protected void SendEmail(string userEmail)
    {

        string MailFrom, MailTo, MailSub, MailMsg;


        MailFrom = "support@practicelifeintheuktest.co.uk";
        MailTo = "mmalik15@gmail.com";
        MailSub = "A user has made a payment";
        MailMsg = "Dear Support,<Br /><Br />" + userEmail.ToString() + " has made us a payment at " + DateTime.Now.ToShortDateString() + ". <br /> Thankyou.";

        DT_SendEmail obj = new DT_SendEmail(MailTo, MailFrom, MailSub, MailMsg);
        obj.SendEmailWithOutCC();

    }
}

Open in new window

Avatar of sjklein42
sjklein42
Flag of United States of America image

What have you specified as your IPN URL?

(When you enable IPN, PayPal sends messages to the IPN listener at the URL you specify in your account’s profile. You can override the URL to associate other IPN listeners with specific transactions. In this case, you specify the listener’s URL when you set up a Website Payment Standard button or a PayPal API operation.)

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNIntro
Avatar of Dave Baldwin
That sounds like a connection error, not a code error.  I believe that means that the URL you gave to Paypal for your IPN page is wrong or is not accessible.
Avatar of mmalik15
mmalik15

ASKER

this is the IPNListerner URL on my website

http://www.practicelifeintheuktest.co.uk/IPNListener.aspx

and the paypal IPN simulator i m using is present on https://developer.paypal.com/us/cgi-bin/devscr

I have specified this test url in IPN simular on the paypal website
Your IPN URL appears to be doing a 302 redirect:.  Here are the headers you're returning from http://www.practicelifeintheuktest.co.uk/IPNListener.aspx

It appears to be redirecting to itself (infinite redirect loop?):

HTTP/1.1 302 Found
Connection: close
Pragma: no-cache
cache-control: no-cache
Location: /IPNListener.aspx

Open in new window

thanks for the comments. Any ideas why it could be doing a 302 redirect or goes in infinite redirect loop?
I am not an ASP expert, so I have idea why you are getting a 302 redirect.  Even your home page URL www.practicelifeintheuktest.co.uk/  returns 302 redirect.

It is even stranger than that.  I tried it several times, and about half the time it redirects to a random folder, see below.  The random string "YhaWT" is different each time.

HTTP/1.1 302 Found
Connection: close
Pragma: no-cache
cache-control: no-cache
Location: /YhaWT/IPNListener.aspx

Open in new window


Something is wrong with the way that the domain www.practicelifeintheuktest.co.uk is set up.  Try to get it fixed so you can return a simple HTML page from that domain before trying to get your IPN URL working.
Try www.practicelifeintheuktest.co.uk/home.aspx to access the website. I manage to find the prob. The prb is on this line of code

if (pay_stat.Equals("Completed"))
            {
                SendEmail(user_email);
            }
 if i remove that IPN simulator returns success.

Any ideas where shall i write code to store the info in the databas coming from IPN. In the above case I tried to call a method inside

 if (strResponse == "VERIFIED")
        {
            //check the payment_status is Completed
            //check that txn_id has not been previously processed
            //check that receiver_email is your Primary PayPal email
            //check that payment_amount/payment_currency are correct
            //process payment

            // pull the values passed on the initial message from PayPal

            NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
            string user_email = these_argies["payer_email"];
            string pay_stat = these_argies["payment_status"];
            //.
            //.  more args as needed look at the list from paypal IPN doc
            //.


            if (pay_stat.Equals("Completed"))
            {
                SendEmail(user_email);
            }


            // more checks needed here specially your account number and related stuff
        }

which does not seem to work
ASKER CERTIFIED SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial