0

I have been using the following code (vb.Net-3.5) in the PayPal Sandbox for 2 months with no problems. Now it throws the error: The underlying connection was closed: An unexpected error occurred on a send

Any ideas on what should be done to make it work again.

Could this have anything to do with the changes PayPal is implementing on the SSL Certificates in Sandbox. This started happening after 1/18/2016. I found this article about the PayPal SSL Certificate update https://www.paypal-knowledge.com/infocenter/index?page=content&id=FAQ1766

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Collections.Generic
Imports System.Object
Imports System.Web
Imports System.Web.Configuration
Imports System.Web.Security
Imports System.Data
Imports System.Data.SqlClient
Imports System.Exception
Imports System.SystemException
Imports System.Threading.ThreadAbortException


Partial Class Payment
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        ' This is the initial start of program 
        Dim QString As String = ""
        Dim parcelID As String = ""
        Dim txID As String = ""

        ' Get Query string Variables
        QString = Request.ServerVariables("QUERY_STRING")
        parcelID = Request.QueryString("item_number")
        txID = Request.QueryString("tx")

        Try

            If Not String.IsNullOrEmpty(txID) Then


                ' Create a request using a URL that can receive a post. 
                ' Sandbox Version
                Dim PayPalWeb As String = "https://www.sandbox.paypal.com/cgi-bin/webscr"
                Dim PayPalID As String = "***********************************************************"
                ' Live Version
                'Dim PayPalWeb As String = "https://www.paypal.com/cgi-bin/webscr"
                'Dim PayPalID As String = ""

                Dim PayPalRequest As WebRequest
                ' Create POST data and convert it to a byte array.
                Dim postData As String = "cmd=_notify-synch" + _
                                         "&tx=" + txID + _
                                         "&at=" + PayPalID + _
                                         "&submit = PDT"

                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)            
                Dim dataStream As Stream

                PayPalRequest = WebRequest.Create(PayPalWeb)
                ' Set the Method property of the request to POST.
                PayPalRequest.Method = "POST"
                ' Set the ContentType property of the WebRequest.
                PayPalRequest.ContentType = "application/x-www-form-urlencoded"
                ' Set the ContentLength property of the WebRequest.
                PayPalRequest.ContentLength = byteArray.Length
                ' Get the request stream.
                dataStream = PayPalRequest.GetRequestStream()  ' <===== Fails on this call
                ' Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length)
                ' Close the Stream object.
                dataStream.Close()
                ' Get the response.
                Dim PayPalResponse As WebResponse = PayPalRequest.GetResponse()

                ' Get the stream containing content returned by the server.
                dataStream = PayPalResponse.GetResponseStream()
                ' Open the stream using a StreamReader for easy access.
                Dim reader As New StreamReader(dataStream)
                ' Read the content.
                Dim responseFromServer As String = reader.ReadToEnd()

                ' Clean up the streams.
                reader.Close()
                dataStream.Close()
                PayPalResponse.Close()


            .......
Corinne
  • 11
  • 1
  • 1
    Could this have anything to do with the changes PayPal is implementing on the Secure Certificates in Sandbox. This started happening after 1/18/2016. I found this article about the PayPal Certificate update https://www.paypal-knowledge.com/infocenter/index?page=content&id=FAQ1766 – Corinne Feb 03 '16 at 13:30

2 Answers2

0

My problem was a result of the update PayPal is making to their security.
See https://www.paypal-knowledge.com/infocenter/index?page=content&id=FAQ1766

In my case, I had to upgrade to .NEt Framework 4.5 and include the following:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

before the call dataStream = PayPalRequest.GetRequestStream()

Corinne
  • 11
  • 1
-1

Try this instead:

PayPalRequest.ContentType = "text/namevalue"

You're passing name/value pairs. A PayPalesque explanation follows.

https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1348&viewlocale=en_US&direct=en

SEFL
  • 539
  • 4
  • 15
  • Huh? What kind of [`Content-Type`](http://www.iana.org/assignments/media-types/media-types.xhtml)? – EdSF Jan 31 '16 at 01:36
  • From what I've seen, it's specific to PayPal. It works with and without the space (i.e. "text/name value"). – SEFL Jan 31 '16 at 02:12
  • @SEFL using PayPalRequest.ContentType = "text/namevalue" throws the same error. – Corinne Feb 02 '16 at 02:00
  • @SEFL Since there was no change to the code from the last 2 months, could it be something on my system that changed and is preventing the datastream from sending? I'm getting the error from my local dev environment as well as when the code is posted on the hosted web server. Does PayPal ever put a 60/90 day time limit on your sandbox account? – Corinne Feb 02 '16 at 02:08