Create Customer In Clover Using C#

Hello guys,

In my last article I have show how we can create charge using token in clover. That will be one time payment. What if we want to make multiple payment for a customer and don’t want to ask for card details again. In such scenario we need to create a customer in clover using the one time token and then make future payments using the customer id received from clover.

So, lets get started. First we need to create class for the customer request which we will send to the API.

Below is the code for the class model.

public class CloverCustomerRequest
{
  public string ecomind { get; set; }
  public string email { get; set; }
  public string firstName { get; set; }
  public string lastName { get; set; }
  public string name { get; set; }
  public string source { get; set; }
  public string phone { get; set; }
  public CloverCustomerShipping shipping { get; set; }
}

public class CloverCustomerShipping
{
  public CloverCustomerAddress address { get; set; }
  public string carrier { get; set; }
  public string name { get; set; }
  public string phone { get; set; }
  public string tracking_number { get; set; }
}

public class CloverCustomerAddress
{
  public string city { get; set; }
  public string country { get; set; }
  public string line1 { get; set; }
  public string line2 { get; set; }
  public string postal_code { get; set; }
  public string state { get; set; }
}

Model class for the customer response will look something like this

public class CloverCreateCustomerResponse
{
  public string id { get; set; }
  public string _object { get; set; }
  public long created { get; set; }
  public string currency { get; set; }
  public string email { get; set; }
  public string name { get; set; }
  public CloverCustomerResSources sources { get; set; }
  public CloverCustomerResShipping shipping { get; set; }
}

public class CloverCustomerResSources
{
  public string _object { get; set; }
  public string[] data { get; set; }
}

public class CloverCustomerResShipping
{
  public string name { get; set; }
  public CloverCustomerResShippingAddress address { get; set; }
}

public class CloverCustomerResShippingAddress
{
  public string country { get; set; }
}

 

Now, we will create the method which will create the json request for the customer API. For the card token please refer the article here

private Response CloverCreateCustomerRequest()
{
  var post_string = "";	
  CloverCustomerRequest CreateCust = new CloverCustomerRequest();
  try
  {
    CreateCust.ecomind = "ecom";
    CreateCust.email = "shailjajariwala.vision@gmail.com"
    CreateCust.firstName = "Shailja";
    CreateCust.lastName = "Jariwala";
    CreateCust.name = "Shailja Jariwala";
    CreateCust.source = "Your Card Token";
    CreateCust.phone = "1234567890";

    CloverCustomerShipping shipping = new CloverCustomerShipping()
    {
      address = new CloverCustomerAddress()
      {
        line1 = "Test Line 1",
        city = "Aptos",
        state = "CA",
        country = "US",
        postal_code = "95001"
      }
    };
    CreateCust.shipping = shipping;

    post_string = new JavaScriptSerializer().Serialize(CreateCust);
    
  }
  catch (Exception ex)
  {
    post_string = "";
  }

  return post_string;
}

 

Once you are ready with the request string, we need to call the API using the web request. Below is the sample code for the same. The below method will return the customer ID returned from clover after creating the customer

 

private string CloverCreateCustomerUsingToken()
{
  var Response_String = string.Empty;
  string CustomerId = "";
  try
  {
    string Url= "https://scl-sandbox.dev.clover.com/v1/customers";
    string ApiKey = "Your API Key";
    string Request_string = CloverCreateCustomerRequest();
    
    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();
    }
    var result = JsonConvert.DeserializeObject<CloverCreateCustomerResponse>(Response_String);
    if (result != null)
    {
      CustomerId = result.id;
    }


  }
  catch (WebException webex)
  {
    WebResponse errResp = webex.Response;
    using (Stream respStream = errResp.GetResponseStream())
    {
      StreamReader reader = new StreamReader(respStream);
      Response_String = reader.ReadToEnd();			 // we can use this to get the exact error message.
    }
  }
  catch (Exception ex)
  {  
    Response_String = ex.ToString();  // we can use this to get the exact error message.		

  }	
  return CustomerId;
}

Note: The API Url which I have shown in all the payment gateway article points to the test environment. To get the production environment URL’s please check the clover documentation.

Submit a Comment

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

Subscribe

Select Categories