tbk 0.9.0 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,6 +1,8 @@
1
1
  *.gem
2
2
  *.rbc
3
3
  .bundle
4
+ .rspec
5
+ .rvmrc
4
6
  .config
5
7
  .yardoc
6
8
  Gemfile.lock
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
+ bundler_args: --without docs
2
3
  rvm:
3
4
  - 1.9.3
4
5
  - 1.8.7
data/Gemfile CHANGED
@@ -6,3 +6,8 @@ gemspec
6
6
  platforms :jruby do
7
7
  gem "jruby-openssl"
8
8
  end
9
+
10
+ group :docs do
11
+ gem 'yard'
12
+ gem 'redcarpet'
13
+ end
data/README.md CHANGED
@@ -1,4 +1,11 @@
1
- Pure Ruby implementation of Transbank's Webpay KCC 6.0 protocol
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/badge.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
+
2
9
 
3
10
  ### Disclaimer
4
11
 
@@ -15,6 +22,15 @@ Add this line to your application's Gemfile:
15
22
  gem 'tbk'
16
23
  ```
17
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
+ end
32
+ ```
33
+
18
34
  To start a payment from your application
19
35
 
20
36
  ```ruby
@@ -24,15 +40,8 @@ class WebpayController < ApplicationController
24
40
 
25
41
  # Start a payment
26
42
  def pay
27
- # Initialyze you commerce
28
- @commerce = TBK::Commerce.new({
29
- id: YOUR_COMMERCE_ID,
30
- key: YOUR_RSA_KEY
31
- })
32
-
33
43
  # Setup the payment
34
44
  @payment = TBK::Webpay::Payment.new({
35
- commerce: @commerce,
36
45
  amount: ORDER_AMOUNT,
37
46
  order_id: ORDER_ID,
38
47
  success_url: webpay_success_url,
@@ -61,17 +70,8 @@ class WebpayController < ApplicationController
61
70
 
62
71
  # Confirmation callback executed from Webpay servers
63
72
  def confirmation
64
- # Initialyze you commerce
65
- @commerce = TBK::Commerce.new({
66
- id: YOUR_COMMERCE_ID,
67
- key: YOUR_RSA_KEY
68
- })
69
-
70
73
  # Read the confirmation data from the request
71
- @confirmation = TBK::Webpay::Confirmation.new({
72
- commerce: @commerce,
73
- post: request.raw_post
74
- })
74
+ @confirmation = TBK::Webpay::Confirmation.new(request.raw_post)
75
75
 
76
76
  if # confirmation is invalid for some reason (wrong order_id or amount, double payment, etc...)
77
77
  render text: @confirmation.reject
data/lib/tbk.rb CHANGED
@@ -5,6 +5,7 @@ require "net/https"
5
5
  require "cgi"
6
6
 
7
7
  require "tbk/version"
8
+ require "tbk/config"
8
9
  require "tbk/errors"
9
10
  require "tbk/keys"
10
11
  require "tbk/commerce"
@@ -4,26 +4,33 @@ module TBK
4
4
  class Commerce
5
5
 
6
6
  # The registered commerce id
7
- attr_accessor :id
7
+ # @return [Integer] the commerce id
8
+ attr_reader :id
8
9
 
9
10
  # The commerce secret RSA key
10
- attr_accessor :key
11
+ # @return [OpenSSL::PKey::RSA] the commerce key
12
+ attr_reader :key
11
13
 
12
- # Initialyzes a new commerce
14
+ # The commerce environment
15
+ # @return [Symbol] the commerce environment
16
+ attr_reader :environment
17
+
18
+ # Initializes a new commerce
13
19
  # @param [Hash] attributes The commerce attributes
14
20
  # @option attributes [Integer] :id The commerce ID
15
21
  # @option attributes [String|OpenSSL::PKey::RSA] :key The commerce RSA private key
16
22
  # @option attributes [Boolean] :test flag to set commerce in test mode
17
23
  def initialize(attributes)
18
- @test = attributes[:test]
24
+ @environment = (attributes[:environment] || :production).to_sym
25
+ raise TBK::CommerceError, "Invalid commerce environment" unless [:production,:test].include? @environment
19
26
 
20
- self.id = attributes[:id]
27
+ @id = attributes[:id]
21
28
  raise TBK::CommerceError, "Missing commerce id" if self.id.nil?
22
29
 
23
- self.key = case attributes[:key]
30
+ @key = case attributes[:key]
24
31
  when String
25
32
  OpenSSL::PKey::RSA.new(attributes[:key])
26
- when OpenSSL::PKey::RSA.new
33
+ when OpenSSL::PKey::RSA
27
34
  attributes[:key]
28
35
  when nil
29
36
  TEST_COMMERCE_KEY if self.test?
@@ -33,14 +40,14 @@ module TBK
33
40
  raise TBK::CommerceError, "Commerce key must be a RSA private key" unless self.key.private?
34
41
  end
35
42
 
36
- # @return [Boolean] wether or not the commerce is in test mode
43
+ # @return [Boolean] whether or not the commerce is in test mode
37
44
  def test?
38
- @test || false
45
+ self.environment == :test
39
46
  end
40
47
 
41
- # @return [Boolean] wether or not the commerce is in production mode
48
+ # @return [Boolean] whether or not the commerce is in production mode
42
49
  def production?
43
- !self.test?
50
+ self.environment == :production
44
51
  end
45
52
 
46
53
  # @return [Integer] RSA key bytes
@@ -48,6 +55,15 @@ module TBK
48
55
  self.key.n.num_bytes
49
56
  end
50
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
+
51
67
  TEST_COMMERCE_KEY = TBK.parse_key('test_commerce')
52
68
  end
53
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
+ # @returns [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
+ # @returns [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
@@ -1,6 +1,6 @@
1
1
  module TBK
2
2
  module VERSION
3
- GEM = "0.9.0"
3
+ GEM = "0.9.2"
4
4
  KCC = "6.0"
5
5
  WEBSITE = "http://sagmor.com/tbk/"
6
6
  end
@@ -1,3 +1,3 @@
1
1
  require 'tbk/webpay/encryption'
2
2
  require 'tbk/webpay/payment'
3
- require 'tbk/webpay/confirmation'
3
+ require 'tbk/webpay/confirmation'
@@ -19,7 +19,9 @@ module TBK
19
19
  attr_reader :params
20
20
 
21
21
  def initialize(options)
22
- self.commerce = options[:commerce]
22
+ options = { :post => options } if options.is_a?(String)
23
+
24
+ self.commerce = options[:commerce] || TBK::Commerce.default_commerce
23
25
  self.parse(options[:post])
24
26
  end
25
27
 
@@ -10,7 +10,7 @@ module TBK
10
10
  attr_accessor :failure_url
11
11
 
12
12
  def initialize(options = {})
13
- self.commerce = options[:commerce]
13
+ self.commerce = options[:commerce] || TBK::Commerce.default_commerce
14
14
  self.amount = options[:amount]
15
15
  self.order_id = options[:order_id]
16
16
  self.session_id = options[:session_id]
@@ -72,7 +72,7 @@ module TBK
72
72
  until response && response['location'].nil?
73
73
  uri = URI.parse( response.nil? ? self.validation_url : response['location'] )
74
74
 
75
- response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
75
+ response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
76
76
  post = Net::HTTP::Post.new uri.path
77
77
  post["user-agent"] = "TBK/#{ TBK::VERSION::GEM } (Ruby/#{ RUBY_VERSION }; +#{ TBK::VERSION::WEBSITE })"
78
78
  post.set_form_data({
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe TBK::Commerce do
4
+ context "creation" do
5
+ it "can be done with all it's arguments" do
6
+ @commerce = TBK::Commerce.new({
7
+ :id => 12345,
8
+ :key => TBK::Commerce::TEST_COMMERCE_KEY
9
+ })
10
+
11
+ expect(@commerce.id).to be_eql 12345
12
+ end
13
+
14
+ it "raises an exception if no id is given" do
15
+ expect{
16
+ TBK::Commerce.new({
17
+ :key => TBK::Commerce::TEST_COMMERCE_KEY
18
+ })
19
+ }.to raise_error TBK::CommerceError
20
+ end
21
+
22
+ it "raises an exception if no key is given" do
23
+ expect{
24
+ TBK::Commerce.new({
25
+ :id => 12345
26
+ })
27
+ }.to raise_error TBK::CommerceError
28
+ end
29
+
30
+ end
31
+
32
+ context "default_commerce" do
33
+ it "should be nil if no default is configured" do
34
+ TBK.config.should_receive(:commerce_id).and_return nil
35
+ expect(TBK::Commerce.default_commerce).to be_nil
36
+ end
37
+
38
+ it "should return a valid commerce if configured" do
39
+ TBK.config.should_receive(:commerce_id).at_least(:once).and_return 12345
40
+ TBK.config.should_receive(:commerce_key).at_least(:once).and_return TBK::Commerce::TEST_COMMERCE_KEY
41
+
42
+ expect(TBK::Commerce.default_commerce).not_to be_nil
43
+ expect(TBK::Commerce.default_commerce.id).to be_eql 12345
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe TBK::Config do
4
+
5
+ it "should inject configuration methods on base module" do
6
+
7
+ expect(TBK).to respond_to :configure
8
+ expect(TBK).to respond_to :config
9
+ expect(TBK.config).to be_kind_of TBK::Config
10
+
11
+ TBK.configure do |config|
12
+ expect(config).to be_kind_of TBK::Config
13
+ expect(config).to be == TBK.config
14
+ end
15
+
16
+ end
17
+
18
+ context('#commerce_id') do
19
+ it "should be nil by default" do
20
+ expect(TBK.config.commerce_id).to be_nil
21
+ end
22
+
23
+ it "should read it's default value from the environment" do
24
+ ENV.should_receive(:[]).with('TBK_COMMERCE_ID').and_return "12345"
25
+
26
+ expect(TBK.config.commerce_id).to be_eql "12345"
27
+ end
28
+ end
29
+
30
+ context('#commerce_key') do
31
+ it "should be nil by default" do
32
+ expect(TBK.config.commerce_key).to be_nil
33
+ end
34
+
35
+ it "should read it's default value from the environment" do
36
+ ENV.should_receive(:[]).with('TBK_COMMERCE_KEY').and_return "PKEY"
37
+
38
+ expect(TBK.config.commerce_key).to be_eql "PKEY"
39
+ end
40
+ end
41
+
42
+ context('#environment') do
43
+ it "should be production by default" do
44
+ expect(TBK.config.environment).to be_eql :production
45
+ end
46
+
47
+ it "should read it's default value from the environment" do
48
+ ENV.should_receive(:[]).with('TBK_COMMERCE_ENVIRONMENT').and_return "test"
49
+
50
+ expect(TBK.config.environment).to be_eql "test"
51
+ end
52
+ end
53
+
54
+ end
@@ -20,6 +20,4 @@ Gem::Specification.new do |gem|
20
20
  gem.add_development_dependency "tzinfo"
21
21
  gem.add_development_dependency "rake"
22
22
  gem.add_development_dependency "rspec"
23
- gem.add_development_dependency "yard"
24
- gem.add_development_dependency "redcarpet"
25
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-25 00:00:00.000000000 Z
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tzinfo
@@ -59,38 +59,6 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: yard
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: redcarpet
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
62
  description: Ruby implementation of Transbank's Webpay protocol
95
63
  email:
96
64
  - me@sagmor.com
@@ -107,6 +75,7 @@ files:
107
75
  - Rakefile
108
76
  - lib/tbk.rb
109
77
  - lib/tbk/commerce.rb
78
+ - lib/tbk/config.rb
110
79
  - lib/tbk/errors.rb
111
80
  - lib/tbk/keys.rb
112
81
  - lib/tbk/keys/test_commerce.pem
@@ -117,6 +86,8 @@ files:
117
86
  - lib/tbk/webpay/confirmation.rb
118
87
  - lib/tbk/webpay/encryption.rb
119
88
  - lib/tbk/webpay/payment.rb
89
+ - spec/commerce_spec.rb
90
+ - spec/config_spec.rb
120
91
  - spec/spec_helper.rb
121
92
  - spec/support/syntax.rb
122
93
  - spec/support/test_commerce.rb
@@ -138,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
109
  version: '0'
139
110
  segments:
140
111
  - 0
141
- hash: -1481680372280631458
112
+ hash: 4580346204296967975
142
113
  required_rubygems_version: !ruby/object:Gem::Requirement
143
114
  none: false
144
115
  requirements:
@@ -147,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
118
  version: '0'
148
119
  segments:
149
120
  - 0
150
- hash: -1481680372280631458
121
+ hash: 4580346204296967975
151
122
  requirements: []
152
123
  rubyforge_project:
153
124
  rubygems_version: 1.8.23
@@ -155,6 +126,8 @@ signing_key:
155
126
  specification_version: 3
156
127
  summary: Pure Ruby implementation of Transbank's Webpay KCC 6.0
157
128
  test_files:
129
+ - spec/commerce_spec.rb
130
+ - spec/config_spec.rb
158
131
  - spec/spec_helper.rb
159
132
  - spec/support/syntax.rb
160
133
  - spec/support/test_commerce.rb