Sha256: aeebf15b3776ab206a1efdde456690085dc67974bec3bba7b5f015a887b12891

Contents?: true

Size: 1.25 KB

Versions: 15

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module SolidusBactracs
  class ShipmentNotice
    attr_reader :shipment_number, :shipment_tracking

    class << self
      def from_payload(params)
        new(
          shipment_number: params[:order_number],
          shipment_tracking: params[:tracking_number],
        )
      end
    end

    def initialize(shipment_number:, shipment_tracking:)
      @shipment_number = shipment_number
      @shipment_tracking = shipment_tracking
    end

    def apply
      unless shipment
        raise ShipmentNotFoundError, shipment
      end

      process_payment
      ship_shipment

      shipment
    end

    private

    def shipment
      @shipment ||= ::Spree::Shipment.find_by(number: shipment_number)
    end

    def process_payment
      return if shipment.order.paid?

      unless SolidusBactracs.configuration.capture_at_notification
        raise OrderNotPaidError, shipment.order
      end

      shipment.order.payments.pending.each do |payment|
        payment.capture!
      rescue ::Spree::Core::GatewayError
        raise PaymentError, payment
      end
    end

    def ship_shipment
      shipment.update!(tracking: shipment_tracking)
      shipment.ship! if shipment.can_ship?
      shipment.order.recalculate
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
solidus_bactracs-4.0.1 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-4.0.0 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.5.2 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.5.1 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.5.0 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.4.0 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.3.2 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.3.1 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.3.0 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.2.2 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.2.1 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.2.0 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.1.2 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.1.1 lib/solidus_bactracs/shipment_notice.rb
solidus_bactracs-3.1.0 lib/solidus_bactracs/shipment_notice.rb