Saturday, May 21, 2016

JSON using JavaScriptSerializer in C#

I had to work with JSON recently to integrate the Payeezy gateway to our website. First time I used the dynamic type in C#. Below is the code I used to create the JSON:



 dynamic payload = new
        {
            merchant_ref = "Payeezy Token test",
            transaction_type = "purchase",
            method = "token",
            amount = amt,
            currency_code = "USD",
            token = new
            {
                token_type = "FDToken",
                token_data = new
                {
                    type = cardType,
                    value = transarmorTokenValue,
                    cardholder_name = cardHolder,
                    exp_date = expMonth+expYear
                }
            }
        };
        jsonString = JSONHelper.ToJSON(payload);



In order to parse the response JSON:

StreamReader responseStream = new StreamReader(webResponse.GetResponseStream());
responseString = responseStream.ReadToEnd();
 var responseValues = JSONHelper.ToDictionaryStringObject(responseString);
  int bankResponse = Convert.ToInt32(responseValues["bank_resp_code"]);



    
The JSONHelper class:

public static class JSONHelper
{
    public static string ToJSON(this object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }


  public Dictionary<string, object> ToDictionaryStringObject(string s)
{
       var json = new JavaScriptSerializer();
       return json.Deserialize<Dictionary<string, object>>(s);
}

}

Thursday, May 19, 2016

2 applications instead of 1

These days whenever you build a complex website such as an insurance/benefits enrollment system you plan for 2 applications instead of 1.

The first application is obvious. It is the system in which your customers would use to enroll into their benefits.

The second application is an administration system for the benefits enrollment system. So that non technical employees can configure the system as per the business requirements and your input is not needed for every small change.

In the self enrollment system we have at our company, I have so far built the following admin features:

1. HTML text-boxes where the user can configure popups to be shown on different pages of the self enrollment system.

2. Make a user admin on the website with special privileges.

3. Delete all future deductions (selected plan amount) of users of a particular group. This feature is useful when the sales team has to give demos of our website.

4. Enable or disable a product on the self enrollment system.

5. Changing whether current deduction (last year's deduction amount) is visible on top of every product screen per group.

I'll keep adding features as our product evolves. 

Saturday, May 14, 2016

Is the field 'tobacco user' redundant?

I work in the insurance industry right now and we have a concept of rate tables which we display the rates that is applicable to a customer as per his demographics such as age and tobacco usage. 

For one particular product we have two rate tables depending on whether the customer is a tobacco user or not. The appropriate rates are shown to customer on the screen depending on his answer to the question about his tobacco usage.



My confusion is how to store the fact whether the customer is a tobacco user or not. I know we should not create redundant columns in a database table  Every time the customer makes a selection and enroll's in a policy his action is recorded in the transactions table. Would it be redundant to create a column in this transactions table that records whether the customer is a tobacco user? Because the rate that the customer selected is recorded and this can be matched to the corresponding rate table to identify whether the user is a tobacco user or not.

But then later, when we have to create reports about customer demographics. In order to identify the tobacco usage status of a customer we would need to execute two queries and if the 'tobacco user' column is present we would need only one.

What is your opinion?

Sunday, May 8, 2016

How to change the width of popup after initialization?


I set the width of the popup as auto when initialized in JQUERY script file which is shared with several pages on my website:


 $("#modal-dialog").dialog({
        autoOpen: false,
        modal: true,
        resizeable: false,
        position: { my: 'top', at: 'top+100' },
        height: 'auto',
        width: 'auto',
        buttons: {
            Close: function () {
                $(this).dialog("close");
            }
        },
        show: {
            effect: "drop",
            duration: 400,
            direction: "up"
        },
        hide: {
            effect: "drop",
            duration: 400,
            direction: "down"
        }
    });


And on a particular page I put the following code in the HTML. I am filling the contents in the server code

  <div id="modal-dialog" title="">
                                <div id="dialogContent" runat="server" clientidmode="Static">
                                    </div>
                                    </div>
    
                          
Now try as I may to resize the width it wouldn't work.

 var dialogW = $(window).width() * 0.7;
                        $("#modal-dialog").css("width", dialogW);
                     
                     
However all I needed to do was add this line and it worked:

  $("#dialogContent").css("width", dialogW);
                   
Which means that you need to re-size the content in order to re-size the popup. This way your popup can be of different widths on different pages.