38

It was working well before a week but now it shows following error. I have tried the following things but of no use.

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

so suggest me with possible solution

public string HttpCall(string NvpRequest) //CallNvpServer
    {
        string url = pendpointurl;

        //To Add the credentials from the profile
        string strPost = NvpRequest + "&" + buildCredentialsNVPString();
        strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        // allows for validation of SSL conversations
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
        objRequest.Timeout = Timeout;
        objRequest.Method = "POST";
        objRequest.ContentLength = strPost.Length;

        try
        {
            using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
            {
                myWriter.Write(strPost);
            }
        }
        catch (Exception e)
        {
            /*
            if (log.IsFatalEnabled)
            {
                log.Fatal(e.Message, this);
            }*/
        }

        //Retrieve the Response returned from the NVP API call to PayPal
        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
        string result;
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
        {
            result = sr.ReadToEnd();
        }

        //Logging the response of the transaction
        /* if (log.IsInfoEnabled)
         {
             log.Info("Result :" +
                       " Elapsed Time : " + (DateTime.Now - startDate).Milliseconds + " ms" +
                      result);
         }
         */
        return result;
    }
serenesat
  • 4,611
  • 10
  • 37
  • 53
vellai durai
  • 1,019
  • 3
  • 16
  • 38

4 Answers4

38

I just ran into this same problem in my testing environment as well (luckily my live payments are going through). I fixed it by changing:

public PayPalAPI(string specialAccount = "")
{
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;

to

public PayPalAPI(string specialAccount = "")
{
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

They disabled support for SSL3 a while ago: https://www.paypal.com/uk/webapps/mpp/ssl-security-update, specifically stating

Ensure you are connecting to PayPal endpoints using TLS 1.0 or 1.2 (not all API endpoints currently support TLS 1.1).

Their latest update (thx for the comment update from @awesome) states:

PayPal is updating its services to require TLS 1.2 for all HTTPS connections. At this time, PayPal will also require HTTP/1.1 for all connections... To avoid any disruption of service, you must verify that your systems are ready for this change by June 17, 2016

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
22

Indeed changing SecurityProtocolType.Tls fixes the problem, if you´re working in VS with a framework lower than 4.5 you won´t be able to change it, you have to upgrade your VS to a highest version 2012/2013/2015 to change it.

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

Gonzdn
  • 337
  • 1
  • 6
  • 29
    Actually you *can* use it (in 4.0 at least) like this: `ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // SecurityProtocolType.Tls12` – James McCormack Feb 10 '16 at 17:02
  • This is very very helpful @JamesMcCormack – scgough Mar 16 '16 at 23:27
  • Thanks, its working perfect for .Net 4.0 using ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; please check-http://joymonscode.blogspot.in/2015/08/how-to-make-net-40-45-use-tls-12.html – ramya Mar 19 '16 at 10:39
11

Add the following code to your global.asax or before calling the (HttpWebRequest)WebRequest.Create(url);

protected void Application_Start()
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    // ...
}

This was caused because PayPal are changing their encryption to TLS instead of SSL. This has already been updated on the Sandbox environments but not yet on the live.

Read more: https://devblog.paypal.com/upcoming-security-changes-notice/

Kevin Farrugia
  • 6,431
  • 4
  • 38
  • 61
1

If your framework version is less than 4.5, you might not be able to set it directly. You can use the below code

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
user2081126
  • 77
  • 1
  • 11