UNPKG

crowdstart-checkout

Version:

One Click Checkout for Crowdstart

199 lines (173 loc) 7.48 kB
// Generated by CoffeeScript 1.10.0 var Events, Screen, Shipping, analytics, crowdcontrol, input, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; crowdcontrol = require('crowdcontrol'); Events = crowdcontrol.Events; Screen = require('./screen'); analytics = require('../../utils/analytics'); input = require('../../utils/input'); Shipping = (function(superClass) { var obj; extend(Shipping, superClass); function Shipping() { return Shipping.__super__.constructor.apply(this, arguments); } Shipping.prototype.tag = 'shipping'; Shipping.prototype.title = 'Shipping Address'; Shipping.prototype.html = require('../../../templates/screens/shipping.jade'); Shipping.prototype.taxRates = null; Shipping.prototype.inputConfigs = [input('order.shippingAddress.line1', '123 Street', 'input required'), input('order.shippingAddress.line2', '123 Apt', 'input'), input('order.shippingAddress.city', 'City', 'input required'), input('order.shippingAddress.state', 'State', 'state-select required'), input('order.shippingAddress.postalCode', 'Zip/Postal Code', 'input postalRequired'), input('order.shippingAddress.country', '', 'country-select required')]; Shipping.prototype.updateTaxRate = function() { var j, len, ref, taxRate; this.model.order.taxRate = 0; ref = this.taxRates; for (j = 0, len = ref.length; j < len; j++) { taxRate = ref[j]; if ((taxRate.city != null) && (this.model.order.shippingAddress.city != null) && taxRate.city.toLowerCase() !== this.model.order.shippingAddress.city.toLowerCase()) { continue; } if ((taxRate.state != null) && (this.model.order.shippingAddress.state != null) && taxRate.state.toLowerCase() !== this.model.order.shippingAddress.state.toLowerCase()) { continue; } if ((taxRate.country != null) && (this.model.order.shippingAddress.country != null) && taxRate.country.toLowerCase() !== this.model.order.shippingAddress.country.toLowerCase()) { continue; } this.model.order.taxRate = taxRate.taxRate; break; } return riot.update(); }; Shipping.prototype.events = ( obj = {}, obj["" + Events.Input.Set] = function() { return this.updateTaxRate(); }, obj ); Shipping.prototype.js = function(opts) { var ref; Shipping.__super__.js.apply(this, arguments); return this.taxRates = (ref = this.model.taxRates) != null ? ref : []; }; Shipping.prototype.show = function() { analytics.track('Viewed Checkout Step', { step: 2 }); return $('body').animate({ scrollTop: $('screen-manager').first().offset().top - 50 }, 500); }; Shipping.prototype._submit = function() { this.screenManagerObs.trigger(Events.Confirm.Lock); this.screenManagerObs.trigger(Events.Confirm.Error, ''); if (this.model.order.type === 'paypal') { return this.submitPaypal(); } else { return this.submitStripe(); } }; Shipping.prototype.submitPaypal = function() { var data; data = { user: this.model.user, order: this.model.order, payment: this.model.payment }; return this.client.checkout.paypal(data).then((function(_this) { return function(order) { _this.payKey = order.payKey; analytics.track('Completed Checkout Step', { step: 2 }); if (_this.model.test.paypal) { return window.location.href = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=" + _this.payKey; } else { return window.location.href = "https://www.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=" + _this.payKey; } }; })(this))["catch"]((function(_this) { return function(err) { console.log("shipping submit Error: " + err); _this.screenManagerObs.trigger(Events.Confirm.Error, 'Sorry, Paypal is unavailable. Please try again later.'); _this.screenManagerObs.trigger(Events.Confirm.Unlock); return _this.screenManagerObs.trigger(Events.Checkout.Done); }; })(this)); }; Shipping.prototype.submitStripe = function() { var data; data = { user: this.model.user, order: this.model.order, payment: this.model.payment }; return this.client.checkout.charge(data).then((function(_this) { return function(order) { var coupon, i, item, j, len, options, ref, ref1, ref2, ref3; coupon = _this.model.order.coupon || {}; _this.model.order = order; analytics.track('Completed Checkout Step', { step: 2 }); options = { orderId: _this.model.order.id, total: parseFloat(_this.model.order.total / 100), shipping: parseFloat(_this.model.order.shipping / 100), tax: parseFloat(_this.model.order.tax / 100), discount: parseFloat(_this.model.order.discount / 100), coupon: coupon.code || '', currency: _this.model.order.currency, products: [] }; ref = _this.model.order.items; for (i = j = 0, len = ref.length; j < len; i = ++j) { item = ref[i]; options.products[i] = { id: item.productId, sku: item.productSlug, name: item.productName, quantity: item.quantity, price: parseFloat(item.price / 100) }; } analytics.track('Completed Order', options); if (((ref1 = _this.model.analytics) != null ? (ref2 = ref1.pixels) != null ? ref2.checkout : void 0 : void 0) != null) { analytics.track((ref3 = _this.model.analytics.pixels) != null ? ref3.checkout : void 0); } if (_this.model.referralProgram != null) { _this.client.referrer.create({ userId: _this.model.order.userId, orderId: _this.model.order.orderId, program: _this.model.referralProgram }).then(function(referrer) { return _this.model.referrerId = referrer.id; })["catch"](function(err) { return console.log("new referralProgram Error: " + err); }); } _this.screenManagerObs.trigger(Events.Screen.Next); _this.screenManagerObs.trigger(Events.Confirm.Unlock); _this.screenManagerObs.trigger(Events.Checkout.Done); return riot.update(); }; })(this))["catch"]((function(_this) { return function(err) { var res; console.log("shipping submit Error: " + err); res = _this.client.lastResponse.responseText; if (res.error.code === 'card-declined') { _this.screenManagerObs.trigger(Events.Confirm.Error, 'Sorry, your card was declined. Please check your payment information.'); } else { _this.screenManagerObs.trigger(Events.Confirm.Error, 'Sorry, unable to complete your transaction. Please try again later.'); } _this.screenManagerObs.trigger(Events.Confirm.Unlock); return _this.screenManagerObs.trigger(Events.Checkout.Done); }; })(this)); }; return Shipping; })(Screen); Shipping.register(); module.exports = Shipping; //# sourceMappingURL=shipping.js.map