Class: Beanstream::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/beanstream/transaction.rb

Direct Known Subclasses

PaymentsAPI, ProfilesAPI, ReportingAPI

Instance Method Summary collapse

Instance Method Details

#encode(merchant_id, api_key) ⇒ Object



8
9
10
11
# File 'lib/beanstream/transaction.rb', line 8

def encode(merchant_id, api_key)
  str = "#{merchant_id}:#{api_key}"
  enc = Base64.encode64(str).gsub("\n", "")
end

#handle_api_error(ex) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/beanstream/transaction.rb', line 48

def handle_api_error(ex)
  #puts "error: #{ex}"
  
  http_status_code = ex.http_code
  message = ex.message
  code = 0
  category = 0
  
  begin
    obj = JSON.parse(ex.http_body)
    obj = Util.symbolize_names(obj)
    code = obj[:code]
    category = obj[:category]
    message = obj[:message]
  rescue JSON::ParserError
    puts "Error parsing json error message"
  end
  
  if http_status_code == 302
    raise InvalidRequestException.new(code, category, "Redirection for IOP and 3dSecure not supported by the Beanstream SDK yet. #{message}", http_status_code)
  elsif http_status_code == 400
    raise InvalidRequestException.new(code, category, message, http_status_code)
  elsif code == 401
    raise UnauthorizedException.new(code, category, message, http_status_code)
  elsif code == 402
    raise BusinessRuleException.new(code, category, message, http_status_code)
  elsif code == 403
    raise ForbiddenException.new(code, category, message, http_status_code)
  elsif code == 405
    raise InvalidRequestException.new(code, category, message, http_status_code)
  elsif code == 415
    raise InvalidRequestException.new(code, category, message, http_status_code)
  elsif code >= 500
    raise InternalServerException.new(code, category, message, http_status_code)
  else
    raise BeanstreamException.new(code, category, message, http_status_code)
  end
end

#handle_restclient_error(e) ⇒ Object

Raises:

  • (APIConnectionError)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/beanstream/transaction.rb', line 87

def handle_restclient_error(e)

  case e
  when RestClient::RequestTimeout
    message = "Could not connect to Beanstream"

  when RestClient::ServerBrokeConnection
    message = "The connection to the server broke before the request completed."

  when RestClient::SSLCertificateNotVerified
    message = "Could not verify Beanstream's SSL certificate. " \
      "Please make sure that your network is not intercepting certificates. "

  when SocketError
    message = "Unexpected error communicating when trying to connect to Beanstream. "

  else
    message = "Unexpected error communicating with Beanstream. "

  end

  raise APIConnectionError.new(message + "\n\n(Network error: #{e.message})")
end

#transaction_post(method, url_path, merchant_id, api_key, data = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/beanstream/transaction.rb', line 13

def transaction_post(method, url_path, merchant_id, api_key, data={})
  enc = encode(merchant_id, api_key)
  
  path = Beanstream.api_host_url+url_path
  #puts "processing the data: #{method} #{path} #{data.to_json}"

  req_params = {
    :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
    :ssl_ca_file => Beanstream.ssl_ca_cert,
    :timeout => Beanstream.timeout,
    :open_timeout => Beanstream.open_timeout,
    :headers => {
      :authorization => "Passcode #{enc}",
      :content_type => "application/json"
    },
    :method => method,
    :url => path,
    :payload => data.to_json
  }
  
  begin
    result = RestClient::Request.execute(req_params)
    returns = JSON.parse(result)
  rescue RestClient::ExceptionWithResponse => ex
    if ex.response
      raise handle_api_error(ex)
    else
      raise handle_restclient_error(ex)
    end
  rescue RestClient::Exception => ex
    raise handle_restclient_error(ex)
  end
  
end