Authorize.Net Payment Integration In C#

Introduction

In this article, we will learn how to implement  Authorize.Net payment gateway in ASP.NET Web application.

Let’s begin.

Go to https://developer.authorize.net/

Click on  “CREATE A SANDBOX ACCOUNT”  then you will display the below page.

Click on “CREATE SANDBOX ACCOUNT” and make it to registration.

After Successfully Registration the Below Page appears and  You will get the API LOGIN ID and TRANSACTION KEY.

Now, Install “AuthorizeNet” SDK in your application

Go to Tools > NuGet Package Manager > Manage NuGet Packages For Solutions.

Click on Browse Tab and search for “AuthorizeNet”. Click on search NuGet and click to Install button.

After installation of SDK, Set LOGIN ID, and TRANSACTION KEY  in web.config.

C# Code Example

Open the HomeController.cs file and add the code in it.

string LoginId = ConfigurationManager.AppSettings["LOGINID"];
string TRANSACTIONKEY = ConfigurationManager.AppSettings["TRANSACTIONKEY"];
public ActionResult Index()
{
   Authentications.Run(LoginId, TRANSACTIONKEY);
    return View();
}

Create the Helpers folder and make a class file name Authentications.cs and add the code in it.

public static void Run(String ApiLoginID, String ApiTransactionKey)
{
    ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;
    ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType()
    {
        name = ApiLoginID,
        ItemElementName = ItemChoiceType.transactionKey,
        Item = ApiTransactionKey,
    };

    var creditCard = new creditCardType
    {
        cardNumber = "4012888818888",
        //cardNumber = "4111111111111111",
        expirationDate = "0720"
    };
    var paymentType = new paymentType { Item = creditCard };
    var transactionRequest = new transactionRequestType
    {
        transactionType = transactionTypeEnum.authCaptureTransaction.ToString(),   // charge the card
        amount = 133.45m,
        payment = paymentType
    };

    var request = new createTransactionRequest { transactionRequest = transactionRequest };

    // instantiate the contoller that will call the service
    var controller = new createTransactionController(request);
    controller.Execute();

    // get the response from the service (errors contained if any)
    var response = controller.GetApiResponse();

    if (response.messages.resultCode == messageTypeEnum.Ok)
    {
        if (response.transactionResponse != null)
        {
            Console.WriteLine("Success, Auth Code : " + response.transactionResponse.authCode);
        }
    }
    else
    {
        Console.WriteLine("Error: " + response.messages.message[0].code + "  " + response.messages.message[0].text);
        if (response.transactionResponse != null)
        {
            Console.WriteLine("Transaction Error : " + response.transactionResponse.errors[0].errorCode + " " + response.transactionResponse.errors[0].errorText);
        }
    }

}

That’s it, you are ready to use the Authorize.Net Payment Integration.

1 Comment

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories