Sha256: 740dd85d2e9846258bdf52a1a75d3023bce87b96f685ad7682181d56722a94b2

Contents?: true

Size: 1.55 KB

Versions: 28

Compression:

Stored size: 1.55 KB

Contents

module Workarea
  class CancelOrder
    attr_reader :order, :params

    def initialize(order, params = {})
      @order = order
      @params = params || {}
    end

    def restock?
      params[:restock].to_s =~ /true/i
    end

    def refund?
      params[:refund].to_s =~ /true/i
    end

    def update_fulfillment?
      params[:fulfillment].to_s =~ /true/i
    end

    def restock
      transaction = Inventory::Transaction.captured_for_order(order.id)
      transaction.rollback unless transaction.blank?
    end

    def refund
      result = Payment::Refund.new(payment: payment, amounts: refund_amounts)
      result.complete!
      result
    end

    def update_fulfillment
      cancellations = order.items.map do |item|
        { 'id' => item.id.to_s, 'quantity' => item.quantity }
      end

      fulfillment.cancel_items(cancellations)
    end

    def perform
      restock if restock?
      refund if refund?
      update_fulfillment if update_fulfillment?

      order.cancel.tap { |canceled| update_metrics if canceled }
    end

    private

    def payment
      @payment ||= Payment.find_or_initialize_by(id: order.id)
    end

    def refund_amounts
      payment.tenders.reduce({}) do |memo, tender|
        memo[tender.id] = tender.captured_amount - tender.refunded_amount
        memo
      end
    end

    def fulfillment
      @fulfillment ||= Fulfillment.find_or_initialize_by(id: order.id)
    end

    def update_metrics
      SaveOrderCancellationMetrics.perform_async(
        order.id,
        occured_at: order.canceled_at
      )
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.6 app/services/workarea/cancel_order.rb
workarea-core-3.5.5 app/services/workarea/cancel_order.rb
workarea-core-3.5.4 app/services/workarea/cancel_order.rb
workarea-core-3.5.3 app/services/workarea/cancel_order.rb
workarea-core-3.5.2 app/services/workarea/cancel_order.rb
workarea-core-3.5.1 app/services/workarea/cancel_order.rb
workarea-core-3.5.0 app/services/workarea/cancel_order.rb
workarea-core-3.5.0.beta.1 app/services/workarea/cancel_order.rb