Class: Upay::Requestor

Inherits:
Object
  • Object
show all
Defined in:
lib/upay/requestor.rb

Instance Method Summary collapse

Instance Method Details

#delete(url, data = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/upay/requestor.rb', line 70

def delete url, data = {}
  request = self.rest.delete do |req|
    req.url "#{url}"
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'application/json; charset=utf-8'
    req.body = data.to_json
  end
  return request.body
end

#get(url, data = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/upay/requestor.rb', line 50

def get url, data = {}
  request = self.rest.get do |req|
    req.url "#{url}"
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'application/json; charset=utf-8'
    req.body = data.to_json
  end
  return request.body
end

#post(url, data = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/upay/requestor.rb', line 60

def post url, data = {}
  request = self.rest.post do |req|
    req.url "#{url}"
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'application/json; charset=utf-8'
    req.body = data.to_json
  end
  return request.body
end

#put(url, data = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/upay/requestor.rb', line 80

def put url, data = {}
  request = self.rest.put do |req|
    req.url "#{url}"
    req.headers['Accept'] = 'application/json'
    req.headers['Content-Type'] = 'application/json; charset=utf-8'
    req.body = data.to_json
  end
  return request.body
end

#request(url, command, data = {}) ⇒ Object



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
47
48
# File 'lib/upay/requestor.rb', line 15

def request(url, command, data={})
  api_uri = URI.parse("https://#{'stg.' if Upay.test}api.payulatam.com")

  http = Net::HTTP.new(api_uri.host, api_uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  data["merchant"] = {
    "apiKey" =>     Upay.api_key,
    "apiLogin" =>   Upay.
  }

  data["command"] = command
  data["test"] = Upay.test || false
  data["language"] = "es" || Upay.lang

  headers = {
    "Accept" =>         "application/json",
    "Content-Type" =>   "application/json; charset=utf-8"
  }

  result = http.send_request("POST", url, data.to_json, headers)

  begin
    response = JSON.parse(result.body)
  rescue Exception => e
    response = result.body
  end

  return {
    "status" => result.code,
    "response" => response
  }
end

#restObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/upay/requestor.rb', line 4

def rest
  api_uri = (Upay.test) ? 'https://stg.api.payulatam.com/payments-api/' : "https://api.payulatam.com/payments-api/"
  @rest = Faraday.new(:url => api_uri, :ssl => {:verify => false},  headers: { accept: 'application/json', :"Content-Type" => 'application/json; charset=utf-8' }) do |faraday|
    faraday.response :logger if Upay.logger == true
    faraday.response :json, :content_type => /\b(json|json-home)$/
    faraday.adapter  Faraday.default_adapter
  end
  @rest.basic_auth Upay., Upay.api_key
  @rest
end