Class: Docdata::Payment

Inherits:
Object
  • Object
show all
Defined in:
lib/docdata/payment.rb

Overview

Object representing a "WSDL" object with attributes provided by Docdata

directly to the bank page (iDeal), you can set the bank id ('0031' for ABN AMRO for example.) any of: [IDEAL, AMAX, VISA, etc.]

Examples:

Payment.new({
  :amount => 2500,
  :currency => "EUR",
  :order_reference => "TJ123"
  :profile => "MyProfile"
  :shopper => @shopper
})

Returns:

  • (Array)

    Errors

Constant Summary collapse

@@amount =
"?"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Payment

Initializer to transform a +Hash+ into an Payment object

Parameters:

  • args (Hash) (defaults to: nil)


64
65
66
67
68
69
70
# File 'lib/docdata/payment.rb', line 64

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

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



43
44
45
# File 'lib/docdata/payment.rb', line 43

def amount
  @amount
end

#bank_idObject

Returns the value of attribute bank_id.



51
52
53
# File 'lib/docdata/payment.rb', line 51

def bank_id
  @bank_id
end

#canceledObject

Returns the value of attribute canceled.



56
57
58
# File 'lib/docdata/payment.rb', line 56

def canceled
  @canceled
end

#currencyObject

Returns the value of attribute currency.



47
48
49
# File 'lib/docdata/payment.rb', line 47

def currency
  @currency
end

#default_actObject

Returns the value of attribute default_act.



55
56
57
# File 'lib/docdata/payment.rb', line 55

def default_act
  @default_act
end

#descriptionObject

Returns the value of attribute description.



45
46
47
# File 'lib/docdata/payment.rb', line 45

def description
  @description
end

#errorsObject

Returns the value of attribute errors.



42
43
44
# File 'lib/docdata/payment.rb', line 42

def errors
  @errors
end

#idObject

Returns the value of attribute id.



57
58
59
# File 'lib/docdata/payment.rb', line 57

def id
  @id
end

#keyObject

Returns the value of attribute key.



54
55
56
# File 'lib/docdata/payment.rb', line 54

def key
  @key
end

#line_itemsObject

Returns the value of attribute line_items.



53
54
55
# File 'lib/docdata/payment.rb', line 53

def line_items
  @line_items
end

#order_referenceObject

Returns the value of attribute order_reference.



48
49
50
# File 'lib/docdata/payment.rb', line 48

def order_reference
  @order_reference
end

#prefered_payment_methodObject

Returns the value of attribute prefered_payment_method.



52
53
54
# File 'lib/docdata/payment.rb', line 52

def prefered_payment_method
  @prefered_payment_method
end

#profileObject

Returns the value of attribute profile.



49
50
51
# File 'lib/docdata/payment.rb', line 49

def profile
  @profile
end

#receipt_textObject

Returns the value of attribute receipt_text.



46
47
48
# File 'lib/docdata/payment.rb', line 46

def receipt_text
  @receipt_text
end

#shopperObject

Returns the value of attribute shopper.



50
51
52
# File 'lib/docdata/payment.rb', line 50

def shopper
  @shopper
end

Class Method Details

.cancel(api_key) ⇒ Object

This method makes it possible to find and cancel a payment with only the key It combines



148
149
150
151
# File 'lib/docdata/payment.rb', line 148

def self.cancel(api_key)
  p = self.find(api_key)
  p.cancel
end

.find(api_key) ⇒ Object

Initialize a Payment object with the key set



162
163
164
165
166
167
168
169
# File 'lib/docdata/payment.rb', line 162

def self.find(api_key)
  p = self.new(key: api_key)
  if p.status.success
    return p
  else
    raise DocdataError.new(p), p.status.message
  end
end

.refund(api_key, amount_to_refund, refund_description = "") ⇒ Object

This method makes it possible to find and refund a payment with only the key exmaple usage: Docdata::Payment.refund("APIT0K3N", 250)



155
156
157
158
# File 'lib/docdata/payment.rb', line 155

def self.refund(api_key, amount_to_refund, refund_description="")
  p = self.find(api_key)
  p.refund(amount_to_refund, refund_description)
end

Instance Method Details

#cancelObject

This calls the 'cancel' method of the SOAP API It cancels the payment and returns a Docdata::Response object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/docdata/payment.rb', line 111

def cancel
  # make the SOAP API call
  response        = Docdata.client.call(:cancel, xml: cancel_xml)
  response_object = Docdata::Response.parse(:cancel, 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
  self.canceled = true
  return true
end

#cancel_xmlString

Returns the xml to send in the SOAP API.

Returns:

  • (String)

    the xml to send in the SOAP API



245
246
247
248
249
250
# File 'lib/docdata/payment.rb', line 245

def cancel_xml
  xml_file        = "#{File.dirname(__FILE__)}/xml/cancel.xml.erb"
  template        = File.read(xml_file)      
  namespace       = OpenStruct.new(payment: self)
  xml             = ERB.new(template).result(namespace.instance_eval { binding })
end

#cleaned_up_descriptionString

where forbidden characters are filtered out and limit is 50 chars.

Returns:

  • (String)

    a cleaned up version of the description string



75
76
77
# File 'lib/docdata/payment.rb', line 75

def cleaned_up_description
  description.gsub("&", "and")[0..49]
end

#createDocdata::Response

This is the most importent method. It uses all the attributes and performs a create action on Docdata Payments SOAP API.

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/docdata/payment.rb', line 90

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

  # make the SOAP 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

#create_xmlString

Returns the xml to send in the SOAP API.

Returns:

  • (String)

    the xml to send in the SOAP API



236
237
238
239
240
241
# File 'lib/docdata/payment.rb', line 236

def create_xml
  xml_file        = "#{File.dirname(__FILE__)}/xml/create.xml.erb"
  template        = File.read(xml_file)      
  namespace       = OpenStruct.new(payment: self, shopper: shopper)
  xml             = ERB.new(template).result(namespace.instance_eval { binding })
end

#redirect_urlString Also known as: url

Returns The URI where the consumer can be redirected to in order to pay.

Returns:

  • (String)

    The URI where the consumer can be redirected to in order to pay



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/docdata/payment.rb', line 197

def redirect_url
  url = {}
  
  base_url = Docdata::Config.return_url
  if Docdata::Config.test_mode
    redirect_base_url = 'https://test.docdatapayments.com/ps/menu'
  else
    redirect_base_url = 'https://secure.docdatapayments.com/ps/menu'
  end
  url[:command]             = "show_payment_cluster"
  url[:payment_cluster_key] = key
  url[:merchant_name]       = Docdata::Config.username
  # only include return URL if present
  if base_url.present?
    url[:return_url_success]  = "#{base_url}/success?key=#{url[:payment_cluster_key]}"
    url[:return_url_pending]  = "#{base_url}/pending?key=#{url[:payment_cluster_key]}"
    url[:return_url_canceled] = "#{base_url}/canceled?key=#{url[:payment_cluster_key]}"
    url[:return_url_error]    = "#{base_url}/error?key=#{url[:payment_cluster_key]}"
  end
  if shopper && shopper.language_code
    url[:client_language]      = shopper.language_code
  end
  if default_act
    url[:default_act]     = "yes"
  end
  if bank_id.present?
    url[:ideal_issuer_id] = bank_id
    url[:default_pm]      = "IDEAL"
  end
  if prefered_payment_method.present?
    url[:default_pm]      = prefered_payment_method
  end
  params = URI.encode_www_form(url)
  uri = "#{redirect_base_url}?#{params}"
end

#refund(amount_to_refund, refund_description = "") ⇒ Object

This calls the 'refund' method of the SOAP API It refunds (part of) the amount paid



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/docdata/payment.rb', line 128

def refund(amount_to_refund, refund_description="")
  p = Docdata::Payment.new(key: key)
  p = p.status.payment
  refund_object = Docdata::Refund.new(
    currency: p.currency,
    amount: amount_to_refund,
    description: refund_description,
    payment: p
  )
  if refund_object.valid?
    refund_object.perform_refund
  else
    raise DocdataError.new(refund_object), refund_object.errors.full_messages.join(", ")
  end
end

#statusDocdata::Response Also known as: check

This is one of the other native SOAP API methods.

Returns:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/docdata/payment.rb', line 174

def status
  # read the xml template
  xml_file        = "#{File.dirname(__FILE__)}/xml/status.xml.erb"
  template        = File.read(xml_file)      
  namespace       = OpenStruct.new(payment: self)
  xml             = ERB.new(template).result(namespace.instance_eval { binding })

  # puts xml

  response        = Docdata.client.call(:status, xml: xml)
  response_object = Docdata::Response.parse(:status, response)

  response_object.set_attributes

  self.id                 = response_object.pid
  self.currency           = response_object.currency
  response_object.key     = key
  response_object.payment = self
  return response_object # Docdata::Response
end

#valid?Boolean

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

Returns:

  • (Boolean)

    true/false, depending if this instanciated object is valid



80
81
82
83
# File 'lib/docdata/payment.rb', line 80

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