Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- 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 AuthorizeCreditCard
- {
- public static ANetApiResponse Run(String ApiLoginID, String ApiTransactionKey, decimal amount)
- {
- Console.WriteLine("Authorize Credit Card Sample");
- ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;
- // define the merchant information (authentication / transaction id)
- ApiOperationBase<ANetApiRequest, ANetApiResponse>.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.authOnlyTransaction.ToString(), // authorize only
- amount = amount,
- 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();
- //validate
- if (response != null)
- {
- if (response.messages.resultCode == messageTypeEnum.Ok)
- {
- if (response.transactionResponse.messages != null)
- {
- Console.WriteLine("Successfully created transaction with Transaction ID: " + response.transactionResponse.transId);
- Console.WriteLine("Response Code: " + response.transactionResponse.responseCode);
- Console.WriteLine("Message Code: " + response.transactionResponse.messages[0].code);
- Console.WriteLine("Description: " + response.transactionResponse.messages[0].description);
- Console.WriteLine("Success, Auth Code : " + response.transactionResponse.authCode);
- }
- else
- {
- Console.WriteLine("Failed Transaction.");
- if (response.transactionResponse.errors != null)
- {
- Console.WriteLine("Error Code: " + response.transactionResponse.errors[0].errorCode);
- Console.WriteLine("Error message: " + response.transactionResponse.errors[0].errorText);
- }
- }
- }
- else
- {
- Console.WriteLine("Failed Transaction.");
- if (response.transactionResponse != null && response.transactionResponse.errors != null)
- {
- Console.WriteLine("Error Code: " + response.transactionResponse.errors[0].errorCode);
- Console.WriteLine("Error message: " + response.transactionResponse.errors[0].errorText);
- }
- else
- {
- Console.WriteLine("Error Code: " + response.messages.message[0].code);
- Console.WriteLine("Error message: " + response.messages.message[0].text);
- }
- }
- }
- else
- {
- Console.WriteLine("Null Response.");
- }
- return response;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment