简体   繁体   中英

IPN listner using .NET SDK Paypal?

I am working with .NET SDK Paypal. I am not sure if SDK provides the IPN listener feature. I have tried below code, which always returns true, is it the correct way to IPN verification or not, I tried to find on google. but didn't find anything.

 var ipn = Request.Form.AllKeys.ToDictionary(k => k, k => Request[k]);

        var param = Request.BinaryRead(Request.ContentLength);
        PayPal.IPNMessage iPNMessage = new PayPal.IPNMessage(ipn, param);
        PayPal.IPNMessage service = null;

        try
        {
            Dictionary<string, string> configurationMap = Configuration.GetAcctAndConfig();
            service = new PayPal.IPNMessage(configurationMap, param);
            var response = service.Validate();
        }
        catch (System.Exception ex)
        {
        }

When I try with HTTPWebRequest, it always send me INVALID for every transaction:

here is my code:

 public HttpStatusCodeResult Receive()
    {
        LogRequest(Request);
        Task.Run(() => VerifyTask(Request));
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }

    private void VerifyTask(HttpRequestBase ipnRequest)
    {
        var verificationResponse = string.Empty;
        try
        {

            var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");
            verificationRequest.Method = "POST";
            verificationRequest.ContentType = "application/x-www-form-urlencoded";
            var param = Request.BinaryRead(ipnRequest.ContentLength);
            var strRequest = Encoding.ASCII.GetString(param);
            strRequest = "cmd=_notify-validate&" + strRequest;
            verificationRequest.ContentLength = strRequest.Length;
            var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();
            var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream());
            verificationResponse = streamIn.ReadToEnd();
            streamIn.Close();
        }
        catch (Exception exception)
        {
        }
        ProcessVerificationResponse(verificationResponse);
    }

You have to use the IPN tester tool on Paypals site, unless you do then there's no transaction on the Paypal end that will match so everything will show as invalid. You're currently using the Sandbox endpoint so that will only validate requests that come from the tester.

For live transactions made with actual payments you don't use the sandbox endpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM