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.27 app/services/workarea/cancel_order.rb
workarea-core-3.5.26 app/services/workarea/cancel_order.rb
workarea-core-3.5.25 app/services/workarea/cancel_order.rb
workarea-core-3.5.23 app/services/workarea/cancel_order.rb
workarea-core-3.5.22 app/services/workarea/cancel_order.rb
workarea-core-3.5.21 app/services/workarea/cancel_order.rb
workarea-core-3.5.20 app/services/workarea/cancel_order.rb
workarea-core-3.5.19 app/services/workarea/cancel_order.rb
workarea-core-3.5.18 app/services/workarea/cancel_order.rb
workarea-core-3.5.17 app/services/workarea/cancel_order.rb
workarea-core-3.5.16 app/services/workarea/cancel_order.rb
workarea-core-3.5.15 app/services/workarea/cancel_order.rb
workarea-core-3.5.14 app/services/workarea/cancel_order.rb
workarea-core-3.5.13 app/services/workarea/cancel_order.rb
workarea-core-3.5.12 app/services/workarea/cancel_order.rb
workarea-core-3.5.11 app/services/workarea/cancel_order.rb
workarea-core-3.5.10 app/services/workarea/cancel_order.rb
workarea-core-3.5.9 app/services/workarea/cancel_order.rb
workarea-core-3.5.8 app/services/workarea/cancel_order.rb
workarea-core-3.5.7 app/services/workarea/cancel_order.rb