Sha256: 43bdb9132f532415f6c1c02313753d9968d9cc35ec0243402b45f411ba088e2e

Contents?: true

Size: 1.61 KB

Versions: 69

Compression:

Stored size: 1.61 KB

Contents

module Spree
  module Checkout
    class AddStoreCredit
      prepend Spree::ServiceModule::Base

      def call(order:, amount: nil)
        @order = order
        return failed unless @order

        remaining_total = amount ? [amount, @order.outstanding_balance].min : @order.outstanding_balance

        ApplicationRecord.transaction do
          @order.payments.store_credits.where(state: :checkout).map(&:invalidate!)
          apply_store_credits(remaining_total) if @order.user&.store_credits&.any?
        end

        @order.reload.payments.store_credits.valid.any? ? success(@order) : failure(@order)
      end

      private

      def apply_store_credits(remaining_total)
        payment_method = Spree::PaymentMethod::StoreCredit.available.first
        raise 'Store credit payment method could not be found' unless payment_method

        @order.user.store_credits.order_by_priority.each do |credit|
          break if remaining_total.zero?
          next if credit.amount_remaining.zero?

          amount_to_take = store_credit_amount(credit, remaining_total)
          create_store_credit_payment(payment_method, credit, amount_to_take)
          remaining_total -= amount_to_take
        end
      end

      def create_store_credit_payment(payment_method, credit, amount)
        @order.payments.create!(
          source: credit,
          payment_method: payment_method,
          amount: amount,
          state: 'checkout',
          response_code: credit.generate_authorization_code
        )
      end

      def store_credit_amount(credit, total)
        [credit.amount_remaining, total].min
      end
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
spree_core-4.2.0.rc3 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.13 app/services/spree/checkout/add_store_credit.rb
spree_core-4.0.6 app/services/spree/checkout/add_store_credit.rb
spree_core-4.2.0.rc2 app/services/spree/checkout/add_store_credit.rb
spree_core-3.7.13 app/services/spree/checkout/add_store_credit.rb
spree_core-4.0.5 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.12 app/services/spree/checkout/add_store_credit.rb
spree_core-3.7.12 app/services/spree/checkout/add_store_credit.rb
spree_core-4.2.0.rc1 app/services/spree/checkout/add_store_credit.rb
spree_core-4.0.4 app/services/spree/checkout/add_store_credit.rb
spree_core-3.7.11 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.11 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.10 app/services/spree/checkout/add_store_credit.rb
spree_core-4.2.0.beta app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.9 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.8 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.7 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.6 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.5 app/services/spree/checkout/add_store_credit.rb
spree_core-4.1.4 app/services/spree/checkout/add_store_credit.rb