Sha256: 9a36321dbb8afd7a3be0091d433501965263de949e1b926fc828ea1e5a712c79

Contents?: true

Size: 1.11 KB

Versions: 27

Compression:

Stored size: 1.11 KB

Contents

module Workarea
  class Checkout
    class CollectPayment
      delegate :payment, to: :@checkout

      def initialize(checkout)
        @checkout = checkout
        @order = checkout.order
      end

      def valid?
        if balance > 0
          payment.errors.add(
            :base,
            I18n.t('workarea.payment.insufficient_payment', balance: balance)
          )

          return false
        end

        true
      end

      def balance
        @order.total_price - payment.tendered_amount
      end

      def purchase
        return true if @order.total_price.zero?
        return false unless valid?

        payment.send(action, checkout: @checkout)
      end

      def action
        # TODO deprecated, remove in v3.6
        return 'purchase!' if Workarea.config.auto_capture

        if @order.items.all?(&:shipping?)
          Workarea.config.checkout_payment_action[:shipping]
        elsif @order.items.any?(&:shipping?)
          Workarea.config.checkout_payment_action[:partial_shipping]
        else
          Workarea.config.checkout_payment_action[:no_shipping]
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
workarea-core-3.5.6 app/models/workarea/checkout/collect_payment.rb
workarea-core-3.5.5 app/models/workarea/checkout/collect_payment.rb
workarea-core-3.5.4 app/models/workarea/checkout/collect_payment.rb
workarea-core-3.5.3 app/models/workarea/checkout/collect_payment.rb
workarea-core-3.5.2 app/models/workarea/checkout/collect_payment.rb
workarea-core-3.5.1 app/models/workarea/checkout/collect_payment.rb
workarea-core-3.5.0 app/models/workarea/checkout/collect_payment.rb