From 0ffbb91c4cfbe8d279b7409a91b95dce57a38d25 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Mon, 16 Mar 2015 11:19:18 -0400 Subject: Epirts.card.createToken(): Implement --- 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) { -- cgit v0.9.1