Using EWAY (Credit Card Gateway) REST/JSON API in C#

Using EWAY (Credit Card Gateway) REST/JSON API in C#

Eway is payment gateway that can be used to facility credit card payment in number of languages such as PHP, ASPNET, JAVA, RUBY, NODE.JS

In this blog we will look at how we can use a EWAY REST/JSON API URL and convert this in ASP.NET C# Object to use this within our Visual Studio Project.

Let look at a REST/JSON URL example :- https://api.sandbox.ewaypayments.com/Customer/{TokenCustomerID}  which for ours purposes is used to query a Customer details that is identify by a Tokenid (TokenCustomerID).

For now a TokenCustomerID is provide by EWAY when we first registered with the payment gateway. This token can then be reused later to query customer information.

The example below is SearchTokenCustomer function that read in the tokenCustomerID and return C# Customer Object -GetTokenCUstomerResults  (is Customer information, object properties is shown below)

public static GetTokenCustomerResult SearchTokenCustomer(string tokenCustomerId)
{
// the REST style URL we are going to POST to to create an access code
string url = https://api.sandbox.ewaypayments.com/Customer/{1}", ConfigurationManager.AppSettings["RapidAPI.REST.URL"], tokenCustomerId);
string json = "{}";
string error = "Unable to retreive access code result";
// create a web request to POST data to
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

// read username & password from web.config
string username = ConfigurationManager.AppSettings["RapidAPI.UserName"].ToString();
string password = ConfigurationManager.AppSettings["RapidAPI.Password"].ToString();
// add Basic HTTP Authentication
webRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)));

// configure request
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
try
{
// send & read in response
using (StreamReader streamIn = new StreamReader(webRequest.GetResponse().GetResponseStream()))
json = streamIn.ReadToEnd();
}
catch (WebException webException)
{
// attempt to get any additional information from the server
using (StreamReader stream = new StreamReader(webException.Response.GetResponseStream()))
error = stream.ReadToEnd();
}
if (!string.IsNullOrEmpty(json))
{
// convert the text json to an object
GetTokenCustomerResult result = JsonConvert.DeserializeObject<GetTokenCustomerResult>(json);

// display to screen
return result;

}
else
{
// something has gone wrong...
return new GetTokenCustomerResult();
}
}

public class GetTokenCustomerResult
{
public List<SearchTokenCustomer> Customers;
}

public class SearchTokenCustomer
{
public long? TokenCustomerID;
public string Reference;
public string Title;
public string FirstName;
public string LastName;
public string CompanyName;
public string JobDescription;
public string Street1;
public string Street2;
public string City;
public string State;
public string PostalCode;
public string Country;
public string Email;
public string Phone;
public string Mobile;
public string Comments;
public string Fax;
public string Url;
public CardDetails CardDetails;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *