epayco-sdk-ruby 1.0.0 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/epayco.rb CHANGED
@@ -1,222 +1,238 @@
1
- require 'rest-client'
2
- require 'json'
3
- require 'openssl'
4
- require 'base64'
5
- require 'open-uri'
6
- require 'socket'
7
- require_relative 'epayco/resources'
8
-
9
- module Epayco
10
-
11
- # Set custom error
12
- class Error < StandardError
13
- include Enumerable
14
- attr_accessor :errors
15
-
16
- # Get code, lang and show custom error
17
- def initialize code, lang
18
- file = open("https://s3-us-west-2.amazonaws.com/epayco/message_api/errors.json").read
19
- data_hash = JSON.parse(file)
20
- super data_hash[code][lang]
21
- @errors = errors
22
- end
23
-
24
- def each
25
- @errors.each { |e| yield *e.first }
26
- end
27
- end
28
-
29
- # Endpoints
30
- @api_base = 'https://api.secure.payco.co'
31
- @api_base_secure = 'https://secure.payco.co'
32
-
33
- # Init sdk parameters
34
- class << self
35
- attr_accessor :apiKey, :privateKey, :lang, :test
36
- end
37
-
38
- # Eject request and show response or error
39
- def self.request(method, url, extra=nil, params={}, headers={}, switch, cashdata, sp, dt)
40
- method = method.to_sym
41
-
42
- auth = authent(apiKey ,privateKey)
43
- bearer_token = 'Bearer '+ auth[:bearer_token]
44
-
45
- if !apiKey || !privateKey || !lang
46
- raise Error.new('100', lang)
47
- end
48
-
49
- payload = JSON.generate(params) if method == :post || method == :patch
50
- params = nil unless method == :get
51
-
52
- # Switch secure or api
53
- if switch
54
- if method == :post || method == :patch
55
- if cashdata
56
- enc = encrypt_aes(payload, true)
57
- payload = enc.to_json
58
- else
59
- enc = encrypt_aes(payload, false)
60
- payload = enc.to_json
61
- end
62
- end
63
- url = @api_base_secure + url
64
- else
65
- if method == :post || method == :patch
66
- rb_hash = JSON.parse(payload)
67
- if dt
68
- payload = rb_hash.to_json
69
- else
70
- rb_hash["test"] = test ? "TRUE" : "FALSE"
71
- rb_hash["ip"] = local_ip
72
- payload = rb_hash.to_json
73
- end
74
- end
75
- url = @api_base + url
76
- end
77
-
78
- if sp
79
- headers = {
80
- :content_type => 'multipart/form-data'
81
- }.merge(headers)
82
-
83
- options = {
84
- :headers => headers,
85
- :user => apiKey,
86
- :method => method,
87
- :url => url,
88
- :payload => payload
89
- }
90
- else
91
- headers = {
92
- :params => params,
93
- :content_type => 'application/json',
94
- :type => 'sdk-jwt',
95
- :lang => 'RUBY',
96
- :Authorization => bearer_token,
97
- }.merge(headers)
98
-
99
- options = {
100
- :headers => headers,
101
- :user => apiKey,
102
- :method => method,
103
- :url => url,
104
- :payload => payload
105
- }
106
-
107
- end
108
- # Open library rest client
109
- begin
110
- #puts options
111
- #abort("Message goes here 1")
112
- response = execute_request(options)
113
- return {} if response.code == 204 and method == :delete
114
- JSON.parse(response.body, :symbolize_names => true)
115
- rescue RestClient::Exception => e
116
- handle_errors e
117
- end
118
- end
119
-
120
- private
121
-
122
- # Get response successful
123
- def self.execute_request(options)
124
- RestClient::Request.execute(options)
125
- end
126
-
127
- # Get response with errors
128
- def self.handle_errors exception
129
- body = JSON.parse exception.http_body
130
- raise Error.new(exception.to_s, body['errors'])
131
- end
132
-
133
- def self.local_ip
134
- orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
135
- UDPSocket.open do |s|
136
- s.connect '64.233.187.99', 1
137
- s.addr.last
138
- end
139
- ensure
140
- Socket.do_not_reverse_lookup = orig
141
- end
142
-
143
- def self.encrypt_aes(data, cashdata)
144
-
145
- sandbox = Epayco.test ? "TRUE" : "FALSE"
146
- @tags = JSON.parse(data)
147
- @seted = {}
148
- if cashdata
149
- @tags.each {
150
- |key, value|
151
- @seted[lang_key(key)] = value
152
- }
153
- @seted["ip"] = local_ip
154
- @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
155
- else
156
- @tags.each {
157
- |key, value|
158
- @seted[lang_key(key)] = encrypt(value, Epayco.privateKey)
159
- }
160
- @seted["ip"] = encrypt(local_ip, Epayco.privateKey)
161
- @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
162
- end
163
-
164
-
165
-
166
-
167
- @seted["public_key"] = Epayco.apiKey
168
- @seted["i"] = Base64.encode64("0000000000000000")
169
- @seted["lenguaje"] = "ruby"
170
- @seted["p"] = ""
171
- return @seted
172
- end
173
-
174
- def self.encrypt(str, key)
175
- cipher = OpenSSL::Cipher.new('AES-128-CBC')
176
- cipher.encrypt
177
- iv = "0000000000000000"
178
- cipher.iv = iv
179
- cipher.key = key.byteslice(0, cipher.key_len)
180
- str = iv + str
181
- data = cipher.update(str) + cipher.final
182
- Base64.urlsafe_encode64(data)
183
- end
184
-
185
- # Traslate secure petitions
186
- def self.lang_key key
187
- file = File.read(File.dirname(__FILE__) + '/keylang.json')
188
- data_hash = JSON.parse(file)
189
- data_hash[key]
190
- end
191
-
192
-
193
- def self.authent(apiKey, privateKey)
194
- @parmas = {}
195
- @parmas["public_key"] = apiKey
196
- @parmas["private_key"] = privateKey
197
- headers = {
198
- # :params => @parmas,
199
- :Accept => 'application/json',
200
- :content_type => 'application/json',
201
- :type => 'sdk'
202
- }
203
- payload = @parmas.to_json
204
- options = {
205
- :headers => headers,
206
- :user => apiKey,
207
- :method => 'post',
208
- :url => 'https://api.secure.payco.co/v1/auth/login',
209
- :payload => payload
210
- }
211
-
212
- begin
213
- response = execute_request(options)
214
- return {} if response.code == 204 and method == :delete
215
- JSON.parse(response.body, :symbolize_names => true)
216
- rescue RestClient::Exception => e
217
- handle_errors e
218
- end
219
-
220
- end
221
-
222
- end
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'openssl'
4
+ require 'base64'
5
+ require 'open-uri'
6
+ require 'socket'
7
+ require_relative 'epayco/resources'
8
+
9
+ module Epayco
10
+
11
+ # Set custom error
12
+ class Error < StandardError
13
+ include Enumerable
14
+ attr_accessor :errors
15
+
16
+ # Get code, lang and show custom error
17
+ def initialize code, lang
18
+ file = open("https://multimedia.epayco.co/message-api/errors.json").read
19
+ data_hash = JSON.parse(file)
20
+ error = "Error"
21
+ if(data_hash[code.to_s])
22
+ error = [data_hash[code.to_s][lang]]
23
+ else
24
+ error = [code, lang]
25
+ end
26
+ super error
27
+ @errors = error
28
+ end
29
+
30
+ def each
31
+ @errors.each { |e| yield *e }
32
+ end
33
+ end
34
+
35
+ # Endpoints
36
+ @api_base = 'https://api.secure.payco.co'
37
+ @api_base_secure = 'https://secure.payco.co'
38
+ @api_base_apify = "https://apify.epayco.co"
39
+
40
+ # Init sdk parameters
41
+ class << self
42
+ attr_accessor :apiKey, :privateKey, :lang, :test
43
+ end
44
+
45
+ # Eject request and show response or error
46
+ def self.request(method, url, extra=nil, params={}, headers={}, switch, cashdata, dt, apify)
47
+ method = method.to_sym
48
+
49
+ auth = authent(apiKey, privateKey, apify, lang)
50
+ bearer_token = 'Bearer '+ (auth[:bearer_token] || auth[:token])
51
+
52
+ if !apiKey || !privateKey || !lang
53
+ raise Error.new('100', lang)
54
+ end
55
+
56
+ payload = JSON.generate(params) if method == :post || method == :patch
57
+ params = nil unless method == :get
58
+
59
+ # Switch secure or api or apify
60
+ if apify
61
+ @tags = JSON.parse(payload)
62
+ seted = {}
63
+ file = File.read(File.dirname(__FILE__) + '/keylang_apify.json')
64
+ data_hash = JSON.parse(file)
65
+ @tags.each {
66
+ |key, value|
67
+ if data_hash[key]
68
+ seted[data_hash[key]] = value
69
+ else
70
+ seted[key] = value
71
+ end
72
+ }
73
+ payload = seted.to_json
74
+ url = @api_base_apify + url
75
+ elsif switch
76
+ if method == :post || method == :patch
77
+ if cashdata
78
+ enc = encrypt_aes(payload, true)
79
+ payload = enc.to_json
80
+ else
81
+ enc = encrypt_aes(payload, false)
82
+ payload = enc.to_json
83
+ end
84
+ end
85
+ url = @api_base_secure + url
86
+ else
87
+ if method == :post || method == :patch
88
+ rb_hash = JSON.parse(payload)
89
+ if dt
90
+ payload = rb_hash.to_json
91
+ else
92
+ rb_hash["test"] = test ? "TRUE" : "FALSE"
93
+ rb_hash["ip"] = local_ip
94
+ payload = rb_hash.to_json
95
+ end
96
+ end
97
+ url = @api_base + url
98
+ end
99
+
100
+
101
+ headers = {
102
+ :params => params,
103
+ :content_type => 'application/json',
104
+ :type => 'sdk-jwt',
105
+ :lang => 'RUBY',
106
+ :Authorization => bearer_token,
107
+ }.merge(headers)
108
+
109
+ options = {
110
+ :headers => headers,
111
+ :user => apiKey,
112
+ :method => method,
113
+ :url => url,
114
+ :payload => payload
115
+ }
116
+
117
+ # Open library rest client
118
+ begin
119
+ #puts options
120
+ #abort("Message goes here 1")
121
+ response = execute_request(options)
122
+ #puts response.body
123
+ return {} if response.code == 204 and method == :delete
124
+ JSON.parse(response.body, :symbolize_names => true)
125
+ rescue RestClient::Exception => e
126
+ handle_errors e
127
+ end
128
+ end
129
+
130
+ private
131
+
132
+ # Get response successful
133
+ def self.execute_request(options)
134
+ RestClient::Request.execute(options)
135
+ end
136
+
137
+ # Get response with errors
138
+ def self.handle_errors exception
139
+ body = JSON.parse exception.http_body
140
+ raise Error.new(exception.to_s, body['errors'])
141
+ end
142
+
143
+ def self.local_ip
144
+ orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
145
+ UDPSocket.open do |s|
146
+ s.connect '64.233.187.99', 1
147
+ s.addr.last
148
+ end
149
+ ensure
150
+ Socket.do_not_reverse_lookup = orig
151
+ end
152
+
153
+ def self.encrypt_aes(data, cashdata)
154
+
155
+ sandbox = Epayco.test ? "TRUE" : "FALSE"
156
+ @tags = JSON.parse(data)
157
+ @seted = {}
158
+ if cashdata
159
+ @tags.each {
160
+ |key, value|
161
+ @seted[lang_key(key)] = value
162
+ }
163
+ @seted["ip"] = local_ip
164
+ @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
165
+ else
166
+ @tags.each {
167
+ |key, value|
168
+ @seted[lang_key(key)] = encrypt(value, Epayco.privateKey)
169
+ }
170
+ @seted["ip"] = encrypt(local_ip, Epayco.privateKey)
171
+ @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
172
+ end
173
+
174
+
175
+
176
+
177
+ @seted["public_key"] = Epayco.apiKey
178
+ @seted["i"] = Base64.encode64("0000000000000000")
179
+ @seted["lenguaje"] = "ruby"
180
+ @seted["p"] = ""
181
+ return @seted
182
+ end
183
+
184
+ def self.encrypt(str, key)
185
+ cipher = OpenSSL::Cipher.new('AES-128-CBC')
186
+ cipher.encrypt
187
+ iv = "0000000000000000"
188
+ cipher.iv = iv
189
+ cipher.key = key.byteslice(0, cipher.key_len)
190
+ str = iv + str
191
+ data = cipher.update(str) + cipher.final
192
+ Base64.urlsafe_encode64(data)
193
+ end
194
+
195
+ # Traslate secure petitions
196
+ def self.lang_key key
197
+ file = File.read(File.dirname(__FILE__) + '/keylang.json')
198
+ data_hash = JSON.parse(file)
199
+ data_hash[key]
200
+ end
201
+
202
+ def self.authent(apiKey, privateKey, apify, lang)
203
+ @parmas = {}
204
+ @parmas["public_key"] = apiKey
205
+ @parmas["private_key"] = privateKey
206
+
207
+ headers = {
208
+ # :params => @parmas,
209
+ :Accept => 'application/json',
210
+ :content_type => 'application/json',
211
+ :type => 'sdk'
212
+ }
213
+ url = 'https://api.secure.payco.co/v1/auth/login'
214
+ if(apify)
215
+ headers[:Authorization] = "Basic " + Base64.strict_encode64(apiKey + ":" + privateKey)
216
+ url = 'https://apify.epayco.co/login'
217
+ end
218
+ payload = @parmas.to_json
219
+ options = {
220
+ :headers => headers,
221
+ :user => apiKey,
222
+ :method => 'post',
223
+ :url => url,
224
+ :payload => if apify then nil else payload end
225
+ }
226
+ begin
227
+ response = execute_request(options)
228
+ return {} if response.code == 204 and method == :delete
229
+ body = JSON.parse(response.body, :symbolize_names => true)
230
+ return body if body[:bearer_token] || body[:token]
231
+ raise Error.new("104", lang)
232
+ rescue RestClient::Exception => e
233
+ handle_errors e
234
+ end
235
+
236
+ end
237
+
238
+ end
data/lib/keylang.json CHANGED
@@ -1,26 +1,36 @@
1
- {
2
- "bank": "banco",
3
- "invoice": "factura",
4
- "description": "descripcion",
5
- "value": "valor",
6
- "tax": "iva",
7
- "tax_base": "baseiva",
8
- "currency": "moneda",
9
- "type_person": "tipo_persona",
10
- "doc_type": "tipo_doc",
11
- "doc_number": "documento",
12
- "name": "nombres",
13
- "last_name": "apellidos",
14
- "email": "email",
15
- "country": "pais",
16
- "department": "depto",
17
- "city": "ciudad",
18
- "phone": "telefono",
19
- "cell_phone": "celular",
20
- "address": "direccion",
21
- "ip": "ip",
22
- "url_response": "url_respuesta",
23
- "url_confirmation": "url_confirmacion",
24
- "method_confirmation": "metodoconfirmacion",
25
- "end_date": "fechaexpiracion"
26
- }
1
+ {
2
+ "bank": "banco",
3
+ "invoice": "factura",
4
+ "description": "descripcion",
5
+ "value": "valor",
6
+ "tax": "iva",
7
+ "tax_base": "baseiva",
8
+ "ico": "ico",
9
+ "currency": "moneda",
10
+ "type_person": "tipo_persona",
11
+ "doc_type": "tipo_doc",
12
+ "doc_number": "documento",
13
+ "name": "nombres",
14
+ "last_name": "apellidos",
15
+ "email": "email",
16
+ "country": "pais",
17
+ "department": "depto",
18
+ "city": "ciudad",
19
+ "phone": "telefono",
20
+ "cell_phone": "celular",
21
+ "address": "direccion",
22
+ "ip": "ip",
23
+ "url_response": "url_respuesta",
24
+ "url_confirmation": "url_confirmacion",
25
+ "method_confirmation": "method_confirmation",
26
+ "metodoconfirmacion": "metodoconfirmacion",
27
+ "end_date": "fechaexpiracion",
28
+ "splitpayment": "splitpayment",
29
+ "split_app_id": "split_app_id",
30
+ "split_merchant_id": "split_merchant_id",
31
+ "split_type": "split_type",
32
+ "split_primary_receiver": "split_primary_receiver",
33
+ "split_primary_receiver_fee": "split_primary_receiver_fee",
34
+ "split_rule": "split_rule",
35
+ "split_receivers": "split_receivers"
36
+ }