app/assets/javascripts/sprangular/services/checkout.coffee in sprangular-0.0.1 vs app/assets/javascripts/sprangular/services/checkout.coffee in sprangular-0.1.0

- old
+ new

@@ -1,42 +1,64 @@ Sprangular.service "Checkout", ($http, $q, _, Env, Account, Cart) -> service = savePromo: (code) -> params = - order: - coupon_code: code + coupon_code: code - @put(params) + Cart.current.loading = true - update: -> - order = Cart.current - card = order.creditCard + config = + ignoreLoadingIndicator: true + headers: + 'X-Spree-Order-Token': Cart.current.token + deferred = $q.defer() + + $http.put("/spree/api/orders/#{Cart.current.number}/apply_coupon_code", $.param(params), config) + .success (response) -> + Cart.load(response.order) + + if response.error + deferred.reject(response) + else + deferred.resolve(response) + + .error (response) -> + response.error ||= "Coupon code #{code} not found." + deferred.reject(response) + + deferred.promise + + setAddresses: -> + order = Cart.current params = order: - use_billing: order.shipToBillAddress - coupon_code: order.couponCode - ship_address_attributes: order.actualShippingAddress().serialize() - bill_address_attributes: order.billingAddress.serialize() - 'order[payments_attributes][][payment_method_id]': @_findPaymentMethodId() - payment_source: {} + ship_address_attributes: order.actualBillingAddress().serialize() + bill_address_attributes: order.shippingAddress.serialize() + state: 'address' - if order.shippingRate - params['order[shipments_attributes][][id]'] = order.shipment.id - params['order[shipments_attributes][][selected_shipping_rate_id]'] = order.shippingRate.id + @put(params, ignoreLoadingIndicator: true) - @put(params) + setDelivery: -> + order = Cart.current + params = + 'order[shipments_attributes][][id]': order.shipment.id + 'order[shipments_attributes][][selected_shipping_rate_id]': order.shippingRate.id + state: 'delivery' - complete: -> + @put(params, ignoreLoadingIndicator: true) + + setPayment: -> order = Cart.current card = order.creditCard paymentMethodId = @_findPaymentMethodId() params = - complete: true 'order[payments_attributes][][payment_method_id]': paymentMethodId + 'order[existing_card]': '' + 'state': 'payment' order: {} payment_source: {} if card.id params.order.existing_card = card.id @@ -49,21 +71,29 @@ sourceParams.year = card.year sourceParams.name = order.billingAddress.fullName() params.payment_source[paymentMethodId] = sourceParams - @put(params) - .success (data) -> + @put(params, ignoreLoadingIndicator: true) + + complete: -> + order = Cart.current + + @put() + .then (data) -> Cart.lastOrder = Sprangular.extend(data, Sprangular.Order) service.trackOrder(Cart.lastOrder) - Account.reload().then -> + if Account.isGuest Cart.init() + else + Account.reload().then -> + Cart.init() trackOrder: (order) -> - return unless ga + return if typeof(ga) is 'undefined' ga "ecommerce:addTransaction", id: order.number revenue: order.total shipping: order.shipTotal @@ -77,24 +107,35 @@ price: item.price quantity: item.quantity ga "ecommerce:send" - put: (params) -> - url = "/api/checkouts/#{Cart.current.number}/quick_update" - params = params ||= {} + put: (params={}, config={}) -> + order = Cart.current + url = "/spree/api/checkouts/#{order.number}" - config = - headers: - 'X-Spree-Order-Token': Cart.current.token + config.headers = + 'X-Spree-Order-Token': order.token - Cart.current.errors = null + order.errors = null + order.loading = true + deferred = $q.defer() + $http.put(url, $.param(params), config) .success (response) -> Cart.load(response) unless response.error + deferred.resolve(Cart.current) + .error (response) -> - Cart.errors(response.errors || response.exception) + if response.errors + Cart.errors(response.errors) + else if response.exception + Cart.errors(base: response.exception) + + deferred.reject(Cart.current) + + deferred.promise _findPaymentMethodId: -> paymentMethod = _.find Env.config.payment_methods, (method) -> method.name == 'Credit Card' alert('Payment method "Credit Card" not found') unless paymentMethod