Class: Veritrans::Result
- Inherits:
-
Object
- Object
- Veritrans::Result
- Defined in:
- lib/veritrans/result.rb
Overview
Midtrans response object, a wrapper for raw response object plus helper methods
Usual response body for Midtrans.charge or Midtrans.status will look like this:
{
"status_code": "200",
"status_message": "Success, Mandiri Clickpay transaction is successful",
"transaction_id": "d788e503-3fab-4296-9c10-83b107324cb9",
"order_id": "2016-11-14 11:54:03 +0800",
"gross_amount": "10000.00",
"payment_type": "mandiri_clickpay",
"transaction_time": "2016-11-14 10:54:02",
"transaction_status": "settlement",
"fraud_status": "accept",
"approval_code": "1479095646260",
"masked_card": "411111-1111"
}
Result object can be used like this:
result.success? # => true
result.status_code # => 200
result.transaction_status # => "settlement"
result.fraud_status # => "accept"
result.approval_code # => "1479095646260"
result.masked_card # => "411111-1111"
result.data # => {:status_code => "200", :status_message => "Success, Mandiri ..."} # add data as hash
result.time # => 1.3501
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Response body parsed as hash.
-
#request_options ⇒ Object
readonly
Request options, a hash with :path, :method, :headers, :body.
-
#response ⇒ Object
readonly
Excon::Response object.
-
#status ⇒ Object
readonly
HTTP status code, should always be 200.
-
#time ⇒ Object
readonly
HTTP request time, a Float.
-
#url ⇒ Object
readonly
Request full URL, e.g.
Instance Method Summary collapse
-
#body ⇒ Object
Raw response body as String.
-
#created? ⇒ Boolean
Return if VT-Link page was created.
-
#initialize(response, url, request_options, time) ⇒ Result
constructor
A new instance of Result.
- #inspect ⇒ Object
- #messages ⇒ Object
- #method_missing(method_name, *args) ⇒ Object
-
#redirect_url ⇒ Object
Return
"redirect_url"field of response. -
#status_code ⇒ Object
Return
"status_code"field of response Docs api-docs.midtrans.com/#status-code. -
#status_message ⇒ Object
Return
"status_message"field of response. -
#success? ⇒ Boolean
Return whenever transaction is successful, based on
status_code. -
#transaction_id ⇒ Object
Return
"transaction_id"field of response.
Constructor Details
#initialize(response, url, request_options, time) ⇒ Result
Returns a new instance of Result.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/veritrans/result.rb', line 46 def initialize(response, url, , time) begin if url =~ %r{/v2/.+/transcript$} @data = {} else @data = Veritrans::Client._json_decode(response.body) # Failback for Hash#symbolize_keys @data.keys.each do |key| @data[(key.to_sym rescue key) || key] = @data.delete(key) end end rescue => e Veritrans.logger.info "Error parsing Veritrans response #{e.message}" Veritrans.logger.info e.backtrace.join("\n") @data = {} end @time = time @status = response.status @response = response @url = url = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/veritrans/result.rb', line 116 def method_missing(method_name, *args) if args.size == 0 && @data && @data.has_key?(method_name) return @data[method_name] else super end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Response body parsed as hash
34 35 36 |
# File 'lib/veritrans/result.rb', line 34 def data @data end |
#request_options ⇒ Object (readonly)
Request options, a hash with :path, :method, :headers, :body
40 41 42 |
# File 'lib/veritrans/result.rb', line 40 def end |
#response ⇒ Object (readonly)
Excon::Response object
38 39 40 |
# File 'lib/veritrans/result.rb', line 38 def response @response end |
#status ⇒ Object (readonly)
HTTP status code, should always be 200
36 37 38 |
# File 'lib/veritrans/result.rb', line 36 def status @status end |
#time ⇒ Object (readonly)
HTTP request time, a Float
42 43 44 |
# File 'lib/veritrans/result.rb', line 42 def time @time end |
#url ⇒ Object (readonly)
Request full URL, e.g. “api.sandbox.midtrans.com/v2/charge”
44 45 46 |
# File 'lib/veritrans/result.rb', line 44 def url @url end |
Instance Method Details
#body ⇒ Object
Raw response body as String
112 113 114 |
# File 'lib/veritrans/result.rb', line 112 def body response.body end |
#created? ⇒ Boolean
Return if VT-Link page was created
78 79 80 |
# File 'lib/veritrans/result.rb', line 78 def created? @data[:status_code] == '201' end |
#inspect ⇒ Object
124 125 126 127 128 |
# File 'lib/veritrans/result.rb', line 124 def inspect time_ms = (@time * 1000).round data = @data.inspect.gsub(/:([^\s]+)=>/, "\\1: ") "#<#{self.class.to_s}:#{object_id} ^^ status: #{@status} time: #{time_ms}ms ^^ data: #{data}>" end |
#messages ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/veritrans/result.rb', line 103 def if @data[:message].present? @data[:message].chomp(']').sub(/^\[/, '').split(',').map(&:strip) else [] end end |
#redirect_url ⇒ Object
Return "redirect_url" field of response
94 95 96 |
# File 'lib/veritrans/result.rb', line 94 def redirect_url @data[:redirect_url] end |
#status_code ⇒ Object
Return "status_code" field of response Docs api-docs.midtrans.com/#status-code
84 85 86 |
# File 'lib/veritrans/result.rb', line 84 def status_code @data[:status_code].to_i end |
#status_message ⇒ Object
Return "status_message" field of response
89 90 91 |
# File 'lib/veritrans/result.rb', line 89 def @data[:status_message] end |
#success? ⇒ Boolean
Return whenever transaction is successful, based on status_code
73 74 75 |
# File 'lib/veritrans/result.rb', line 73 def success? @data[:status_code] == '200' || @data[:status_code] == '201' || @data[:status_code] == '407' end |
#transaction_id ⇒ Object
Return "transaction_id" field of response
99 100 101 |
# File 'lib/veritrans/result.rb', line 99 def transaction_id @data[:transaction_id] end |