Integrate Xojo Apps with Authorize.Net

I had to put together an app for a merchant client that could process transactions through Authorize.net and store customer information on their servers to remain PCI compliant. It uses their JSON API for integration. I figured I would make the code public in case any one else needed this kind of functionality in the future. Feel free to use it/contribute to make it better/talk about how you would have done it differently/etc.

The code is here here

We’ve been testing this code and we are getting an error in aNetController.parseResponse.

[code]Private Function parseResponse(content as xojo.Core.MemoryBlock) As Dictionary
#Pragma BreakOnExceptions false
try
dim json as text = TextEncoding.UTF8.ConvertDataToText(content)
return ParseJSON(json)

catch e as JSONException
dim err as new xojo.Net.NetException()
err.ErrorNumber = 128794
err.Reason = “Unable to parse ANet response. Please manually check the status of your last request using their online portal”

raise err

end try

#Pragma BreakOnExceptions true
End Function[/code]

The error is occuring at ParseJSON(json). It seems the “content” memory block has 3 bytes before the first open bracket “{” which is causing Xojo to think the json is invalid. We have been able to fix the code by creating a new memory block and omitting the first three bytes.

So, we are wondering, is this a bug in the xojo.net.httpsocket where it is adding the three bytes to the beginning of the content? Or maybe @erin jamroz is using a Mac and this is a Windows only bug as he seems to have tested this code and it worked for him. Any ideas?

I am trying to implement Erin’s Authorize.Net routines. The demo works app great and my code works if pushing a button that only sends one message to the ANet server. When I try to put two or more together, create a customer profile then a payment profile, the first request executes properly but the second one fails in the send method of the ANetController class with the message “A request is already in progress.”

[code]Protected Sub send(request as JSONItem)
dim postData as MemoryBlock = convertMessageToPostableForm(request)
aNetSocket.ClearRequestHeaders
aNetSocket.SetRequestContent(postData, “application/x-www-form-urlencoded”)

try
self.aNetSocket.send(“POST”, gateway)
catch err as UnsupportedOperationException
raise err
end try
End Sub
[/code]
I added the aNetSocket.Clear Request Headers command with the hope that it would fix the problem. It does not appear to be a timing issue as it happens when I step through the routine with the debugger. aNetSocket is a xojo.net.httpsocket.

You need to use a Timer to break out of the HTTPSocket routines…

How do I break out of the HTTPSocket routines? It seems to happen automatically when aNetSocket is called from a different push button, but not when it is called a second time in a push button method.

http://developer.xojo.com/xojo-net-httpsocket$Disconnect

In your socket class, in the PageReceived event, I would recommend to call a method, instead of parsing the reply…

PageReceived(URL As Text, HTTPStatus As Integer, content As MemoryBlock) Xojo.Core.Timer 0, WeakAddressOf PageReceivedHandler, content

Then create a private method that handles all the parsing of the data

PageReceivedHandler content As MemoryBlock // do your stuff here

By using the Timer, you allow the PageReceived event (and all the calling methods before it) to return, which allows you to do multiple calls to the socket from the same button, or even from within the socket itself… Here’s where I found the solution https://forum.xojo.com/28387-reusing-xojo-net-httpsocket/p1#p233621 Thanks to Wayne for posting it and for Joe’s follow-up

Thanks for posting the solution Shao. It turned out to be much simpler than I was trying to make it. In the GitHub example, I simply needed to add Xojo.Core.Timer.CallLater(0, WeakAddressOf ANetPayProfileCreate) to the ANetProfileManager1.ResponseRecieved event after I converted the code in AddPaymentInfoButton to a new method named ANetPayProfileCreate. This lets me create the customer profile and the payment profile with one button click.

@Brandon Warlick, I have only tested this on OSX. Others who use Windows have had similar issues that you are experiencing. Do you have a sample of the code that you have used to fix it on windows that you wold be willing to share?

As far as the socket goes, yes, one needs to use a timer because the socket does not block UI while it waits for/processes responses.

This is what I was doing to correct the issue:

'remove the first three bytes of the memory block Dim mbContent as New xojo.Core.MemoryBlock(content.size - 3) mbContent = content.right(Content.size -3)

What are the 3 bytes?
Not perhaps the header that signifies unicode encoding?

I really don’t know.