sermepa_web_tpv 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cea5def5710b4a30eaf4e4132c73897b4cb717aa
4
+ data.tar.gz: 0980e6c6776408beac945ba5e9b147869fef9fbf
5
+ SHA512:
6
+ metadata.gz: b8ce025ec8bfa7e0bc1720323f4a5b98da8584ea1fb6a505acc1eea731cbded29805680fa59ca8becc6a5bfe40a58f47cb4ff034e4b4906cab131ae65700e49b
7
+ data.tar.gz: 29be593d02f6ef515ee2e396171e4359b2f9fdde8f66e9d496d034e73dc8a5c7a09cd48ecb94e9b653cd90e8316a54e64ccb8d285e7bc80e8664461818840723
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Sermepa web TPV
2
2
 
3
- Add simple web payment tpv to your rails application
3
+ Add simple web payment tpv to your rails application. This version is compliance with new hmac 256 signature. For older sha1 signature use version 0.0.2.
4
4
 
5
5
  Example app source at: https://github.com/ritxi/sermepa_web_tpv_sample_app
6
6
 
@@ -24,7 +24,7 @@ Steps to use it:
24
24
  module MyApp
25
25
  class Application < Rails::Application
26
26
  config.web_tpv.bank_url = 'https://sis-t.sermepa.es:25443/sis/realizarPago'
27
- config.web_tpv.response_host = 'my_application_host.com'
27
+ config.web_tpv.response_host = 'https://my_application_host.com'
28
28
  config.web_tpv.merchant_code = '000000000'
29
29
  config.web_tpv.terminal = 1
30
30
  config.web_tpv.callback_response_path = '/payments/validate'
@@ -36,6 +36,7 @@ module MyApp
36
36
  config.web_tpv.transaction_type = 0
37
37
  config.web_tpv.language = '003' #Catala
38
38
  config.web_tpv.merchant_name = 'MY MERCHANT NAME'
39
+ config.web_tpv.pay_methods = 'C' # Only credit card
39
40
  end
40
41
  end
41
42
  ```
@@ -42,8 +42,12 @@ module SermepaWebTpv
42
42
  mattr_accessor :redirect_failure_path
43
43
  @@redirect_failure_path = nil #"/payments/failure"
44
44
 
45
+ mattr_accessor :pay_methods
46
+ @@pay_methods = '' # 'C' for credit card only (see official doc)
47
+
45
48
  autoload :Request, 'sermepa_web_tpv/request'
46
49
  autoload :Response, 'sermepa_web_tpv/response'
50
+ autoload :Signature, 'sermepa_web_tpv/signature'
47
51
 
48
52
  end
49
53
  require 'sermepa_web_tpv/persistence/active_record'
@@ -1,5 +1,4 @@
1
1
  require 'uri'
2
- require 'digest/sha1'
3
2
 
4
3
  module SermepaWebTpv
5
4
  class Request < Struct.new(:transaction, :description)
@@ -9,38 +8,45 @@ module SermepaWebTpv
9
8
  SermepaWebTpv.bank_url
10
9
  end
11
10
 
12
- def options
13
- optional_options.merge(must_options)
14
- end
15
-
16
11
  def transact(&block)
17
12
  generate_transaction_number!
18
13
  yield(transaction)
19
- self
14
+ transaction
20
15
  end
21
16
 
22
- private
17
+ def options
18
+ optional_options.merge(must_options)
19
+ end
23
20
 
24
- def transaction_number_attribute
25
- SermepaWebTpv.transaction_model_transaction_number_attribute
21
+ def merchant_parameters_json
22
+ options.to_json
26
23
  end
27
24
 
28
- def transaction_model_amount_attribute
29
- SermepaWebTpv.transaction_model_amount_attribute
25
+ def merchant_parameters
26
+ Base64.urlsafe_encode64(
27
+ merchant_parameters_json).split("\n").join('')
30
28
  end
31
29
 
32
- def amount
33
- (transaction_amount * 100).to_i.to_s
30
+ def params
31
+ {
32
+ 'Ds_SignatureVersion' => 'HMAC_SHA256_V1',
33
+ 'Ds_MerchantParameters' => merchant_parameters,
34
+ 'Ds_Signature' => signature
35
+ }
36
+ end
37
+ private
38
+
39
+ def signature
40
+ @signature ||= Signature.new(self).signature
34
41
  end
35
42
 
36
43
  def must_options
37
44
  {
38
45
  'Ds_Merchant_Amount' => amount,
39
- 'Ds_Merchant_Currency' => SermepaWebTpv.currency, #EURO
40
46
  'Ds_Merchant_Order' => transaction_number,
41
47
  'Ds_Merchant_ProductDescription' => description,
48
+ 'Ds_Merchant_Currency' => SermepaWebTpv.currency,
42
49
  'Ds_Merchant_MerchantCode' => SermepaWebTpv.merchant_code,
43
- 'Ds_Merchant_MerchantSignature' => signature,
44
50
  'Ds_Merchant_Terminal' => SermepaWebTpv.terminal,
45
51
  'Ds_Merchant_TransactionType' => SermepaWebTpv.transaction_type,
46
52
  'Ds_Merchant_ConsumerLanguage' => SermepaWebTpv.language,
@@ -48,16 +54,6 @@ module SermepaWebTpv
48
54
  }
49
55
  end
50
56
 
51
- def signature
52
- #Ds_Merchant_Amount + Ds_Merchant_Order +Ds_Merchant_MerchantCode + Ds_Merchant_Currency +Ds_Merchant_TransactionType + Ds_Merchant_MerchantURL + CLAVE SECRETA
53
- merchant_code = SermepaWebTpv.merchant_code
54
- currency = SermepaWebTpv.currency
55
- transaction_type = SermepaWebTpv.transaction_type
56
- callback_url = url_for(:callback_response_path)
57
- merchant_secret_key = SermepaWebTpv.merchant_secret_key
58
- Digest::SHA1.hexdigest("#{amount}#{transaction_number}#{merchant_code}#{currency}#{transaction_type}#{callback_url}#{merchant_secret_key}").upcase
59
- end
60
-
61
57
  # Available options
62
58
  # redirect_success_path
63
59
  # redirect_failure_path
@@ -66,19 +62,29 @@ module SermepaWebTpv
66
62
  host = SermepaWebTpv.response_host
67
63
  path = SermepaWebTpv.send(option)
68
64
 
69
- if host.present? && path.present?
70
- URI.join("http://#{host}", path).to_s
71
- end
65
+ return if !host.present? || !path.present?
66
+ URI.join(host, path).to_s
72
67
  end
73
68
 
74
69
  def optional_options
75
70
  {
76
71
  'Ds_Merchant_Titular' => SermepaWebTpv.merchant_name,
77
72
  'Ds_Merchant_UrlKO' => url_for(:redirect_failure_path),
78
- 'Ds_Merchant_UrlOK' => url_for(:redirect_success_path)
79
- }.delete_if {|key, value| value.blank? }
73
+ 'Ds_Merchant_UrlOK' => url_for(:redirect_success_path),
74
+ 'Ds_Merchant_PayMethods' => SermepaWebTpv.pay_methods
75
+ }.delete_if { |_key, value| value.blank? }
76
+ end
77
+
78
+ def transaction_number_attribute
79
+ SermepaWebTpv.transaction_model_transaction_number_attribute
80
80
  end
81
81
 
82
+ def transaction_model_amount_attribute
83
+ SermepaWebTpv.transaction_model_amount_attribute
84
+ end
82
85
 
86
+ def amount
87
+ (transaction_amount * 100).to_i.to_s
88
+ end
83
89
  end
84
90
  end
@@ -1,26 +1,37 @@
1
- require 'digest/sha1'
1
+
2
2
 
3
3
  module SermepaWebTpv
4
- class Response < Struct.new(:params)
4
+ BASE = Struct.new(:params) unless defined?(BASE)
5
+ class Response < BASE
6
+ def transaction_number
7
+ merchant_parameters_hash['Ds_Order']
8
+ end
9
+
10
+
5
11
  def valid?
6
- params[:Ds_Signature] == signature
12
+ params['Ds_Signature'] == signature
7
13
  end
8
14
 
9
15
  def success?
10
- params[:Ds_Response].to_i == 0
16
+ merchant_parameters_hash['Ds_Response'].to_i == 0
11
17
  end
12
18
 
13
- private
14
19
  def signature
15
- response = %W(
16
- #{params[:Ds_Amount]}
17
- #{params[:Ds_Order]}
18
- #{params[:Ds_MerchantCode]}
19
- #{params[:Ds_Currency]}
20
- #{params[:Ds_Response]}
21
- #{SermepaWebTpv.merchant_secret_key}
22
- ).join
23
- Digest::SHA1.hexdigest(response).upcase
20
+ Signature.new(self).signature
21
+ end
22
+
23
+ def merchant_parameters
24
+ params['Ds_MerchantParameters']
25
+ end
26
+
27
+ def merchant_paramters_json
28
+ @merchant_paramters_json ||=
29
+ Base64.urlsafe_decode64(merchant_parameters)
30
+ end
31
+
32
+ def merchant_parameters_hash
33
+ @merchant_parameters_hash ||=
34
+ JSON.parse(merchant_paramters_json)
24
35
  end
25
36
  end
26
- end
37
+ end
@@ -0,0 +1,46 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'mcrypt'
4
+
5
+ module SermepaWebTpv
6
+ class Signature
7
+ include SermepaWebTpv::Persistence::ActiveRecord
8
+
9
+ attr_reader :action
10
+
11
+ class << self
12
+ def iv
13
+ # "\x00\x00\x00\x00\x00\x00\x00\x00"
14
+ 0.chr * 8
15
+ end
16
+
17
+ def merchant_key
18
+ Base64.decode64(SermepaWebTpv.merchant_secret_key)
19
+ end
20
+
21
+ def cipher
22
+ @cipher ||= Mcrypt.new(:tripledes, :cbc, merchant_key,
23
+ iv, :zeros)
24
+ end
25
+
26
+ def digestor
27
+ @digestor ||= OpenSSL::Digest.new('sha256')
28
+ end
29
+ end
30
+
31
+ def initialize(action)
32
+ @action = action
33
+ end
34
+
35
+ def signature
36
+ Base64.encode64(
37
+ OpenSSL::HMAC.digest(self.class.digestor, key,
38
+ action.merchant_parameters))
39
+ .strip
40
+ end
41
+
42
+ def key
43
+ self.class.cipher.encrypt(action.transaction_number)
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module SermepaWebTpv
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,94 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sermepa_web_tpv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ricard Forniol Agustí
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-20 00:00:00.000000000 Z
11
+ date: 2015-11-21 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-mcrypt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
28
39
  - !ruby/object:Gem::Version
29
40
  version: '0'
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: sqlite3
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ! '>='
45
+ - - ">="
36
46
  - !ruby/object:Gem::Version
37
47
  version: '0'
38
48
  type: :development
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ! '>='
52
+ - - ">="
44
53
  - !ruby/object:Gem::Version
45
54
  version: '0'
46
55
  - !ruby/object:Gem::Dependency
47
56
  name: rspec-rails
48
57
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
58
  requirements:
51
- - - ! '>='
59
+ - - ">="
52
60
  - !ruby/object:Gem::Version
53
61
  version: '0'
54
62
  type: :development
55
63
  prerelease: false
56
64
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
65
  requirements:
59
- - - ! '>='
66
+ - - ">="
60
67
  - !ruby/object:Gem::Version
61
68
  version: '0'
62
69
  - !ruby/object:Gem::Dependency
63
70
  name: fuubar
64
71
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
72
  requirements:
67
- - - ! '>='
73
+ - - ">="
68
74
  - !ruby/object:Gem::Version
69
75
  version: '0'
70
76
  type: :development
71
77
  prerelease: false
72
78
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
79
  requirements:
75
- - - ! '>='
80
+ - - ">="
76
81
  - !ruby/object:Gem::Version
77
82
  version: '0'
78
83
  - !ruby/object:Gem::Dependency
79
84
  name: nyan-cat-formatter
80
85
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
86
  requirements:
83
- - - ! '>='
87
+ - - ">="
84
88
  - !ruby/object:Gem::Version
85
89
  version: '0'
86
90
  type: :development
87
91
  prerelease: false
88
92
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
93
  requirements:
91
- - - ! '>='
94
+ - - ">="
92
95
  - !ruby/object:Gem::Version
93
96
  version: '0'
94
97
  description: Simple rails engine to add payments to your web application using sermepa
@@ -99,38 +102,38 @@ executables: []
99
102
  extensions: []
100
103
  extra_rdoc_files: []
101
104
  files:
105
+ - MIT-LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - lib/sermepa_web_tpv.rb
102
109
  - lib/sermepa_web_tpv/persistence/active_record.rb
103
110
  - lib/sermepa_web_tpv/railtie.rb
104
111
  - lib/sermepa_web_tpv/request.rb
105
112
  - lib/sermepa_web_tpv/response.rb
113
+ - lib/sermepa_web_tpv/signature.rb
106
114
  - lib/sermepa_web_tpv/version.rb
107
- - lib/sermepa_web_tpv.rb
108
115
  - lib/tasks/sermepa_web_tpv.rake
109
- - MIT-LICENSE
110
- - Rakefile
111
- - README.md
112
116
  homepage: http://github.com/ritxi/sermepa_web_tpv
113
117
  licenses: []
118
+ metadata: {}
114
119
  post_install_message:
115
120
  rdoc_options: []
116
121
  require_paths:
117
122
  - lib
118
123
  required_ruby_version: !ruby/object:Gem::Requirement
119
- none: false
120
124
  requirements:
121
- - - ! '>='
125
+ - - ">="
122
126
  - !ruby/object:Gem::Version
123
127
  version: '0'
124
128
  required_rubygems_version: !ruby/object:Gem::Requirement
125
- none: false
126
129
  requirements:
127
- - - ! '>='
130
+ - - ">="
128
131
  - !ruby/object:Gem::Version
129
132
  version: '0'
130
133
  requirements: []
131
134
  rubyforge_project:
132
- rubygems_version: 1.8.23
135
+ rubygems_version: 2.4.5
133
136
  signing_key:
134
- specification_version: 3
137
+ specification_version: 4
135
138
  summary: Add simple web tpv from sermepa to your rails application
136
139
  test_files: []