cancel
Showing results for 
Search instead for 
Did you mean: 

Visual Studio C# Reading Response

Hello,

 

I have worked with a number of APIs but I am getting stuck on a small issue with Authorize.net.

 

I am able to successfully charge a card externally by using the API but for some reason I am unable to read the response. Below is the code related to the issue;

 

IRestResponse ChargeCardResponse =  API_Callouts.ChargeCreditCard(textboxFirstName.Text, textboxLastName.Text, textboxAddress.Text, textboxCity.Text, textboxState.Text, textboxZip.Text, textboxCountry.Text, textboxPhone.Text, textboxEmail.Text, textboxInvoice.Text, textboxDescription.Text, textboxCardNumber.Text, textboxExpirationDate.Text, textboxSecurityCode.Text, textboxTotal.Text);

var TransactionObject = JsonConvert.DeserializeObject<TransactionRootobject>(ChargeCardResponse); //Provides the error

 

 

public static IRestResponse ChargeCreditCard(string first, string last, string address, string city, string state, string zip, string country, string phone, string email, string invoice, string desc, string cardnumber, string expdate, string security, string total)
{
    var client = new RestClient("https://apitest.authorize.net/xml/v1/request.api?");
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddParameter("undefined", "{\r\n    \"createTransactionRequest\": {\r\n        \"merchantAuthentication\": {\r\n            \"name\": \"###########\",\r\n         \"transactionKey\": \"#############\"\r\n            \r\n        },\r\n        \"refId\": \"\",\r\n        \"transactionRequest\": {\r\n            \"transactionType\": \"authCaptureTransaction\",\r\n            \"amount\": \"" + total + "\",\r\n\r\n          \"payment\": {\r\n                \"creditCard\": {\r\n                    \"cardNumber\": \"" + cardnumber + "\",\r\n                \"expirationDate\": \"" + expdate + "\",\r\n                    \"cardCode\": \"" + security + "\"\r\n                }\r\n            },\r\n            \"order\" : {\r\n            \t\"invoiceNumber\": \"" + invoice + "\",\r\n            \t\"description\": \"" + desc + "\"\r\n            },\r\n\t\t        \"customer\" :{\r\n        \t\"email\": \"" + email + "\"\r\n        },\r\n            \"billTo\": {\r\n                \"firstName\": \"" + first + "\",\r\n                \"lastName\": \"" + last + "\",\r\n                \"company\": \"\",\r\n                \"address\": \"" + address + "\",\r\n                \"city\": \"" + city + "\",\r\n                \"state\": \"" + state + "\",\r\n                \"zip\": \"" + zip + "\",\r\n                \"country\": \"" + country + "\",\r\n                \"phoneNumber\" : \"" + phone + "\"\r\n            }\r\n            \r\n            \r\n        },\r\n\r\n        \r\n    }\r\n}", ParameterType.RequestBody);
            
IRestResponse response = client.Execute(request); return response; }
public class ChargedCardResponse
{
    public Transactionresponse transactionResponse { get; set; }
    public string refId { get; set; }
    public TransactionMessages messages { get; set; }
}

public class Transactionresponse
{
    public string responseCode { get; set; }
    public string authCode { get; set; }
    public string avsResultCode { get; set; }
    public string cvvResultCode { get; set; }
    public string cavvResultCode { get; set; }
    public string transId { get; set; }
    public string refTransID { get; set; }
    public string transHash { get; set; }
    public string testRequest { get; set; }
    public string accountNumber { get; set; }
    public string accountType { get; set; }
    public TransactionMessage[] messages { get; set; }
    public string transHashSha2 { get; set; }
    public int SupplementalDataQualificationIndicator { get; set; }
}

public class TransactionMessage
{
    public string code { get; set; }
    public string description { get; set; }
}

public class TransactionMessages
{
    public string resultCode { get; set; }
    public TransactionMessage1[] message { get; set; }
}

public class TransactionMessage1
{
    public string code { get; set; }
    public string text { get; set; }
}

 

Running this code provides me with an error where it tries to deserialize the response:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: . Path '', line 0, position 0.'

 

 

This code works perfectly fine if instead of trying to deserialize the variable ChargeCardResponse.content, I hard code the same exact response as what the variable gives me and deserialize it shown below;

 

var TransactionObject = JsonConvert.DeserializeObject<TransactionRootobject>("transactionResponse":{"responseCode":"1","authCode":"36PMD5","avsResultCode":"Y","cvvResultCode":"P","cavvResultCode":"2","transId":"60125450883","refTransID":"","transHash":"","testRequest":"0","accountNumber":"XXXX0015","accountType":"MasterCard","messages":[{"code":"1","description":"This transaction has been approved."}],"transHashSha2":"","SupplementalDataQualificationIndicator":0},"refId":"","messages":{"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}}); //NO ERROR!

 

I appreciate any help given!

 

1 ACCEPTED SOLUTION

Accepted Solutions

Figured out my problem. For some reason, there was a space (" ") at the beginning of the response.content which was throwing off the deserializing of data. Removing the first character of the json data fixed the problem!

View solution in original post

2 REPLIES 2

Figured out my problem. For some reason, there was a space (" ") at the beginning of the response.content which was throwing off the deserializing of data. Removing the first character of the json data fixed the problem!

Thanks for the solution it had solved my problem.

Booke_S
Member