khipu-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Helper khipu_button that generates a form with hidden inputs and a single image input according to Khipu's documentation.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in khipu-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Klaus Hott Vidal
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # KhipuRails
2
+
3
+ [Khipu](https://khipu.com/home) is a service that facilitates web billing and collection in Chile.
4
+ It handles very elegantly bank transfers and allows to generate bills in batches.
5
+ It offers an API for developers to create via POST new charges.
6
+
7
+ ## Installation
8
+
9
+ Add to your Gemfile and run the `bundle` command to install it.
10
+ ```ruby
11
+ gem 'khipu-rails'
12
+ ```
13
+
14
+ Create an initializer with the following code.
15
+ ```ruby
16
+ KhipuRails::Config.user_id = ID
17
+ KhipuRails::Config.api_key = API KEY
18
+ ```
19
+
20
+ *ID and API KEY can be found [here](https://khipu.com/merchant/profile#instant-notification-data)*
21
+
22
+ # Features
23
+
24
+ ## Creating a button for a single bill
25
+
26
+ This gem provides a helper to build a button using the right parameters provided in the [API documentation](https://khipu.com/page/api#creacion-formulario).
27
+
28
+ ### khipu_button
29
+ **khipu_button subject, amount, options = {}**
30
+
31
+ Params:
32
+ * **subject**: The subject of the bill. *(max 255 chars)*
33
+ * **amount**: Amount to charge.
34
+
35
+ Options:
36
+ * **:body**: Further description of the charge.
37
+ * **:return_url**: callback URL to specify to the browser once the payment is comlpeted.
38
+ * **:cancel_url**: callback URL to specify to the browser if the user decides not to proceed with the transaction.
39
+ * **:transaction_id**: additional identifier related to the transaction.
40
+ * **:payer_email**: The email of the payer. If this is set, Khipu will pre-fill this field in the payment page.
41
+ * **:picture_url**: The URL of the product or service related to this charge.
42
+ * **:custom**: Additional information related to this charge such as shipment instructions.
43
+ * **:button_image**: Identifier of the button to display. If you designed your own button you can also provide the URL. *(It defaults to __"50x25"__ when __nil__ given)*
44
+ * **:receiver_id**: You can specify a receiver if you don't want to use the data stored in the **KhipuRails::Config.user_id** variable.
45
+ * **:secret**: You can specify a secret if you don't want to use the data stored in the **KhipuRails::Config.api_key** variable.
46
+
47
+ **[Button Images provided by Khipu](https://khipu.com/page/botones-de-pago)**
48
+
49
+ **50x25**: ![50x25 Button](https://s3.amazonaws.com/static.khipu.com/buttons/50x25.png)
50
+
51
+ **100x25**: ![100x25 Button](https://s3.amazonaws.com/static.khipu.com/buttons/100x25.png)
52
+
53
+ **100x50**: ![100x50 Button](https://s3.amazonaws.com/static.khipu.com/buttons/100x50.png)
54
+
55
+ **150x25**: ![150x25 Button](https://s3.amazonaws.com/static.khipu.com/buttons/150x25.png)
56
+
57
+ **150x50**: ![150x50 Button](https://s3.amazonaws.com/static.khipu.com/buttons/150x50.png)
58
+
59
+ **150x75**: ![150x75 Button](https://s3.amazonaws.com/static.khipu.com/buttons/150x75.png)
60
+
61
+ **150x75-B**: ![150x75-B Button](https://s3.amazonaws.com/static.khipu.com/buttons/150x75-B.png)
62
+
63
+ **200x50**: ![200x50 Button](https://s3.amazonaws.com/static.khipu.com/buttons/200x50.png)
64
+
65
+ **200x75**: ![200x75 Button](https://s3.amazonaws.com/static.khipu.com/buttons/200x75.png)
66
+
67
+ # TODO:
68
+
69
+ ## Validation of payment notifications
70
+
71
+ If you provide an URL for Khipu to deliver [notifications](https://khipu.com/page/api#notification-instantanea) on succesfull payments, this gem provides a validation method to prevent forgery of notifications.
72
+
73
+ ### khipu_validation
74
+
75
+ **khipu_validation params, local_validation = false**
76
+
77
+ * **params**: The POST params delivered by Khipu.
78
+ * **local_validation**: Wether the validation should be done at the [local machine](https://khipu.com/page/api#validacion-local) or by [Khipu's API](https://khipu.com/page/api#validacion-web-service).
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'khipu_rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "khipu-rails"
8
+ gem.version = KhipuRails::VERSION
9
+ gem.authors = ["Klaus Hott Vidal"]
10
+ gem.email = ["klahott@gmail.com"]
11
+ gem.description = %q{This gem provides a rails wrapper for the khipu api.}
12
+ gem.summary = %q{This gem provides a rails wrapper for the khipu api.}
13
+ gem.homepage = %q{https://github.com/janther/khipu-rails}
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rails", "~> 3.1.10"
21
+
22
+ gem.add_development_dependency "rspec-rails"
23
+ gem.add_development_dependency "nokogiri"
24
+ gem.add_development_dependency "capybara", "~> 1.1.2"
25
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ require "khipu_rails"
3
+ require "khipu_rails/version"
4
+ require "khipu_rails/config"
5
+ require "khipu_rails/button_helper"
6
+
7
+ ActionView::Base.send :include, KhipuRails::ButtonHelper
@@ -0,0 +1,2 @@
1
+ module KhipuRails
2
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ module KhipuRails
3
+ module ButtonHelper
4
+ def khipu_button subject, amount, options = {}
5
+ options.reverse_merge! body: '',
6
+ return_url: '',
7
+ cancel_url: '',
8
+ transaction_id: '',
9
+ payer_email: '',
10
+ picture_url: '',
11
+ custom: '',
12
+ button_image: '50x25', #Default Button Image
13
+ receiver_id: KhipuRails::Config.user_id, #Loads user id from configuration
14
+ secret: KhipuRails::Config.api_key #Loads api key from configuration
15
+
16
+ options[:subject] = subject #Adds the subject param to the options for khipu_hash
17
+ options[:amount] = amount #Adds the amount param to the options for khipu_hash
18
+
19
+ button_image = KhipuRails::Config.khipu_images()[options[:button_image]] || options[:button_image]
20
+
21
+ form_tag 'https://khipu.com/payment/api/createPaymentPage', authenticity_token: false do
22
+ [].tap do |i|
23
+ i << hidden_field_tag(:receiver_id, options[:receiver_id])
24
+ i << hidden_field_tag(:subject, subject)
25
+ i << hidden_field_tag(:body, options[:body])
26
+ i << hidden_field_tag(:amount, amount)
27
+ i << hidden_field_tag(:return_url, options[:return_url])
28
+ i << hidden_field_tag(:cancel_url, options[:cancel_url])
29
+ i << hidden_field_tag(:custom, options[:custom])
30
+ i << hidden_field_tag(:transaction_id, options[:transaction_id])
31
+ i << hidden_field_tag(:payer_email, options[:payer_email])
32
+ i << hidden_field_tag(:picture_url, options[:picture_url])
33
+ i << hidden_field_tag(:hash, khipu_hash(options))
34
+ i << image_submit_tag(button_image, name: :submit)
35
+ end.join.html_safe
36
+ end
37
+ end
38
+
39
+ private
40
+ def khipu_hash options = {}
41
+ raw = [
42
+ "receiver_id=#{options[:receiver_id]}",
43
+ "subject=#{options[:subject]}",
44
+ "body=#{options[:body]}",
45
+ "amount=#{options[:amount]}",
46
+ "return_url=#{options[:return_url]}",
47
+ "cancel_url=#{options[:cancel_url]}",
48
+ "custom=#{options[:custom]}",
49
+ "transaction_id=#{options[:transaction_id]}",
50
+ "picture_url=#{options[:picture_url]}",
51
+ "payer_email=#{options[:payer_email]}",
52
+ "secret=#{options[:secret]}"
53
+ ].join('&')
54
+ Digest::SHA1.hexdigest raw
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ module KhipuRails
2
+ class Config
3
+ def self.khipu_images
4
+ {
5
+ "50x25" => "https://s3.amazonaws.com/static.khipu.com/buttons/50x25.png",
6
+ "100x25" => "https://s3.amazonaws.com/static.khipu.com/buttons/100x25.png",
7
+ "100x50" => "https://s3.amazonaws.com/static.khipu.com/buttons/100x50.png",
8
+ "150x25" => "https://s3.amazonaws.com/static.khipu.com/buttons/150x25.png",
9
+ "150x50" => "https://s3.amazonaws.com/static.khipu.com/buttons/150x50.png",
10
+ "150x75" => "https://s3.amazonaws.com/static.khipu.com/buttons/150x75.png",
11
+ "150x75-B" => "https://s3.amazonaws.com/static.khipu.com/buttons/150x75.png",
12
+ "200x50" => "https://s3.amazonaws.com/static.khipu.com/buttons/200x50.png",
13
+ "200x75" => "https://s3.amazonaws.com/static.khipu.com/buttons/200x75.png"
14
+ }
15
+ end
16
+
17
+ def self.user_id= value
18
+ @@user_id = value
19
+ end
20
+
21
+ def self.api_key= value
22
+ @@api_key = value
23
+ end
24
+
25
+ def self.user_id
26
+ @@user_id
27
+ end
28
+
29
+ def self.api_key
30
+ @@api_key
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module KhipuRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe KhipuRails::ButtonHelper do
5
+ before :all do
6
+ @view = ActionView::Base.new
7
+ end
8
+
9
+ it "generates a form with the action provided by khipu" do
10
+ button = Nokogiri::HTML.parse(@view.khipu_button "minimal", 2000)
11
+ form = button.css('form')
12
+ form.attribute('action').value.should == 'https://khipu.com/payment/api/createPaymentPage'
13
+ end
14
+
15
+ it "generates a form with the method post" do
16
+ button = Nokogiri::HTML.parse(@view.khipu_button "minimal", 2000)
17
+ form = button.css('form')
18
+ form.attribute('method').value.should == 'post'
19
+ end
20
+
21
+ context "given a form with minimal data was correctly generated, it" do
22
+ before :all do
23
+ @receiver_id = KhipuRails::Config.user_id
24
+ @secret = KhipuRails::Config.api_key
25
+ @button = Nokogiri::HTML.parse(@view.khipu_button "minimal", 2000)
26
+ end
27
+
28
+ it "has an input called reciever_id" do
29
+ input = @button.css('form input[name=receiver_id]')
30
+ input.attribute('value').value.to_i.should == @receiver_id
31
+ end
32
+
33
+ it "has an input called subject" do
34
+ input = @button.css('form input[name=subject]')
35
+ input.attribute('value').value.should == 'minimal'
36
+ end
37
+
38
+ it "has an input called body" do
39
+ input = @button.css('form input[name=body]')
40
+ input.attribute('value').value.should == ''
41
+ end
42
+
43
+ it "has an input called amount" do
44
+ input = @button.css('form input[name=amount]')
45
+ input.attribute('value').value.should == '2000'
46
+ end
47
+
48
+ it "has an input called return_url" do
49
+ input = @button.css('form input[name=return_url]')
50
+ input.attribute('value').value.should == ''
51
+ end
52
+
53
+ it "has an input called cancel_url" do
54
+ input = @button.css('form input[name=cancel_url]')
55
+ input.attribute('value').value.should == ''
56
+ end
57
+
58
+ it "has an input called transaction_id" do
59
+ input = @button.css('form input[name=transaction_id]')
60
+ input.attribute('value').value.should == ''
61
+ end
62
+
63
+ it "has an input called custom" do
64
+ input = @button.css('form input[name=custom]')
65
+ input.attribute('value').value.should == ''
66
+ end
67
+
68
+ it "has an input called payer_email" do
69
+ input = @button.css('form input[name=payer_email]')
70
+ input.attribute('value').value.should == ''
71
+ end
72
+
73
+ it "has an input called picture_url" do
74
+ input = @button.css('form input[name=picture_url]')
75
+ input.attribute('value').value.should == ''
76
+ end
77
+
78
+ it "has an input called hash" do
79
+ input = @button.css('form input[name=hash]')
80
+ input.attribute('type').value.should == 'hidden'
81
+ def get_value attribute
82
+ @button.css("form input[name=#{attribute}]").attribute('value').value
83
+ end
84
+ raw = "receiver_id=#{get_value :receiver_id}&"
85
+ raw += "subject=#{get_value :subject}&"
86
+ raw += "body=#{get_value :body}&"
87
+ raw += "amount=#{get_value :amount}&"
88
+ raw += "return_url=#{get_value :return_url}&"
89
+ raw += "cancel_url=#{get_value :cancel_url}&"
90
+ raw += "custom=#{get_value :custom}&"
91
+ raw += "transaction_id=#{get_value :transaction_id}&"
92
+ raw += "picture_url=#{get_value :picture_url}&"
93
+ raw += "payer_email=#{get_value :payer_email}&"
94
+ raw += "secret=#{@secret}"
95
+ input.attribute('value').value.should == Digest::SHA1.hexdigest(raw)
96
+ end
97
+
98
+ it "has an image submit" do
99
+ input = @button.css('form input[name=submit]')
100
+ input.attribute('type').value.should == 'image'
101
+ input.attribute('src').value.should == 'https://s3.amazonaws.com/static.khipu.com/buttons/50x25.png'
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe KhipuRails do
5
+ it "adds the khipu_button helper to the view" do
6
+ ActionView::Base.method_defined?(:khipu_button).should == true
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+
4
+ require "action_view/railtie"
5
+ require "nokogiri"
6
+ require 'khipu-rails'
7
+
8
+ KhipuRails::Config.user_id = 1234
9
+ KhipuRails::Config.api_key = '1234567890asdfghjkl'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: khipu-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Klaus Hott Vidal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-21 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.1.10
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.1.10
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
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: nokogiri
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: capybara
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.1.2
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: 1.1.2
78
+ description: This gem provides a rails wrapper for the khipu api.
79
+ email:
80
+ - klahott@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - CHANGELOG.md
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - khipu-rails.gemspec
93
+ - lib/khipu-rails.rb
94
+ - lib/khipu_rails.rb
95
+ - lib/khipu_rails/button_helper.rb
96
+ - lib/khipu_rails/config.rb
97
+ - lib/khipu_rails/version.rb
98
+ - spec/khipu_rails/khipu_helpers_spec.rb
99
+ - spec/khipu_rails_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/janther/khipu-rails
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.24
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: This gem provides a rails wrapper for the khipu api.
125
+ test_files:
126
+ - spec/khipu_rails/khipu_helpers_spec.rb
127
+ - spec/khipu_rails_spec.rb
128
+ - spec/spec_helper.rb