Class: Wheretocard::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/wheretocard/order.rb

Overview

Object representing a response object with attributes provided by WTC

Returns:

  • (Array)

    Errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Order

Initializer to transform a Hash into an Payment object

Parameters:

  • args (Hash) (defaults to: nil)


43
44
45
46
47
48
49
50
51
# File 'lib/wheretocard/order.rb', line 43

def initialize(args=nil)
  @line_items = []
  @delivery_format = "BARCODE"
  @delivery_channel = "WEB"
  return if args.nil?
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#cityObject

Returns the value of attribute city.



27
28
29
# File 'lib/wheretocard/order.rb', line 27

def city
  @city
end

#clientObject

Returns the value of attribute client.



33
34
35
# File 'lib/wheretocard/order.rb', line 33

def client
  @client
end

#countryObject

Returns the value of attribute country.



29
30
31
# File 'lib/wheretocard/order.rb', line 29

def country
  @country
end

#delivery_channelObject

Returns the value of attribute delivery_channel.



36
37
38
# File 'lib/wheretocard/order.rb', line 36

def delivery_channel
  @delivery_channel
end

#delivery_formatObject

Returns the value of attribute delivery_format.



35
36
37
# File 'lib/wheretocard/order.rb', line 35

def delivery_format
  @delivery_format
end

#emailObject

Returns the value of attribute email.



30
31
32
# File 'lib/wheretocard/order.rb', line 30

def email
  @email
end

#errorsObject

Returns the value of attribute errors.



20
21
22
# File 'lib/wheretocard/order.rb', line 20

def errors
  @errors
end

#first_nameObject

Returns the value of attribute first_name.



22
23
24
# File 'lib/wheretocard/order.rb', line 22

def first_name
  @first_name
end

#house_numberObject

Returns the value of attribute house_number.



26
27
28
# File 'lib/wheretocard/order.rb', line 26

def house_number
  @house_number
end

#infixObject

Returns the value of attribute infix.



23
24
25
# File 'lib/wheretocard/order.rb', line 23

def infix
  @infix
end

#last_nameObject

Returns the value of attribute last_name.



24
25
26
# File 'lib/wheretocard/order.rb', line 24

def last_name
  @last_name
end

#line_itemsObject

Returns the value of attribute line_items.



34
35
36
# File 'lib/wheretocard/order.rb', line 34

def line_items
  @line_items
end

#phone_numberObject

Returns the value of attribute phone_number.



31
32
33
# File 'lib/wheretocard/order.rb', line 31

def phone_number
  @phone_number
end

#postal_codeObject

Returns the value of attribute postal_code.



28
29
30
# File 'lib/wheretocard/order.rb', line 28

def postal_code
  @postal_code
end

#reference_idObject

Returns the value of attribute reference_id.



21
22
23
# File 'lib/wheretocard/order.rb', line 21

def reference_id
  @reference_id
end

#streetObject

Returns the value of attribute street.



25
26
27
# File 'lib/wheretocard/order.rb', line 25

def street
  @street
end

#valueObject

Returns the value of attribute value.



32
33
34
# File 'lib/wheretocard/order.rb', line 32

def value
  @value
end

Instance Method Details

#add_line_item(args = nil) ⇒ Object

adds a line item of type LineItem to the line_items atribute (array)



83
84
85
86
87
# File 'lib/wheretocard/order.rb', line 83

def add_line_item(args=nil)
  line_item = Wheretocard::LineItem.new(args)
  line_items << line_item
  return line_item
end

#createWheretocard::Response

This is the most importent method. It uses all the attributes and performs a ‘order` action on Wheretocard API.

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/wheretocard/order.rb', line 64

def create
  # if there are any line items, they should all be valid.
  validate_line_items

  # make the API call
  # response        = Docdata.client.call(:create, xml: create_xml)
  # response_object = Docdata::Response.parse(:create, response)
  if response_object.success?
    self.key = response_object.key
  end

  # set `self` as the value of the `payment` attribute in the response object
  response_object.payment = self
  response_object.url     = redirect_url

  return response_object
end

#submitObject

submit the xml to the WtC API url return object is a Wheretocard::Response



100
101
102
103
# File 'lib/wheretocard/order.rb', line 100

def submit
  # validate_line_items
  Wheretocard::Response.from_order_request(self)
end

#to_xmlString Also known as: xml

Returns the xml to send in the SOAP API.

Returns:

  • (String)

    the xml to send in the SOAP API



90
91
92
93
94
95
# File 'lib/wheretocard/order.rb', line 90

def to_xml
  xml_file        = "#{File.dirname(__FILE__)}/xml/order_request.xml.erb"
  template        = File.read(xml_file)      
  namespace       = OpenStruct.new(order: self, client: client)
  xml             = ERB.new(template).result(namespace.instance_eval { binding })
end

#valid?Boolean

Returns true/false, depending if this instanciated object is valid.

Returns:

  • (Boolean)

    true/false, depending if this instanciated object is valid



54
55
56
57
# File 'lib/wheretocard/order.rb', line 54

def valid?
  validator = OrderValidator.new
  validator.valid?(self)
end