cancel
Showing results for 
Search instead for 
Did you mean: 

trying to get authorizecredtcard to work in C#

I am trying to get the authorized credit card to work i made a simple website to test the code before putting it in the website. i am geting an error near this code

 I did get authcapturetransaction to work

the error is

Object reference not set to an instance of an object."}

 

var response = controller.getapiresponse();

if(response != null

 

Below is the the full code without login information

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

using System.Threading.Tasks;

using AuthorizeNet.Api.Controllers;

using AuthorizeNet.Api.Controllers.Bases;

using System.Net;

using AuthorizeNet.Api.Contracts.V1;

public partial class AutrorizeCreditcard : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;

ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType()

{

name = "",

ItemElementName = ItemChoiceType.transactionKey,

Item = "",

};

var creditCart = new creditCardType

{

cardCode = "41111111111111111",

expirationDate = "0718"

};

// starderd api call to retrieve response

var paymentType = new paymentType

{

Item = creditCart

};

var transactionRequest = new transactionRequestType

{

transactionType = transactionTypeEnum.authOnlyTransaction.ToString(), // authorize only

amount = 133.45m,

payment = paymentType

};

var request = new createTransactionRequest

{

transactionRequest = transactionRequest

};

var controller = new createTransactionController(request);

controller.Execute();

var response = controller.GetApiResponse();

if (response.messages != null)

{

if (response.messages.resultCode == messageTypeEnum.Ok)

{

if (response.transactionResponse.messages != null)

{

txtOutput.Text = "Successfully created transaction with transaction id " + response.transactionResponse.transId;

txtOutput.Text = txtOutput.Text + "\n\nResponse code " + response.transactionResponse.responseCode;

txtOutput.Text = txtOutput.Text + "\n\nMessage Code " + response.transactionResponse.messages[0];

txtOutput.Text = txtOutput.Text = "n\nSuccess, Auth Code" + response.transactionResponse.authCode;

}

else

{

txtOutput.Text = "Failed transaction.";

if (response.transactionResponse.errors != null)

{

txtOutput.Text = txtOutput.Text + "\n\nError Code " + response.transactionResponse.errors[0];

txtOutput.Text = txtOutput.Text + "\n\nError Message " + response.transactionResponse.errors[0].errorText;

}

}

}

else

{

txtOutput.Text = "Failed transaction.";

if (response.transactionResponse.errors != null && response.transactionResponse.errors != null)

{

txtOutput.Text = txtOutput.Text + "\n\nError Code " + response.transactionResponse.errors[0];

txtOutput.Text = txtOutput.Text + "\n\nError Message " + response.transactionResponse.errors[0].errorText;

}

else

{

txtOutput.Text = txtOutput.Text + "\n\nError Code " + response.messages.message[0].code;

txtOutput.Text = txtOutput.Text + "\n\nError Message " + response.messages.message[0].text;

}

}

}

else

{

txtOutput.Text = "Null Response";

}

}

}

 

 

 

 

 

 

rrr944276
Contributor
1 REPLY 1

Is txtOutput defined using the same case in your page? Try the following:

using System;
using AuthorizeNet.Api.Controllers;
using AuthorizeNet.Api.Contracts.V1;
using AuthorizeNet.Api.Controllers.Bases;
using System.Security.Authentication;
using System.Net;

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 = "",
                ItemElementName = ItemChoiceType.transactionKey,
                Item = "",
            };

            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 = 133.45M,
                payment = paymentType
            };
            const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
            const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
            ServicePointManager.SecurityProtocol = Tls12;
            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;
        }
    }
}
Powered by NexWebSites.com -
Certified Authorize.net developers
NexusSoftware
Trusted Contributor