diff options
-rw-r--r-- | epirts.js | 60 |
1 files changed, 59 insertions, 1 deletions
@@ -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) { |