Class: SpreeBoxnow::CreateVoucher

Inherits:
Object
  • Object
show all
Includes:
Spree::IntegrationsConcern
Defined in:
app/services/spree_boxnow/create_voucher.rb

Defined Under Namespace

Classes: VoucherError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shipment) ⇒ CreateVoucher

Returns a new instance of CreateVoucher.



7
8
9
10
# File 'app/services/spree_boxnow/create_voucher.rb', line 7

def initialize(shipment)
  @shipment = shipment
  @order    = shipment.order
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



5
6
7
# File 'app/services/spree_boxnow/create_voucher.rb', line 5

def order
  @order
end

#shipmentObject (readonly)

Returns the value of attribute shipment.



5
6
7
# File 'app/services/spree_boxnow/create_voucher.rb', line 5

def shipment
  @shipment
end

Instance Method Details

#callObject



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/spree_boxnow/create_voucher.rb', line 12

def call
  integration = store_integration('boxnow')
  raise VoucherError, 'BoxNow integration not configured' unless integration

  destination_id = shipment.['boxnow.destination_location_id']
  raise VoucherError, 'No BoxNow locker selected for this shipment' if destination_id.blank?

  address = shipment.address || order.ship_address
  cod     = order.payment_method&.cod_payment?

  attempt = (shipment.['boxnow.voucher_attempt'].to_i + 1)
  shipment.['boxnow.voucher_attempt'] = attempt
  order_number = attempt == 1 ? shipment.number : "#{shipment.number}-#{attempt}"

  params = {
    orderNumber:         order_number,
    invoiceValue:        order.total.to_s,
    paymentMode:         cod ? 'cod' : 'prepaid',
    amountToBeCollected: cod ? shipment.final_price_with_items.to_f.to_s : '0.00',
    origin: {
      locationId:    integration.preferred_origin_location_id,
      contactName:   integration.preferred_contact_name.presence || '',
      contactNumber: integration.preferred_contact_phone.presence || '',
      contactEmail:  integration.preferred_contact_email.presence || ''
    },
    destination: {
      locationId:    destination_id,
      contactName:   address.full_name,
      contactNumber: address.phone.to_s,
      contactEmail:  order.email.to_s
    },
    items: [{
      id:              '1',
      name:            'voucher',
      value:           '0.00',
      compartmentSize: parcel_size,
      weight:          shipment.item_weight.to_f.round
    }]
  }

  result = SpreeBoxnow::ApiClient.new.create_delivery_request(params)

  parcels = result['parcels'] || []
  raise VoucherError, 'BoxNow returned no parcels' if parcels.empty?

  {
    parcel_id:        parcels[0]['id'],
    child_parcel_ids: parcels[1..].map { |p| p['id'] }
  }
rescue SpreeBoxnow::ApiClient::ApiError => e
  raise VoucherError, e.message
end