We help IT Professionals succeed at work.

Using sample Autorize.net c# simple example

Ron Renninger
on
293 Views
Last Modified: 2017-04-26
I am trying a very simple sample of authorize.net in c#. I pasted what i am trying to do below. Just wondering how to get this to work.



Step 1


Get the Authorize.Net C# Sample Code

Our C# sample code project provides samples of all our API features & functions. Get the sample code project from GitHub.  If you are adding the SDK to an existing project simply install with the nuget package manager.  For example:

 

PM> Install-Package AuthorizeNet





 

Step 2


Charge a Credit Card

The Hello World program for payments is charging a credit card.  The code below (source code) will charge a test card and print the auth code & transaction ID to the console.  For a full list of C# samples see our sample code repository on GitHub. You can also try all of our API requests in the API Reference.


using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using AuthorizeNet.Api.Controllers;
using AuthorizeNet.Api.Contracts.V1;
using AuthorizeNet.Api.Controllers.Bases;

namespace net.authorize.sample
{
	public class ChargeCreditCard
	{
		public static void Run(String ApiLoginID, String ApiTransactionKey)
		{
			Console.WriteLine("Charge Credit Card Sample");

			ApiOperationBase.RunEnvironment = AuthorizeNet.Environment.SANDBOX;

			// define the merchant information (authentication / transaction id)
			ApiOperationBase.MerchantAuthentication = new merchantAuthenticationType()
			{
				name = ApiLoginID,
				ItemElementName = ItemChoiceType.transactionKey,
				Item = ApiTransactionKey,
			};

			var creditCard = new creditCardType
			{
				cardNumber = "4111111111111111",
				expirationDate = "0718"
			};

			//standard api call to retrieve response
			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);
				}
			}
		   
		}
	}
}

Open in new window


 





Step 3

Run It

Build the project to create the SampleCode exe.  Then run it:

 SampleCode ChargeCreditCard
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.