Create Charge In Clover Using C#

Hey guys, hope you all are doing well. Today, I will show you how we can create a charge in clover using c# code. We will use the API given by clover for creating a charge.

I have shown the same call we can do in postman. Please review the link here for the same

https://www.thecodehubs.com/create-charge-in-clover-using-postman 

Create a model class to for the data to be sent to the API for charge

public class CloverNewTransactionRequest 
{ 
  public long amount { get; set; } 
  public string currency { get; set; } 
  public bool capture { get; set; } 
  public string description { get; set; } 
  public string ecomind { get; set; } 
  public string source { get; set; } 
}

public class Response
{
  public string Message { get; set; }
  public bool IsSuccess { get; set; }
  public bool IsWebError { get; set; }
}

Now, create a function to create the request for payment. Please use the below code for the same.

private string CloverAuthCaptureRequest()
        {
            var post_string = "";
            CloverNewTransactionRequest saleRequest = new CloverNewTransactionRequest();
            try
            {                
                saleRequest.amount = 100;
                saleRequest.capture = true;
                saleRequest.ecomind = "ecom";
                saleRequest.currency = "usd";
                saleRequest.description = "test charge";                
                saleRequest.source = "Your card Token";    
                post_string = new JavaScriptSerializer().Serialize(saleRequest);               
            }
            catch (Exception ex)
            {
               post_string = ""
            }
            return post_string;
        }

As we are ready with the class and also the request now, we will create the API using HttpWebRequest. Use the below method for the same.

private Response CloverWebRequest()
{
  string URL = "https://scl-sandbox.dev.clover.com/v1/charges";
  string Request_string = CloverAuthCaptureRequest();
  string ApiKey = "Your Ecomm API key";
  Response ObjResponse = new Response();
  var Response_String = string.Empty;
  try
  {
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL);
    objRequest.Method = "POST";
    objRequest.ContentLength = Request_string.Length;
    objRequest.ContentType = "application/json; charset=utf-8";		
    objRequest.Headers.Add("Authorization", "Bearer " + ApiKey);		
    objRequest.Accept = "application/json; charset=utf-8";
    objRequest.Timeout = 120000;

    // post data is sent as a stream
    StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream());
    myWriter.Write(Request_string);
    myWriter.Close();

    // returned values are returned as a stream, then read into a string
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
    {
      var response = responseStream.ReadToEnd();
      Response_String = response;
      responseStream.Close();
    }
  
    ObjResponse.IsSuccess = true;
    ObjResponse.Message = Response_String;
  }
  catch (WebException webex)
  {
    WebResponse errResp = webex.Response;
    using (Stream respStream = errResp.GetResponseStream())
    {
      StreamReader reader = new StreamReader(respStream);
      Response_String = reader.ReadToEnd();			
      ObjResponse.IsSuccess = false;
      ObjResponse.IsWebError = true;

    }
  }
  catch (Exception ex)
  {
    Response_String = ex.ToString();
    ObjResponse.IsSuccess = false;
    ObjResponse.IsWebError = false;

  }

  ObjResponse.Message = Response_String;
  return ObjResponse;
}

In the above response object you will receive the response from the clover API in serialize form. We can deserialize the response as per our requirement.

sample class for the response is here

public class CloverNewTransactionResponse
{
  public string id { get; set; }
  public int amount { get; set; }
  public int amount_refunded { get; set; }
  public string currency { get; set; }
  public long created { get; set; }
  public string description { get; set; }
  public bool captured { get; set; }
  public string ref_num { get; set; }
  public string auth_code { get; set; }
  public string warning_message { get; set; }
  public Outcome outcome { get; set; }
  public bool paid { get; set; }
  public string status { get; set; }
  public Source source { get; set; }
  public string ecomind { get; set; }
}

The result will be same as I have show in the charge using postman article.

https://www.thecodehubs.com/create-charge-in-clover-using-postman

Submit a Comment

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

Subscribe

Select Categories