Help needed please to migrate from VB2008 HttpWebRequest functions to Gambas

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Help needed please to migrate from VB2008 HttpWebRequest functions to Gambas

Post by AndyGable »

Hi everyone,

I would like some help with the below code
        Try
            Dim Request As HttpWebRequest = HttpWebRequest.Create(PS_URL & "/terminals/" & TerminalIDNumber)
            Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(PS_USER & ":" & PS_PASS))
            txtResults.Text = vbNullString

            With Request
                .Proxy = Nothing
                .Headers(HttpRequestHeader.Authorization) = String.Format("Basic {0}", credentials)
                .Accept = "application/connect.v2+json"
                .Headers.Add("Software-House-Id: SD459T92") ' algPoS ID Number
                .Headers.Add("Installer-Id:ABC12319785")
                .UserAgent = PS_USER
            End With

            Dim response As HttpWebResponse = Request.GetResponse()
            Dim dataStream As Stream = response.GetResponseStream
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()

            txtResults.Text = responseFromServer


            If responseFromServer = "0" Then
                MsgBox("Retreal of Status Failed")
            Else
                Dim json As String = responseFromServer
                Dim ser As JObject = JObject.Parse(json)
                Dim data As List(Of JToken) = ser.Children().ToList
                For Each item As JProperty In data
                    item.CreateReader()
                    Select Case item.Name
                        Case "status"
                            Select Case item.Value
                                Case "AVAILABLE"
                                    GetStatus = 1
                                    If DontSendDataToPoS = 0 Then SendToPoSterminal("TerminalOnLine|")
                                    Me.ListBox1.Items.Clear
                                    addtoStatusList("Terminal Ready")

                                Case "BUSY"
                                    If DontSendDataToPoS = 0 Then SendToPoSterminal("TerminalBusy|")
                                    addtoStatusList("Terminal busy please wait 10 seconds and try again")

                                Case "Offline", "OFFLINE", "offline"
                                    If DontSendDataToPoS = 0 Then SendToPoSterminal("offline|")
                                    addtoStatusList("Terminal OFFLINE NO Card processing Possible - Please check with Payment Sense")
                            End Select
                    End Select
                Next
            End If
        Catch ex As Exception
            addtoStatusList(ex.ToString)
            If DebugActive = "Yes" Then AddToDebugList(ex.ToString)
            If DontSendDataToPoS = 0 Then SendToPoSterminal("ProcessingError")
            FromPoSTCP.Stop()
            FromPoSTCP.Start()
        End Try
As you can see this is code I am using in my VB application that talks to one of the card processing servers that I talk to for card processing (this code gets the status of the Terminal as this is the simplest example of what the terminal does I figured this would be the best one to start with)

I have tried for weeks to get something to work and I have given up.

could some one please convert this so it would work in Gambas (once i can see what was converted I would have a better understanding of how to do the rest of the code I need to do) I know I am asking a lot of someone to covert the above code but I just can not work it out.
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

Re: Help needed please to migrate from VB2008 HttpWebRequest functions to Gambas

Post by thatbruce »

Andy,
Why not throw out the VB code, read the Gambas help files and write it the Gambas way?
Or at least convert the VB code into a pseudo-code summary and post that.

(I've said it before and no doubt I'll say it again: I couldn't be bothered less reading VB code, let alone trying to understand it. I am sorry if this attitude offends some people but it is the truth.)
Have you ever noticed that software is never advertised using the adjective "spreadable".
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help needed please to migrate from VB2008 HttpWebRequest functions to Gambas

Post by AndyGable »

I can not throw the vb code out as that is the code that has been approved by the card processing people.

I just need to see what I have to change for Gambas but so far no matter what I read up on and try nothing works.

Any advise most welcome at the moment.
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

Re: Help needed please to migrate from VB2008 HttpWebRequest functions to Gambas

Post by thatbruce »

Andy,
I just spent two hours writing a treatise on how this stuff works only to have phpBB time out and delete that entire work. :evil: :evil: :evil: :evil: So briefly

Advice, as requested.
1) find out and understand how all the upstream processing for a payment works and the subtle differences between "authorised", "declined" and "failed" response codes.
2) research the Gambas http components and how they work (IOW RTFM) then ask questions about them, don't ask "homework" questions.
3) find out about the payment processors transaction cancellation, refund and transaction amendment protocols
4) find out about how to do "post transaction queries" and the protocols there
5) figure out how to handle communications failures between your node and the payment processor node.
6) figure out how to get your till reports back in order after a 5).

Wishing you the best of luck, this stuff can take in the order of 6-18 man months to implement, test and get "approved" (hah! any payments processor that is still claiming that level of authority is either above themselves or spruiking an insecure or unstable interface. )
thatbruce

p.s. By way of qualification, my c.v. has better than 15 years of working with payments systems.
Have you ever noticed that software is never advertised using the adjective "spreadable".
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help needed please to migrate from VB2008 HttpWebRequest functions to Gambas

Post by AndyGable »

The card processor I use does all the work (talk to the card machine etc) the only connection between the POS and the card is a cloud API that sends the data

I have all ready been approved for the windows version (took 2 days to Implement)

I may just have to bite the bullet and have windows on a raspberry pi.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help needed please to migrate from VB2008 HttpWebRequest functions to Gambas

Post by BruceSteers »

I'd focus on getting the following 2 lines converted correctly.

Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(PS_USER & ":" & PS_PASS))


.Headers(HttpRequestHeader.Authorization) = String.Format("Basic {0}", credentials)
The rest of the code looks almost like Gambas and should not be that hard to convert.
the exact differences in the conversion of your credentials string is going to be the fall down i reckon.

Can you edit the VB app to display the exact credential string it produces and sends and then get the gambas to do the same to see if they match.

Print Quote(credentials) in gambas will give you a quoted string showing all the escape chars too.
If at first you don't succeed , try doing something differently.
BruceS
Post Reply