diff --git a/src/index.html b/src/index.html index fd2abe5..4a6a57d 100644 --- a/src/index.html +++ b/src/index.html @@ -21,5 +21,11 @@ + + + + diff --git a/src/js/app.js b/src/js/app.js index 6be0b73..1373f47 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -156,9 +156,9 @@ define(function(require) { }, createOrder: function() { - var order = this._generateOrder(); - - celeryClient.createOrder(order, this.handleOrder); + this._generateOrder(function(order) { + celeryClient.createOrder(order, this.handleOrder); + }.bind(this)); }, handleOrder: function(err, res) { @@ -275,63 +275,92 @@ define(function(require) { this.$form.find('.Celery-OrderSummary-price--total').text(total); }, - _generateOrder: function() { - var $form = this.$form; - var order = { - buyer: {}, - shipping_address: {}, - line_items: [], - payment_source: { - card: { - number: '', - exp_month: '', - exp_year: '', - cvc: '' + _generateOrder: function(callback) { + this._createStripeToken(this._getCard(), function(err, token) { + if (err) { + return this.handleError(err); + } + + var order = { + buyer: {}, + shipping_address: {}, + line_items: [], + payment_source: { + card: { + stripe_token: '', + } } + }; + + // Set stripe token + if (token) { + order.payment_source.card.stripe_token = token.id; } - }; - order.user_id = shop.data.user_id; - order.buyer.email = this._getFieldValue('email'); + order.user_id = shop.data.user_id; + order.buyer.email = this._getFieldValue('email'); + + // Set shipping + order.shipping_address.country = this._getCountry(); - // Card - var card = order.payment_source.card; + // Set taxes + if (config.features.taxes) { + order.shipping_address.zip = this._getZip(); + } - card.number = this._getFieldValue('card_number'); - card.cvc = this._getFieldValue('cvc'); + // Set coupon + if (config.features.coupons) { + var couponCode = this._getCouponCode(); - // Card Expiry - var expiry = this._getFieldValue('expiry'); - var expiryParts = expiry.split('/'); + if (couponCode) { + order.discount_codes = [couponCode]; + } + } - card.exp_month = expiryParts[0].trim(); - card.exp_year = expiryParts[1].trim(); + // Set line item + var lineItem = { + product_id: shop.data.product._id, + quantity: this._getQuantity() + }; - // Shipping - order.shipping_address.country = this._getCountry(); + order.line_items.push(lineItem); - if (config.features.taxes) { - order.shipping_address.zip = this._getZip(); + return callback(order) + }.bind(this)); + }, + + _getCard: function() { + if (this._getTotal() === 0) { + return null; } - // Coupon - if (config.features.coupons) { - var couponCode = this._getCouponCode(); + // Card Expiry + var expiry = this._getFieldValue('expiry'); + var expiryParts = expiry.split('/'); - if (couponCode) { - order.discount_codes = [couponCode]; - } + return { + number: this._getFieldValue('card_number'), + cvc: this._getFieldValue('cvc'), + exp_month: expiryParts[0].trim(), + exp_year: expiryParts[1].trim(), + address_zip: this._getFieldValue('billing_zip'), } + }, - // Line Item - var lineItem = { - product_id: shop.data.product._id, - quantity: this._getQuantity() - }; + _createStripeToken: function(card, callback) { + var Stripe = window.Stripe || {}; - order.line_items.push(lineItem); + if (!card) { + return callback(null, null); + } - return order; + Stripe.card.createToken(card, function(status, response) { + if (response.error) { + return callback(response.error); + } + + return callback(null, response); + }); }, _getDiscount: function() {