Solved

ASP.NET Authorize.NET Gateway - Credit Card Payments

Posted on 2004-09-28
9
731 Views
Last Modified: 2013-11-29
I am having a hard time find code examples, tutorials, or any resources for using ASP.NET to submit credit card payments to the Authorize.NET payment gateway.

I want to have a custom control that I can edit to include more descriptive errors for declines, etc.

Authorize.NET has no documentation for it and they only have code samples for standard ASP.

Does anyone have some code they can share or links to good tutorials and/or resources?

Thanks
Mike Miller
0
Comment
Question by:MikeMiller
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
Comment Utility
Complete, 100% functional with error reporting:

Public Class AuthorizeNet

#Region " Definitions "
    Private _Login As String =
    Private _Password As String =
    Private _TranKey As String

    Private _TestRequest As Boolean

    Private _BillingAddress As New Address
    Private _ShippingAddress As New Address
    Private _AdditionalInfo As PaymentCustomerInformation

    Private _CreditCard As CreditCard
    Private _ElectronicFundsTransfer As BankTransfer
    Private _TransactionType As TransactionFlow

    Private _Amount As Single
    Private _Tax As Single
    Private _Freight As Single

    Private _AuthCode As String
    Private _TransID As String
#End Region

#Region " Types "
    Public Enum TransactionFlow
        Capture
        Credit
    End Enum
#End Region

#Region " Properties "
    Public Property Login() As String
        Get
            Return _Login
        End Get
        Set(ByVal Value As String)
            _Login = Login
        End Set
    End Property
    Public Property Password() As String
        Get
            Return _Password
        End Get
        Set(ByVal Value As String)
            _Password = Password
        End Set
    End Property
    Public Property TranKey() As String
        Get
            Return _TranKey
        End Get
        Set(ByVal Value As String)
            _TranKey = Value
        End Set
    End Property
    Public Property TestRequest() As Boolean
        Get
            Return _TestRequest
        End Get
        Set(ByVal Value As Boolean)
            _TestRequest = True
        End Set
    End Property
    Public ReadOnly Property CreditCard() As CreditCard
        Get
            Return _CreditCard
        End Get
    End Property
    Public ReadOnly Property ElectronicFundsTransfer() As BankTransfer
        Get
            Return _ElectronicFundsTransfer
        End Get
    End Property
    Public ReadOnly Property AuthCode() As String
        Get
            Return _AuthCode
        End Get
    End Property
    Public ReadOnly Property TransID() As String
        Get
            Return _TransID
        End Get
    End Property
    Public ReadOnly Property BillingAddress() As Address
        Get
            Return _BillingAddress
        End Get
    End Property
    Public Property ShippingAddress() As Address
        Get
            Return _ShippingAddress
        End Get
        Set(ByVal Value As Address)
            _ShippingAddress = Value
        End Set
    End Property
    Public Property TransactionType() As TransactionFlow
        Get
            Return _TransactionType
        End Get
        Set(ByVal Value As TransactionFlow)
            _TransactionType = Value
        End Set
    End Property
    Public Property AdditionalInfo() As PaymentCustomerInformation
        Get
            Return _AdditionalInfo
        End Get
        Set(ByVal Value As PaymentCustomerInformation)
            _AdditionalInfo = Value
        End Set
    End Property

    Public Property Amount() As Single
        Get
            Return _Amount
        End Get
        Set(ByVal Value As Single)
            _Amount = Value
        End Set
    End Property
    Public Property Tax() As Single
        Get
            Return _Tax
        End Get
        Set(ByVal Value As Single)
            _Tax = Value
        End Set
    End Property
    Public Property Freight() As Single
        Get
            Return _Freight
        End Get
        Set(ByVal Value As Single)
            _Freight = Value
        End Set
    End Property
#End Region

#Region " Constructors "
    Public Sub New(ByVal CreditCard As CreditCard, ByVal BillingAddress As Address, _
        ByVal Amount As Single, ByVal TransactionType As TransactionFlow, _
        Optional ByVal AdditionalInfo As PaymentCustomerInformation = Nothing)
        _CreditCard = CreditCard
        _BillingAddress = BillingAddress
        _Amount = Amount
        _TransactionType = TransactionType
        If Not AdditionalInfo Is Nothing Then
            _AdditionalInfo = New PaymentCustomerInformation
            _AdditionalInfo = AdditionalInfo
        End If

        _ElectronicFundsTransfer = Nothing
    End Sub

    Public Sub New(ByVal ElectronicFundsTransfer As BankTransfer, _
        ByVal BillingAddress As Address, ByVal Amount As Single, _
        ByVal TransactionType As TransactionFlow, _
        Optional ByVal AdditionalInfo As PaymentCustomerInformation = Nothing)
        _ElectronicFundsTransfer = ElectronicFundsTransfer
        _BillingAddress = BillingAddress
        _Amount = Amount
        _TransactionType = TransactionType
        If Not AdditionalInfo Is Nothing Then
            _AdditionalInfo = New PaymentCustomerInformation
            _AdditionalInfo = AdditionalInfo
        End If

        _CreditCard = Nothing
    End Sub
#End Region

#Region " Methods "
    Public Sub PostTransaction()

        Dim AuthorizeRequest As New Net.WebClient
        Dim Information As New Collections.Specialized.NameValueCollection(30)
        Dim ReturnInformation As New Collections.Specialized.NameValueCollection(30)
        Dim ReturnBytes As Byte()
        Dim ReturnValues As String()
        Dim ReturnValue, ReturnError As String
        If Login = "" Then Throw New AuthorizeNetException("You must set the login value of the AuthorizeNet class.")

        With Information

            .Add("x_Version", "3.1")
            .Add("x_Delim_Data", "True")
            .Add("x_Login", _Login)
            .Add("x_Password", _Password)
            '.Add("x_Tran_Key", _TranKey)
            .Add("x_Test_Request", "True") '_TestRequest.ToString()

            'A comma is used to seperate the fields while a pipe is used to encapsulate the data.
            '   Authorize.NET doesn't have an escape character for data, so I had to use the pipe to
            '   encapsulate it -- regular expressions up to this point will prevent the pipe symbol
            '   from occuring within the actual data.
            .Add("x_Delim_Char", ",")
            .Add("x_Encap_Char", "|")

            .Add("x_First_Name", BillingAddress.FirstName)
            .Add("x_Last_Name", BillingAddress.LastName)
            .Add("x_Address", BillingAddress.Address)
            .Add("x_City", BillingAddress.City)
            .Add("x_State", BillingAddress.State)
            .Add("x_Zip", BillingAddress.Zip)

            If Not ShippingAddress Is Nothing Then
                .Add("x_Ship_To_First_Name", ShippingAddress.FirstName)
                .Add("x_Ship_To_Last_Name", ShippingAddress.LastName)
                .Add("x_Ship_To_Address", ShippingAddress.Zip)
                .Add("x_Ship_To_City", ShippingAddress.City)
                .Add("x_Ship_To_State", ShippingAddress.State)
                .Add("x_Ship_To_Zip", ShippingAddress.Zip)
            End If

            If Not AdditionalInfo Is Nothing Then
                If AdditionalInfo.Phone <> "" Then .Add("x_Phone", AdditionalInfo.Phone)
                If AdditionalInfo.CustomerID <> 0 Then _
                    .Add("x_Cust_ID", AdditionalInfo.CustomerID.ToString())
                If AdditionalInfo.CustomerIP.ToString() <> "0.0.0.0" Then
                    .Add("x_Customer_IP", AdditionalInfo.CustomerIP.ToString())
                End If
                If AdditionalInfo.EmailAddress <> "" Then .Add("x_Email", AdditionalInfo.EmailAddress)

                If AdditionalInfo.InvoiceNumber.ToString() <> "" Then _
                    .Add("x_Invoice_Num", AdditionalInfo.InvoiceNumber.ToString())
                If AdditionalInfo.Description <> "" Then .Add("x_Description", AdditionalInfo.Description)
            End If

            If Not _CreditCard Is Nothing Then
                'It's a credit card transaction
                .Add("x_Card_Num", _CreditCard.CardNumber)
                .Add("x_Exp_Date", _CreditCard.ExpDate.ToString("MM/yy"))
                .Add("x_Method", "CC")
            ElseIf Not _ElectronicFundsTransfer Is Nothing Then
                'It's an e-check transaction
                .Add("x_Bank_ABA_Code", _ElectronicFundsTransfer.RoutingNumber)
                .Add("x_Bank_Acct_Num", _ElectronicFundsTransfer.AccountNumber)
                Select Case _ElectronicFundsTransfer.AccountType
                    Case BankTransfer.BankAccountType.Savings
                        .Add("x_Bank_Acct_Type", "Savings")
                    Case BankTransfer.BankAccountType.Checking
                        .Add("x_Bank_Acct_Type", "Checking")
                End Select
                .Add("x_Bank_Name", _ElectronicFundsTransfer.BankName)
                .Add("x_Bank_Acct_Name", _ElectronicFundsTransfer.AccountName)
                .Add("x_Method", "ECHECK")
            End If

            Select Case TransactionType
                Case TransactionFlow.Capture
                    'It's a payment to our account
                    .Add("x_Type", "AUTH_CAPTURE")
                Case TransactionFlow.Credit
                    'It's a credit to their account
                    .Add("x_Type", "CREDIT")
            End Select
            .Add("x_Amount", Amount.ToString())

            'Tax and Freight are NOT added onto the total; these values are the tax value (in dollars)
            '   of the transaction and the freight cost of the sale. Again, the total already includes
            '   the sum of the tax and freight.
            If Tax <> 0 Then .Add("x_Tax", Tax.ToString())
            If Freight <> 0 Then .Add("x_Freight", Freight.ToString())

        End With

        AuthorizeRequest.BaseAddress = "https://certification.authorize.net/gateway/transact.dll"
        ReturnBytes = AuthorizeRequest.UploadValues(AuthorizeRequest.BaseAddress, "POST", Information)
        ReturnValues = System.Text.Encoding.ASCII.GetString(ReturnBytes).Split(",".ToCharArray())
               
        If ReturnValues(0).Trim(CChar("|")) = "1" Then
            _AuthCode = ReturnValues(4).Trim(CChar("|"))
            _TransID = ReturnValues(6).Trim(CChar("|"))
        Else
            ReturnError = ReturnValues(3).Trim(CChar("|"))
            If ReturnValues(2).Trim(CChar("|")) = "45" Then
                ReturnError &= " Our Address Verification System (AVS) returned the following error: "
                Select Case ReturnValues(5).Trim(CChar("|"))
                    Case "A"
                        ReturnError &= " the zip code entered does not match the billing address."
                    Case "B"
                        ReturnError &= " no information was provided for the AVS check."
                    Case "E"
                        ReturnError &= " a general error occurred in the AVS system."
                    Case "G"
                        ReturnError &= " the credit card was issued by a non-US bank."
                    Case "N"
                        ReturnError &= " neither the entered street address nor zip code matches the billing address."
                    Case "P"
                        ReturnError &= " AVS is not applicable for this transaction."
                    Case "R"
                        ReturnError &= " please retry the transaction; the AVS system was unavailable or timed out."
                    Case "S"
                        ReturnError &= " the AVS service is not supported by your credit card issuer."
                    Case "U"
                        ReturnError &= " address information is unavailable for the credit card."
                    Case "W"
                        ReturnError &= " the 9 digit zip code matches, but the street address does not."
                    Case "Z"
                        ReturnError &= " the zip code matches, but the address does not."
                End Select
            End If
            Throw New AuthorizeNetException(ReturnError)
        End If

    End Sub
#End Region

End Class

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Note I have some additional classes such as additional information that you will need to create on your own.
0
 
LVL 3

Author Comment

by:MikeMiller
Comment Utility
Awesome. Thank you!
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Your welcome my friend.

Regards,

Aeros
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:luckyinc
Comment Utility
Aeros

How did you write the classes for example: Address?

LuckyInc
0
 

Expert Comment

by:luckyinc
Comment Utility
0
 
LVL 3

Author Comment

by:MikeMiller
Comment Utility
I want to know too.

Paste the code! hehe

I ended up using a lot of that code, but just put it directly into the page. I am starting to get a firm grasp of .NET and classes, so i want to write it well and have stuff like auhtorize.net shared as a common class.

I would like to see the syntax of how it's called as well.
0
 

Expert Comment

by:luckyinc
Comment Utility
0
 

Expert Comment

by:luckyinc
Comment Utility
Mike,

How did you call your data from your webform to the class?

THanks
LuckyInc
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now