summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2015-03-16 11:19:18 (EDT)
committer P. J. McDermott <pj@pehjota.net>2015-03-16 11:19:18 (EDT)
commit0ffbb91c4cfbe8d279b7409a91b95dce57a38d25 (patch)
treeb777a7f055ac154da36042b46f70822b8ab5c87a
parentd9e53698d50c534ee33512c55ac3531481c32b72 (diff)
downloadepirts.js-0ffbb91c4cfbe8d279b7409a91b95dce57a38d25.zip
epirts.js-0ffbb91c4cfbe8d279b7409a91b95dce57a38d25.tar.gz
epirts.js-0ffbb91c4cfbe8d279b7409a91b95dce57a38d25.tar.bz2
Epirts.card.createToken(): Implement
-rw-r--r--epirts.js60
1 files changed, 59 insertions, 1 deletions
diff --git a/epirts.js b/epirts.js
index 9a6334d..eb6e4df 100644
--- a/epirts.js
+++ b/epirts.js
@@ -35,10 +35,68 @@ var Stripe = Epirts = (function() {
var pub = {};
var _key = undefined;
+ pub.endpoint = 'https://api.stripe.com/v1';
+
pub.card = (function() {
var pub = {};
- pub.createToken = function(data, callback) {
+ var _properties = ['number', 'exp_month', 'exp_year',
+ 'cvc', 'name', 'address_line1', 'address_line2',
+ 'address_city', 'address_state', 'address_zip',
+ 'address_country'];
+ var _cb_time = (new Date()).getTime();
+
+ pub.createToken = function(data, arg2, arg3) {
+ var callback;
+ var amount;
+ var where;
+ var script;
+ var func;
+ var url;
+ var i;
+ var prop;
+
+ if (!data || typeof data !== 'object') {
+ throw new Error("Invalid card");
+ } else if (_key === undefined) {
+ throw new Error("Publishable key not set");
+ }
+ if (typeof arg2 === 'function') {
+ callback = arg2;
+ } else if (typeof arg2 === 'number' ||
+ typeof arg2 === 'string') {
+ amount = arg2;
+ callback = arg3;
+ }
+
+ where = document.getElementsByTagName('script')[0];
+ script = document.createElement('script');
+ script.type = 'text/javascript';
+ script.async = true;
+
+ func = 'sjsonp' + ++_cb_time;
+ window[func] = function(result) {
+ callback(result);
+ script.parentNode.removeChild(script);
+ }
+
+ url = Epirts.endpoint + '/tokens?';
+ for (i = 0; i < _properties.length; ++i) {
+ prop = _properties[i];
+ if (data.hasOwnProperty(prop)) {
+ url += 'card[' + prop + ']=' +
+ data[prop] + '&';
+ }
+ }
+ if (amount !== undefined) {
+ url += 'amount=' + amount + '&';
+ }
+ url += 'key=' + _key;
+ url += '&callback=' + func;
+ url += '&_method=POST';
+ script.src = url;
+
+ where.parentNode.insertBefore(script, where);
};
pub.validateCardNumber = function(number) {