Showing posts with label Web Service. Show all posts
Showing posts with label Web Service. Show all posts


Convert Any Web Page To A PDF Document


There are times when we need a web page to be downloaded and viewed as a PDF document. Web pages display differently based on several factors such as the type and version of browser and size of the computer's screen. To be able to share the web page with other users and ensure that it displays correctly and uniformly regardless of these factors,  you can opt to view it as a PDF document.

If you don't have the software to convert a web page to pdf, you can use the tool I've provided here using a little bit of JavaScript and a service from PDFmyURL.com. Just input the url of the web page that you want to download as pdf and click "Generate PDF" button. Make sure though that the url that you input are not "log-in" protected. 








PDF, or otherwise known as  Portable Document Format, is the de facto standard for printable documents on the web. Most books now are published as a PDF. Ebooks, as they're called, has a few if not many advantages over its HardCover counterpart. To name a few, an Ebook can be searched automatically, you can bring a 5000-page ebook in your pocket, and it can't worn out over time.

A PDF document can be viewed in your PC or Mac using Ebook readers software such as Acrobat Reader, Foxit Reader, Google Chrome,etc. But when you want to read your ebook like a traditional book,  a must have is either Amazon's Kindle or Apple's IPad. Both are great for reading pdf documents.







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.








Creating a VB.NET Web Service in VS 2010




I was given the task of creating a program to communicate with a web service provided by VideoNext Network Solutions, Inc. There are various ways you can achieve communications in .NET world. For communicatons through internet, you can either use the awesome WCF technology, use the barebones WebRequest Class, etc. For this project, I've used the power of Web Service technology.

It's a little bit tricky to create a Web Service in a Visual Studio 2010 Class Library Project because currently, WCF is the default tool to do it. Anyhow, here's how I do it:

1. First, right click on the Class Library Project in Solution Explorer and click "Add Service Reference".




2. On the Add Service Reference Dialog, click Advance.



3. On the Service Reference Settings, click Add Web Reference.



4. On the Add Web Reference Dialog, type in the URL address box the URL address of the service that you wish to communicate. In my project, URL is  http://207.207.160.4:80/axis2/services/EventLogService?wsdl


After you type in the url, if Visual Studio's been able to communicate with the service, it will display the methods available in the service. In my case, there are only 2 methods - event_action and get_events methods.
You can change the Web Reference name if you wish. In my project, I've renamed it from WebReference to EventLogReference.

5. After you've done the above successfully, you should be able to see the EventLogReference in Web References folder.

 
6. Behind the scenes, Visual Studio automatically generated a lot of codes for you for communicating to the specified service. All you need to do is to use the generated codes and call the appropriate methods.

In my project, Visual Studio generated the EventLogService class. I need to create an instance of this service and call either of the 2 available methods - eventaction and get_events methods. In my project, I will be using the event_action method. Based from the above display, this method requires an ActionRequestType object as a parameter.

Finally, here's the code in VB.NET on how to instantiate the required parameters and call the appropriate method: The code that you use must be based on how the method that you want to call must be called.



'create CREATE ACTION TYPE
Dim actType As ACTIONType = New ACTIONType()
actType.NAME = "create"

'create ACTIONREQUESTType 
Dim actionRequesttype As ACTIONREQUESTType = New ACTIONREQUESTType()
actionRequesttype.ACTION = actType

'call Service
Dim eventLog As EventLogService = New EventLogService()

Dim response As ACTIONRESPONSEType = eventLog.event_action(actionRequesttype)

If Not response Is Nothing AndAlso Not response.ACTIONSTATUS Is Nothing _
   AndAlso Not response.ACTIONSTATUS.PARAM Is Nothing _
   AndAlso response.ACTIONSTATUS.PARAM.Count > 0 _
   Then
   result = response.ACTIONSTATUS.PARAM(0).VALUE
End If