CGC_Codes

AuthorizeCreditCard sample

Feb 21st, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using System.Collections.Generic;
  5. using AuthorizeNet.Api.Controllers;
  6. using AuthorizeNet.Api.Contracts.V1;
  7. using AuthorizeNet.Api.Controllers.Bases;
  8.  
  9. namespace net.authorize.sample
  10. {
  11.     public class AuthorizeCreditCard
  12.     {
  13.         public static ANetApiResponse Run(String ApiLoginID, String ApiTransactionKey, decimal amount)
  14.         {
  15.             Console.WriteLine("Authorize Credit Card Sample");
  16.  
  17.             ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;
  18.  
  19.             // define the merchant information (authentication / transaction id)
  20.             ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType()
  21.             {
  22.                 name = ApiLoginID,
  23.                 ItemElementName = ItemChoiceType.transactionKey,
  24.                 Item = ApiTransactionKey,
  25.             };
  26.  
  27.             var creditCard = new creditCardType
  28.             {
  29.                 cardNumber = "4111111111111111",
  30.                 expirationDate = "0718"
  31.             };
  32.  
  33.             //standard api call to retrieve response
  34.             var paymentType = new paymentType { Item = creditCard };
  35.  
  36.             var transactionRequest = new transactionRequestType
  37.             {
  38.                 transactionType = transactionTypeEnum.authOnlyTransaction.ToString(),    // authorize only
  39.                 amount = amount,
  40.                 payment = paymentType
  41.             };
  42.  
  43.             var request = new createTransactionRequest { transactionRequest = transactionRequest };
  44.  
  45.             // instantiate the contoller that will call the service
  46.             var controller = new createTransactionController(request);
  47.             controller.Execute();
  48.  
  49.             // get the response from the service (errors contained if any)
  50.             var response = controller.GetApiResponse();
  51.  
  52.             //validate
  53.             if (response != null)
  54.             {
  55.                 if (response.messages.resultCode == messageTypeEnum.Ok)
  56.                 {
  57.                     if (response.transactionResponse.messages != null)
  58.                     {
  59.                         Console.WriteLine("Successfully created transaction with Transaction ID: " + response.transactionResponse.transId);
  60.                         Console.WriteLine("Response Code: " + response.transactionResponse.responseCode);
  61.                         Console.WriteLine("Message Code: " + response.transactionResponse.messages[0].code);
  62.                         Console.WriteLine("Description: " + response.transactionResponse.messages[0].description);
  63.                         Console.WriteLine("Success, Auth Code : " + response.transactionResponse.authCode);
  64.                     }
  65.                     else
  66.                     {
  67.                         Console.WriteLine("Failed Transaction.");
  68.                         if (response.transactionResponse.errors != null)
  69.                         {
  70.                             Console.WriteLine("Error Code: " + response.transactionResponse.errors[0].errorCode);
  71.                             Console.WriteLine("Error message: " + response.transactionResponse.errors[0].errorText);
  72.                         }
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     Console.WriteLine("Failed Transaction.");
  78.                     if (response.transactionResponse != null && response.transactionResponse.errors != null)
  79.                     {
  80.                         Console.WriteLine("Error Code: " + response.transactionResponse.errors[0].errorCode);
  81.                         Console.WriteLine("Error message: " + response.transactionResponse.errors[0].errorText);
  82.                     }
  83.                     else
  84.                     {
  85.                         Console.WriteLine("Error Code: " + response.messages.message[0].code);
  86.                         Console.WriteLine("Error message: " + response.messages.message[0].text);
  87.                     }
  88.                 }
  89.             }
  90.             else
  91.             {
  92.                 Console.WriteLine("Null Response.");
  93.             }
  94.  
  95.             return response;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment