epayco-sdk-ruby 0.0.9 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/epayco +1 -1
- data/lib/epayco/operations.rb +174 -169
- data/lib/epayco/resources.rb +58 -54
- data/lib/epayco-sdk-ruby.rb +2 -2
- data/lib/epayco.rb +238 -222
- data/lib/keylang.json +36 -26
- data/lib/keylang_apify.json +29 -0
- data/tests/test_helper.rb +123 -123
- data/tests/testing.rb +209 -209
- metadata +6 -3
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://
|
19
|
-
data_hash = JSON.parse(file)
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
if
|
68
|
-
|
69
|
-
else
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
:
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
def self.
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
def self.
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
@tags.
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
@seted["
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
:
|
209
|
-
:
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
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
|
-
"
|
9
|
-
"
|
10
|
-
"
|
11
|
-
"
|
12
|
-
"
|
13
|
-
"
|
14
|
-
"
|
15
|
-
"
|
16
|
-
"
|
17
|
-
"
|
18
|
-
"
|
19
|
-
"
|
20
|
-
"
|
21
|
-
"
|
22
|
-
"
|
23
|
-
"
|
24
|
-
"
|
25
|
-
"
|
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
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
{
|
2
|
+
"cash": "cash",
|
3
|
+
"end_date": "expirationDate",
|
4
|
+
"ref_payco": "refPayco",
|
5
|
+
"id_session_token": "idSessionToken",
|
6
|
+
"otp": "otp",
|
7
|
+
"invoice": "invoice",
|
8
|
+
"description": "description",
|
9
|
+
"value": "value",
|
10
|
+
"tax": "tax",
|
11
|
+
"ico": "ico",
|
12
|
+
"tax_base": "taxBase",
|
13
|
+
"currency": "currency",
|
14
|
+
"doc_type": "docType",
|
15
|
+
"doc_number": "document",
|
16
|
+
"name": "name",
|
17
|
+
"last_name": "lastName",
|
18
|
+
"email": "email",
|
19
|
+
"ind_country": "indCountry",
|
20
|
+
"country": "country",
|
21
|
+
"city": "city",
|
22
|
+
"phone": "phone",
|
23
|
+
"address": "address",
|
24
|
+
"ip": "ip",
|
25
|
+
"test": "testMode",
|
26
|
+
"url_response": "urlResponse",
|
27
|
+
"url_confirmation": "urlConfirmation",
|
28
|
+
"method_confirmation": "methodConfirmation"
|
29
|
+
}
|