Skip to content

Instantly share code, notes, and snippets.

@jdcauley
Created April 9, 2015 19:42
Show Gist options
  • Save jdcauley/15bd3626bf41bc2883ae to your computer and use it in GitHub Desktop.
Save jdcauley/15bd3626bf41bc2883ae to your computer and use it in GitHub Desktop.
bloblocks
var Blocks = new Object();
function loadScript(url, callback){
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function(){
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
Blocks.init = function(key){
Blocks.key = key;
loadScript('https://js.stripe.com/v2/', function(){
var init = new XMLHttpRequest();
init.responseType = 'json';
var url = 'http://localhost:1337/inbound/init';
init.open("get", url, true);
init.onreadystatechange = function() {
if(init.readyState == 4 && init.status == 200) {
Blocks.stripeKey = init.response.stripePublicKey;
Stripe.setPublishableKey(Blocks.stripeKey);
}
}
init.send();
});
}
Blocks.req = function(form, callback){
var req = new XMLHttpRequest();
req.responseType = 'json';
var url = 'http://localhost:1337/inbound/new' + '/' + Blocks.key;
var formData = new FormData(form);
req.open("post", url, true);
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
return callback(null, req.response);
}
}
req.send(formData);
};
Blocks.mailer = function(message, callback){
}
Blocks.pay = function(charge, callback){
}
Blocks.upload = function(form, callback){
}
function stop(e){
e.preventDefault();
};
function createReq(){
Blocks.req(document.getElementById('main-form'), function(err, res){
console.log(res);
});
}
Blocks.init('654786452134');
console.log(Blocks);
document.getElementById('submit').addEventListener('click', stop, false);
document.getElementById('submit').addEventListener('click', createReq, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment