Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PayPal.ConnectionException: Invalid HTTP response: The request was aborted: Could not create SSL/TLS secure channel

Tags:

ssl

paypal

I am getting a problem with the PayPal REST Api, sometimes it works fine! (and on sandbox it works 100%) But on the live system when connecting to the live PayPal system. I sometimes get the follow error.

PayPal.ConnectionException: Invalid HTTP response: The request was aborted: Could not create SSL/TLS secure channel. at 
PayPal.Api.HttpConnection.Execute(String payLoad, HttpWebRequest 
    httpRequest) at PayPal.Api.PayPalResource.ConfigureAndExecute[T]
    (APIContext apiContext, HttpMethod httpMethod, String resource, String 
    payload, String endpoint, Boolean setAuthorizationHeader) at 
    PayPal.Api.OAuthTokenCredential.GenerateOAuthToken() at 
    PayPal.Api.OAuthTokenCredential.GetAccessToken()

Basically is just keep crashing when trying to get the access token from PayPal

I have updated the SDK to the latest version and the code had been happily working for +3 months.

like image 687
TheAlbear Avatar asked Oct 01 '15 21:10

TheAlbear


3 Answers

It looks like in some cases the PayPal connection is not released for some reason. this creates an error as there is a limit in .Net to the number of concurrent web connections to any single host, that limit is 2.

Add this before you try and connect to PayPal, remember it needs to go before every connection e.g if you are using the SDK and the express checkout.

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.DefaultConnectionLimit = 9999;

//PayPay SDK connection
var apiContext = Configuration.GetAPIContext();
like image 133
TheAlbear Avatar answered Oct 19 '22 20:10

TheAlbear


This error is occurring because PayPal have started to drop support for SSLv3. Regarding the accepted answer, only the Tls12 setting is required to get the PayPal connection working again.

I didn't want to have to implement this setting all over my code. I found that putting this setting in the Application_Start() method of my global.asax file was the best solution.

Also, my version 4.0 NET framework didn't allow me to use the SecurityProtocolType.Tls12 flag - but it works if I cast it like this:

protected void Application_Start()
{
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // This is equivalent to SecurityProtocolType.Tls12
}
like image 22
James McCormack Avatar answered Oct 19 '22 21:10

James McCormack


In my first answer I recommended add

ServicePointManager.SecurityProtocol =  SecurityProtocolType.Tls12;

But My Project using Amazon service also, and it not working with TLS12. It's work fine when I replace it on pack of protocols:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
like image 2
Viktor Mezhenskyi Avatar answered Oct 19 '22 19:10

Viktor Mezhenskyi