Class: SolidusShipstation::Api::ShipmentSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_shipstation/api/shipment_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_id:) ⇒ ShipmentSerializer

Returns a new instance of ShipmentSerializer.



8
9
10
# File 'lib/solidus_shipstation/api/shipment_serializer.rb', line 8

def initialize(store_id:)
  @store_id = store_id
end

Instance Attribute Details

#store_idObject (readonly)

Returns the value of attribute store_id.



6
7
8
# File 'lib/solidus_shipstation/api/shipment_serializer.rb', line 6

def store_id
  @store_id
end

Instance Method Details

#call(shipment) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/solidus_shipstation/api/shipment_serializer.rb', line 12

def call(shipment)
  order = shipment.order

  state = case shipment.state
          when 'ready'
            'awaiting_shipment'
          when 'shipped'
            'shipped'
          when 'pending'
            if ::Spree::Config.require_payment_to_ship && !shipment.order.paid?
              'awaiting_payment'
            else
              'on_hold'
            end
          when 'canceled'
            'cancelled'
          end

  {
    orderNumber: shipment.number,
    orderKey: shipment.number,
    orderDate: order.completed_at.iso8601,
    paymentDate: order.payments.find(&:valid?)&.created_at&.iso8601,
    orderStatus: state,
    customerId: order.user&.id,
    customerUsername: order.email,
    customerEmail: order.email,
    billTo: serialize_address(order.bill_address),
    shipTo: serialize_address(order.ship_address),
    items: shipment.line_items.map(&method(:serialize_line_item)),
    shippingAmount: shipment.cost,
    paymentMethod: 'Credit Card',
    advancedOptions: {
      storeId: store_id,
    },
  }
end