Sha256: 40056854bfb7de271f5bf7973a1fc891658613a851b032a035561ba8fb9ca034

Contents?: true

Size: 1.96 KB

Versions: 15

Compression:

Stored size: 1.96 KB

Contents

module Spree
  module Api
    class PaymentsController < Spree::Api::BaseController
      respond_to :json

      before_filter :find_order
      before_filter :find_payment, :only => [:show, :authorize, :purchase, :capture, :void, :credit]

      def index
        @payments = @order.payments.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
        respond_with(@payments)
      end

      def new
        @payment_methods = Spree::PaymentMethod.where(:environment => Rails.env)
        respond_with(@payment_method)
      end

      def create
        @payment = @order.payments.build(params[:payment])
        if @payment.save
          respond_with(@payment, :status => 201, :default_template => :show)
        else
          invalid_resource!(@payment)
        end
      end

      def show
        respond_with(@payment)
      end

      def authorize
        perform_payment_action(:authorize)
      end

      def capture
        perform_payment_action(:capture)
      end

      def purchase
        perform_payment_action(:purchase)
      end

      def void
        perform_payment_action(:void_transaction)
      end

      def credit
        if params[:amount].to_f > @payment.credit_allowed
          render "spree/api/payments/credit_over_limit", :status => 422
        else
          perform_payment_action(:credit, params[:amount])
        end
      end

      private

      def find_order
        @order = Order.find_by_number(params[:order_id])
        authorize! :read, @order
      end

      def find_payment
        @payment = @order.payments.find(params[:id])
      end

      def perform_payment_action(action, *args)
        authorize! action, Payment

        begin
          @payment.send("#{action}!", *args)
          respond_with(@payment, :default_template => :show)
        rescue Spree::Core::GatewayError => e
          @error = e.message
          render "spree/api/errors/gateway_error", :status => 422
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
spree_api-1.3.5 app/controllers/spree/api/payments_controller.rb
spree_api-1.3.4 app/controllers/spree/api/payments_controller.rb
spree_api-2.0.5 app/controllers/spree/api/payments_controller.rb
spree_api-2.0.4 app/controllers/spree/api/payments_controller.rb
spree_api-2.0.3 app/controllers/spree/api/payments_controller.rb
spree_api-1.3.3 app/controllers/spree/api/payments_controller.rb
spree_api-2.0.2 app/controllers/spree/api/payments_controller.rb
spree_api-2.0.1 app/controllers/spree/api/payments_controller.rb
spree_api-2.0.0 app/controllers/spree/api/payments_controller.rb
spree_api-2.0.0.rc1 app/controllers/spree/api/payments_controller.rb
spree_api-1.3.2 app/controllers/spree/api/payments_controller.rb
spree_api-1.3.1 app/controllers/spree/api/payments_controller.rb
spree_api-1.3.0 app/controllers/spree/api/payments_controller.rb
spree_api-1.3.0.rc2 app/controllers/spree/api/payments_controller.rb
dup_spree_api-1.3.0.rc1 app/controllers/spree/api/payments_controller.rb