epayco-sdk-ruby 0.0.7 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,187 +1,223 @@
1
- require 'rest-client'
2
- require 'json'
3
- require 'openssl'
4
- require 'base64'
5
- require 'open-uri'
6
- require 'socket'
7
- require_relative 'epayco-sdk/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
- if !apiKey || !privateKey || !lang
43
- raise Error.new('100', lang)
44
- end
45
-
46
- payload = JSON.generate(params) if method == :post || method == :patch
47
- params = nil unless method == :get
48
-
49
- # Switch secure or api
50
- if switch
51
- if method == :post || method == :patch
52
- if cashdata
53
- enc = encrypt_aes(payload, true)
54
- payload = enc.to_json
55
- else
56
- enc = encrypt_aes(payload, false)
57
- payload = enc.to_json
58
- end
59
- end
60
- url = @api_base_secure + url
61
- else
62
- if method == :post || method == :patch
63
- rb_hash = JSON.parse(payload)
64
- if dt
65
- payload = rb_hash.to_json
66
- else
67
- rb_hash["test"] = test ? "TRUE" : "FALSE"
68
- rb_hash["ip"] = local_ip
69
- payload = rb_hash.to_json
70
- end
71
- end
72
- url = @api_base + url
73
- end
74
-
75
- if sp
76
- headers = {
77
- :content_type => 'multipart/form-data'
78
- }.merge(headers)
79
-
80
- options = {
81
- :headers => headers,
82
- :user => apiKey,
83
- :method => method,
84
- :url => url,
85
- :payload => payload
86
- }
87
- else
88
- headers = {
89
- :params => params,
90
- :content_type => 'application/json',
91
- :type => 'sdk'
92
- }.merge(headers)
93
-
94
- options = {
95
- :headers => headers,
96
- :user => apiKey,
97
- :method => method,
98
- :url => url,
99
- :payload => payload
100
- }
101
-
102
- end
103
- # Open library rest client
104
- begin
105
- #puts options
106
- #abort("Message goes here 1")
107
- response = execute_request(options)
108
- return {} if response.code == 204 and method == :delete
109
- JSON.parse(response.body, :symbolize_names => true)
110
- rescue RestClient::Exception => e
111
- handle_errors e
112
- end
113
- end
114
-
115
- private
116
-
117
- # Get response successful
118
- def self.execute_request(options)
119
- RestClient::Request.execute(options)
120
- end
121
-
122
- # Get response with errors
123
- def self.handle_errors exception
124
- body = JSON.parse exception.http_body
125
- raise Error.new(exception.to_s, body['errors'])
126
- end
127
-
128
- def self.local_ip
129
- orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
130
- UDPSocket.open do |s|
131
- s.connect '64.233.187.99', 1
132
- s.addr.last
133
- end
134
- ensure
135
- Socket.do_not_reverse_lookup = orig
136
- end
137
-
138
- def self.encrypt_aes(data, cashdata)
139
-
140
- sandbox = Epayco.test ? "TRUE" : "FALSE"
141
- @tags = JSON.parse(data)
142
- @seted = {}
143
- if cashdata
144
- @tags.each {
145
- |key, value|
146
- @seted[lang_key(key)] = value
147
- }
148
- @seted["ip"] = local_ip
149
- @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
150
- else
151
- @tags.each {
152
- |key, value|
153
- @seted[lang_key(key)] = encrypt(value, Epayco.privateKey)
154
- }
155
- @seted["ip"] = encrypt(local_ip, Epayco.privateKey)
156
- @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
157
- end
158
-
159
-
160
-
161
-
162
- @seted["public_key"] = Epayco.apiKey
163
- @seted["i"] = Base64.encode64("0000000000000000")
164
- @seted["lenguaje"] = "ruby"
165
- @seted["p"] = ""
166
- return @seted
167
- end
168
-
169
- def self.encrypt(str, key)
170
- cipher = OpenSSL::Cipher.new('AES-128-CBC')
171
- cipher.encrypt
172
- iv = "0000000000000000"
173
- cipher.iv = iv
174
- cipher.key = key.byteslice(0, cipher.key_len)
175
- str = iv + str
176
- data = cipher.update(str) + cipher.final
177
- Base64.urlsafe_encode64(data)
178
- end
179
-
180
- # Traslate secure petitions
181
- def self.lang_key key
182
- file = File.read(File.dirname(__FILE__) + '/keylang.json')
183
- data_hash = JSON.parse(file)
184
- data_hash[key]
185
- end
186
-
187
- 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://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
+ #puts response.body
114
+ return {} if response.code == 204 and method == :delete
115
+ JSON.parse(response.body, :symbolize_names => true)
116
+ rescue RestClient::Exception => e
117
+ handle_errors e
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ # Get response successful
124
+ def self.execute_request(options)
125
+ RestClient::Request.execute(options)
126
+ end
127
+
128
+ # Get response with errors
129
+ def self.handle_errors exception
130
+ body = JSON.parse exception.http_body
131
+ raise Error.new(exception.to_s, body['errors'])
132
+ end
133
+
134
+ def self.local_ip
135
+ orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
136
+ UDPSocket.open do |s|
137
+ s.connect '64.233.187.99', 1
138
+ s.addr.last
139
+ end
140
+ ensure
141
+ Socket.do_not_reverse_lookup = orig
142
+ end
143
+
144
+ def self.encrypt_aes(data, cashdata)
145
+
146
+ sandbox = Epayco.test ? "TRUE" : "FALSE"
147
+ @tags = JSON.parse(data)
148
+ @seted = {}
149
+ if cashdata
150
+ @tags.each {
151
+ |key, value|
152
+ @seted[lang_key(key)] = value
153
+ }
154
+ @seted["ip"] = local_ip
155
+ @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
156
+ else
157
+ @tags.each {
158
+ |key, value|
159
+ @seted[lang_key(key)] = encrypt(value, Epayco.privateKey)
160
+ }
161
+ @seted["ip"] = encrypt(local_ip, Epayco.privateKey)
162
+ @seted["enpruebas"] = encrypt(sandbox, Epayco.privateKey)
163
+ end
164
+
165
+
166
+
167
+
168
+ @seted["public_key"] = Epayco.apiKey
169
+ @seted["i"] = Base64.encode64("0000000000000000")
170
+ @seted["lenguaje"] = "ruby"
171
+ @seted["p"] = ""
172
+ return @seted
173
+ end
174
+
175
+ def self.encrypt(str, key)
176
+ cipher = OpenSSL::Cipher.new('AES-128-CBC')
177
+ cipher.encrypt
178
+ iv = "0000000000000000"
179
+ cipher.iv = iv
180
+ cipher.key = key.byteslice(0, cipher.key_len)
181
+ str = iv + str
182
+ data = cipher.update(str) + cipher.final
183
+ Base64.urlsafe_encode64(data)
184
+ end
185
+
186
+ # Traslate secure petitions
187
+ def self.lang_key key
188
+ file = File.read(File.dirname(__FILE__) + '/keylang.json')
189
+ data_hash = JSON.parse(file)
190
+ data_hash[key]
191
+ end
192
+
193
+
194
+ def self.authent(apiKey, privateKey)
195
+ @parmas = {}
196
+ @parmas["public_key"] = apiKey
197
+ @parmas["private_key"] = privateKey
198
+ headers = {
199
+ # :params => @parmas,
200
+ :Accept => 'application/json',
201
+ :content_type => 'application/json',
202
+ :type => 'sdk'
203
+ }
204
+ payload = @parmas.to_json
205
+ options = {
206
+ :headers => headers,
207
+ :user => apiKey,
208
+ :method => 'post',
209
+ :url => 'https://api.secure.payco.co/v1/auth/login',
210
+ :payload => payload
211
+ }
212
+
213
+ begin
214
+ response = execute_request(options)
215
+ return {} if response.code == 204 and method == :delete
216
+ JSON.parse(response.body, :symbolize_names => true)
217
+ rescue RestClient::Exception => e
218
+ handle_errors e
219
+ end
220
+
221
+ end
222
+
223
+ end
data/lib/keylang.json CHANGED
@@ -1,26 +1,35 @@
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": "metodoconfirmacion",
26
+ "end_date": "fechaexpiracion",
27
+ "splitpayment": "splitpayment",
28
+ "split_app_id": "split_app_id",
29
+ "split_merchant_id": "split_merchant_id",
30
+ "split_type": "split_type",
31
+ "split_primary_receiver": "split_primary_receiver",
32
+ "split_primary_receiver_fee": "split_primary_receiver_fee",
33
+ "split_rule": "split_rule",
34
+ "split_receivers": "split_receivers"
35
+ }