Class: SecondAmendmentWholesale::Order

Inherits:
Base
  • Object
show all
Includes:
API
Defined in:
lib/second_amendment_wholesale/order.rb

Constant Summary collapse

ENDPOINTS =
{
  billing_address: 'customers/me/billingAddress'.freeze,
  cart: 'carts/mine'.freeze,
  customer_note: 'carts/mine/set-customer-note'.freeze,
  items: 'carts/mine/items'.freeze,
  licence: "carts/licence".freeze,
  payment_information: 'carts/mine/payment-information'.freeze,
  po_number: 'carts/mine/set-po-number'.freeze,
  regions: 'directory/countries/US'.freeze,
  shipping_information: 'carts/mine/shipping-information'.freeze,
  shipping_methods: 'carts/mine/estimate-shipping-methods'.freeze,
}

Constants included from API

API::ROOT_API_URL

Instance Method Summary collapse

Methods included from API

#delete_request, #get_request, #post_request, #put_request

Constructor Details

#initialize(options = {}) ⇒ Order

Returns a new instance of Order.



19
20
21
22
23
24
25
26
27
# File 'lib/second_amendment_wholesale/order.rb', line 19

def initialize(options = {})
  requires!(options, :token)

  @options = options
  @headers = [ 
    *auth_header(@options[:token]),
    *content_type_header('application/json'),
  ].to_h
end

Instance Method Details

#add_cart_item(item) ⇒ Object



51
52
53
54
55
# File 'lib/second_amendment_wholesale/order.rb', line 51

def add_cart_item(item)
  endpoint = ENDPOINTS[:items]

  post_request(endpoint, item, @headers)
end

#add_customer_note(customer_note) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/second_amendment_wholesale/order.rb', line 121

def add_customer_note(customer_note)
  body = {
    "customerNote": {
      "customer_note": customer_note
    }
  }

  put_request(ENDPOINTS[:customer_note], body, @headers)
end

#add_po_number(po_number, quote_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/second_amendment_wholesale/order.rb', line 57

def add_po_number(po_number, quote_id)
  endpoint = ENDPOINTS[:po_number]

  body = {
    "cartId": "#{quote_id}",
    "poNumber": {
        "poNumber": "#{po_number}"
    }
  }

 put_request(endpoint, body, @headers)
end

#add_shipping_information(address_info) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/second_amendment_wholesale/order.rb', line 92

def add_shipping_information(address_info)
  endpoint = ENDPOINTS[:shipping_information]

  body = {
    "address_information": address_info
  }

  post_request(endpoint, body, @headers)
end

#create_cartObject



35
36
37
38
39
# File 'lib/second_amendment_wholesale/order.rb', line 35

def create_cart
  endpoint = ENDPOINTS[:cart]

  post_request(endpoint, {}, @headers)
end

#delete_cart_items(cart_items) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/second_amendment_wholesale/order.rb', line 41

def delete_cart_items(cart_items)
  responses = []

  cart_items.each do |item|
    responses << delete_request([ENDPOINTS[:items], item[:item_id]].join("/"), @headers).body
  end

  responses.all?
end

#get_billing_addressObject



86
87
88
89
90
# File 'lib/second_amendment_wholesale/order.rb', line 86

def get_billing_address
  endpoint = ENDPOINTS[:billing_address]

  get_request(endpoint, @headers)
end

#get_cartObject



29
30
31
32
33
# File 'lib/second_amendment_wholesale/order.rb', line 29

def get_cart
  endpoint = ENDPOINTS[:cart]

  get_request(endpoint, @headers)
end

#get_increment_id(order_id) ⇒ Object



117
118
119
# File 'lib/second_amendment_wholesale/order.rb', line 117

def get_increment_id(order_id)
  get_request("orders/#{order_id}", @headers)
end

#get_regionsObject



70
71
72
73
74
# File 'lib/second_amendment_wholesale/order.rb', line 70

def get_regions
  endpoint = ENDPOINTS[:regions]

  get_request(endpoint, @headers)[:available_regions]
end

#get_shipping_methods(address) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/second_amendment_wholesale/order.rb', line 76

def get_shipping_methods(address)
  endpoint = ENDPOINTS[:shipping_methods]

  body = {
    "address": address
  }

  post_request(endpoint, body, @headers)
end

#submit_payment(payment_method) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/second_amendment_wholesale/order.rb', line 102

def submit_payment(payment_method)
  endpoint = ENDPOINTS[:payment_information]

  body = {
    "payment_method": {
      "method": payment_method,
      "extension_attributes": {
        "agreement_ids": get_agreement_ids
      }
    }
  }
  
  post_request(endpoint, body, @headers)
end