Wednesday, December 20, 2017

Find a Penny, Pick It Up...

A coworker of mine was working on a TypeScript/JavaScript application that ran in a web browser. The application would display a dollar amount that was about to be charged to a customer's credit card. When the customer acknowledged the amount, the customer would be taken to a page hosted by the payment processor to actually perform the transaction. This seemed to be working correctly, but our QA tester found that certain dollar amounts, like $8.95, would show in our application as $8.95, but would be displayed as $8.94 on the payment processor's page.

The payment processors required monetary amounts to be sent in as cents, rather than dollars. For example, $54.12 would be sent as 5412. The developer had written something similar to the following to do this value conversion:

1
2
const amount: number = 8.95;
const paymentProcessorValue: number = amount * 100;

The problem ended up being caused by the way JavaScript does math. JavaScript uses floating-point arithmetic to do calculations. In this particular case, 8.95 * 100 was not returning the expected 895, but instead was returning 894.9999999999999. The payment processor truncated the value at the decimal point, therefore it interpreted this value as $8.94, not $8.95.

The issue was corrected by rounding the value before sending it on to the payment processor.

1
2
const amount: number = 8.95;
const paymentProcessorValue: number = Math.round(amount * 100);

Example

Saturday, May 20, 2017

How to install Java, SDKMAN!, Groovy, and Grails on the Windows Subsystem for Linux

This article explains how to install various development tools using the new Windows Subsystem for Linux that is available on Windows 10.

Install Windows Subsystem for Linux

In order to use the Windows Subsystem for Linux, it must be installed. The following link explains how to install this functionality in Windows 10:
https://msdn.microsoft.com/en-us/commandline/wsl/install_guide

Update Bash on Ubuntu on Windows 

Once installed, it is a good idea to update to the latest version of the Ubuntu binaries. Issuing the following commands from the Bash shell will accomplish this:

me@COMPUTER:~$ sudo apt-get update 
me@COMPUTER:~$ sudo apt-get upgrade 
Source: https://msdn.microsoft.com/en-us/commandline/wsl/faq#how-do-i-update-bash-on-ubuntu-on-windows

Install zip and unzip

Before you can install the Java JDK or any of the other tools, zip and unzip will need to be installed.

Install unzip

To install unzip:
me@COMPUTER:~sudo apt-get install unzip

To verify unzip was installed correctly:
me@COMPUTER:~unzip -v

Install zip

To install zip:
me@COMPUTER:~sudo apt-get install zip

To verify zip was installed correctly:
me@COMPUTER:~zip -v


Install the latest Java JDK

A nice shell script that can be used to install the Java JDK on the Windows Subsystem for Linux can be found on the following Stack Overflow article:
http://stackoverflow.com/questions/36478741/installing-oracle-jdk-on-windows-subsystem-for-linux#41072208

The URL for the latest version of the Java JDK can be found at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

To verify which version of Java was installed:
me@COMPUTER:~$ java -version 


Install SDKMAN! 

SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems. Instructions on how to install SDKMAN! can be found on the SDKMAN! website:
https://sdkman.io/install

Once SDKMAN! is installed, it can be used to install other tools, like Groovy or Grails.

Install Groovy

To install Groovy, issue the following command from the Bash shell:
me@COMPUTER:~sdk install groovy

Install Grails

To install Grails, issue the following command from the Bash shell:
me@COMPUTER:~sdk install grails

Once grails is installed, the steps outlined in the Grails documentation can be followed to create a “Hello World” application:
http://docs.grails.org/latest/guide/gettingStarted.html#creatingAnApplication

Note: I had to create the helloworld directory and navigate to it before I issued the create-app command:
me@COMPUTER:~mkdir helloworld
me@COMPUTER:~cd helloworld
me@COMPUTER:~/helloworldgrails create-app helloworld

Once I created my Grails application and the HelloController, I was able to start my application from either the Bash shell or the Windows Command Line using the following commands:

Bash Shell

me@COMPUTER:~/helloworld./gradlew bootRun
:compileJava NO-SOURCE
:compileGroovy
:buildProperties
:processResources
:classes
:findMainClass
:bootRun
Grails application running at http://localhost:8080 in environment: development

> Building 85% > :bootRun

Windows Command Line 

C:\...\helloworld>gradlew bootRun
:compileJava NO-SOURCE
:compileGroovy
:buildProperties
:processResources
:classes
:findMainClass
:bootRun
Grails application running at http://localhost:8080 in environment: development

> Building 85% > :bootRun



Friday, May 05, 2017

Inspect HTTP Requests with RequestBin

I came across this handy website for inspecting HTTP requests, called RequestBin (http://requestbin.net/ https://requestb.in/). RequestBin creates a URL that will collect requests made to it and lets you review the requests in a human-friendly way. RequestBin is a great tool for debugging what your HTTP application is sending. The following are some screenshots of the data RequestBin provides: