cancel
Showing results for 
Search instead for 
Did you mean: 

C# ,net Core error calling api

I'm new to Auth.net, it appears as though the SDK doesn't support .net core, so i'm writing an API wrapper using Flurl to make the requests. I'm getting an error in the response message, 

XmlNodeConverter can only convert JSON that begins with an object.

That generally occurs when it's json rather than xml, but according to the guides, Auth.Net supports JSON.

Here is my base class that i'm sending.


public class PaymentRequest
{
[JsonProperty("merchantAuthentication")]
public MerchantAuthentication AuthHeader { get; set; }
[JsonProperty("refId")]
public string ReferenceId { get; set; }
[JsonProperty("transactionRequest")]
public TransactionRequest transactionRequest { get; set; }
[JsonProperty("billTo")]
public BillingDetails BillTo { get; set; }

[JsonProperty("customerIP")]
public string IpAddress { get; set; }

[JsonProperty("authorizationIndicatorType")]
public AuthIndicatorType AuthorizationIndicator { get; set; }
}

public class AuthIndicatorType
{
public string authorizationIndicator { get { return "pre"; } }
}
}

Here is the actual call that sends the request

public async Task<PaymentResponse> PreAuthorization(CreditCard cardInfo, decimal feeAmount)
{

var url = new Flurl.Url(BaseUrl);
try
{
var authRequest = new PaymentRequest();
authRequest.AuthHeader = new MerchantAuthentication { AuthName = ApiLogin, TransactionKey = ApiKey };
authRequest.ReferenceId = "0";
authRequest.transactionRequest = new TransactionRequest { RequestType = "authOnlyTransaction", Amount = feeAmount, Payment = cardInfo };
authRequest.BillTo = _billingInfo;
authRequest.IpAddress = "";
authRequest.AuthorizationIndicator = new AuthIndicatorType();

var response = await url.PostJsonAsync(JsonConvert.SerializeObject(authRequest)).ReceiveJson<PaymentResponse>();

return response;
}
catch (Exception ex)
{
return null;
}

What am i missing here?

3 REPLIES 3

Quick update, i did have a few issues in how it was rendering in Json. I am now able to generate the correct Json object (which executes correctly in the sandbox (https://developer.authorize.net/api/reference/index.html#payment-transactions-authorize-a-credit-car...) but i still get the same error when running it through Visual Studio. 

This is the json string i'm rendering : "{"createTransactionRequest":{"merchantAuthentication":{"name":"5KP3u95bQpv","transactionKey":"346HZ32z3fP4hTG2"},"refId":"123456","transactionRequest":{"transactionType":"authOnlyTransaction","amount":2.99,"currencyCode":"USD","payment":{"creditCard":{"cardNumber":"5424000000000015","expirationDate":"2025-12","cardCode":"999"}},"billTo":{"firstName":"Anonymous","lastName":"User","company":null,"address":"123 Main Street","city":"Birmingham","state":"Alabama","zip":"12345","country":"US"},"authorizationIndicatorType":{"authorizationIndicator":"pre"}}}}"

I verified the JSON you last posted is valid.
I don't use visual studio to interact with AN.
I recommend that if possible, you post the RAW REQUEST that is being made to AN, as it may reveal the issue.
HTH!

marine2026
Trusted Contributor