0

How to integrate paygate payment gateway's PayGate PayXML with asp.net using with c# or with vb. When i run the sample application given by the paygate developer login i got an error "Authentication failed because the remote party has closed the transport stream." Hope any expert from this forum will help me. Thanks in advance.

Tom B.
  • 2,892
  • 3
  • 13
  • 34
  • Have a look at this question. http://stackoverflow.com/questions/30664566/authentication-failed-because-remote-party-has-closed-the-transport-stream –  Jan 25 '16 at 06:52
  • Hi did you manage to get PayGate working in C# ? – Donald Jansen Oct 17 '16 at 14:56

2 Answers2

1

Here is my code if anyone is ever stuck with Paygate it work you need to import restsharp from the nuget

Imports RestSharp
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Net
Imports System.Collections.Specialized
Imports System.Text
Imports WebApplication1.ServiceReference1
Imports System.Security.Cryptography.HashAlgorithm


Public Class _Default
    'Inherits Page
    Inherits System.Web.UI.Page
    Private encryptionKey As String = "secret"   ' Addd secret in from comes from Paygate
    Public PAYGATE_ID As String = "10011072130"
    Public RETURN_URL As String = "http://www.webbis.co.za"
    Dim CheckSum As String
    Dim PaymentRequestId As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'amount in cents
        Paygate("James@webbis.co.za", "12399", "TEST001")

        Response.Write(PaygateForm().ToString)
        Me.PaygatePostScript(Page)


    End Sub

    Private Sub PaygatePostScript(ByVal Page As System.Web.UI.Page)
        'This registers Javascript to the page which is used to post the Netcash Form details
        Dim strScript As New StringBuilder()
        strScript.Append("<script language='javascript'>")
        strScript.Append("var ctlForm = document.getElementById('Paygate');")
        strScript.Append("ctlForm.submit();")
        strScript.Append("</script>")
        ClientScript.RegisterClientScriptBlock(Me.GetType, "PPSubmit", strScript.ToString)

    End Sub


    Private Function PaygateForm()
        Dim client = New RestClient("https://secure.paygate.co.za/payweb3/process.trans")
        client.Timeout = -1
        Dim request = New RestRequest(Method.POST)
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
        request.AddHeader("Cookie", "paygate_payweb3=u9tjr031eluc6jsflq952uatup")
        request.AddParameter("PAY_REQUEST_ID", PaymentRequestId)
        request.AddParameter("CHECKSUM", CheckSum)
        Dim response As IRestResponse = client.Execute(request)


        Dim ppForm As New StringBuilder
        '<form action="https://secure.paygate.co.za/payweb3/process.trans" method="POST" >
        ppForm.Append("<Form method=" + """" + "post" + """" + """" + " id=" + """" + "Paygate" + """" + " action=" + """" + "https://secure.paygate.co.za/payweb3/process.trans" + """" + ">")
        ppForm.Append(response.Content.ToString)

        Return ppForm.ToString


    End Function


    Public Function generateChecksum(ByVal postData As Dictionary(Of String, String)) As String
        Dim checksum As String = ""

        For Each item In postData

            If item.Value <> "" Then
                checksum += item.Value
            End If
        Next

        checksum += Me.getEncryptionKey()


        Dim hash = System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(checksum))
        Dim sb As StringBuilder = New StringBuilder()

        For i As Integer = 0 To hash.Length - 1
            sb.Append(hash(i).ToString("x2"))
        Next

        Return sb.ToString()
    End Function

    Public Function getEncryptionKey() As String
        Return Me.encryptionKey
    End Function

    Private Sub Paygate(ByVal EMAIL As String, ByVal AMOUNT As String, ByVal REFERENCE As String)
        Dim client = New RestClient("https://secure.paygate.co.za/payweb3/initiate.trans")
        Dim postData As New Dictionary(Of String, String)
        client.Timeout = -1
        Dim request = New RestRequest(Method.POST)
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
        request.AddHeader("Cookie", "paygate_payweb3=u9tjr031eluc6jsflq952uatup")



        request.AddParameter("PAYGATE_ID", PAYGATE_ID)
        postData.Add("PAYGATE_ID", PAYGATE_ID)

        request.AddParameter("REFERENCE", REFERENCE)
        postData.Add("REFERENCE", REFERENCE)

        request.AddParameter("AMOUNT", AMOUNT)
        postData.Add("AMOUNT", AMOUNT)

        request.AddParameter("CURRENCY", "ZAR")
        postData.Add("CURRENCY", "ZAR")

        request.AddParameter("RETURN_URL", "https://my.return.url/page")
        postData.Add("RETURN_URL", "https://my.return.url/page")

        Dim adate As Date = DateTime.Now
        Dim sDate As String = adate.Year.ToString("D4") & "-" & adate.Month.ToString("D2") & "-" & adate.Day.ToString("D2") & " " & adate.Hour.ToString("D2") & ":" & adate.Minute.ToString("D2") & ":" & adate.Second.ToString("D2")

        request.AddParameter("TRANSACTION_DATE", sDate)
        postData.Add("TRANSACTION_DATE", sDate)

        request.AddParameter("LOCALE", "en-za")
        postData.Add("LOCALE", "en-za")

        request.AddParameter("COUNTRY", "ZAF")
        postData.Add("COUNTRY", "ZAF")

        request.AddParameter("EMAIL", EMAIL)
        postData.Add("EMAIL", EMAIL)
        'System.Security.Cryptography.HashAlgorithm
        Dim checksum1 As String = generateChecksum(postData)



        request.AddParameter("CHECKSUM", checksum1)

        'request.AddParameter("CHECKSUM", "59229d9c6cb336ae4bd287c87e6f0220")




        Dim response As IRestResponse = client.Execute(request)
        CheckSum = response.Content.ToString

        Dim array1 As String() = CheckSum.Split("&")
        CheckSum = array1(3)
        CheckSum = Replace(CheckSum, "CHECKSUM=", "")
        PaymentRequestId = array1(1)
        PaymentRequestId = Replace(PaymentRequestId, "PAY_REQUEST_ID=", "")
        ' GetCheckSumPayGate()

    End Sub







End Class
0

I received this error trying to integrate into their PayWeb module using .Net Framework 4.0. I created simple WebRequest POST to https://secure.paygate.co.za/payweb3/initiate.trans and would receive the "Authentication failed because the remote party has closed the transport stream." error.

All the stackoverflow posts about this error pointed to a TLS1 problem with .Net 4.0 however activating TLS1.1 and TLS1.2 on the hosting server didn't fix the issue. Even changing exceptions on the firewall did not resolve the error. PayGate had no idea how to fix this error, even after 60 emails back and forth.

In the end I upgraded to .Net 4.5.2 on the server which fixed the problem.

Jonty
  • 662
  • 7
  • 8