Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Epayco
Epayco Ruby
=====

Ruby wrapper for Epayco API
Expand All @@ -20,6 +20,7 @@ $ gem install epayco-sdk-ruby
## Usage

```ruby

require 'epayco-sdk-ruby'

Epayco.apiKey = 'YOU_PUBLIC_API_KEY'
Expand Down Expand Up @@ -115,6 +116,7 @@ update_customer_info = {

begin
customer = Epayco::Customers.update "id_customer", update_customer_info
puts customer
rescue Epayco::Error => e
puts e
end
Expand All @@ -132,6 +134,7 @@ delete_customer_info = {

begin
customer = Epayco::Customers.delatetetoken delete_customer_info
puts customer
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -202,6 +205,7 @@ plan_info = {

begin
plan = Epayco::Plan.create plan_info
puts plan
rescue Epayco::Error => e
puts e
end
Expand All @@ -212,6 +216,7 @@ end
```ruby
begin
plan = Epayco::Plan.get "coursereact"
puts plan
rescue Epayco::Error => e
puts e
end
Expand All @@ -222,6 +227,7 @@ end
```ruby
begin
plan = Epayco::Plan.list
puts plan
rescue Epayco::Error => e
puts e
end
Expand All @@ -232,6 +238,7 @@ end
```ruby
begin
plan = Epayco::Plan.delete "coursereact"
puts plan
rescue Epayco::Error => e
puts e
end
Expand All @@ -258,7 +265,7 @@ plan_info = {
}
begin
plan = Epayco::Plan.update "coursereact2", plan_info
print(plan)
puts plan
rescue Epayco::Error => e
puts e
end
Expand All @@ -282,7 +289,7 @@ subscription_info = {

begin
sub = Epayco::Subscriptions.create subscription_info
assert(sub)
puts sub
rescue Epayco::Error => e
puts e
end
Expand All @@ -293,6 +300,7 @@ end
```ruby
begin
sub = Epayco::Subscriptions.get "id_subscription"
puts sub
rescue Epayco::Error => e
puts e
end
Expand All @@ -303,6 +311,7 @@ end
```ruby
begin
sub = Epayco::Subscriptions.list
puts sub
rescue Epayco::Error => e
puts e
end
Expand All @@ -313,6 +322,7 @@ end
```ruby
begin
sub = Epayco::Subscriptions.cancel "id_subscription"
puts sub
rescue Epayco::Error => e
puts e
end
Expand All @@ -334,6 +344,7 @@ subscription_info = {

begin
sub = Epayco::Subscriptions.charge subscription_info
puts sub
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -387,6 +398,7 @@ pse_info = {

begin
pse = Epayco::Bank.create pse_info
puts pse
rescue Epayco::Error => e
puts e
end
Expand All @@ -397,6 +409,7 @@ end
```ruby
begin
pse = Epayco::Bank.get "ticketId"
puts pse
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -426,6 +439,7 @@ payment_info = {
}
begin
split_pse = Epayco::Bank.create pse_info
puts split_pse
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -470,6 +484,7 @@ cash_info = {

begin
cash = Epayco::Cash.create cash_info, "efecty"
puts cash
rescue Epayco::Error => e
puts e
end
Expand All @@ -491,6 +506,7 @@ end
```ruby
begin
cash = Epayco::Cash.get "transactionID"
puts cash
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -520,6 +536,7 @@ payment_info = {
}
begin
split_cash = Epayco::Cash.create cash_info, "efecty"
puts split_cash
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -564,6 +581,7 @@ payment_info = {

begin
pay = Epayco::Charge.create payment_info
puts pay
rescue Epayco::Error => e
puts e
end
Expand All @@ -574,6 +592,7 @@ end
```ruby
begin
pay = Epayco::Charge.get "transactionID"
puts pay
rescue Epayco::Error => e
puts e
end
Expand All @@ -596,6 +615,7 @@ payment_info = {
}
begin
split_payment_info = Epayco::Charge.create payment_info
puts split_payment_info
rescue Epayco::Error => e
puts e
end
Expand All @@ -620,6 +640,7 @@ payment_info = {
}
begin
split_payment_info = Epayco::Charge.create payment_info
puts split_payment_info
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -664,6 +685,7 @@ payment_info = {

begin
daviplata = Epayco::Daviplata.create payment_info
puts daviplata
rescue Epayco::Error => e
puts e
end
Expand All @@ -679,6 +701,7 @@ confirm = {
}
begin
daviplata = Epayco::Daviplata.confirm confirm
puts daviplata
rescue Epayco::Error => e
puts e
end
Expand Down Expand Up @@ -726,6 +749,7 @@ payment_info = {
}
begin
safetypay = Epayco::Safetypay.create payment_info
puts safetypay
rescue Epayco::Error => e
puts e
end
Expand Down
117 changes: 71 additions & 46 deletions lib/epayco.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ class Error < StandardError

# Get code, lang and show custom error
def initialize(code, lang)
begin
file = open("https://multimedia.epayco.co/message-api/errors.json").read
data_hash = JSON.parse(file)
rescue OpenURI::HTTPError, Errno::ENOENT => e
# Si no se puede acceder al archivo remoto, usar un archivo local como respaldo
local_file_path = File.join(File.dirname(__FILE__), 'errors.json')
if File.exist?(local_file_path)
file = File.read(local_file_path)
data_hash = JSON.parse(file)
else
raise "No se pudo cargar el archivo de errores: #{e.message}"
end
end
# Usar solo errores por defecto
data_hash = {
"100" => {"ES" => "[100] Error inicializando el sdk, compruebe que los parametros son correctos", "EN" => "[100] SDK initialization error, please check the parameters are correct"},
"101" => {"ES" => "[101] Error de comunicación con el servicio", "EN" => "[101] Communication error with the service"},
"102" => {"ES" => "[102] Error desconocido, verifique con soporte", "EN" => "[102] Unknown error, please verify with support"},
"103" => {"ES" => "[103] La información suministrada es erronea, verifique con la documentación", "EN" => "[103] The information provided is incorrect, please check the documentation"},
"104" => {"ES" => "[104] El servicio no se puede autenticar, verifique su llave pública o llave privada", "EN" => "[104] The service cannot authenticate, please verify your public key or private key"},
"105" => {"ES" => "[105] No de puede obtener comunicación con el servicio", "EN" => "[105] Cannot establish communication with the service"},
"106" => {"ES" => "[106] Llave pública o Llave privada inválida, compruebela", "EN" => "[106] Public key or Private key invalid, please check"},
"107" => {"ES" => "[107] Método no válido, compruebe la petición", "EN" => "[107] Invalid method, please check the request"},
"108" => {"ES" => "[108] Error al encriptar los datos, re intente la operación", "EN" => "[108] Error encrypting data, please retry the operation"},
"109" => {"ES" => "[109] Método de pago en efectivo no válido, unicamnete soportados: efecty, baloto, gana, redservi, puntored, sured, apostar, susuerte", "EN" => "[109] Invalid cash payment method, only supported: efecty, baloto, gana, redservi, puntored, sured, apostar, susuerte"},

"default" => {"ES" => "Error desconocido", "EN" => "Unknown error"}
}

error = "Error"
if(data_hash[code.to_s])
Expand All @@ -45,12 +47,10 @@ def each
end
end

# Endpoints
@api_base = 'https://api.secure.payco.co'
@api_base_secure = 'https://secure.payco.co'
@api_base_apify = 'https://apify.epayco.co'
@api_entorno = "/restpagos"

@api_base = (ENV['BASE_URL_SDK'] && !ENV['BASE_URL_SDK'].empty?) ? ENV['BASE_URL_SDK'] : 'https://api.secure.payco.co'
@api_base_secure = (ENV['BASE_URL_SECURE_SDK'] && !ENV['BASE_URL_SECURE_SDK'].empty?) ? ENV['BASE_URL_SECURE_SDK'] : 'https://secure.payco.co'
@api_entorno = (ENV['BASE_URL_ENTORNO_SDK'] && !ENV['BASE_URL_ENTORNO_SDK'].empty?) ? ENV['BASE_URL_ENTORNO_SDK'] : '/restpagos'
@api_base_apify = (ENV['BASE_APIFY_SDK'] && !ENV['BASE_APIFY_SDK'].empty?) ? ENV['BASE_APIFY_SDK'] : 'https://apify.epayco.co'


# Init sdk parameters
Expand All @@ -68,26 +68,35 @@ def self.request(method, url, extra=nil, params={}, headers={}, switch, cashdata
if !apiKey || !privateKey || !lang
raise Error.new('100', lang)
end
params["extras_epayco"] = {extra5:"P45"}

if method == :post && params && params.is_a?(Hash) && !params.key?("extras_epayco")
params["extras_epayco"] = {extra5:"P45"}
end
payload = JSON.generate(params) if method == :post || method == :patch
params = nil unless method == :get

# Switch secure or api or apify
if apify
if method == :post
@tags = JSON.parse(payload)
seted = {}
file = File.read(File.dirname(__FILE__) + '/keylang_apify.json')
data_hash = JSON.parse(file)
@tags.each {
|key, value|
if data_hash[key]
seted[data_hash[key]] = value
else
seted[key] = value
end
}
payload = seted.to_json
begin
@tags = JSON.parse(payload)
seted = {}
file = File.read(File.dirname(__FILE__) + '/keylang_apify.json')
data_hash = JSON.parse(file)
@tags.each {
|key, value|
if data_hash[key]
seted[data_hash[key]] = value
else
seted[key] = value
end
}
payload = seted.to_json
rescue JSON::ParserError => e
raise Error.new('103', lang) # Error en formato de datos
rescue Errno::ENOENT => e
raise Error.new('101', lang) # Error de comunicación
end
end
url = @api_base_apify + url
elsif switch
Expand Down Expand Up @@ -169,8 +178,12 @@ def self.local_ip
def self.encrypt_aes(data, cashdata)

sandbox = Epayco.test ? "TRUE" : "FALSE"
@tags = JSON.parse(data)
@seted = {}
begin
@tags = JSON.parse(data)
@seted = {}
rescue JSON::ParserError => e
raise Error.new('103', Epayco.lang) # Error en formato de datos
end
if cashdata
@tags.each {
|key, value|
Expand Down Expand Up @@ -202,21 +215,33 @@ def self.encrypt_aes(data, cashdata)
end

def self.encrypt(str, key)
cipher = OpenSSL::Cipher.new('AES-128-CBC')
cipher.encrypt
iv = "0000000000000000"
cipher.iv = iv
cipher.key = key.byteslice(0, cipher.key_len)
str = iv + str
data = cipher.update(str) + cipher.final
Base64.urlsafe_encode64(data)
begin
cipher = OpenSSL::Cipher.new('AES-128-CBC')
cipher.encrypt
iv = "0000000000000000"
cipher.iv = iv
cipher.key = key.byteslice(0, cipher.key_len)
str = iv + str
data = cipher.update(str) + cipher.final
Base64.urlsafe_encode64(data)
rescue OpenSSL::CipherError => e
raise Error.new('108', Epayco.lang) # Error al encriptar los datos
rescue ArgumentError => e
raise Error.new('103', Epayco.lang) # Error en parámetros de encriptación
end
end

# Traslate secure petitions
def self.lang_key key
file = File.read(File.dirname(__FILE__) + '/keylang.json')
data_hash = JSON.parse(file)
data_hash[key]
begin
file = File.read(File.dirname(__FILE__) + '/keylang.json')
data_hash = JSON.parse(file)
data_hash[key]
rescue Errno::ENOENT => e
raise Error.new('101', Epayco.lang) # Error de comunicación
rescue JSON::ParserError => e
raise Error.new('103', Epayco.lang) # Error en formato de datos
end
end

def self.authent(apiKey, privateKey, apify, lang)
Expand Down
2 changes: 1 addition & 1 deletion lib/epayco/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def list params={}, extra=nil
elsif self.url == "subscriptions"
url = "/recurring/v1/subscriptions/" + Epayco.apiKey
elsif self.url == "bank"
url = "/pse/bancos.json?public_key=" + Epayco.apiKey
url = "/pse/bancos.json?public_key=" + Epayco.apiKey + "&test=" + (Epayco.test ? "TRUE" : "FALSE")
end
Epayco.request :get, url, extra, params, self.switch, cashdata, dt, apify = false
end
Expand Down
Loading