Sha256: 13029849f16de91782dbeb059424e3cd7ee1524b5e8e9201e4876adc1e7d3cc8

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'stripe'

module Locomotive
  module Ecommerce
    class PurchaseController < ::Locomotive::Ecommerce::ApplicationController
      before_filter :authenticate_user!, except: [:do_new_purchase]

      def create
        @purchase = current_user_cart(self).purchase
        @purchase.shipping_info = params[:shipping_info]
        @purchase.shipping_method = params[:shipping_method]
        if @purchase.save
          redirect_to confirm_order_path
          return
        elsif @purchase.errors.any?
          flash_ar = []
          @purchase.errors.full_messages.each { |msg| flash_ar << msg }
          flash[:error] = flash_ar.join(', ')
        end

        redirect_to checkout_path
      end

      def self.complete(purchase_id, user, cart, stripeToken)
        purchase = Purchase.where(_id: purchase_id).first

        #Reset user cart
        purchase.cart.user_id = nil
        purchase.cart.save!
        new_cart = Cart.create
        new_cart.user_id = user.id
        new_cart.save!

        #complete purchase
        purchase.stripe_token = stripeToken
        purchase.complete
        purchase.completed = true
        purchase.user_id = user.id
        purchase.save!
        PurchaseMailer.purchase_confirmation(user, purchase).deliver
        after_purchase_hook(purchase, user)
      end

      private

      def self.after_purchase_hook(purchase, user)

        site = Thread.current[:site]
        cxt = site.plugin_object_for_id('ecommerce').js3_context
        cxt['user'] = user
        cxt['purchase'] = purchase
        last = cxt.eval(Engine.config_or_default('after_purchase_hook'))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotive_ecommerce_plugin-1.0.1 app/controllers/locomotive/ecommerce/purchase_controller.rb
locomotive_ecommerce_plugin-1.0.0 app/controllers/locomotive/ecommerce/purchase_controller.rb