Class: Apruve::Order

Inherits:
ApruveObject show all
Defined in:
lib/apruve/resources/order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApruveObject

#logger, logger, #to_hash, #to_json

Constructor Details

#initialize(params) ⇒ Order

Returns a new instance of Order.



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/apruve/resources/order.rb', line 58

def initialize(params)
  super
  # hydrate line items if appropriate
  if @order_items.nil?
    @order_items = []
  elsif @order_items.is_a?(Array) && @order_items.first.is_a?(Hash)
    hydrated_items = []
    @order_items.each do |item|
      hydrated_items << Apruve::OrderItem.new(item)
    end
    @order_items = hydrated_items
  end
end

Instance Attribute Details

#accepts_payment_termsObject

Returns the value of attribute accepts_payment_terms.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def accepts_payment_terms
  @accepts_payment_terms
end

#accepts_payments_viaObject

Returns the value of attribute accepts_payments_via.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def accepts_payments_via
  @accepts_payments_via
end

#amount_centsObject

Returns the value of attribute amount_cents.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def amount_cents
  @amount_cents
end

#created_atObject

Returns the value of attribute created_at.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def created_at
  @created_at
end

#currencyObject

Returns the value of attribute currency.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def currency
  @currency
end

#default_payment_methodObject

Returns the value of attribute default_payment_method.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def default_payment_method
  @default_payment_method
end

#expire_atObject

Returns the value of attribute expire_at.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def expire_at
  @expire_at
end

#final_state_atObject

Returns the value of attribute final_state_at.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def final_state_at
  @final_state_at
end

#finalize_on_createObject

Returns the value of attribute finalize_on_create.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def finalize_on_create
  @finalize_on_create
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def id
  @id
end

#invoice_on_createObject

Returns the value of attribute invoice_on_create.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def invoice_on_create
  @invoice_on_create
end

Returns the value of attribute links.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def links
  @links
end

#merchant_idObject

Returns the value of attribute merchant_id.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def merchant_id
  @merchant_id
end

#merchant_order_idObject

Returns the value of attribute merchant_order_id.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def merchant_order_id
  @merchant_order_id
end

#order_itemsObject

Returns the value of attribute order_items.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def order_items
  @order_items
end

#payment_termObject

Returns the value of attribute payment_term.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def payment_term
  @payment_term
end

#po_numberObject

Returns the value of attribute po_number.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def po_number
  @po_number
end

#secure_hashObject

Returns the value of attribute secure_hash.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def secure_hash
  @secure_hash
end

#shipping_centsObject

Returns the value of attribute shipping_cents.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def shipping_cents
  @shipping_cents
end

#shopper_idObject

Returns the value of attribute shopper_id.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def shopper_id
  @shopper_id
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def status
  @status
end

#tax_centsObject

Returns the value of attribute tax_cents.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def tax_cents
  @tax_cents
end

#updated_atObject

Returns the value of attribute updated_at.



3
4
5
# File 'lib/apruve/resources/order.rb', line 3

def updated_at
  @updated_at
end

Class Method Details

.cancel!(id) ⇒ Object



46
47
48
49
50
# File 'lib/apruve/resources/order.rb', line 46

def self.cancel!(id)
  response = Apruve.post("orders/#{id}/cancel")
  logger.debug response.body
  Order.new(response.body)
end

.finalize!(id) ⇒ Object



31
32
33
34
35
# File 'lib/apruve/resources/order.rb', line 31

def self.finalize!(id)
  response = Apruve.post("orders/#{id}/finalize")
  logger.debug response.body
  Order.new(response.body)
end

.find(id) ⇒ Object



8
9
10
11
12
# File 'lib/apruve/resources/order.rb', line 8

def self.find(id)
  response = Apruve.get("orders/#{id}")
  logger.debug response.body
  Order.new(response.body)
end

.find_all(merchant_order_id = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/apruve/resources/order.rb', line 14

def self.find_all(merchant_order_id = nil)
  if merchant_order_id.nil?
    response = Apruve.get('orders')
  else
    response = Apruve.get("orders?merchant_order_id=#{merchant_order_id}")
  end
  logger.debug response.body
  response.body.map { |order| Order.new(order) }
end

.find_by_hash(hash) ⇒ Object



24
25
26
27
28
29
# File 'lib/apruve/resources/order.rb', line 24

def self.find_by_hash(hash)
  response = Apruve.get("orders?secure_hash=#{hash}")
  logger.debug response.body
  orders = response.body.map { |order| Order.new(order) }
  orders.max_by { |order| order.created_at }
end

.invoices_for(order_id) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/apruve/resources/order.rb', line 37

def self.invoices_for(order_id)
  response = Apruve.get("orders/#{order_id}/invoices")
  ret = []
  response.body.each do |i|
    ret << Invoice.new(i)
  end
  ret
end

Instance Method Details

#payment_termsObject

The field is actually named ‘payment_term’, but some integrations are currently using ‘payment_terms’. Pass those requests through to the correct attribute.



109
110
111
# File 'lib/apruve/resources/order.rb', line 109

def payment_terms
  self.payment_term
end

#payment_terms=(pt) ⇒ Object



113
114
115
# File 'lib/apruve/resources/order.rb', line 113

def payment_terms=(pt)
  self.payment_term=pt
end

#save!Object



78
79
80
81
82
83
84
# File 'lib/apruve/resources/order.rb', line 78

def save!
  validate
  response = Apruve.post('orders', self.to_json)
  self.id = response.body['id']
  self.status = response.body['status']
  self.created_at = response.body['created_at']
end

#update!Object



52
53
54
55
56
# File 'lib/apruve/resources/order.rb', line 52

def update!
  response = Apruve.patch("orders/#{id}", {order: self}.to_json )
  logger.debug response.body
  initialize response.body
end

#validateObject



72
73
74
75
76
# File 'lib/apruve/resources/order.rb', line 72

def validate
  errors = []
  errors << 'merchant_id must be set' if merchant_id.nil?
  raise Apruve::ValidationError.new(errors) if errors.length > 0
end

#value_stringObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/apruve/resources/order.rb', line 86

def value_string
  # add each field in the PR
  str = "#{merchant_id}#{merchant_order_id}#{amount_cents}#{currency}#{tax_cents}#{shipping_cents}#{expire_at}#{accepts_payment_terms}#{finalize_on_create}#{invoice_on_create}"

  # add the line items
  self.order_items.each do |item|
    str += "#{item.title}#{item.plan_code}#{item.price_total_cents}#{item.price_ea_cents}"\
    "#{item.quantity}#{item.merchant_notes}#{item.description}#{item.variant_info}#{item.sku}"\
    "#{item.vendor}#{item.view_product_url}"
  end

  str
end