Skip to content

Instantly share code, notes, and snippets.

@cowwoc
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowwoc/5cfcc58edc3b1a8aee40 to your computer and use it in GitHub Desktop.
Save cowwoc/5cfcc58edc3b1a8aee40 to your computer and use it in GitHub Desktop.
webpackJsonp([3],{
/***/ 0:
/***/ function(module, exports, __webpack_require__) {
var Preconditions = __webpack_require__(6);
var Authentications = __webpack_require__(9);
var LoginPanel = __webpack_require__(15);
var Form = __webpack_require__(14);
var ForbiddenException = __webpack_require__(16);
var RequestScope = __webpack_require__(12);
var Utilities = __webpack_require__(13);
var AssertionException = __webpack_require__(18);
var Q = __webpack_require__(4);
var $ = __webpack_require__(3);
var Stripe = __webpack_require__(5);
__webpack_require__(17);
// WORKAROUND: https://github.com/webpack/webpack/issues/754
//require("sugar");
__webpack_require__(29);
__webpack_require__(31);
// WORKAROUND: https://github.com/webpack/webpack/issues/536
__webpack_require__(33);
__webpack_require__(34);
__webpack_require__(35);
/**
* Creates a new Cards HTML page.
*
* @class
* @property {ResourceTable} table a table of resources
* @property {Modal} addUserModal the modal used to add a resource
* @property {Modal} editUserModal the modal used to edit a resource
*/
function Cards()
{
"use strict";
Object.defineProperty(this, "$form",
{
value: this.createForm(),
enumerable: true
});
Object.defineProperty(this, "idToComponent",
{
value: {},
enumerable: true
});
this.idToComponent[this.$form.$form.attr("id")] = this.table;
var $popup = $("#security-code");
$popup.popup(
{
on: "manual",
popup: ".ui.popup",
hoverable: true,
delay:
{
show: 50,
hide: 5000
}
});
$("#security-code").hover(function()
{
$popup.popup("show");
}, function()
{
$popup.popup("hide");
});
}
/**
* @return {jQuery} a new form
*/
Cards.prototype.createForm = function()
{
"use strict";
// /**
// * Ensures that a field is either empty, or has a minimum length.
// *
// * @param {String} value a value
// * @param {String} min the minimum field length
// * @return {Boolean} true if the value is empty or has a length greater than {@code min}
// */
// $.fn.form.settings.rules.emptyOrMinLength = function(value, min)
// {
// if (value.trim().length === 0)
// return true;
// // Delegate to "length" rule
// return $.fn.form.settings.rules.length(value, min);
// };
// Validating credit card numbers: https://www.mint.com/blog/trends/credit-card-code-01202011/
var validationRules =
{
number:
{
identifier: "number",
rules:
[
{
type: "empty",
prompt: "Number must be specified"
},
{
type: "integer",
prompt: "Invalid number"
}
]
},
name:
{
identifier: "name",
rules:
[
{
type: "empty",
prompt: "Name must be specified"
}
]
},
"expiration-month":
{
identifier: "expiration-month",
rules:
[
{
type: "empty",
prompt: "Month must be specified"
},
{
type: "integer",
prompt: "Invalid number"
}
]
},
"expiration-year":
{
identifier: "expiration-year",
rules:
[
{
type: "empty",
prompt: "Year must be specified"
},
{
type: "integer",
prompt: "Invalid number"
}
]
},
cvc:
{
identifier: "cvc",
rules:
[
{
type: "empty",
prompt: "Security code must be specified"
},
{
type: "integer",
prompt: "Invalid number"
}
]
},
"address-line1":
{
identifier: "address-line1",
rules:
[
{
type: "empty",
prompt: "Address must be specified"
}
]
},
city:
{
identifier: "city",
rules:
[
{
type: "empty",
prompt: "City must be specified"
}
]
},
state:
{
identifier: "state",
rules:
[
{
type: "empty",
prompt: "Province smust be specified"
}
]
},
zipcode:
{
identifier: "zipcode",
rules:
[
{
type: "empty",
prompt: "Postal code must be specified"
}
]
},
country:
{
identifier: "country",
rules:
[
{
type: "empty",
prompt: "Country must be specified"
}
]
}
};
/**
* @return {Boolean} true to suppress the default form submission mechanism
*/
function onSubmit()
{
// jshint validthis:true
// Disable the submit button to prevent repeated clicks
$(this).prop("disabled", true);
Stripe.card.createToken(this.$form, function(status, response)
{
switch (status)
{
case 200:
break;
case 402:
throw new AssertionException("Failed to charge the card");
case 400:
case 401:
case 404:
case 500:
case 502:
case 503:
case 504:
/* falls through */
default:
throw new AssertionException("Unexpected response code: " + status);
}
// See https://stripe.com/docs/stripe.js
if (response.error)
{
// Show the errors on the form
this.$form.find("errors").text(response.error.message);
this.$form.find("button").prop("disabled", false);
return;
}
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form and submit
this.$form.append($("<input type='hidden' name='stripeToken'/>").val(token));
this.$form.get(0).submit();
});
// Prevent the form from submitting with the default action
return false;
}
return new Form($("#form"), validationRules, onSubmit.bind(this), function()
{
});
};
/**
* Invoked after the page's state changes.
*/
Cards.prototype.afterStateChanged = function()
{
"use strict";
history.replaceState(this.getState(), window.title, window.location.href);
};
/**
* @typedef {Object} CardsState
*
* @property {String} id the DOM id of the component that generated the state
* @property {Object} state the page's state
*/
/**
* @return {CardsState} the page's state
*/
Cards.prototype.getState = function()
{
"use strict";
return {
id: "main",
state:
{
form: this.$form.getState()
}
};
};
/**
* Sets the page's state.
*
* @param {CardsState} data the page's state
*/
Cards.prototype.setState = function(data)
{
"use strict";
Preconditions.requireThat(data, "data").isSet();
Preconditions.requireThat(data.id, "data.id").isSet();
Preconditions.requireThat(data.state, "data.state").isSet();
Preconditions.requireThat(data.state.form, "data.state.form").isSet();
this.$form.setState(data.state.form);
};
/**
* Invoked on page load.
*
* @return {Q} a Promise that returns {@code undefined} on success and {@code Error} on
* failure
*/
Cards.prototype.onLoad = function()
{
"use strict";
LoginPanel.configure();
return new Q();
};
/**
* Ensures that a field is either empty, or has a minimum length.
*
* @param {String} value a value
* @param {String} min the minimum field length
* @return {Boolean} true if the value is empty or has a length greater than {@code min}
*/
$.fn.form.settings.rules.emptyOrMinLength = function(value, min)
{
"use strict";
if (value.trim().length === 0)
return true;
// Delegate to "length" rule
return $.fn.form.settings.rules.length(value, min);
};
$(function()
{
"use strict";
Authentications.reload().then(function(authenticated)
{
if (!authenticated)
return;
RequestScope.csrfToken = $("input[name='" + Utilities.csrfToken + "']").val();
var page = new Cards();
return page.onLoad().then(function()
{
window.addEventListener("popstate", function()
{
// See /js/html/page/Authentications.prototype.loadState() for a detailed explanation of this
// function.
if (!history.state)
return;
Utilities.assert(history.state.id === "main", history.state.id);
page.setState(history.state);
});
// Set initial state
page.afterStateChanged();
}).catch(function(error)
{
if (error instanceof ForbiddenException)
alert(error.message);
else
{
alert("An unexpected error has occurred. See the Javascript console for more information.");
throw error;
}
});
}).done();
});
/***/ },
/***/ 5:
/***/ function(module, exports, __webpack_require__) {
module.exports = Stripe;
/***/ },
/***/ 17:
/***/ function(module, exports, __webpack_require__) {
(function initStripe()
{
"use strict";
var stripe = __webpack_require__(5);
stripe.setPublishableKey("pk_test_jfzppEs7QcWYsa4p3rBOoxSP");
})();
/***/ },
/***/ 31:
29,
/***/ 33:
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__.p + "cards//ssl.png"
/***/ },
/***/ 34:
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__.p + "cards//VisaCVC.png"
/***/ },
/***/ 35:
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__.p + "cards//AmericanExpressCID.png"
/***/ }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment