Class: Transbank::Webpay::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/transbank/webpay/response.rb

Constant Summary collapse

RESPONSE_CODE =
{
  get_transaction_result: {
    '0'   => 'transacción aprobada',
    '-1'  => 'rechazo de transacción',
    '-2'  => 'transacción debe reintentarse',
    '-3'  => 'error en transacción',
    '-4'  => 'rechazo de transacción',
    '-5'  => 'rechazo por error de tasa',
    '-6'  => 'excede cupo máximo mensual',
    '-7'  => 'excede límite diario por transacción',
    '-8'  => 'rubro no autorizado'
  },
  default: {
    '0'   => 'aprobado',
    '-98' => 'Error'
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, action) ⇒ Response

Returns a new instance of Response.



25
26
27
28
29
30
31
# File 'lib/transbank/webpay/response.rb', line 25

def initialize(content, action)
  self.content = content
  self.action = action
  self.attributes = xml_result
  self.errors = []
  validate!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



102
103
104
# File 'lib/transbank/webpay/response.rb', line 102

def method_missing(method_name, *args, &block)
  attributes[method_name.to_sym] || super
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/transbank/webpay/response.rb', line 5

def action
  @action
end

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/transbank/webpay/response.rb', line 5

def attributes
  @attributes
end

#contentObject

Returns the value of attribute content.



5
6
7
# File 'lib/transbank/webpay/response.rb', line 5

def content
  @content
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/transbank/webpay/response.rb', line 5

def errors
  @errors
end

#exceptionObject

Returns the value of attribute exception.



5
6
7
# File 'lib/transbank/webpay/response.rb', line 5

def exception
  @exception
end

Instance Method Details

#bodyObject



33
34
35
# File 'lib/transbank/webpay/response.rb', line 33

def body
  content.body
end

#docObject



41
42
43
# File 'lib/transbank/webpay/response.rb', line 41

def doc
  @doc ||= Nokogiri::XML body
end

#errors_displayObject



57
58
59
# File 'lib/transbank/webpay/response.rb', line 57

def errors_display
  errors.join ', '
end

#exception?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/transbank/webpay/response.rb', line 94

def exception?
  false
end

#http_codeObject



37
38
39
# File 'lib/transbank/webpay/response.rb', line 37

def http_code
  content.code
end

#inspectObject



87
88
89
90
91
92
# File 'lib/transbank/webpay/response.rb', line 87

def inspect
  result = ["valid: #{valid?}"]
  result << attributes_display if attributes.any?
  result << "error: \"#{errors_display}\"" if errors.any?
  "#<#{self.class} #{result.join(', ')} >"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/transbank/webpay/response.rb', line 106

def respond_to_missing?(method_name, include_private = false)
  attributes.keys.include?(method_name.to_sym) || super
end

#response_codeObject



77
78
79
# File 'lib/transbank/webpay/response.rb', line 77

def response_code
  @response_code ||= doc.xpath("//responseCode").text
end

#response_code_displayObject



81
82
83
84
85
# File 'lib/transbank/webpay/response.rb', line 81

def response_code_display
  if response_code.present?
    RESPONSE_CODE.fetch(action, RESPONSE_CODE[:default]).fetch(response_code, response_code)
  end
end

#signature_decodeObject



73
74
75
# File 'lib/transbank/webpay/response.rb', line 73

def signature_decode
  Base64.decode64(signature_node.content)
end

#signature_nodeObject



69
70
71
# File 'lib/transbank/webpay/response.rb', line 69

def signature_node
  doc.at_xpath('//ds:SignatureValue', {'ds' => 'http://www.w3.org/2000/09/xmldsig#'})
end

#signed_nodeObject



65
66
67
# File 'lib/transbank/webpay/response.rb', line 65

def signed_node
  doc.at_xpath '//ds:SignedInfo', {'ds' => 'http://www.w3.org/2000/09/xmldsig#'}
end

#valid?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/transbank/webpay/response.rb', line 61

def valid?
  errors.empty?
end

#xml_errorObject



53
54
55
# File 'lib/transbank/webpay/response.rb', line 53

def xml_error
  doc.xpath("//faultstring")
end

#xml_error_displayObject



110
111
112
# File 'lib/transbank/webpay/response.rb', line 110

def xml_error_display
  xml_error.map { |e| e.text.gsub(/<!--|-->/, '').strip }
end

#xml_resultObject



45
46
47
48
49
50
51
# File 'lib/transbank/webpay/response.rb', line 45

def xml_result
  parser.parse(doc.at_xpath("//return").to_s).fetch( :return, {}).tap do |hash|
    hash[:detail_output].tap {|r| r[:amount] = r[:amount].to_i }

    hash.each { |k, v| hash.store(k, OpenStruct.new(v)) if v.is_a?(Hash) }
  end
end