Sha256: 3538f3f5d53fad5e0dcccb67ae3eb93fb1657080bbd543a6d37e315933bfdda7

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module SolidusBacktracs
  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 SolidusBacktracs.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

8 entries across 8 versions & 2 rubygems

Version Path
solidus_bactracs-3.0.0 lib/solidus_backtracs/shipment_notice.rb
solidus_backtracs-2.2.6 lib/solidus_backtracs/shipment_notice.rb
solidus_backtracs-2.2.5 lib/solidus_backtracs/shipment_notice.rb
solidus_backtracs-2.2.4 lib/solidus_backtracs/shipment_notice.rb
solidus_backtracs-2.2.3 lib/solidus_backtracs/shipment_notice.rb
solidus_backtracs-2.2.2 lib/solidus_backtracs/shipment_notice.rb
solidus_backtracs-2.2.1 lib/solidus_backtracs/shipment_notice.rb
solidus_backtracs-2.2.0 lib/solidus_backtracs/shipment_notice.rb