tbk 1.0.1 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99cb29a42231d962dba1a8c07dc0711c53ea7dd5
4
- data.tar.gz: 712f5a91ebc80e5a5d2dbfb50d8c41eb648ed14d
3
+ metadata.gz: 23651dab425128f9a304cfc75827c22133accc87
4
+ data.tar.gz: cccbb98ef6fc50bf809e57e02fa8a018c96aa30b
5
5
  SHA512:
6
- metadata.gz: 6403bd694ccc81e3a614ff8b45600861b6f4e90f7605943df23e23fdd17528540810314ac4c57e7127b01ed47cec648c851039ff7910e0f06d8216194686681a
7
- data.tar.gz: a2b3ed5bf65e15f30b96651ba4819bb91688c32072a7fca7951d218288c237720a106808f908b400a66aaaf21115dcd22df370f9f249e00579b91fed8781898d
6
+ metadata.gz: ec0da50f8c95611d8c9eff7badd39551848dd4b45bb20c545549d6d31e6deded37309badc69ee3cb15a1b5f72bcace893a707acb6374725c97462219a817d72d
7
+ data.tar.gz: 35783d42fec9ac1bade1ccd4016a223d2bf602481dd66cf1843e2422eb6124927727a492944760d6b45bb8d4c43ddfdd8c2fd9589da2de9907edf6569e8d45ee
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .rspec
5
+ .rvmrc
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ cache: bundler
3
+ bundler_args: --without docs
4
+ rvm:
5
+ - 2.1.0
6
+ - 2.0.0
7
+ - 1.9.3
8
+ - jruby-19mode
9
+ addons:
10
+ code_climate:
11
+ repo_token: 993125436d81e62150b5b089e19b7d51a677b4f0ce59012b8cc9e17b4d72fb60
12
+
@@ -0,0 +1,5 @@
1
+ --no-private
2
+ --hide-void-return
3
+
4
+ --markup=markdown
5
+ --markup-provider=redcarpet
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tbk.gemspec
4
+ gemspec
5
+
6
+ platforms :jruby do
7
+ gem "jruby-openssl"
8
+ end
9
+
10
+ group :docs do
11
+ gem 'yard'
12
+ gem 'redcarpet'
13
+ end
14
+
15
+ group :test do
16
+ gem "codeclimate-test-reporter", require: false
17
+ end
18
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Seba Gamboa
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.
@@ -0,0 +1,115 @@
1
+ ### About
2
+ [![Gem Version](https://badge.fury.io/rb/tbk.png)](http://badge.fury.io/rb/tbk)
3
+ [![Build Status](https://travis-ci.org/sagmor/tbk.png)](https://travis-ci.org/sagmor/tbk)
4
+ [![Code Climate](https://codeclimate.com/github/sagmor/tbk.png)](https://codeclimate.com/github/sagmor/tbk)
5
+
6
+ This is a pure ruby replacement of Transbank's Binary Integration Kit (aka. KCC)
7
+ developed to simplify the integration with it's payment gateway Webpay.
8
+
9
+
10
+ ### Disclaimer
11
+
12
+ This library is not developed, supported nor endorsed in any way by Transbank S.A.
13
+ and is the result of reverse engineering Transbank's Integration Kit (aka. KCC)
14
+ for interoperability purposes as allowed by
15
+ [Chilean Law 20.435 Article 71 Ñ Section b](http://www.leychile.cl/Navegar?idNorma=1012827)
16
+
17
+ ### Usage
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'tbk'
23
+ ```
24
+
25
+ Configure your commerce
26
+
27
+ ```ruby
28
+ TBK.configure do |config|
29
+ config.commerce_id YOUR_COMMERCE_ID
30
+ config.commerce_key YOUR_RSA_KEY
31
+
32
+ # When certifying with Transbank you'll need to use the official logger
33
+ config.webpay_logger :official do |logger|
34
+ # On this path will be the config file and logs in the KCC format.
35
+ logger.directory Rails.root.join('log/webpay')
36
+ end
37
+ end
38
+ ```
39
+
40
+ To start a payment from your application
41
+
42
+ ```ruby
43
+ class WebpayController < ApplicationController
44
+
45
+ # ...
46
+
47
+ # Start a payment
48
+ def pay
49
+ # Setup the payment
50
+ @payment = TBK::Webpay::Payment.new({
51
+ request_ip: request.ip,
52
+ amount: ORDER_AMOUNT,
53
+ order_id: ORDER_ID,
54
+ success_url: webpay_success_url,
55
+ # Webpay can only access the HTTP protocol to a direct IP address (keep that in mind)
56
+ confirmation_url: webpay_confirmation_url(host: SERVER_IP_ADDRESS, protocol: 'http'),
57
+
58
+ # Optionaly supply:
59
+ session_id: SOME_SESSION_VALUE,
60
+ failure_url: webpay_failure_url # success_url is used by default
61
+ })
62
+
63
+ # Redirect the user to Webpay
64
+ redirect_to @payment.redirect_url
65
+ end
66
+
67
+ # ...
68
+ end
69
+ ```
70
+
71
+ And to process a payment
72
+
73
+ ```ruby
74
+ class WebpayController < ApplicationController
75
+
76
+ # ...
77
+
78
+ # Confirmation callback executed from Webpay servers
79
+ def confirmation
80
+ # Read the confirmation data from the request
81
+ @confirmation = TBK::Webpay::Confirmation.new({
82
+ request_ip: request.ip,
83
+ body: request.raw_post
84
+ })
85
+
86
+ if # confirmation is invalid for some reason (wrong order_id or amount, double payment, etc...)
87
+ render text: @confirmation.reject
88
+ return # reject and stop execution
89
+ end
90
+
91
+ if @confirmation.success?
92
+ # EXITO!
93
+ # perform everything you have to do here.
94
+ end
95
+
96
+ # Acknowledge payment
97
+ render text: @confirmation.acknowledge
98
+ end
99
+
100
+ # ...
101
+
102
+ end
103
+ ```
104
+
105
+ ### Contributing
106
+
107
+ 1. Fork it
108
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
110
+ 4. Push to the branch (`git push origin my-new-feature`)
111
+ 5. Create new Pull Request
112
+
113
+
114
+ [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/sagmor/tbk/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
115
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,12 @@
1
+ require "base64"
2
+ require "openssl"
3
+ require "securerandom"
4
+ require "net/https"
5
+ require "cgi"
6
+
7
+ require "tbk/version"
8
+ require "tbk/config"
9
+ require "tbk/errors"
10
+ require "tbk/keys"
11
+ require "tbk/commerce"
12
+ require "tbk/webpay"
@@ -0,0 +1,69 @@
1
+
2
+ module TBK
3
+ # Represents a commerce registered with Transbank
4
+ class Commerce
5
+
6
+ # The registered commerce id
7
+ # @return [Integer] the commerce id
8
+ attr_reader :id
9
+
10
+ # The commerce secret RSA key
11
+ # @return [OpenSSL::PKey::RSA] the commerce key
12
+ attr_reader :key
13
+
14
+ # The commerce environment
15
+ # @return [Symbol] the commerce environment
16
+ attr_reader :environment
17
+
18
+ # Initializes a new commerce
19
+ # @param [Hash] attributes The commerce attributes
20
+ # @option attributes [Integer] :id The commerce ID
21
+ # @option attributes [String|OpenSSL::PKey::RSA] :key The commerce RSA private key
22
+ # @option attributes [Boolean] :test flag to set commerce in test mode
23
+ def initialize(attributes)
24
+ @environment = (attributes[:environment] || :production).to_sym
25
+ raise TBK::CommerceError, "Invalid commerce environment" unless [:production,:test].include? @environment
26
+
27
+ @id = attributes[:id]
28
+ raise TBK::CommerceError, "Missing commerce id" if self.id.nil?
29
+
30
+ @key = case attributes[:key]
31
+ when String
32
+ OpenSSL::PKey::RSA.new(attributes[:key])
33
+ when OpenSSL::PKey::RSA
34
+ attributes[:key]
35
+ when nil
36
+ TEST_COMMERCE_KEY if self.test?
37
+ end
38
+
39
+ raise TBK::CommerceError, "Missing commerce key" if self.key.nil?
40
+ raise TBK::CommerceError, "Commerce key must be a RSA private key" unless self.key.private?
41
+ end
42
+
43
+ # @return [Boolean] whether or not the commerce is in test mode
44
+ def test?
45
+ self.environment == :test
46
+ end
47
+
48
+ # @return [Boolean] whether or not the commerce is in production mode
49
+ def production?
50
+ self.environment == :production
51
+ end
52
+
53
+ # @return [Integer] RSA key bytes
54
+ def key_bytes
55
+ self.key.n.num_bytes
56
+ end
57
+
58
+ # @return [TBK::Commerce] The default commerce
59
+ def self.default_commerce
60
+ @default_commerce ||= Commerce.new({
61
+ :id => TBK.config.commerce_id,
62
+ :key => TBK.config.commerce_key,
63
+ :environment => TBK.config.environment
64
+ }) unless TBK.config.commerce_id.nil?
65
+ end
66
+
67
+ TEST_COMMERCE_KEY = TBK.parse_key('test_commerce')
68
+ end
69
+ end
@@ -0,0 +1,43 @@
1
+ module TBK
2
+ class Config
3
+
4
+ # Set and gets the default commerce id
5
+ def commerce_id(id = nil)
6
+ @id = id if id
7
+ @id || ENV['TBK_COMMERCE_ID']
8
+ end
9
+
10
+ # Sets and gets the default commerce key
11
+ def commerce_key(key = nil)
12
+ @key = key if key
13
+ @key || ENV['TBK_COMMERCE_KEY']
14
+ end
15
+
16
+ # Sets the default commerce environment
17
+ # @return [Symbol] the default commerce environment
18
+ def environment(environment = nil)
19
+ @environment = environment if environment
20
+ (@environment || ENV['TBK_COMMERCE_ENVIRONMENT'] || :production).to_sym
21
+ end
22
+ end
23
+
24
+ # Returns the configuration object
25
+ # @return [TBK::Config] the configuration object
26
+ def self.config
27
+ @config ||= Config.new
28
+ end
29
+
30
+ # Configure the app defaults simply by doing
31
+ #
32
+ # TBK.configure do |config|
33
+ # config.commerce_id 123456
34
+ # config.commerce_key File.read(COMMERCE_KEY_PATH)
35
+ # end
36
+ #
37
+ # @yield [TBK::Config] The config object
38
+ def self.configure(&block)
39
+ yield(self.config)
40
+ nil
41
+ end
42
+
43
+ end
@@ -0,0 +1,16 @@
1
+ module TBK
2
+ class Error < StandardError
3
+ attr_reader :origin
4
+ def initialize(msg, origin=nil)
5
+ super(msg)
6
+ @origin = origin
7
+ end
8
+ end
9
+
10
+ class CommerceError < Error; end
11
+
12
+ module Webpay
13
+ class PaymentError < Error; end
14
+ class EncryptionError < Error; end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module TBK
2
+ def self.parse_key(name)
3
+ OpenSSL::PKey::RSA.new( File.read(File.expand_path("../keys/#{name}.pem",__FILE__)) )
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ -----BEGIN RSA PRIVATE KEY-----
2
+ MIIEowIBAAKCAQEAn3HzPC1ZBzCO3edUCf/XJiwj3bzJpjjTi/zBO9O+DDzZCaMp
3
+ 14aspxQryvJhv8644E19Q+NHfxtz1cxd2wnSYKvay1gJx30ZlTOAkzUj4QMimR16
4
+ vomLlQ3T2MAz1znt/PVPVU7T/JOG9R+EbiHNVKa/hUjwJEFVXLQNME97nHoLjb3v
5
+ V5yV2aVhmox7b54n6F3UVPHvCsHKbJpXpE+vnLpVmdETbNpFVrDygX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ION
3
+ GEM = "1.0.2"
4
+ KCC = "6.0"
5
+ WEBSITE = "http://sagmor.com/tbk/"
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ require 'tbk/webpay/encryption'
2
+ require 'tbk/webpay/payment'
3
+ require 'tbk/webpay/confirmation'
4
+ require 'tbk/webpay/logger'