Reply
Highlighted
Member
Posts: 1
Registered: ‎09-11-2018

using SDK on sendbox and getting: An existing connection was forcibly closed by the remote host

Hi,

 

I just downloaded C# sdk and I am running as web app on VS 2017. Windows Server 2008 R2.

 

I am getting the error :

 

System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a receive.'

SocketException: An existing connection was forcibly closed by the remote host'

 

I saw some other messages here in relation to a similar error. 

I added the code : 

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

var request = new createTransactionRequest { transactionRequest = transactionRequest };

// instantiate the controller that will call the service
var controller = new createTransactionController(request);
controller.Execute();

 

But nothing works. I am just sending test values to the Sandbox server, but nothing works

 

protected ANetApiResponse ChargeCard(String ApiLoginID, String ApiTransactionKey, decimal amount)
{

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 = "0725",
cardCode = "123"
};

var billingAddress = new customerAddressType
{
firstName = "John",
lastName = "Doe",
address = "123 My St",
city = "OurTown",
zip = "98004"
};

//standard api call to retrieve response
var paymentType = new paymentType { Item = creditCard };

// Add line Items
var lineItems = new lineItemType[2];
lineItems[0] = new lineItemType { itemId = "1", name = "t-shirt", quantity = 2, unitPrice = new Decimal(15.00) };
lineItems[1] = new lineItemType { itemId = "2", name = "snowboard", quantity = 1, unitPrice = new Decimal(450.00) };

var transactionRequest = new transactionRequestType
{
transactionType = transactionTypeEnum.authCaptureTransaction.ToString(), // charge the card

amount = amount,
payment = paymentType,
billTo = billingAddress,
lineItems = lineItems
};

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var request = new createTransactionRequest { transactionRequest = transactionRequest };

// instantiate the controller 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 response
if (response != null)
{SocketException: An existing connection was forcibly closed by the remote host

if (response.messages.resultCode == messageTypeEnum.Ok)
{
if(response.transactionResponse.messages != null)
{
Response.Write("Successfully created transaction with Transaction ID: " + response.transactionResponse.transId);
Response.Write("<br>");
Response.Write("Response Code: " + response.transactionResponse.responseCode);
Response.Write("<br>");
Response.Write("Message Code: " + response.transactionResponse.messages[0].code);
Response.Write("<br>");
Response.Write("Description: " + response.transactionResponse.messages[0].description);
Response.Write("<br>");
Response.Write("Success, Auth Code : " + response.transactionResponse.authCode);
Response.Write("<br>");
}
else
{
Response.Write("Failed Transaction.");
if (response.transactionResponse.errors != null)
{
Response.Write("Error Code: " + response.transactionResponse.errors[0].errorCode);
Response.Write("Error message: " + response.transactionResponse.errors[0].errorText);
}
}
}
else
{
Response.Write("Failed Transaction.");
if (response.transactionResponse != null && response.transactionResponse.errors != null)
{
Response.Write("Error Code: " + response.transactionResponse.errors[0].errorCode);
Response.Write("<br>");
Response.Write("Error message: " + response.transactionResponse.errors[0].errorText);
Response.Write("<br>");
}
else
{
Response.Write("Error Code: " + response.messages.message[0].code);
Response.Write("<br>");
Response.Write("Error message: " + response.messages.message[0].text);
}
}
}
else
{
Response.Write("Null Response.");
}

return response;
}

 

Any help ??

 

Thanks a lot

Highlighted
Administrator
Posts: 149
Registered: ‎03-28-2014

Re: using SDK on sendbox and getting: An existing connection was forcibly closed by the remote host

One quick sanity check would be to pull down the sample C# project and run that (should only take a minute) to confirm that is working on your machine/environment.  It's here : https://github.com/AuthorizeNet/sample-code-csharp

 

Let us know how that goes.

 

Brian