Sha256: e216c003a50d3914c996fcf649401ea623c37b90cb967db0144b5dbbf4cc242d

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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?(&:requires_shipping?)
          Workarea.config.checkout_payment_action[:shipped]
        elsif @order.items.any?(&:requires_shipping?)
          Workarea.config.checkout_payment_action[:mixed]
        else
          Workarea.config.checkout_payment_action[:not_shipped]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workarea-core-3.5.0.beta.1 app/models/workarea/checkout/collect_payment.rb