22 April 2006

Authorize.Net integration using VB.NET

Posted by Mikhail Esteves under: C#; Tips .

JBob sent in the Authorize.Net integration code written in VB.NET. Here it is.

Public Class Authorization

'Public events
Event TransactionSuccess(ByVal sAuthCode As String, ByVal TransactionID As String)
Event TransactionFailed(ByVal sErrorDesc As String)

Public Sub AuthorizePayment(ByVal OrderDescription As String,
  ByVal FirstName As String, ByVal LastName As String,
  ByVal Address As String, ByVal City As String,
  ByVal State As String, ByVal Zip As String,
  ByVal Country As String, ByVal Amount As Double)

Dim AuthNetVersion As String = "3.1"
Dim AuthNetLoginID = ""
Dim AuthNetPassword = ""
Dim AuthNetTransKey = ""

Dim objRequest As New System.Net.WebClient
Dim objInf As New System.Collections.Specialized.NameValueCollection(30)
Dim objRetInf As New System.Collections.Specialized.NameValueCollection(30)
Dim objRetBytes As Byte()
Dim objRetVals As String()
Dim strError As String = ""

objInf.Add("x_version", AuthNetVersion)
objInf.Add("x_delim_data", "True")
objInf.Add("x_login", AuthNetLoginID)
objInf.Add("x_password", AuthNetPassword)
objInf.Add("x_tran_key", AuthNetTransKey)
objInf.Add("x_relay_response", "False")

' Switch this to False once you go live
objInf.Add("x_test_request", "True")

objInf.Add("x_delim_char", ",")
objInf.Add("x_encap_char", "|")

' Billing Address
objInf.Add("x_first_name", FirstName)
objInf.Add("x_last_name", LastName)
objInf.Add("x_address", Address)
objInf.Add("x_city", City)
objInf.Add("x_state", State)
objInf.Add("x_zip", Zip)
objInf.Add("x_country", Country)

objInf.Add("x_description", OrderDescription)

' Card Details
objInf.Add("x_card_num", "4111111111111111")
objInf.Add("x_exp_date", "01/06")

' Authorisation code of the card (CCV)
objInf.Add("x_card_code", "123")

objInf.Add("x_method", "CC")
objInf.Add("x_type", "AUTH_CAPTURE")
objInf.Add("x_amount", Amount.ToString())

' Currency setting. Check the guide for other supported currencies
objInf.Add("x_currency_code", "USD")

Try
'Test/Live server
objRequest.BaseAddress = "https://certification.authorize.net/gateway/transact.dll"
'objRequest.BaseAddress = "https://secure.authorize.net/gateway/transact.dll"

objRetBytes = objRequest.UploadValues(objRequest.BaseAddress, "POST", objInf)
objRetVals = System.Text.Encoding.ASCII.GetString(objRetBytes).Split(",".ToCharArray())

If objRetVals(0).Trim(Char.Parse("|")) = "1" Then
  'Return the authorization code
  RaiseEvent TransactionSuccess(objRetVals(4).Trim(Char.Parse("|")), objRetVals(6).Trim(Char.Parse("|")))
Else
  strError = objRetVals(3).Trim(Char.Parse("|")) & " (" & objRetVals(2).Trim(Char.Parse("|")) + ")"

If (objRetVals(2).Trim(Char.Parse("|")) = "44") Then
  'CCV transaction declined
  Select Case objRetVals(38).Trim(Char.Parse("|"))
    Case "N"
      strError += "Card Code does not match."
    Case "P"
      strError += "Card Code was not processed."
    Case "S"
      strError += "Card Code should be on card but was not indicated."
    Case "U"
      strError += "Issuer was not certified for Card Code."
  End Select

If (objRetVals(2).Trim(Char.Parse("|")) = "45") Then
  Select Case objRetVals(5).Trim(Char.Parse("|"))
    Case "A"
      strError += " the zip code entered does not match " & "the billing address."
    Case "B"
      strError += " no information was provided for the AVS check."
    Case "E"
      strError += " a general error occurred in the AVS system."
    Case "G"
      strError += " the credit card was issued by a non-US bank."
    Case "N"
      strError += " neither the entered street address nor zip " & "code matches the billing address."
    Case "P"
      strError += " AVS is not applicable for this transaction."
    Case "R"
      strError += " please retry the transaction; the AVS system " & "was unavailable or timed out."
    Case "S"
      strError += " the AVS service is not supported by your " & "credit card issuer."
    Case "U"
      strError += " address information is unavailable for the " & "credit card."
    Case "W"
      strError += " the 9 digit zip code matches, but the " & "street address does not."
    Case "Z"
      strError += " the zip code matches, but the address does not."
  End Select
  End If

End If

RaiseEvent TransactionFailed(strError)
End If
Catch ex As Exception

End Try

End Sub
End Class

Note: I haven’t tested this code. I don’t see any reason why it shouldn’t work though.

Technorati Tags , , , , ,


One Comment so far...

Sanat Gersappa Says:

27 April 2006 at 8:46 am.

http://developer.authorize.net/samplecode/ gives sample code in various languages.

Leave a Reply

Browse

Photography

Projects

Pages

Calendar

April 2006
M T W T F S S
« Mar   May »
 12
3456789
10111213141516
17181920212223
24252627282930

Categories

www.flickr.com