In this article, we will learn how to implement a Authorize.Net gateway in ASP.NET MVC.
Now Go https://developer.authorize.net/
Now Go CREATE A SANDBOX ACCOUNT
Now Create Account
create ASP.NET MVC Project
Now, Install AuthorizeNet SDK in your application
Create Payment Model
namespace AuthorizedPayment.Models { public class PaymentModel { public string FirstName { get; set; } public string LastName { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string Postcode { get; set; } public string Phone { get; set; } public decimal Amount { get; set; } public string CardNumber { get; set; } public string CardCode { get; set; } public string Month { get; set; } public string Year { get; set; } } public class TransactionResponse { public AuthorizeNet.Api.Contracts.V1.messageTypeEnum resultCode { get; set; } public string transId { get; set; } public string responseCode { get; set; } public string messageCode { get; set; } public string authCode { get; set; } public string description { get; set; } public string errorCode { get; set; } public string errorText { get; set; } } }
Now create class file and insert below code in file
using System; using AuthorizeNet.Api.Contracts.V1; using AuthorizeNet.Api.Controllers; using AuthorizeNet.Api.Controllers.Bases; namespace AuthorizedPayment.Models { public class PaymentProcesses { const string apiLoginId = "Your LoginId"; const string transactionKey = "Your transactionKey"; public TransactionResponse ChargeCredit(PaymentModel payment) { ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX; ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType() { name = apiLoginId, ItemElementName = ItemChoiceType.transactionKey, Item = transactionKey, }; var creditCard = new creditCardType { cardNumber = payment.CardNumber,//"Ex: 4111111111111111", expirationDate = payment.Month + payment.Year,//"Ex: 0522" cardCode = payment.CardCode,//"Ex: 111" }; var billingAddress = new customerAddressType { firstName = payment.FirstName, lastName = payment.LastName, city = payment.Address1, address = payment.Address2, zip = payment.Postcode, phoneNumber = payment.Phone }; var paymentType = new paymentType { Item = creditCard }; var lineItems = new lineItemType[2]; lineItems[0] = new lineItemType { itemId = "1", name = "t-shirt", quantity = 2, unitPrice = new Decimal(1.00) }; lineItems[1] = new lineItemType { itemId = "2", name = "snowboard", quantity = 1, unitPrice = new Decimal(1.00) }; var transactionRequest = new transactionRequestType { transactionType = transactionTypeEnum.authCaptureTransaction.ToString(), // charge the card amount = payment.Amount, payment = paymentType, billTo = billingAddress, lineItems = lineItems }; var request = new createTransactionRequest { transactionRequest = transactionRequest }; var controller = new createTransactionController(request); controller.Execute(); var resCode = controller.GetResultCode(); var resAll = controller.GetResults(); var response = controller.GetApiResponse(); TransactionResponse result = new TransactionResponse(); if (response != null) { result.resultCode = response.messages.resultCode; if (response.messages.resultCode == messageTypeEnum.Ok) { if (response.transactionResponse.messages != null) { result.transId = response.transactionResponse.transId; result.responseCode = response.transactionResponse.responseCode; result.messageCode = response.transactionResponse.messages[0].code; result.description = response.transactionResponse.messages[0].description; result.authCode = response.transactionResponse.authCode; } else { if (response.transactionResponse.errors != null) { result.errorCode = response.transactionResponse.errors[0].errorCode; result.errorText = response.transactionResponse.errors[0].errorText; } } } else { if (response.transactionResponse != null && response.transactionResponse.errors != null) { result.errorCode = response.transactionResponse.errors[0].errorCode; result.errorText = response.transactionResponse.errors[0].errorText; } else { result.errorCode = response.messages.message[0].code; result.errorText = response.messages.message[0].text; } } } else { result.errorCode = "NONE"; result.errorText = "Failed Transaction,, Unkown Error"; } return result; } } }
Now insert below code in Controller
using AuthorizedPayment.Models; using System.Web.Mvc; namespace AuthorizedPayment.Controllers { public class PaymentController : Controller { public ActionResult Index() { ViewBag.css = "hidden"; return View(); } [HttpPost] public ActionResult PaymentProcess(PaymentModel payment) { TransactionResponse result = new PaymentProcesses().ChargeCredit(payment); string respMsg = ""; if (result != null && result.resultCode == AuthorizeNet.Api.Contracts.V1.messageTypeEnum.Ok) { respMsg += "Successfully created transaction with Transaction ID: " + result.transId + "<br />"; respMsg += "Response Code: " + result.responseCode + "<br />"; respMsg += "Message Code: " + result.messageCode + "<br />"; respMsg += "Description: " + result.description + "<br />"; respMsg += "Success, Auth Code : " + result.authCode + "<br />"; ViewBag.css = "alert alert-success"; } else { respMsg = "Error Code: " + result.errorCode + "<br />" + "Error message: " + result.errorText; ViewBag.css = "alert alert-danger"; } ViewBag.Msg = respMsg; return View("index"); } } }
View Page
@model AuthorizedPayment.Models.PaymentModel @{ ViewBag.Title = "ASP.NET MVC - Payment Process"; } <div class="jumbotron"> <h1>Payment Gateway for ASP.NET MVC</h1> <p class="lead">Make easly Payment Processes using <a target="_blank" href="http://Authorize.Net">Authorize.Net</a> Payment Gateway integrated with ASP.NET MVC</p> </div> @using (Html.BeginForm("PaymentProcess", "Payment", FormMethod.Post)) { <div class="row"> <div class="col-sm-6"> <div class="panel panel-primary"> <div class="panel-heading"> Billing Address </div> <div class="panel-body"> <div class="form-horizontal"> <div class="form-group"> <label class="control-label col-sm-3">Name</label> <div class="col-sm-4"> <input type="text" class="form-control input-sm" name="FirstName" placeholder="First Name" required /> </div> <div class="col-sm-5"> <input type="text" class="form-control input-sm" name="LastName" placeholder="Last Name" required /> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">Address</label> <div class="col-sm-9"> <input type="text" class="form-control input-sm" name="Address1" required placeholder="City" /> </div> </div> <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <input type="text" class="form-control input-sm" name="Address2" placeholder="Address" /> </div> </div> <div class="form-group"> <label class="control-label col-sm-3">Postcode</label> <div class="col-sm-5"> <input type="number" class="form-control input-sm" name="Postcode" placeholder="ZIP Code" required /> </div> </div> <div class="form-group"> <label class="control-label col-sm-3">Phone</label> <div class="col-sm-5"> <input type="text" class="form-control input-sm" name="Phone" placeholder="Phone Number" required /> </div> </div> </div> </div> </div> </div> <div class="col-sm-6"> <div class="panel panel-success"> <div class="panel-heading"> Cridet Card Information </div> <div class="panel-body"> <div class="text-center" style="margin-bottom:10px"> <img src="@Url.Content("~/Content/visa.png")" class="" style="max-width:280px;" /> <img src="@Url.Content("~/Content/paypal2.png")" class="" style="max-width:280px;" /> <img src="@Url.Content("~/Content/mastercard.png")" class="" style="max-width:280px;" /> <img src="@Url.Content("~/Content/american-express.png")" class="" style="max-width:280px;" /> </div> <div class="form-horizontal"> <div class="form-group"> <label class="control-label col-sm-3">Amount</label> <div class="col-sm-5"> <input type="number" class="form-control input-sm" name="Amount" placeholder="Price" required /> </div> </div> <div class="form-group"> <label class="control-label col-sm-3">Card Number</label> <div class="col-sm-9"> <input type="text" class="form-control input-sm" name="CardNumber" placeholder="Ex. 4111111111111111" required /> </div> </div> <div class="form-group"> <label class="control-label col-sm-3">Card Code</label> <div class="col-sm-5"> <input type="number" class="form-control input-sm" name="CardCode" placeholder="Security No." required /> </div> </div> <div class="form-group"> <label class="control-label col-sm-3">Expire Date</label> <div class="col-sm-2"> <select class="form-control input-sm" name="Month" required> <option value="">Month</option> <option value="01">1</option> <option value="02">2</option> <option value="03">3</option> <option value="04">4</option> <option value="05">5</option> <option value="06">6</option> <option value="07">7</option> <option value="08">8</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> </select> </div> <div class="col-sm-3"> <select class="form-control input-sm" name="Year" required> <option value="">Year</option> <option value="17">2017</option> <option value="18">2018</option> <option value="19">2019</option> <option value="20">2020</option> <option value="21">2021</option> <option value="22">2022</option> <option value="23">2023</option> <option value="24">2024</option> </select> </div> </div> </div> </div> </div> </div> </div> <div class="row"> <div class="col-sm-12"> <div class="@ViewBag.css"> @Html.Raw(ViewBag.Msg) </div> <div class="well well-sm text-center"> <button type="submit" class="btn btn-primary">Submit Order</button> </div> </div> </div> }