redsys-rails 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -13
- data/app/controllers/redsys/tpv_controller.rb +23 -0
- data/app/models/redsys/tpv.rb +87 -0
- data/app/views/redsys/tpv/form.html.erb +8 -0
- data/config/routes.rb +3 -0
- data/lib/generators/redsys/install_generator.rb +15 -0
- data/lib/generators/redsys/notifications_generator.rb +18 -0
- data/lib/generators/templates/controllers/notifications_controller.rb +19 -0
- data/lib/generators/templates/redsys-rails.rb +12 -0
- data/lib/redsys-rails/version.rb +1 -1
- metadata +11 -4
- data/app/models/redsys_tpv.rb +0 -69
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b82968f40f5fc6e3c85abd2504ebf0a1bbb380f
|
4
|
+
data.tar.gz: 2df72a13f967db89c5324897f54c409acb73a2fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d85f101c14b361ab407069de6f2f2fbb67569ca9ecca43b27b2032f9a21867c5b1a433d85381efa3a81153ec5e00b6d71ce6cff4e0f5507576adf2a95965fe3
|
7
|
+
data.tar.gz: 035e5278265f1b4aabb372691a19795b5e274ddc1c042f07a4ab037f4860454bc54d929f8da59af2eee95d3613cd802c9395b1f92e99f7c191326df443c17d0f
|
data/README.md
CHANGED
@@ -1,33 +1,79 @@
|
|
1
1
|
# Redsys::Rails
|
2
2
|
|
3
|
-
|
3
|
+
Redsys-Rails es una pequeña solución para integrar de forma rápida y sencilla la pasarela de pago de [Redsys.es](http://www.redsys.es/).
|
4
4
|
|
5
|
-
|
5
|
+
## Requerimientos
|
6
|
+
- Esta gema solo proporciona las herramientas necesarias para integrar la opción de redirección desde la página web del comercio al TPV virtual contratado.
|
7
|
+
- Es necesario disponer de las claves de acceso que la entidad financiera habrá proporcionado al comercio para poder instalar esta gema.
|
6
8
|
|
7
|
-
##
|
9
|
+
## Instalación
|
8
10
|
|
9
|
-
|
11
|
+
Añade la siguiente línea al Gemfile de tu aplicación:
|
10
12
|
|
11
13
|
```ruby
|
12
14
|
gem 'redsys-rails'
|
13
15
|
```
|
14
16
|
|
15
|
-
|
17
|
+
Ejecuta:
|
16
18
|
|
17
19
|
$ bundle
|
18
20
|
|
19
|
-
|
21
|
+
O instala la gema tu mismo:
|
20
22
|
|
21
23
|
$ gem install redsys-rails
|
22
24
|
|
23
|
-
|
25
|
+
Genera el inicializador ejecutando el generador:
|
24
26
|
|
27
|
+
$ rails g redsys:install
|
25
28
|
|
29
|
+
Por último, configura los parámetros del TPV virutal en [config/initializers/redsys-rails.rb](lib/generators/templates/redsys-rails.rb)
|
26
30
|
|
27
|
-
##
|
31
|
+
## Utilización
|
28
32
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
En el controlador donde realice el tratamiento de su pedido, una vez obtenido el id único del mismo,
|
34
|
+
redireccione al formulario de salto del tpv virtual mediante el siguiente código.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
redirect_to redsys_form_path(amount: '', order: '', language: '')
|
38
|
+
```
|
39
|
+
|
40
|
+
Un ejemplo:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
redirect_to redsys_form_path(amount: '20.35', order: '0001', language: '001')
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Códigos de idioma
|
47
|
+
|
48
|
+
Castellano-001, Inglés-002, Catalán-003, Francés-004, Alemán-005, Holandés-006, Italiano-007, Sueco-008, Portugués-009,
|
49
|
+
Valenciano-010, Polaco-011, Gallego-012 y Euskera-013
|
50
|
+
|
51
|
+
#### URL de retorno
|
52
|
+
|
53
|
+
Si no ha configurado las url's de retorno para transacción correcta y para transacción fallida en el panel de control de su comercio,
|
54
|
+
puede proporcionarlas en la redirección mediante los parámetros url_ok y url_ko.
|
55
|
+
|
56
|
+
Un ejemplo:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
redirect_to redsys_form_path(amount: '20.35', order: '0001', language: '001', url_ok: 'http://misite.com/pedido_ok', url_ko: 'http://misite.com/pedido_error')
|
60
|
+
```
|
61
|
+
|
62
|
+
## Notificación on-line
|
63
|
+
|
64
|
+
Instalación del webhook para la notificación online.
|
65
|
+
|
66
|
+
$ rails g redsys:notifications
|
67
|
+
|
68
|
+
El módulo es funcional y hace accesible una ruta para que el TPV virtual pueda notificar la transacción. Es aquí donde el comercio
|
69
|
+
ha de realizar las acciones necesarias, como por ejemplo actualizar el estado de su pedido confirmando el pago.
|
70
|
+
|
71
|
+
*Falta una explicación más detallada de esta funcionalidad.*
|
72
|
+
|
73
|
+
## Contribuir
|
74
|
+
|
75
|
+
1. Fork ( https://github.com/[my-github-username]/redsys-rails/fork )
|
76
|
+
2. Crea la rama de tu funcionalidad (`git checkout -b my-new-feature`)
|
77
|
+
3. Commita tus cambios (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push a la rama (`git push origin my-new-feature`)
|
79
|
+
5. Crea un nuevo Pull Request
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Redsys
|
2
|
+
class TpvController < ApplicationController
|
3
|
+
skip_before_filter :verify_authenticity_token, only: [:confirmation]
|
4
|
+
|
5
|
+
#
|
6
|
+
# Formulario de salto a la pasarela de pago
|
7
|
+
# - amount:decimal => Importe a cobrar
|
8
|
+
# - order:integer => identidificador del pedido/reserva, ha de ser único
|
9
|
+
# - language:string => '001' Español, '002' Inglés...
|
10
|
+
# - url_ok:string => url de vuelta del tpv para pago correcto
|
11
|
+
# - url_ko:string => url de vuelta del tpv cuando ocurre un error
|
12
|
+
#
|
13
|
+
def form
|
14
|
+
amount = BigDecimal.new(params[:amount] || '0')
|
15
|
+
order = Integer(params[:order] || '0')
|
16
|
+
language = params[:language]
|
17
|
+
url_ok = params[:url_ok]
|
18
|
+
url_ko = params[:url_ko]
|
19
|
+
@tpv = Redsys::Tpv.new(amount, order, language, redsys_notification_url, url_ok, url_ko)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'base64'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Redsys
|
6
|
+
class Tpv
|
7
|
+
attr_accessor :amount, :language, :order, :currency, :merchant_code, :terminal,
|
8
|
+
:transaction_type, :merchant_url, :url_ok, :url_ko, :sha1, :signature
|
9
|
+
|
10
|
+
def self.tpv_url
|
11
|
+
Rails.configuration.redsys_rails[:url]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.signature_version
|
15
|
+
Rails.configuration.redsys_rails[:signature_version]
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(amount, order, language, merchant_url = nil, url_ok = nil, url_ko = nil)
|
19
|
+
amount ||= 0
|
20
|
+
order ||= 0
|
21
|
+
language ||= '001'
|
22
|
+
merchant_url ||= ''
|
23
|
+
url_ok ||= ''
|
24
|
+
url_ko ||= ''
|
25
|
+
|
26
|
+
@amount = (amount * 100).to_i.to_s
|
27
|
+
@order = order.to_s.rjust(4, '0')
|
28
|
+
@language = language
|
29
|
+
@merchant_url = merchant_url
|
30
|
+
@url_ok = url_ok
|
31
|
+
@url_ko = url_ko
|
32
|
+
|
33
|
+
@currency = Rails.configuration.redsys_rails[:merchant_currency]
|
34
|
+
@merchant_code = Rails.configuration.redsys_rails[:merchant_code]
|
35
|
+
@terminal = Rails.configuration.redsys_rails[:merchant_terminal]
|
36
|
+
@transaction_type = Rails.configuration.redsys_rails[:merchant_transaction_type]
|
37
|
+
end
|
38
|
+
|
39
|
+
def merchant_params
|
40
|
+
"#{Base64.strict_encode64(merchant_params_json)}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def merchant_params_json
|
44
|
+
merchant_parameters = {
|
45
|
+
:DS_MERCHANT_AMOUNT => @amount,
|
46
|
+
:DS_MERCHANT_ORDER => @order,
|
47
|
+
:DS_MERCHANT_MERCHANTCODE => @merchant_code,
|
48
|
+
:DS_MERCHANT_CURRENCY => @currency,
|
49
|
+
:DS_MERCHANT_TRANSACTIONTYPE => @transaction_type,
|
50
|
+
:DS_MERCHANT_TERMINAL => @terminal,
|
51
|
+
:DS_MERCHANT_MERCHANTURL => @merchant_url,
|
52
|
+
:DS_MERCHANT_URLOK => @url_ok,
|
53
|
+
:DS_MERCHANT_URLKO => @url_ko
|
54
|
+
}
|
55
|
+
JSON.generate(merchant_parameters)
|
56
|
+
end
|
57
|
+
|
58
|
+
def merchant_signature_3des
|
59
|
+
Base64.strict_encode64(encrypt_3DES(@order, Base64.strict_decode64(Rails.configuration.redsys_rails[:sha_256_key])))
|
60
|
+
end
|
61
|
+
|
62
|
+
def merchant_signature
|
63
|
+
key = Base64.strict_decode64(Rails.configuration.redsys_rails[:sha_256_key])
|
64
|
+
key = encrypt_3DES(@order, key)
|
65
|
+
encrypt_mac256(merchant_params, key)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def encrypt_mac256(data, key)
|
71
|
+
Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, data))
|
72
|
+
end
|
73
|
+
|
74
|
+
def encrypt_3DES(data, key)
|
75
|
+
cipher = OpenSSL::Cipher::Cipher.new('DES3')
|
76
|
+
cipher.encrypt
|
77
|
+
cipher.key = key
|
78
|
+
cipher.padding = 0
|
79
|
+
block_length = 8
|
80
|
+
data_str = data
|
81
|
+
data_str += "\0" until data_str.bytesize % block_length == 0
|
82
|
+
output = cipher.update(data_str)
|
83
|
+
output << cipher.final
|
84
|
+
output
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<%= form_tag(Redsys::Tpv.tpv_url, id: 'redsys_tpv_form') do %>
|
2
|
+
<%= hidden_field_tag 'Ds_SignatureVersion', Redsys::Tpv.signature_version %>
|
3
|
+
<%= hidden_field_tag 'Ds_MerchantParameters', @tpv.merchant_params %>
|
4
|
+
<%= hidden_field_tag 'Ds_Signature', @tpv.merchant_signature %>
|
5
|
+
<% end %>
|
6
|
+
<script type="text/javascript">
|
7
|
+
document.getElementById("redsys_tpv_form").submit();
|
8
|
+
</script>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/named_base'
|
3
|
+
|
4
|
+
module Redsys
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
source_root File.expand_path("../../templates", __FILE__)
|
8
|
+
|
9
|
+
def copy_initializer
|
10
|
+
template "redsys-rails.rb", "config/initializers/redsys-rails.rb"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/named_base'
|
3
|
+
|
4
|
+
module Redsys
|
5
|
+
module Generators
|
6
|
+
class NotificationsGenerator < Rails::Generators::Base
|
7
|
+
source_root File.expand_path("../../templates", __FILE__)
|
8
|
+
|
9
|
+
def copy_controller
|
10
|
+
template "controllers/notifications_controller.rb", "app/controllers/redsys/notifications_controller.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_routes
|
14
|
+
route "post 'redsys/notification' => 'redsys/notifications#notification', as: :redsys_notification"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Redsys
|
2
|
+
class NotificationsController < ApplicationController
|
3
|
+
skip_before_filter :verify_authenticity_token
|
4
|
+
|
5
|
+
#
|
6
|
+
# Tratamiento para la notificación online
|
7
|
+
# - Ds_Response == "0000" => Transacción correcta
|
8
|
+
#
|
9
|
+
def notification
|
10
|
+
json_params = JSON.parse(Base64.strict_decode64(params[:Ds_MerchantParameters]))
|
11
|
+
if json_params["Ds_Response"].present? && json_params["Ds_Response"] == "0000"
|
12
|
+
status = :ok
|
13
|
+
else
|
14
|
+
status = :bad_request
|
15
|
+
end
|
16
|
+
render nothing: true, layout: false, status: status
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#
|
2
|
+
# * Valores proporcionados por su entidad financiera
|
3
|
+
#
|
4
|
+
Rails.configuration.redsys_rails = {
|
5
|
+
url: '', # * Url TPV
|
6
|
+
sha_256_key: '', # * Clave para firma HMAC SHA256
|
7
|
+
merchant_code: '', # * Código de comercio
|
8
|
+
merchant_terminal: '', # * Terminal
|
9
|
+
merchant_transaction_type: '', # * Tipo de transacción
|
10
|
+
merchant_currency: '978', # * 978: euro, 840: dólares, 826: libras, 392: yenes
|
11
|
+
signature_version: 'HMAC_SHA256_V1' # Versión firma HMAC SHA256
|
12
|
+
}
|
data/lib/redsys-rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redsys-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- guzmanweb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01
|
11
|
+
date: 2016-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -50,7 +50,14 @@ files:
|
|
50
50
|
- LICENSE.txt
|
51
51
|
- README.md
|
52
52
|
- Rakefile
|
53
|
-
- app/
|
53
|
+
- app/controllers/redsys/tpv_controller.rb
|
54
|
+
- app/models/redsys/tpv.rb
|
55
|
+
- app/views/redsys/tpv/form.html.erb
|
56
|
+
- config/routes.rb
|
57
|
+
- lib/generators/redsys/install_generator.rb
|
58
|
+
- lib/generators/redsys/notifications_generator.rb
|
59
|
+
- lib/generators/templates/controllers/notifications_controller.rb
|
60
|
+
- lib/generators/templates/redsys-rails.rb
|
54
61
|
- lib/redsys-rails.rb
|
55
62
|
- lib/redsys-rails/version.rb
|
56
63
|
- redsys-rails.gemspec
|
@@ -74,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
81
|
version: '0'
|
75
82
|
requirements: []
|
76
83
|
rubyforge_project:
|
77
|
-
rubygems_version: 2.4.
|
84
|
+
rubygems_version: 2.4.5
|
78
85
|
signing_key:
|
79
86
|
specification_version: 4
|
80
87
|
summary: A Ruby gem for communicating with the payment platform of Redsys
|
data/app/models/redsys_tpv.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'openssl'
|
2
|
-
require 'base64'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
class RedsysTpv
|
6
|
-
attr_accessor :amount, :language, :order, :currency, :merchant_code, :terminal,
|
7
|
-
:transaction_type, :merchant_url, :url_ok, :url_ko, :sha1, :signature
|
8
|
-
|
9
|
-
def initialize(amount, order, language)
|
10
|
-
@amount = (amount * 100).to_i.to_s
|
11
|
-
@language = (language == :es) ? '001' : '002'
|
12
|
-
@order = order.to_s.rjust(4, '0')
|
13
|
-
|
14
|
-
@currency = Rails.application.secrets.tpv_merchant_currency
|
15
|
-
@merchant_code = Rails.application.secrets.tpv_merchant_code
|
16
|
-
@terminal = Rails.application.secrets.tpv_merchant_terminal
|
17
|
-
@transaction_type = Rails.application.secrets.tpv_merchant_transaction_type
|
18
|
-
|
19
|
-
#@merchant_url = "#{Rails.application.secrets.tpv_merchant_url}?booking_id=#{booking.id}&language=#{I18n.locale}"
|
20
|
-
end
|
21
|
-
|
22
|
-
def merchant_params
|
23
|
-
Base64.strict_encode64(merchant_params_json)
|
24
|
-
end
|
25
|
-
|
26
|
-
def merchant_params_json
|
27
|
-
merchant_parameters = {
|
28
|
-
:DS_MERCHANT_AMOUNT => @amount,
|
29
|
-
:DS_MERCHANT_ORDER => @order,
|
30
|
-
:DS_MERCHANT_MERCHANTCODE => @merchant_code,
|
31
|
-
:DS_MERCHANT_CURRENCY => @currency,
|
32
|
-
:DS_MERCHANT_TRANSACTIONTYPE => @transaction_type,
|
33
|
-
:DS_MERCHANT_TERMINAL => @terminal,
|
34
|
-
:DS_MERCHANT_MERCHANTURL => @merchant_url,
|
35
|
-
:DS_MERCHANT_URLOK => @url_ok,
|
36
|
-
:DS_MERCHANT_URLKO => @url_ko
|
37
|
-
}
|
38
|
-
JSON.generate(merchant_parameters)
|
39
|
-
end
|
40
|
-
|
41
|
-
def merchant_signature_3des
|
42
|
-
Base64.strict_encode64(encrypt_3DES(@order, Base64.strict_decode64(Rails.application.secrets.tpv_sha_256_key)))
|
43
|
-
end
|
44
|
-
|
45
|
-
def merchant_signature
|
46
|
-
key = Base64.strict_decode64(Rails.application.secrets.tpv_sha_256_key)
|
47
|
-
key = encrypt_3DES(@order, key)
|
48
|
-
encrypt_mac256(merchant_params, key)
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def encrypt_mac256(data, key)
|
54
|
-
Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, data))
|
55
|
-
end
|
56
|
-
|
57
|
-
def encrypt_3DES(data, key)
|
58
|
-
cipher = OpenSSL::Cipher::Cipher.new('DES3')
|
59
|
-
cipher.encrypt
|
60
|
-
cipher.key = key
|
61
|
-
cipher.padding = 0
|
62
|
-
block_length = 8
|
63
|
-
data_str = data
|
64
|
-
data_str += "\0" until data_str.bytesize % block_length == 0
|
65
|
-
output = cipher.update(data_str)
|
66
|
-
output << cipher.final
|
67
|
-
output
|
68
|
-
end
|
69
|
-
end
|