I am trying to test CardConnect's API, and whenever I make a request to https://fts.cardconnect.com:6443/rest/auth I receive an unauthorized response. I've tried rolling back the version of rest sharp to an older version, but that didn't seem to help.
Here is the main function.
public static void Main(String[] args)
{
// Send an Auth Transaction request
String retref = authTransaction();
// Void transaction
voidTransaction(retref);
}
This is where I'm building my JObject to send in the request.
public static String authTransaction()
{
Console.WriteLine("\nAuthorization Request");
// Create Authorization Transaction request
JObject request = new JObject();
// Merchant ID
request.Add("merchid", "496160873888");
// Card Type
request.Add("accttype", "VI");
// Card Number
request.Add("account", "4444333322221111");
// Card Expiry
request.Add("expiry", "0914");
// Card CCV2
request.Add("cvv2", "776");
// Transaction amount
request.Add("amount", "100");
// Transaction currency
request.Add("currency", "USD");
// Order ID
request.Add("orderid", "12345");
// Cardholder Name
request.Add("name", "Test User");
// Cardholder Address
request.Add("Street", "123 Test St");
// Cardholder City
request.Add("city", "TestCity");
// Cardholder State
request.Add("region", "TestState");
// Cardholder Country
request.Add("country", "US");
// Cardholder Zip-Code
request.Add("postal", "11111");
// Return a token for this card number
request.Add("tokenize", "Y");
// Create the REST client
CardConnectRestClient client = new CardConnectRestClient(ENDPOINT, USERNAME, PASSWORD);
// Send an AuthTransaction request
JObject response = client.authorizeTransaction(request);
foreach (var x in response)
{
String key = x.Key;
JToken value = x.Value;
Console.WriteLine(key + ": " + value.ToString());
}
return (String)response.GetValue("retref");
}
From here, this is where the object is finally sent.
public JObject authorizeTransaction(JObject request)
{
return (JObject)send(ENDPOINT_AUTH, OPERATIONS.PUT, request);
}
private Object send(String endpoint, OPERATIONS operation, JObject request)
{
// Create REST client
RestClient client = new RestClient(url);
// Set authentication credentials
client.Authenticator = new HttpBasicAuthenticator(username, password);
// Create REST request
RestRequest rest = null;
switch (operation)
{
case OPERATIONS.PUT: rest = new RestRequest(endpoint, Method.Put); break;
case OPERATIONS.GET: rest = new RestRequest(endpoint, Method.Get); break;
case OPERATIONS.POST: rest = new RestRequest(endpoint, Method.Post); break;
case OPERATIONS.DELETE: rest = new RestRequest(endpoint, Method.Delete); break;
}
rest.RequestFormat = DataFormat.Json;
rest.AddHeader("Content-Type", "application/json");
String data = (request != null) ? request.ToString() : "";
rest.AddParameter("application/json", data, ParameterType.RequestBody);
RestResponse response = client.ExecuteAsync(rest).GetAwaiter().GetResult();
JsonTextReader jsreader = new JsonTextReader(new StringReader(response.Content));
try
{
return new JsonSerializer().Deserialize(jsreader);
}
catch (JsonReaderException jx)
{
return null;
}
}
For the most part, this is just the file provided by Cardpoint on their Github page, so it's got me stumped as to why this wouldn't work.
I have also tried changing my credentials and manually adding the Authorization header. The documentation mentions that this would happen because it received invalid credentials. However, using the same credentials in postman returns a 200 response.
I figured out the issue. Typically RestSharp handles setting the content-type, but CardConnect is one of the times it has to be set manually. So the problem that I was having was how I was setting the body parameter. I had to change
rest.RequestFormat = DataFormat.Json;
rest.AddHeader("Content-Type", "application/json");
String data = (request != null) ? request.ToString() : "";
rest.AddParameter("application/json", data, ParameterType.RequestBody);
RestResponse response = client.ExecuteAsync(rest).GetAwaiter().GetResult();
to
String data = (request != null) ? request.ToString() : "";
rest.AddBody(data,"application/json");
rest.AddHeader("Content-Type", "application/json");
var response = await client.ExecutePutAsync(rest);
Once I did this my requests started going through without issue.