Sha256: 514fc36babf72247ad5ef92e25e0e2a7c7a212f383bb21111c0e2a9362ad9bc7

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

module Spree
  class Order < Spree::Base
    module Payments
      extend ActiveSupport::Concern
      included do
        # processes any pending payments and must return a boolean as it's
        # return value is used by the checkout state_machine to determine
        # success or failure of the 'complete' event for the order
        #
        # Returns:
        #
        # - true if all pending payments processed successfully
        #
        # - true if a payment failed, ie. raised a GatewayError
        #   which gets rescued and converted to TRUE when
        #   :allow_checkout_gateway_error is set to true
        #
        # - false if a payment failed, ie. raised a GatewayError
        #   which gets rescued and converted to FALSE when
        #   :allow_checkout_on_gateway_error is set to false
        #
        def process_payments!
          process_payments_with(:process!)
        end

        def authorize_payments!
          process_payments_with(:authorize!)
        end

        def capture_payments!
          process_payments_with(:purchase!)
        end

        def unprocessed_payments
          payments.select { |payment| payment.checkout? }
        end

        private

        def process_payments_with(method)
          # Don't run if there is nothing to pay.
          return if payment_total >= total
          # Prevent orders from transitioning to complete without a successfully processed payment.
          raise Core::GatewayError.new(Spree.t(:no_payment_found)) if unprocessed_payments.empty?

          unprocessed_payments.each do |payment|
            break if payment_total >= total

            payment.public_send(method)

            if payment.completed?
              self.payment_total += payment.amount
            end
          end
        rescue Core::GatewayError => e
          result = !!Spree::Config[:allow_checkout_on_gateway_error]
          errors.add(:base, e.message) and return result
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solidus_core-1.0.1 app/models/spree/order/payments.rb
solidus_core-1.0.0 app/models/spree/order/payments.rb
solidus_core-1.0.0.rc2 app/models/spree/order/payments.rb
solidus_core-1.0.0.rc1 app/models/spree/order/payments.rb
solidus_core-1.0.0.pre3 app/models/spree/order/payments.rb
solidus_core-1.0.0.pre2 app/models/spree/order/payments.rb