ClickBank API in C#




Founded in 1998, ClickBank is a secure online retail outlet for more than 50,000 digital products and 100,000 active affiliate marketers. Just like Paypal, you can use ClickBank in your websites to handle payment transactions for your customers.

ClickBank publishes an API (a set of programming rules and specifications) for properly using their services. You can read about their API in http://www.clickbank.com/help/account-help/account-tools/clickbank-api/.

The current version of ClickBank’s Service API is: 1.2 and it uses REST for communication through the internet. ClickBanks provide example in C# but it's not detailed and hard to grasp especially if you're still new to REST.


In order to access their API, first you need the Clerk API key and the Developer Key. These keys are used for authorization and security so you must keep these in private and not show these keys to other people.

To acquire Developer and Clerk API keys, simply login to your ClickBank account and go to the Account Settings tab. Then, click “edit” in the Developer and Clerk API Key areas. Each key must be approved, present, and active for successful authentication to occur.

In one of our projects, we use ClickBank's API for determining if a customer's subscription is active and for canceling a customer's subscription.

Determining If A Customer Is Active

To determine if a user is active, you need to use the Orders API. This API's specification is published in https://api.clickbank.com/rest/1.2/orders. And here's how I've coded it in a C# procedure.


private bool IsClickBankAccountActive(string transactionId)
{
             //get authorization key string
            string authorizationKey = "DEV-A32" + ":"
               + "API-1SE";

            //set base uri
            Uri clickBankBaseUri = new Uri(_configuration.ClickBankRestUrl);

            //set uri template
            UriTemplate clickBankUriTemplate = new UriTemplate("/1.2/orders/{receipt}");

            //set complete uri
            Uri clickBankTicketUri = clickBankUriTemplate.BindByPosition(
                clickBankBaseUri, transactionId);

            //create request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
                clickBankTicketUri);
            request.Accept = "application/xml";
            request.Headers.Add(HttpRequestHeader.Authorization, authorizationKey);
            request.Method = "HEAD";

            HttpWebResponse response = null;

            //get response
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                HttpStatusCode c = response.StatusCode;
                if (c == HttpStatusCode.NoContent)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }

        }
}



Canceling A Customer's Subscription

To cancel a customer's subscription, you need to use the Tickets API. This API's specification is published in https://api.clickbank.com/rest/1.2/tickets.

When you call this Ticket API, it returns a data that determines the status of Ticket and other relevant info.
I've created a TicketData class for representing the data that is returned.

Here's the TicketData class:


public class TicketData
{
        public enum StatusOfTicket
        {
            OPEN = 0,
            REOPEN = 1,
            CLOSED =2
        }

        public enum TypeOfTicket
        {
            TECH_SUPPORT = 0,
            REFUND =1,
            CANCEL=2,
            ORDER_LOOKUP=3,
            ESCALATED=4,
            APPROVAL_AD=5,
            APPROVAL_IMAGE=6,
            APPROVAL_UPSELL=7,
            APPROVAL_CATEGORY_CHANGE=8,
            APPROVAL_MAX_PRICE=9,
            APPROVAL_BLOG_POST=10,
            APPROVAL_PRODUCT=11,
            CATEGORY_SUGGESTION=12,
            BUSINESS_DEVELOPMENT=13,
            ACCT_QUESTION_MARKETING=14,
            ACCT_QUESTION_ACCOUNTS=15,
            ACCT_QUESTION_ACCOUNTING=16,
            APPROVAL_GENERIC=17,
            SPAM=18,
            ABUSE=19

        }

        public int TicketId
        { get; set; }

        public string Receipt
        { get; set; }

        public StatusOfTicket TicketStatus
        { get; set; }

        public TypeOfTicket TicketType
        { get; set; }

}


Also, when cancelling a customer, the API requires a reason on why it's cancelled.
I've coded the reasons in a Dictionary:

//Dictionary for determining cancellation reasons
public Dictionary<string, string> GetCancellationReasons()
{
   Dictionary<string, string> reasons = new Dictionary<string, string>();
   reasons.Add(CancellationReason.ticket_type_cancel_1.ToString(), 
     "I did not receive additional value for the recurring payments");
   reasons.Add(CancellationReason.ticket_type_cancel_2.ToString(), 
     "I was not satisfied with the subscription / Subscription " + 
     "did not meet expectations");
   reasons.Add(CancellationReason.ticket_type_cancel_3.ToString(), 
     "I was unable to get support from the vendor");
   reasons.Add(CancellationReason.ticket_type_cancel_4.ToString(), 
     "Product was not compatible with my computer");
   reasons.Add(CancellationReason.ticket_type_cancel_5.ToString(), 
     "I am unable to afford continuing payments for this subscription");
   reasons.Add(CancellationReason.ticket_type_cancel_6.ToString(), 
     "I did not realize that I accepted the terms for continuing payments");
   reasons.Add(CancellationReason.ticket_type_cancel_7.ToString(), 
     "Other");
   return reasons;
}


And here's the code for cancelling the user's subscription:


//this procedure cancel's the subscription of a customer
public TicketData CancelSubscription(
   string ReceiptNo,CancellationReason reason, String Comment)
{
           
  TicketData responseTicket = null;

  //get cancel reason string
  string cancelReason = reason.ToString().Replace('_', '.');

  //get cancel type string
  string cancelType = CancellationType.cncl.ToString();
            
  //get authorization key string
  string authorizationKey = "DEV-A32" + ":"
     + "API-1SE8";
            
  string cancelComment = string.IsNullOrEmpty(Comment) ? Comment : string.Empty;
            
  //set base uri
  Uri clickBankBaseUri = new Uri(_configuration.ClickBankRestUrl);
            
  //set uri template
  UriTemplate clickBankUriTemplate = new UriTemplate(
   "/1.2/tickets/{receipt}/?type={type}&reason={reason}&comment={comment}");
            
  //set complete uri
  Uri clickBankTicketUri = clickBankUriTemplate.BindByPosition(
     clickBankBaseUri, ReceiptNo, cancelType, cancelReason, cancelComment);

  //create request
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
    clickBankTicketUri);
  request.Accept = "application/xml";
  request.Headers.Add(HttpRequestHeader.Authorization, 
    authorizationKey);
  request.Method = "POST";

  HttpWebResponse response = null;

 //get response
 try
  {
     response = (HttpWebResponse)request.GetResponse();
     string responseMessage = string.Empty;

     //Get Stream Response
     using (StreamReader reader = new
      StreamReader(response.GetResponseStream()))
     {
       responseMessage = reader.ReadToEnd();
     }

     //Parse Response using XDOCUMENT
     if (!string.IsNullOrEmpty(responseMessage))
     {
        XDocument document = XDocument.Parse(responseMessage);

        responseTicket = new TicketData();

        responseTicket.TicketId = Convert.ToInt32(document.Element(
           "ticketData").Element("ticketid").Value);
        responseTicket.Receipt = document.Element(
           "ticketData").Element("receipt").Value;
        responseTicket.TicketStatus = (
           Ncf.InsideEdge.Model.TicketData.StatusOfTicket)
           Enum.Parse(typeof(
           Ncf.InsideEdge.Model.TicketData.StatusOfTicket), 
            document.Element(
             "ticketData").Element("status").Value, true);
             responseTicket.TicketType = 
             (Ncf.InsideEdge.Model.TicketData.TypeOfTicket)Enum.Parse(
             typeof(Ncf.InsideEdge.Model.TicketData.TypeOfTicket),
              document.Element(
             "ticketData").Element("type").Value, true);
     }

     }
     catch
     {

     }

    //check response ticket
    if (responseTicket != null && responseTicket.Receipt == ReceiptNo
         && responseTicket.TicketType ==
         Ncf.InsideEdge.Model.TicketData.TypeOfTicket.CANCEL)
    {
        return responseTicket;
    }
    else
    {
        return null;
    }
  }
 }


I hope you find my article useful and it's able to help you understand better how to use RESTFUL Services, specifically the ClickBank API Service.






1 comments:

Anonymous said...

hi..

if we need to login user through api for click bank then which api will be use..?

here u enter Clerk key and Developer key, but how to get these two key's of user ?
or if i miss understand then explain me because i want to create iphone application for clickbank site, where user login throuth application and can see them data which they can see in webpage..

I need to knw authentication process for click back

please help me and reply me on my mail id..

my mail id is:
irsh_it@yahoo.com


Thank You Sir..,
- Irshad

Post a Comment