def admin_refund
      return if !user_is_allowed('orders', 'edit')
    
      response = Caboose::StdClass.new({
        'refresh' => nil,
        'error' => nil,
        'success' => nil
      })
    
      order = Order.find(params[:id])
    
      if order.financial_status != 'captured'
        response.error = "This order hasn't been captured yet, you will need to void instead"
      else
        if PaymentProcessor.refund(order)
          order.update_attributes(
            :financial_status => 'refunded',
            :status => 'cancelled'
          )
          
          response.success = 'Order refunded successfully'
        else
          response.error = 'Error refunding order'
        end
        
        #if order.calculate_net < (order.amount_discounted || 0) || PaymentProcessor.refund(order)
        #  order.financial_status = 'refunded'
        #  order.status = 'refunded'
        #  order.save
        #  
        #  if order.discounts.any?
        #    discount = order.discounts.first
        #    amount_to_refund = order.calculate_net < order.amount_discounted ? order.calculate_net : order.amount_discounted
        #    discount.update_attribute(:amount_current, amount_to_refund + discount.amount_current)
        #  end
        #  
        #  response.success = "Order refunded successfully"
        #else
        #  response.error = "Error refunding order."
        #end
      end
    
      render json: response
      
      # return if !user_is_allowed('orders', 'edit')
      #     
      # response = Caboose::StdClass.new({
      #   'refresh' => nil,
      #   'error' => nil,
      #   'success' => nil
      # })
      #     
      # order = Order.find(params[:id])
      #     
      # if order.financial_status != 'captured'
      #   response.error = "This order hasn't been captured yet, you will need to void instead"
      # else
      #   if PaymentProcessor.refund(order)
      #     order.financial_status = 'refunded'
      #     order.status = 'refunded'
      #     order.save
      #     
      #     # Add the variant quantities ordered back
      #     order.cancel
      #     
      #     response.success = "Order refunded successfully"
      #   else
      #     response.error = "Error refunding order."
      #   end
      # end
      #     
      # render :json => response
    end
def admin_refund
      return if !user_is_allowed('orders', 'edit')
    
      response = Caboose::StdClass.new({
        'refresh' => nil,
        'error' => nil,
        'success' => nil
      })
    
      order = Order.find(params[:id])
    
      if order.financial_status != 'captured'
        response.error = "This order hasn't been captured yet, you will need to void instead"
      else
        if PaymentProcessor.refund(order)
          order.update_attributes(
            :financial_status => 'refunded',
            :status => 'cancelled'
          )
          
          response.success = 'Order refunded successfully'
        else
          response.error = 'Error refunding order'
        end
        
        #if order.calculate_net < (order.amount_discounted || 0) || PaymentProcessor.refund(order)
        #  order.financial_status = 'refunded'
        #  order.status = 'refunded'
        #  order.save
        #  
        #  if order.discounts.any?
        #    discount = order.discounts.first
        #    amount_to_refund = order.calculate_net < order.amount_discounted ? order.calculate_net : order.amount_discounted
        #    discount.update_attribute(:amount_current, amount_to_refund + discount.amount_current)
        #  end
        #  
        #  response.success = "Order refunded successfully"
        #else
        #  response.error = "Error refunding order."
        #end
      end
    
      render json: response
      
      # return if !user_is_allowed('orders', 'edit')
      #     
      # response = Caboose::StdClass.new({
      #   'refresh' => nil,
      #   'error' => nil,
      #   'success' => nil
      # })
      #     
      # order = Order.find(params[:id])
      #     
      # if order.financial_status != 'captured'
      #   response.error = "This order hasn't been captured yet, you will need to void instead"
      # else
      #   if PaymentProcessor.refund(order)
      #     order.financial_status = 'refunded'
      #     order.status = 'refunded'
      #     order.save
      #     
      #     # Add the variant quantities ordered back
      #     order.cancel
      #     
      #     response.success = "Order refunded successfully"
      #   else
      #     response.error = "Error refunding order."
      #   end
      # end
      #     
      # render :json => response
    end
GET /admin/orders/:id/refund