Home IPN validation doesn't fail when it should fail
Reply: 0

IPN validation doesn't fail when it should fail

user6572
1#
user6572 Published in September 21, 2018, 8:34 am

I am trying to set up PayPal to process payments. Everything is working OK except the fact that payments are not being cancelled when my IPN doesn't send a validation message to PayPal. As far as I understand if the IPN didn't validate the payment the transaction supposed to be cancelled. The code looks like that:

[HttpPost]
public IActionResult Receive()
{
    //Store the IPN received from PayPal
    LogRequest(Request);

    Task.Run(() => VerifyTask(Request));

    //Reply back a 200 code
    return new EmptyResult();
}

private void VerifyTask(HttpRequest ipnRequest)
{
    try
    {
        VerifyIpnParamsWithException(ipnRequest);
        var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/cgi-bin/webscr");

        //Set values for the verification request
        verificationRequest.Method = "POST";
        verificationRequest.ContentType = "application/x-www-form-urlencoded";
        var param = ReadRequestBody(ipnRequest.Body);
        var strRequest = Encoding.ASCII.GetString(param);

        //Add cmd=_notify-validate to the payload
        strRequest = "cmd=_notify-validate&" + strRequest;
        verificationRequest.ContentLength = strRequest.Length;

        //Attach payload to the verification request
        using (var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII))
        {
            streamOut.Write(strRequest);
            streamOut.Close();
        }
        var verificationResponse = string.Empty;

        //Send the request to PayPal and get the response
        using (var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream()))
        {
            verificationResponse = streamIn.ReadToEnd();
            streamIn.Close();
        }
        ProcessVerificationResponse(verificationResponse);
    }
    catch (Exception exception)
    {
        DAL.LogError(exception, ipnRequest);
    }
}

When I tested it, according to my log an exception was thrown before a verification message was sent to PayPal. It didn't change the fact that the money was transferred to the PayPal account and the transaction is completed. Did I misunderstand? Is there anything else I'm supposed to do in order to cancel the transaction?

share|improve this question

1 Answer 1

active oldest votes
up vote 0 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301975 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO