culqi 1.1.8

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12bf58ccff76e1bb52736c31d886a25953a4d7f7
4
+ data.tar.gz: 05d124c23b3a3ecfa44c042f3a095bf757d4cb3e
5
+ SHA512:
6
+ metadata.gz: 31ed099b08bc11b13ba96decf42509db47a3a8f7c0019a1f0c882c64a19b3189451de8060f05e68f9169a8d295a4cab9631a1f9a3373a792e7b122af822f2c11
7
+ data.tar.gz: 57508ebdb79c37a27ffacae9c58779f0bdf312888e96ae6ba9d76225849be67f341c10be80e1ecb5a84e69a48b648bc08ef8722afa316d7cc199149688b73aa5
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .env
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.12.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in culqiruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Augusto Samame
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # Culqiruby
2
+
3
+ This gem allows your Rails or Ruby app to quickly integrate with Culqi Payment processor. It handles Culqi's flavor of encryption, decryption and Culqi ticket creation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'culqiruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install culqiruby
20
+
21
+ ## Versiones
22
+
23
+ La versión 1.x de este gem podrá ser usada en producción con la versión 1.x del API de Culqi
24
+
25
+ Compatible con Ruby versiones 1.9.2 y superior
26
+
27
+ Compatible con Rails versiones 3.2 y superior
28
+
29
+
30
+ ## Requerimientos
31
+
32
+ Este gem requiere de la existencia de las siguientes environment variables en el ambiente de Desarrollo / Pruebas / Producción:
33
+
34
+ CULQI_KEY="llave de encriptación entregada por Culqi"
35
+
36
+ CULQI_ENDPOINT="dominio del API de Culqi"
37
+
38
+ CULQI_CODIGO_COMERCIO="código de comercio asignado por Culqi"
39
+
40
+
41
+
42
+ Para definir los environments variables en su ambiente recomendamos:
43
+
44
+
45
+ Ruby:
46
+ dotenv gem
47
+ https://github.com/bkeepers/dotenv
48
+
49
+
50
+ Rails:
51
+ figaro gem
52
+ https://github.com/laserlemon/figaro
53
+
54
+
55
+ ## Forma de Uso
56
+
57
+ Instanciamos el cliente Culqi para exponer sus métodos:
58
+
59
+ ```ruby
60
+ culqi = Culqi.default_client
61
+ ```
62
+
63
+ ## Métodos
64
+
65
+ ## crear_venta
66
+
67
+ Este método permitirá crear una venta en Culqi, para luego invocar el formulario según lo indicado por la documentación de Culqi.
68
+ Para que la creación de la venta sea exitosa, los siguientes atributos le deberán ser pasados al método como un hash:
69
+
70
+ numero_pedido, moneda, monto, descripcion, correo_electronico, cod_pais, ciudad, direccion, num_tel, id_usuario_comercio, nombres, apellidos
71
+
72
+ Ejemplo:
73
+
74
+ ```ruby
75
+ #instanciamos el cliente Culqi
76
+ culqi = Culqi.default_client
77
+
78
+ #creamos la venta
79
+
80
+ datos_venta = {
81
+ numero_pedido: '12367',
82
+ moneda: 'PEN',
83
+ monto: '5000',
84
+ descripcion: 'Venta de prueba',
85
+ correo_electronico: 'augustosamame@gmail.com',
86
+ cod_pais: 'PE',
87
+ ciudad: 'Lima',
88
+ direccion: 'Av Javier Prado 2320, San Borja',
89
+ num_tel: '986976309',
90
+ id_usuario_comercio: '2',
91
+ nombres: 'Augusto',
92
+ apellidos: 'Samame'
93
+ }
94
+
95
+ venta = culqi.crear_venta(datos_venta)
96
+
97
+ ```
98
+
99
+ El método crear_venta devolverá un arreglo con 2 valores: el primer valor es un string con el resultado de la creación de la venta y el segundo valor un hash con la respuesta del API de Culqi
100
+
101
+ Ej de respuesta exitosa:
102
+
103
+ {"mensaje_respuesta_usuario"=>"", "monto"=>"5000", "mensaje_respuesta"=>"Venta creada exitosamente.", "ticket"=>"6F6tIyUlOmrMKo3EI5rxcsvamwHRrKAEgsb", "codigo_respuesta"=>"venta_registrada", "numero_pedido"=>"1234", "codigo_comercio"=>"3zMquUkbF5s8", "informacion_venta"=>"EtEfsG1iD1Ur8YYHO98hRX1kXE2KOVlq4vbmmLVkKKX5eCBUFAHeuAhRrefRIwuUAHTWH6tiNWIthJ67Fi11LGkBEF861R2iQNjG5vsHSy8RsFuXfUU_zKTYYwGhaYkR4tAewbDRnBX3YKJOxPcMqA=="}
104
+
105
+
106
+ Ej de respuesta con error
107
+
108
+ {"mensaje_respuesta_usuario"=>"", "mensaje_respuesta"=>"La transacción ya existe. ", "codigo_respuesta"=>"parametro_invalido", "codigo_comercio"=>"3zMquUkbF5s8"}
109
+
110
+
111
+ ## Encryptor Class
112
+
113
+ Para poder utilizar los métodos de encriptación y desencriptación de Culqi, debemos instanciar la clase Culqi::Encryptor.
114
+ Recuerde que de acuerdo a los requerimientos del gem indicados arriba, el environment variable CULQI_KEY debe haber sido seteado para que esta clase funcione:
115
+
116
+
117
+ ```ruby
118
+ encryptor = Culqi::Encryptor.new
119
+ ```
120
+
121
+ Esto expondrá los métodos: encrypt y decrypt
122
+
123
+ ```ruby
124
+ texto_encriptado = encryptor.encrypt('Texto Prueba')
125
+
126
+ => "yozxCEGWPDw_uiHdsOXW-4EAuKDfCxCcd0GO2bESegE="
127
+
128
+ texto_plano = encryptor.decrypt("yozxCEGWPDw_uiHdsOXW-4EAuKDfCxCcd0GO2bESegE=")
129
+
130
+ => "Texto Prueba"
131
+
132
+ (su cadena encriptada será distinta ya que Culqi asigna una llave de encriptación distinta a cada cliente)
133
+ ```
134
+
135
+
136
+ ## Contributing
137
+
138
+ Bug reports and pull requests are welcome on GitHub at https://github.com/augustosamame/culqiruby.
139
+
140
+
141
+ ## License
142
+
143
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "culqiruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/culqi.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'culqi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'culqi'
8
+ spec.version = Culqi::VERSION
9
+ spec.authors = ['Augusto Samame']
10
+ spec.email = ['augustosamame@gmail.com']
11
+
12
+ spec.summary = %q{Facilitates integration with Culqi payment processor}
13
+ spec.description = %q{This gem allows your Ruby (1.9.2+) or Rails (3.2+) app to quickly integrate with Culqi Payment processor.
14
+ It handles Culqi's flavor of encryption, decryption and Culqi ticket creation.}
15
+ spec.homepage = 'https://github.com/augustosamame/culqiruby'
16
+ spec.license = 'MIT'
17
+ spec.required_ruby_version = '>= 1.9.2'
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = %w[ lib ]
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.12'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'minitest', '~> 5.0'
26
+ end
@@ -0,0 +1,51 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Culqi
5
+ class Client
6
+ ERROR_MESSAGES = {
7
+ failed_request: 'Error al crear venta, HTTP code:'
8
+ }
9
+
10
+ def initialize(key, endpoint, code)
11
+ @key = key
12
+ @url = URI(endpoint + '/api/v1/web/crear')
13
+ @code = code
14
+ end
15
+
16
+ def crear_venta(attrs)
17
+ sale = Culqi::Sale.new(attrs)
18
+ request = http_request(sale.payload)
19
+
20
+ http_response(request)
21
+ end
22
+
23
+ private
24
+
25
+ def http_client
26
+ http = Net::HTTP.new(@url.host, @url.port)
27
+ http.use_ssl = true
28
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
29
+ http
30
+ end
31
+
32
+ def http_request(payload)
33
+ request = Net::HTTP::Post.new(@url)
34
+ request['content-type'] = 'application/json'
35
+ request.body = { codigo_comercio: @code, informacion_venta: encryptor.encrypt(payload) }.to_json
36
+ request
37
+ end
38
+
39
+ def http_response(request)
40
+ response = http_client.request(request)
41
+
42
+ raise "#{ ERROR_MESSAGES[:failed_request] } #{ response.code }" unless response.code == '200'
43
+
44
+ JSON.parse(encryptor.decrypt(response.body))
45
+ end
46
+
47
+ def encryptor
48
+ @encryptor ||= Culqi::Encryptor.new
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,35 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module Culqi
5
+ class Encryptor
6
+ def initialize
7
+ @key = Base64.urlsafe_decode64(ENV['CULQI_KEY'])
8
+ end
9
+
10
+ def encrypt(plaintext)
11
+ cipher = build_cipher(:encrypt)
12
+ cipher.iv = iv = cipher.random_iv
13
+ decoded = iv + cipher.update(plaintext) + cipher.final
14
+
15
+ Base64.urlsafe_encode64(decoded)
16
+ end
17
+
18
+ def decrypt(encrypted)
19
+ decoded = Base64.urlsafe_decode64(encrypted)
20
+ decipher = build_cipher(:decrypt)
21
+ decipher.iv = decoded.slice!(0, 16)
22
+
23
+ decipher.update(decoded) + decipher.final
24
+ end
25
+
26
+ private
27
+
28
+ def build_cipher(type)
29
+ cipher = OpenSSL::Cipher::AES.new(256, :CBC)
30
+ cipher.send(type)
31
+ cipher.key = @key
32
+ cipher
33
+ end
34
+ end
35
+ end
data/lib/culqi/sale.rb ADDED
@@ -0,0 +1,45 @@
1
+ module Culqi
2
+ class Sale
3
+ DEFAULTS = {
4
+ moneda: 'PEN',
5
+ cod_pais: 'PE',
6
+ codigo_comercio: ENV['CULQI_CODIGO_COMERCIO']
7
+ }
8
+
9
+ attr_accessor :codigo_comercio,
10
+ :numero_pedido,
11
+ :moneda,
12
+ :monto,
13
+ :descripcion,
14
+ :correo_electronico,
15
+ :cod_pais,
16
+ :ciudad,
17
+ :direccion,
18
+ :num_tel,
19
+ :id_usuario_comercio,
20
+ :nombres,
21
+ :apellidos
22
+
23
+ def initialize(attrs)
24
+ attrs.each { |key, value| public_send("#{ key }=", value) }
25
+ end
26
+
27
+ def payload
28
+ {
29
+ codigo_comercio: (codigo_comercio || DEFAULTS[:codigo_comercio]),
30
+ numero_pedido: numero_pedido,
31
+ moneda: (moneda || DEFAULTS[:moneda]),
32
+ monto: monto,
33
+ descripcion: descripcion[0..79],
34
+ correo_electronico: correo_electronico[0..49],
35
+ cod_pais: (cod_pais || DEFAULTS[:cod_pais]),
36
+ ciudad: ciudad,
37
+ direccion: direccion,
38
+ num_tel: num_tel[0..14],
39
+ id_usuario_comercio: id_usuario_comercio,
40
+ nombres: nombres[0..49],
41
+ apellidos: apellidos[0..49]
42
+ }.to_json
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Culqi
2
+ VERSION = '1.1.8'
3
+ end
data/lib/culqi.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'culqi/encryptor'
2
+ require 'culqi/sale'
3
+ require 'culqi/client'
4
+
5
+ module Culqi
6
+ def self.default_client
7
+ variables = %w{CULQI_KEY CULQI_ENDPOINT CULQI_CODIGO_COMERCIO}
8
+ missing = variables - ENV.keys
9
+ raise "The following env vars are missing and are needed to use this gem: #{missing.join(', ')}." unless missing.empty?
10
+ Client.new(ENV['CULQI_KEY'], ENV['CULQI_ENDPOINT'], ENV['CULQI_CODIGO_COMERCIO'])
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: culqi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.8
5
+ platform: ruby
6
+ authors:
7
+ - Augusto Samame
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: |-
56
+ This gem allows your Ruby (1.9.2+) or Rails (3.2+) app to quickly integrate with Culqi Payment processor.
57
+ It handles Culqi's flavor of encryption, decryption and Culqi ticket creation.
58
+ email:
59
+ - augustosamame@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - culqi.gemspec
73
+ - lib/culqi.rb
74
+ - lib/culqi/client.rb
75
+ - lib/culqi/encryptor.rb
76
+ - lib/culqi/sale.rb
77
+ - lib/culqi/version.rb
78
+ homepage: https://github.com/augustosamame/culqiruby
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.9.2
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.4
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Facilitates integration with Culqi payment processor
102
+ test_files: []