Sha256: ecce7c2b892456f8b1283caa40d03b55d0651bf181971ca137d386c489de0c99

Contents?: true

Size: 940 Bytes

Versions: 1

Compression:

Stored size: 940 Bytes

Contents

# frozen_string_literal: true

require 'forwardable'

module SolidusEasypost
  class ReturnAuthorization
    extend Forwardable

    attr_reader :return_authorization

    def_delegators :@return_authorization, :stock_location, :order, :inventory_units

    def initialize(return_authorization)
      @return_authorization = return_authorization
    end

    def easypost_shipment
      @easypost_shipment ||= ::EasyPost::Shipment.create(
        from_address: stock_location.easypost_address,
        to_address: order.ship_address.easypost_address,
        parcel: build_parcel,
        is_return: true
      )
    end

    def return_label(rate)
      easypost_shipment.buy(rate) unless easypost_shipment.postage_label
      easypost_shipment.postage_label
    end

    private

    def build_parcel
      total_weight = inventory_units.joins(:variant).sum(:weight)
      ::EasyPost::Parcel.create weight: total_weight
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_easypost-2.0.0 app/models/solidus_easypost/return_authorization.rb