UNPKG

crowdstart-checkout

Version:

One Click Checkout for Crowdstart

384 lines (330 loc) 10.2 kB
// Generated by CoffeeScript 1.10.0 var Checkout, Crowdstart, Events, Views, Widget, analytics, crowdcontrol, head, requestAnimationFrame, riot, select2Css, style, theme; theme = require('./utils/theme'); analytics = require('./utils/analytics'); riot = require('riot'); window.riot = riot; crowdcontrol = require('crowdcontrol'); requestAnimationFrame = crowdcontrol.utils.shim.requestAnimationFrame; Events = crowdcontrol.Events; Crowdstart = require('crowdstart.js'); require('./events'); Views = require('./views'); Widget = Views.Widget; require('../vendor/js/select2'); select2Css = require('../vendor/css/select2'); head = document.head || document.getElementsByTagName('head')[0]; style = document.createElement('STYLE'); style.type = 'text/css'; if (style.styleSheet) { style.styleSheet.cssText = select2Css; } else { style.appendChild(document.createTextNode(select2Css)); } head.appendChild(style); Checkout = (function() { Checkout.prototype.key = ''; Checkout.prototype.order = null; Checkout.prototype.payment = null; Checkout.prototype.user = null; Checkout.prototype.itemUpdateQueue = null; Checkout.prototype.obs = null; Checkout.prototype.model = null; Checkout.prototype.config = null; Checkout.prototype.thankyou = null; Checkout.prototype.theme = null; Checkout.prototype.analytics = null; Checkout.prototype.referralProgram = null; Checkout.prototype.taxRates = null; Checkout.prototype.reset = true; Checkout.prototype.waits = 0; Checkout.prototype.currentScript = null; Checkout.prototype.script = ['payment', 'shipping', 'thankyou']; function Checkout(key, opts) { var id, match, modal, q, qs, ref, ref1, ref2, ref3, search, widget, widgetTag; this.key = key; if (opts == null) { opts = {}; } this.client = new Crowdstart.Api({ key: this.key, endpoint: opts != null ? (ref = opts.test) != null ? ref.endpoint : void 0 : void 0 }); search = /([^&=]+)=?([^&]*)/g; q = window.location.href.split('?')[1]; qs = {}; if (q != null) { while ((match = search.exec(q))) { qs[decodeURIComponent(match[1])] = decodeURIComponent(match[2]); } } this.config = { showPromoCode: true, termsUrl: '', callToActions: [], processors: { stripe: true, paypal: true } }; if (opts.config != null) { this.config = $.extend(this.config, opts.config); } this.user = opts.user || {}; this.order = {}; if (opts.order != null) { this.order = $.extend(this.order, opts.order); } this.order.items = []; this.order.currency = ((ref1 = opts.config) != null ? ref1.currency : void 0) || this.order.currency || 'usd'; this.order.taxRate = ((ref2 = opts.config) != null ? ref2.taxRate : void 0) || this.order.taxRate || 0; this.order.shippingRate = ((ref3 = opts.config) != null ? ref3.shippingRate : void 0) || this.order.shippingRate || 0; this.order.shippingAddress = { country: 'us' }; this.order.discount = 0; if (this.config.processors.stripe) { this.order.type = 'stripe'; } else if (this.config.processors.paypal) { this.order.type = 'paypal'; } if (qs.referrer != null) { this.order.referrerId = qs.referrer || this.order.referrerId; } this.payment = { account: { _type: 'stripe' } }; this.itemUpdateQueue = []; this.thankyou = { header: 'Thank You!', body: 'Check Your Email For The Order Confirmation.' }; if (opts.thankyou != null) { this.thankyou = $.extend(this.thankyou, opts.thankyou); } this.theme = {}; if (opts.theme != null) { this.theme = $.extend(this.theme, opts.theme); } this.test = {}; if (opts.test != null) { this.test = $.extend(this.test, opts.test); } this.analytics = {}; if (opts.analytics != null) { this.analytics = $.extend(this.analytics, opts.analytics); } this.referralProgram = opts.referralProgram; this.taxRates = opts.taxRates || []; this.model = { user: this.user, order: this.order, payment: this.payment, config: this.config, thankyou: this.thankyou, test: this.test, analytics: this.analytics, referralProgram: this.referralProgram, taxRates: this.taxRates, scripts: { basic: this.script } }; this.obs = {}; riot.observable(this.obs); modal = document.createElement('MODAL'); widgetTag = Widget.prototype.tag; widget = document.createElement(widgetTag.toUpperCase()); widget.setAttribute('model', '{ model }'); widget.setAttribute('obs', '{ obs }'); widget.setAttribute('client', '{ client }'); modal.appendChild(widget); document.body.appendChild(modal); theme.setTheme(this.theme); riot.mount('modal', { obs: this.obs, model: this.model, client: this.client }); this.obs.on(Events.Checkout.Done, (function(_this) { return function() { return _this.reset = true; }; })(this)); if (window.location.hash === '#checkoutsuccess') { this.obs.trigger(Events.Screen.UpdateScript, this.script, 2); this.reset = false; this.open(); id = setInterval(function() { return $(window).resize(); }, 50); setTimeout((function(_this) { return function() { clearInterval(id); riot.update(); return _this.reset = true; }; })(this), 1000); } else { this.obs.trigger(Events.Screen.UpdateScript, this.script); } } Checkout.prototype.open = function() { var item, j, len, ref; if (this.reset) { this.obs.trigger(Events.Screen.UpdateScript, this.script); this.reset = false; } this.obs.trigger(Events.Modal.Open); this.obs.trigger(Events.Modal.DisableClose); setTimeout((function(_this) { return function() { return _this.obs.trigger(Events.Modal.EnableClose); }; })(this), 600); ref = this.order.items; for (j = 0, len = ref.length; j < len; j++) { item = ref[j]; analytics.track('Added Product', { id: item.productId, sku: item.productSlug, name: item.productName, quantity: item.quantity, price: parseFloat(item.price / 100) }); } analytics.track('Viewed Checkout Step', { step: 1 }); $('.crowdstart-modal-target').css('top', $(window).scrollTop() + 'px'); return false; }; Checkout.prototype.one = function() { return this.obs.one.apply(this.obs, arguments); }; Checkout.prototype.on = function() { return this.obs.on.apply(this.obs, arguments); }; Checkout.prototype.off = function() { return this.obs.off.apply(this.obs, arguments); }; Checkout.prototype.update = function() { var items; if (this.waits === 0) { items = this.order.items; this.order.items = []; riot.update(); this.order.items = items; riot.update(); this.obs.trigger(Events.Checkout.Update, { user: this.user, order: this.order, config: this.config }); return riot.update(); } }; Checkout.prototype.setConfig = function(config) { this.config = config; return this.update(); }; Checkout.prototype.setUser = function(user) { if (user == null) { user = {}; } if (user == null) { return; } this.user = $.extend(this.user, user); this.model.user = this.user; return this.update(); }; Checkout.prototype.setItem = function(id, quantity) { this.itemUpdateQueue.push([id, quantity]); if (this.itemUpdateQueue.length === 1) { return this._setItem(); } }; Checkout.prototype._setItem = function() { var i, id, item, j, k, len, len1, quantity, ref, ref1, ref2; if (this.itemUpdateQueue.length === 0) { this.update(); return; } ref = this.itemUpdateQueue.shift(), id = ref[0], quantity = ref[1]; if (quantity === 0) { ref1 = this.order.items; for (i = j = 0, len = ref1.length; j < len; i = ++j) { item = ref1[i]; if (item.productId === id || item.productSlug === id) { break; } } if (i < this.order.items.length) { this.order.items.splice(i, 1); } this._setItem(); return; } ref2 = this.order.items; for (i = k = 0, len1 = ref2.length; k < len1; i = ++k) { item = ref2[i]; if (item.productId !== id && item.productSlug !== id) { continue; } item.quantity = quantity; this._setItem(); return; } this.order.items.push({ id: id, quantity: quantity }); this.waits++; return this.client.product.get(id).then((function(_this) { return function(product) { var l, len2, ref3; _this.waits--; ref3 = _this.order.items; for (i = l = 0, len2 = ref3.length; l < len2; i = ++l) { item = ref3[i]; if (product.id === item.id || product.slug === item.id) { _this._updateItem(product, item); break; } } return _this._setItem(); }; })(this))["catch"]((function(_this) { return function(err) { _this.waits--; console.log("setItem Error: " + err); return _this._setItem(); }; })(this)); }; Checkout.prototype._updateItem = function(product, item) { item.id = void 0; item.productId = product.id; item.productSlug = product.slug; item.productName = product.name; item.price = product.price; return item.listPrice = product.listPrice; }; return Checkout; })(); Checkout.countries = require('./data/countries'); Checkout.currencies = require('./data/currencies'); if (window.Crowdstart != null) { window.Crowdstart.Checkout = Checkout; } else { window.Crowdstart = { Checkout: Checkout }; } if (typeof module !== "undefined" && module !== null) { module.exports = Checkout; } //# sourceMappingURL=index.js.map