CS174
Chris Pollett
May 10,2017
<!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>
<!DOCTYPE html> <html> <head><title>Credit Card Test - <%=message %></title></head> <body> <h1><%=message %></h1> </body> </html>
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;
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!') })
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.
<!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>
<ul data-role="listview" data-inset="true" data-filter="true"> <li><a href="#">Red</a></li> <li><a href="#">Green</a></li> <li><a href="#">Blue</a></li> <li><a href="#">Violet</a></li> <li><a href="#">Ultra-Violet</a></li> </ul>
document.getElementById("yo").onclick = function() {alert("yo");}in jQuery we could do:
$("#yo").onclick = function() {alert("yo");}
$(document).ready(handler) // or $(handler)
<!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> <head> <title>Local Storage Test</title> </head> <body> <script type="text/javascript"> if (localStorage) { document.write("<h1>Local Storage is Working!</h1>"); testStorage(); } else { document.write("<h1>Local Storage is Unavailable!</h1>"); } function testStorage() { myvalue = localStorage.getItem("mykey1"); myvalue2 = localStorage["mykey2"]; if(myvalue) { document.write("<p>Found stored value for mykey1 ="+ myvalue+"</p>"); localStorage.removeItem("mykey1"); //can use localStorage.clear(); to get rid of everything } else { localStorage.setItem("mykey1", "hello"); } if(myvalue2) { document.write("<p>Found stored value for mykey2 ="+ myvalue2+"</p>"); } else { localStorage["mykey2"] = "yo"; } document.write("<h1>List all that's stored after update</h1>"); for (i=0; i<=localStorage.length-1; i++) { key = localStorage.key(i); value = localStorage.getItem(key); document.write("<p>("+key+", "+value+")</p>"); } } </script> </body> </html>
function downloadDelayed() { $.ajax({ url : "myscript.php", success : function (data) { $("#contentArea").html(data); } }); } setTimeout("downloadDelayed()", 1000);
<canvas id="drawpad" ></canvas> <style type="text/css"> #drawpad { width: 200px; height: 100px; background-color: light-gray; } </style>
<script type="text/javascript"> drawpad_object = document.getElementById("drawpad"); drawpad_context = drawpad_object.getContext("2d"); drawpad_context.strokeStyle = "blue"; drawpad_context.strokeRect(10,10,50,50); drawpad_context.fillStyle = "green"; drawpad_context.font = "32pt Arial"; max_width = 100; drawpad_context.rotate(3.14/3); //pi/3 drawpad_context.fillText("hello", 80, -10, max_width); </script>
<canvas id="drawpad" ontouchstart="startHandler(event);" ontouchmove="moveHandler(event);" ontouchend="endHandler(event);" ontouchcancel="cancelHandler(event);" ></canvas>
drawpad_object.addEventListener("touchstart", startHandler2, false);