Class: Tappay::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/tappay/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net_http_response) ⇒ Response

Returns a new instance of Response.



64
65
66
67
68
# File 'lib/tappay/client.rb', line 64

def initialize(net_http_response)
  @code = net_http_response.code.to_i
  @body = net_http_response.body
  @headers = net_http_response.to_hash
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



62
63
64
# File 'lib/tappay/client.rb', line 62

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



62
63
64
# File 'lib/tappay/client.rb', line 62

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



62
63
64
# File 'lib/tappay/client.rb', line 62

def headers
  @headers
end

Instance Method Details

#[](key) ⇒ Object



99
100
101
# File 'lib/tappay/client.rb', line 99

def [](key)
  parsed_response[key]
end

#parsed_responseObject

Every TapPay endpoint answers with a JSON object, so a body that is not one means something other than TapPay replied - a maintenance page, a proxy, a WAF. Returning the raw body instead turned parsed_response['status'] into a String#[] substring search, which answers nil without complaining. Raise rather than hand back something that reads like a result.



76
77
78
79
80
81
82
83
84
# File 'lib/tappay/client.rb', line 76

def parsed_response
  @parsed_response ||= JSON.parse(@body.to_s).then do |parsed|
    raise ConnectionError, unexpected_body unless parsed.is_a?(Hash)

    parsed
  end
rescue JSON::ParserError
  raise ConnectionError, unexpected_body
end

#success?Boolean

TapPay signals business failures (declined card, insufficient funds, expired card) with HTTP 200 and a non-zero status, so the HTTP code alone says nothing. Client only builds a Response once validate_response has accepted the HTTP code, so status is the only question left.

This means "TapPay processed the request", not "the money moved": for a credit card the transaction is authorised but capture is asynchronous, and for LINE Pay / JKO Pay / iPass Money it means only that a payment_url was created and the customer has yet to pay. Confirm with Transaction::Query.

Returns:

  • (Boolean)


95
96
97
# File 'lib/tappay/client.rb', line 95

def success?
  parsed_response['status'] == 0
end