sermepa_web_tpv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Ricard Forniol Agustí
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Sermepa web TPV
2
+
3
+ Add simple web payment tpv to your rails application
4
+
5
+
6
+ Steps to use it:
7
+
8
+ 1. Create a new rails application
9
+
10
+ rails new payment_app
11
+
12
+ 2. Add sermepa_web_tpv to your Gemfile
13
+
14
+ gem 'sermepa_web_tpv'
15
+
16
+ 3. Add a transaction model with an amount and transaction_number fields
17
+
18
+ rails g model Transaction amount:float transaction_number:integer
19
+
20
+ 4. Configure tpv options
21
+
22
+ ```ruby
23
+ module MyApp
24
+ class Application < Rails::Application
25
+ config.web_tpv.bank_url = 'https://sis-t.sermepa.es:25443/sis/realizarPago'
26
+ config.web_tpv.response_host = 'my_application_host.com'
27
+ config.web_tpv.merchant_code = '000000000'
28
+ config.web_tpv.terminal = 1
29
+ config.web_tpv.callback_response_path = '/payments/validate'
30
+ config.web_tpv.redirect_success_path = '/payments/success'
31
+ config.web_tpv.redirect_failure_path = '/payments/failure'
32
+ config.web_tpv.merchant_secret_key = 'superseeeecretstring'
33
+ config.web_tpv.currency = 978 # Euro
34
+
35
+ config.web_tpv.transaction_type = 0
36
+ config.web_tpv.language = '003' #Catala
37
+ config.web_tpv.merchant_name = 'MY MERCHANT NAME'
38
+ end
39
+ end
40
+ ```
41
+
42
+ 5. Add a controller payment to perform a new request payment and receive payment result
43
+
44
+ ```ruby
45
+ MyApp::Application.routes.draw do
46
+ get 'payments/new' => 'payments#new', :as => 'new_payment'
47
+ post 'payments/validate' => 'payments#validate', :as => 'payment_validate'
48
+ get 'payments/success' => 'payments#success', :as => 'payment_success'
49
+ get 'payments/failure' => 'payments#failure', :as => 'payment_failure'
50
+ end
51
+ ```
52
+
53
+ ```ruby
54
+ class PaymentsController
55
+ skip_before_filter :verify_authenticity_token
56
+
57
+ def new
58
+ transaction = Transaction.new(amount: 10)
59
+ request = SermepaWebTpv::Request.new(transaction, "Transaction description")
60
+ request.transact {|t| t.save }
61
+ render json: { options: sermepa_request.options, url: sermepa_request.bank_url }
62
+ # Submit a form with given options to the url
63
+ end
64
+
65
+ def success
66
+ flash[:notice] = "Payment done successfuly!"
67
+ redirect_to root_path
68
+ end
69
+
70
+ def failure
71
+ flash[:failure] = "Payment failed, try again later."
72
+ redirect_to root_path
73
+ end
74
+
75
+ def validate
76
+ response = SermepaWebTpv::Response.new(params)
77
+ if response.valid? && response.success?
78
+ # mark your transaction as finished
79
+ end
80
+ end
81
+ end
82
+ ```
83
+
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+
17
+ require 'rspec/core'
18
+ require 'rspec/core/rake_task'
19
+
20
+ RSpec::Core::RakeTask.new(:spec)
21
+ task :default => :spec
22
+
23
+ RDoc::Task.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'SermepaWebTpv'
26
+ rdoc.options << '--line-numbers'
27
+ rdoc.rdoc_files.include('README.rdoc')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
30
+
31
+
32
+
33
+
@@ -0,0 +1,20 @@
1
+ module SermepaWebTpv
2
+ module Persistence
3
+ module ActiveRecord
4
+ def transaction_number
5
+ generate_transaction_number! if transaction.new_record?
6
+
7
+ transaction.send(transaction_number_attribute)
8
+ end
9
+
10
+ def transaction_amount
11
+ transaction.send(transaction_model_amount_attribute)
12
+ end
13
+
14
+ def generate_transaction_number!
15
+ number = Time.now.strftime('%y%m%d%H%M%S')
16
+ transaction.update_attribute(transaction_number_attribute, number)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module SermepaWebTpv
2
+ class Railtie < Rails::Railtie
3
+ config.web_tpv = SermepaWebTpv
4
+ end
5
+ end
@@ -0,0 +1,84 @@
1
+ require 'uri'
2
+ require 'digest/sha1'
3
+
4
+ module SermepaWebTpv
5
+ class Request < Struct.new(:transaction, :description)
6
+ include SermepaWebTpv::Persistence::ActiveRecord
7
+
8
+ def bank_url
9
+ SermepaWebTpv.bank_url
10
+ end
11
+
12
+ def options
13
+ optional_options.merge(must_options)
14
+ end
15
+
16
+ def transact(&block)
17
+ generate_transaction_number!
18
+ yield(transaction)
19
+ self
20
+ end
21
+
22
+ private
23
+
24
+ def transaction_number_attribute
25
+ SermepaWebTpv.transaction_model_transaction_number_attribute
26
+ end
27
+
28
+ def transaction_model_amount_attribute
29
+ SermepaWebTpv.transaction_model_amount_attribute
30
+ end
31
+
32
+ def amount
33
+ (transaction_amount * 100).to_i.to_s
34
+ end
35
+
36
+ def must_options
37
+ {
38
+ 'Ds_Merchant_Amount' => amount,
39
+ 'Ds_Merchant_Currency' => SermepaWebTpv.currency, #EURO
40
+ 'Ds_Merchant_Order' => transaction_number,
41
+ 'Ds_Merchant_ProductDescription' => description,
42
+ 'Ds_Merchant_MerchantCode' => SermepaWebTpv.merchant_code,
43
+ 'Ds_Merchant_MerchantSignature' => signature,
44
+ 'Ds_Merchant_Terminal' => SermepaWebTpv.terminal,
45
+ 'Ds_Merchant_TransactionType' => SermepaWebTpv.transaction_type,
46
+ 'Ds_Merchant_ConsumerLanguage' => SermepaWebTpv.language,
47
+ 'Ds_Merchant_MerchantURL' => url_for(:callback_response_path)
48
+ }
49
+ end
50
+
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
+ # Available options
62
+ # redirect_success_path
63
+ # redirect_failure_path
64
+ # callback_response_path
65
+ def url_for(option)
66
+ host = SermepaWebTpv.response_host
67
+ path = SermepaWebTpv.send(option)
68
+
69
+ if host.present? && path.present?
70
+ URI.join("http://#{host}", path).to_s
71
+ end
72
+ end
73
+
74
+ def optional_options
75
+ {
76
+ 'Ds_Merchant_Titular' => SermepaWebTpv.merchant_name,
77
+ 'Ds_Merchant_UrlKO' => url_for(:redirect_failure_path),
78
+ 'Ds_Merchant_UrlOK' => url_for(:redirect_success_path)
79
+ }.delete_if {|key, value| value.blank? }
80
+ end
81
+
82
+
83
+ end
84
+ end
@@ -0,0 +1,25 @@
1
+ require 'digest/sha1'
2
+
3
+ module SermepaWebTpv
4
+ class Response < Struct.new(:params)
5
+ def valid?
6
+ params[:Ds_Signature] == signature
7
+ end
8
+
9
+ def success?
10
+ params[:Ds_Response].to_i == 0
11
+ end
12
+
13
+ private
14
+ 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
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module SermepaWebTpv
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support/core_ext/module/attribute_accessors'
2
+ module SermepaWebTpv
3
+
4
+ mattr_accessor :transaction_model_transaction_number_attribute
5
+ @@transaction_model_transaction_number_attribute = :transaction_number
6
+
7
+ mattr_accessor :transaction_model_amount_attribute
8
+ @@transaction_model_amount_attribute = :amount
9
+
10
+ mattr_accessor :bank_url
11
+ @@bank_url = 'https://sis-t.sermepa.es:25443/sis/realizarPago'
12
+
13
+ mattr_accessor :terminal
14
+ @@terminal = 1
15
+
16
+ mattr_accessor :merchant_secret_key
17
+
18
+ mattr_accessor :currency
19
+ @@currency = 978 # Euro
20
+
21
+ mattr_accessor :merchant_code
22
+
23
+ mattr_accessor :transaction_type
24
+ @@transaction_type = 0
25
+
26
+ mattr_accessor :language
27
+ @@language = '003' #Catala
28
+
29
+ mattr_accessor :response_host
30
+ @@response_host = '' #'shop_name.com'
31
+
32
+ mattr_accessor :callback_response_path
33
+ @@callback_response_path = '' #"/payments/callback"
34
+
35
+ # Optional
36
+ mattr_accessor :merchant_name
37
+ @@merchant_name = '' #'shop name'
38
+
39
+ mattr_accessor :redirect_success_path
40
+ @@redirect_success_path = nil #"/payments/success"
41
+
42
+ mattr_accessor :redirect_failure_path
43
+ @@redirect_failure_path = nil #"/payments/failure"
44
+
45
+ autoload :Request, 'sermepa_web_tpv/request'
46
+ autoload :Response, 'sermepa_web_tpv/response'
47
+
48
+ end
49
+ require 'sermepa_web_tpv/persistence/active_record'
50
+ require 'sermepa_web_tpv/railtie'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :defaultable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sermepa_web_tpv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ricard Forniol Agustí
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.17
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.17
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: fuubar
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: nyan-cat-formatter
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Simple rails engine to add payments to your web application using sermepa
95
+ standard web tpv
96
+ email:
97
+ - ricard@forniol.cat
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - lib/sermepa_web_tpv/persistence/active_record.rb
103
+ - lib/sermepa_web_tpv/railtie.rb
104
+ - lib/sermepa_web_tpv/request.rb
105
+ - lib/sermepa_web_tpv/response.rb
106
+ - lib/sermepa_web_tpv/version.rb
107
+ - lib/sermepa_web_tpv.rb
108
+ - lib/tasks/sermepa_web_tpv.rake
109
+ - MIT-LICENSE
110
+ - Rakefile
111
+ - README.md
112
+ homepage: http://github.com/ritxi/sermepa_web_tpv
113
+ licenses: []
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.23
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Add simple web tpv from sermepa to your rails application
136
+ test_files: []