Finish Stripe App, Multimedia HTML




CS174

Chris Pollett

May 10,2017

Outline

Stripe Example - index.ejs

<!DOCTYPE html>
<html>
<head><title>Credit Card Test</title></head>
<body>
<form id="purchase-stuff-form" method="post" action="/charge">
<input type="hidden" id="credit-token"  name="credit_token" value="" />
<p><label for="amount">Amount:</label><input type="text" id="amount"
    size="2" name="amount" /></p>
<p><label for="card-number">Card Number:</label><input type="text"
    id="card-number" size="20" data-stripe='number'
    name="card-number" /></p>
<p><label for="cvc">CVC:</label><input type="text" id="cvc" size="4"
    data-stripe='cvc' name="cvc" /></p>
<p><label for="exp-month">Expiration Month:</label><input type="text"
    id="exp-month" size="2" data-stripe='exp-month' name="exp-month" /></p>
<p><label for="exp-year">Expiration Year:</label><input type="text"
    id="exp-year" size="2" data-stripe='exp-year' name="exp-year" /></p>
<p><input type="submit" id="purchase" name="Purchase" value="Purchase"></p>
</form>
<script>
function elt(id)
{
    return document.getElementById(id);
}
elt('purchase').onclick = function(event) {
    var purchase_form = elt('purchase-stuff-form');
    elt('purchase').disabled = true; // prevent additional clicks
    Stripe.card.createToken(purchase_form, tokenResponseHandler);
    event.preventDefault(); //prevent form submitting till get all clear
}
function tokenResponseHandler(status, response) 
{
    var purchase_form = elt('purchase-stuff-form');
    if (response.error) {
        alert(response.error.message);
        elt('purchase').disabled = false;
    } else {
        elt('credit-token').value = response.id;
        purchase_form.submit();
    }
}
</script>
<script src="https://js.stripe.com/v2/"  ></script>
<script>
Stripe.setPublishableKey('<%=PUBLISHABLE_KEY %>');
</script>
</body>
</html>

Stripe Example - message.ejs

<!DOCTYPE html>
<html>
<head><title>Credit Card Test - <%=message %></title></head>
<body>
<h1><%=message %></h1>
</body>
</html>

Stripe Example - config.js

var config = {
    "SECRET_KEY": "sk_test_key",
    "PUBLISHABLE_KEY": "pk_test_key",
    "CHARGE_URL": "https://api.stripe.com/v1/charges",
    "CHARGE_CURRENCY": "usd",
    "CHARGE_DESCRIPTION": "Buyer sees this on their statement",
    "CHARGE_USERAGENT": "CreditCardTester",
    "TIMEOUT": 20
}
module.exports = config;

Stripe Example - app.js

var express = require('express');

var body_parser = require('body-parser'); //to handle posted data
var path = require('path'); // for directory paths
var config = require(path.join(__dirname, 'config')); // has our keys
var request = require('request'); // to make backend requests to stripe

var app = express();
app.use(body_parser.urlencoded({extended: true}));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.get('/', function(req, res) {
  res.render('index', { 'PUBLISHABLE_KEY': config.PUBLISHABLE_KEY });
});
app.post('/charge', function(req, res) {
    /*here we use the request module to make a stripe request using
      the token we received from our form*/
    request.post({
        url:config.CHARGE_URL, 
        form: {
            //swipe charges in cents * 100 to convert to dollars
            "amount": req.body.amount * 100,
            "currency": config.CHARGE_CURRENCY,
            "source": req.body.credit_token,
            "description": config.CHARGE_DESCRIPTION
            },
        auth: {
            'user': config.SECRET_KEY,
            'pass': ''
            }
        },
        function(err, http_response, body) {
            stripe_result = JSON.parse(body);
            if (typeof stripe_result.status === 'undefined') {
                if (typeof stripe_result.message === 'undefined') {
                    res.render('message', { 'message': req.body.amount +
                        "charge did not do through!<br />" +
                        stripe_result.credit_message});
                }
            } else if (stripe_result.status == 'succeeded') {
                res.render('message', { 'message': req.body.amount +
                    "charged" });
            }
        }
    );
});
app.listen(8888, function () {
    console.log('Credit Server up!')
})

In-Class Exercise

Write a short Express app.js that listens to 8888. On a route /*foo*, it should output a validating HTML 5 page which is the result of interpolating, the value of the config.foo coming from an exported object from the module in config.js. On all other routes, it should output the value of config.goo.

Your project should use a middleware function to console.log the ip address of any request coming into your app.

Post your solutions to the May 10 In-Class Exercise Thread.

Multimedia and HTML 5

Mobile HTML 5

A Template HTML 5 Document with jQuery Mobile

<!DOCTYPE html>
<html> 
    <head> 
    <title>Hello HTML 5</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script></head> 
<body> 

<div data-role="page">

    <div data-role="header">
        <h1>My First HTML APP!</h1>
    </div><!-- /header -->

    <div data-role="content">    
        <p>This is an HTML 5 app.</p>        
    </div><!-- /content -->

</div><!-- /page -->

</body>
</html>

Hello HTML 5 app.

The Head of a Document

Remarks on the Body of our Example

Where to Find Examples

jQuery

Detecting Orientation Changes in HTML 5

Orientation Change Code

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Portrait or Landscape</title> 
    <meta id="myview" name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
    <script>
    $(function(){
        // Orientation Change
        // When orientation changes event is triggered
        // exposing an orientation property of either
        // landscape or portrait
        $('body').bind('orientationchange', function(event){
            changeOrientation(event.orientation)
        })

        // Change the style depending on orientation
        function changeOrientation(ori) {
            // Remove all classes
            $("#orientation").removeClass('portrait landscape');
            $("#orientation").addClass(ori);
            $("#orientation").html("<p>"+ori.toUpperCase()+"</p>");
        }

        $("body").trigger("orientationchange");
    })
    </script>
    <style>
    div.portrait {
      background-color: blue;
      width: 100%;
      height: 100%;
    }
    div.landscape {
      background-color: red;
      width: 100%;
      height: 100%;
    }
    p {
        font-size:24px;
        color:white;
    }
    </style>
</head> 
<body> 

<div data-role="page">
 
    <div data-role="header">
        <h1>Orientation</h1>
    </div><!-- /header -->
 
    <div data-role="content">
    <div id="orientation" class="portrait" ></div>
    </div><!-- /content -->
 
    <div data-role="footer">
        <h4>My Footer</h4>
    </div><!-- /footer -->
</div><!-- /page -->

</body>
</html>

HTML 5 Local Storage

Ajax in jQuery

Two-Dimensional Graphics in HTML

Canvas Tag

Touch Events HTML5