gonow_braspag 1.0

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.
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Redistribution and use in source and binary forms, with or without modification,
2
+ are permitted provided that the following conditions are met:
3
+
4
+ * Redistributions of source code must retain the above copyright notice,
5
+ this list of conditions and the following disclaimer.
6
+ * Redistributions in binary form must reproduce the above copyright notice,
7
+ this list of conditions and the following disclaimer in the documentation
8
+ and/or other materials provided with the distribution.
9
+ * Neither the name of the Rails Dog LLC nor the names of its
10
+ contributors may be used to endorse or promote products derived from this
11
+ software without specific prior written permission.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,33 @@
1
+ README.markdown
2
+ ## ABOUT
3
+
4
+ Spree Extension for Braspag, a Brazilian Payment Gateway
5
+
6
+ ## DEPENDENCIES
7
+
8
+ * [Braspag](http://github.com/gonow/braspag)
9
+
10
+ ## LICENSE
11
+
12
+ (The MIT License)
13
+
14
+ Copyright © 2010
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining
17
+ a copy of this software and associated documentation files (the
18
+ ‘Software’), to deal in the Software without restriction, including
19
+ without limitation the rights to use, copy, modify, merge, publish,
20
+ distribute, sublicense, and/or sell copies of the Software, and to
21
+ permit persons to whom the Software is furnished to do so, subject to
22
+ the following conditions:
23
+
24
+ The above copyright notice and this permission notice shall be
25
+ included in all copies or substantial portions of the Software.
26
+
27
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
28
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
31
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
32
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
33
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/packagetask'
5
+ require 'rake/gempackagetask'
6
+
7
+ gemfile = File.expand_path('../spec/test_app/Gemfile', __FILE__)
8
+ if File.exists?(gemfile) && (%w(spec).include?(ARGV.first.to_s) || ARGV.size == 0)
9
+ require 'bundler'
10
+ ENV['BUNDLE_GEMFILE'] = gemfile
11
+ Bundler.setup
12
+
13
+ require 'rspec'
14
+ require 'rspec/core/rake_task'
15
+ RSpec::Core::RakeTask.new
16
+ end
17
+
18
+ desc "Default Task"
19
+ task :default => [:spec]
20
+
21
+ spec = eval(File.read('gonow_braspag.gemspec'))
22
+
23
+ Rake::GemPackageTask.new(spec) do |p|
24
+ p.gem_spec = spec
25
+ end
26
+
27
+ desc "Release to gemcutter"
28
+ task :release => :package do
29
+ require 'rake/gempackagetask'
30
+ Rake::Gemcutter::Tasks.new(spec).define
31
+ Rake::Task['gem:push'].invoke
32
+ end
33
+
34
+ desc "Default Task"
35
+ task :default => [ :spec ]
36
+
37
+ desc "Regenerates a rails 3 app for testing"
38
+ task :test_app do
39
+ require '../spree/lib/generators/spree/test_app_generator'
40
+ class GonowBraspagTestAppGenerator < Spree::Generators::TestAppGenerator
41
+
42
+ def install_gems
43
+ inside "test_app" do
44
+ run 'bundle exec rake spree_core:install'
45
+ run 'bundle exec rake gonow_braspag:install'
46
+ end
47
+ end
48
+
49
+ def migrate_db
50
+ run_migrations
51
+ end
52
+
53
+ protected
54
+ def full_path_for_local_gems
55
+ <<-gems
56
+ gem 'spree_core', :path => \'#{File.join(File.dirname(__FILE__), "../spree/", "core")}\'
57
+ gem 'gonow_braspag', :path => \'#{File.dirname(__FILE__)}\'
58
+ gems
59
+ end
60
+
61
+ end
62
+ GonowBraspagTestAppGenerator.start
63
+ end
64
+
65
+ namespace :test_app do
66
+ desc 'Rebuild test and cucumber databases'
67
+ task :rebuild_dbs do
68
+ system("cd spec/test_app && bundle exec rake db:drop db:migrate RAILS_ENV=test")
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ "0.60.1" => { :branch => "master", :version => "1.0" }
2
+
3
+
@@ -0,0 +1,7 @@
1
+ class Gateway::Braspag < Gateway
2
+ preference :merchant_id, :string
3
+
4
+ def provider_class
5
+ ActiveMerchant::Billing::BraspagGateway
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ # Add your extension routes here
3
+ end
@@ -0,0 +1,2 @@
1
+ Gateway::Braspag.create! :name => "Cartão de Crédito", :description => "Pagamento em cartão de crédito",
2
+ :environment => "production", :preferred_merchant_id => "{YOUR_MERCHANT_ID_HERE}"
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.platform = Gem::Platform::RUBY
3
+ s.name = 'gonow_braspag'
4
+ s.version = '1.0'
5
+ s.summary = "Extension to use on braspag's service"
6
+ s.description = "Extension to use on braspag's service"
7
+ s.required_ruby_version = '>= 1.8.7'
8
+
9
+ s.author = 'Gonow'
10
+ # s.email = 'david@loudthinking.com'
11
+ s.homepage = 'https://github.com/gonow/spree-braspag'
12
+ # s.rubyforge_project = 'actionmailer'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.require_path = 'lib'
17
+ s.requirements << 'none'
18
+
19
+ s.add_dependency('spree_core', '>= 0.60.1')
20
+ end
@@ -0,0 +1,72 @@
1
+ module ActiveMerchant
2
+ module Billing
3
+ class BraspagGateway < Gateway
4
+ def initialize(options = {})
5
+ @options = options
6
+ end
7
+
8
+ def purchase(money, creditcard, options = {})
9
+ full_name = full_name_from creditcard
10
+ authorize_response = gateway.authorize! :merchantId => merchant_id,
11
+ :orderId => options[:order_id],
12
+ :customerName => full_name,
13
+ :amount => format_amount(options[:subtotal]),
14
+ :paymentMethod => code_for_brand_of(creditcard),
15
+ :holder => full_name,
16
+ :cardNumber => creditcard[:number],
17
+ :expiration => format_expiration_from(creditcard),
18
+ :securityCode => creditcard[:verification_value],
19
+ :numberPayments => 1,
20
+ :typePayment => 0
21
+ if success?(authorize_response)
22
+ capture_response = gateway.capture! :merchantId => merchant_id, :orderId => options[:order_id] if success?(authorize_response)
23
+ Response.new success?(authorize_response), capture_response['message'], { :transaction_id => authorize_response['transactionId'] }, :authorization => authorize_response['authorisationNumber']
24
+ else
25
+ Response.new false, authorize_response['message']
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def success?(response)
32
+ %w(0 1).include? response['status']
33
+ end
34
+
35
+ def gateway
36
+ @gateway ||= Braspag::Gateway.new connection
37
+ end
38
+
39
+ def connection
40
+ Braspag::Connection.new merchant_id, @options[:server]
41
+ end
42
+
43
+ def code_for_brand_of(creditcard)
44
+ case CreditCard.type? creditcard[:number]
45
+ when 'visa' then 22
46
+ when 'master' then 23
47
+ when 'american_express' then 18
48
+ end
49
+ end
50
+
51
+ def format_expiration_from(creditcard)
52
+ "#{last_digits_from('00' + creditcard[:month])}/#{last_digits_from(creditcard[:year])}"
53
+ end
54
+
55
+ def merchant_id
56
+ @options[:merchant_id]
57
+ end
58
+
59
+ def format_amount(amount)
60
+ (amount / 100).to_s.sub ".", ","
61
+ end
62
+
63
+ def last_digits_from(string)
64
+ string[-2,2]
65
+ end
66
+
67
+ def full_name_from(creditcard)
68
+ "#{creditcard[:first_name]} #{creditcard[:last_name]}"
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,17 @@
1
+ require 'spree_core'
2
+ require 'gonow_braspag_hooks'
3
+
4
+ module GonowBraspag
5
+ class Engine < Rails::Engine
6
+
7
+ config.autoload_paths += %W(#{config.root}/lib)
8
+
9
+ def self.activate
10
+ Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
11
+ Rails.env.production? ? require(c) : load(c)
12
+ end
13
+ end
14
+
15
+ config.to_prepare &method(:activate).to_proc
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ class GonowBraspagHooks < Spree::ThemeSupport::HookListener
2
+ # custom hooks go here
3
+ end
@@ -0,0 +1 @@
1
+ # add custom rake tasks here
@@ -0,0 +1,25 @@
1
+ namespace :gonow_braspag do
2
+ desc "Copies all migrations and assets (NOTE: This will be obsolete with Rails 3.1)"
3
+ task :install do
4
+ Rake::Task['gonow_braspag:install:migrations'].invoke
5
+ Rake::Task['gonow_braspag:install:assets'].invoke
6
+ end
7
+
8
+ namespace :install do
9
+ desc "Copies all migrations (NOTE: This will be obsolete with Rails 3.1)"
10
+ task :migrations do
11
+ source = File.join(File.dirname(__FILE__), '..', '..', 'db')
12
+ destination = File.join(Rails.root, 'db')
13
+ Spree::FileUtilz.mirror_files(source, destination)
14
+ end
15
+
16
+ desc "Copies all assets (NOTE: This will be obsolete with Rails 3.1)"
17
+ task :assets do
18
+ source = File.join(File.dirname(__FILE__), '..', '..', 'public')
19
+ destination = File.join(Rails.root, 'public')
20
+ puts "INFO: Mirroring assets from #{source} to #{destination}"
21
+ Spree::FileUtilz.mirror_files(source, destination)
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,125 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Gateway::Braspag do
4
+ it 'should have a merchant_id as preference' do
5
+ subject.preferred(:merchant_id).should be_nil
6
+ end
7
+
8
+ it 'should have ActiveMerchant::Billing::BraspagGateway as a provider' do
9
+ subject.provider_class.should eql(ActiveMerchant::Billing::BraspagGateway)
10
+ end
11
+ end
12
+
13
+ def stub_braspag
14
+ @connection = mock(Braspag::Connection)
15
+ response = { 'message' => "Transaction Successful 123", 'status' => "0" }
16
+ @buyer = mock(Braspag::Gateway, :authorize! => response, :capture! => response)
17
+ Braspag::Connection.stub(:new).with(@merchant_id, RAILS_ENV).and_return(@connection)
18
+ Braspag::Gateway.should_receive(:new).once.with(@connection).and_return(@buyer)
19
+ end
20
+
21
+ describe ActiveMerchant::Billing::BraspagGateway do
22
+ before :each do
23
+ @merchant_id = '3A93CA3-19D1-10AB-F11A-10A37BA55F1B'
24
+ @gateway = subject.class.new :server => Rails.env, :merchant_id => @merchant_id
25
+ @card = Creditcard.new :number => "4051885600446623", :month => "5", :year => "2010", :verification_value => "123", :last_digits => "2432", :first_name => "Piero", :last_name => "Alva"
26
+ @options = { :subtotal => BigDecimal.new('5998'), :tax => 0, :order_id => "R447661256" }
27
+ stub_braspag
28
+ end
29
+
30
+ context "on purchase" do
31
+ it 'should authorize a payment' do
32
+ @buyer.should_receive(:authorize!).with(:merchantId => @merchant_id, :orderId => @options[:order_id], :customerName => "Piero Alva", :amount => "59,98", :paymentMethod => 22, :holder => "Piero Alva", :cardNumber => @card[:number], :expiration => '05/10', :securityCode => '123', :numberPayments => 1, :typePayment => 0).and_return({})
33
+ @gateway.purchase 12345, @card, @options
34
+ end
35
+
36
+ it "should return an instance of ActiveMerchant::Billing::Response" do
37
+ @gateway.purchase(12345, @card, @options).should be_instance_of(ActiveMerchant::Billing::Response)
38
+ end
39
+
40
+ it 'should capture the gateway message' do
41
+ @gateway.purchase(4343, @card, @options).message.should eql("Transaction Successful 123")
42
+ end
43
+
44
+ it 'should send paymentMethod as 22 when the card is visa' do
45
+ ActiveMerchant::Billing::CreditCard.stub(:type?).with(@card[:number]).and_return("visa")
46
+ @buyer.should_receive(:authorize!).with(hash_including(:paymentMethod => 22))
47
+ @gateway.purchase 1234, @card, @options
48
+ end
49
+
50
+ it 'should send paymentMethod as 23 when the card is master' do
51
+ ActiveMerchant::Billing::CreditCard.stub(:type?).with(@card[:number]).and_return("master")
52
+ @buyer.should_receive(:authorize!).with(hash_including(:paymentMethod => 23))
53
+ @gateway.purchase 1234, @card, @options
54
+ end
55
+
56
+ it 'should send paymentMethod as 18 when the card is amex' do
57
+ ActiveMerchant::Billing::CreditCard.stub(:type?).with(@card[:number]).and_return("american_express")
58
+ @buyer.should_receive(:authorize!).with(hash_including(:paymentMethod => 18))
59
+ @gateway.purchase 1234, @card, @options
60
+ end
61
+
62
+ it 'should interpret status code 1 as a good transaction' do
63
+ @buyer.stub(:authorize!).and_return 'message' => "Transaction Successful", 'status' => "1"
64
+ @gateway.purchase(100, @card, @options).should be_success
65
+ end
66
+
67
+ context "with good data" do
68
+ before :each do
69
+ @buyer.stub(:authorize!).and_return 'message' => "Transaction Successful", 'status' => "0", 'authorisationNumber' => '1234', 'transactionId' => 'x123'
70
+ @buyer.stub(:capture!).and_return 'message' => "Transaction Successful 2", 'status' => "1", 'authorisationNumber' => '', 'transactionId' => ''
71
+ end
72
+
73
+ it 'should return a response without error' do
74
+ @gateway.purchase(100, @card, @options).should be_success
75
+ end
76
+
77
+ it 'should capture the payment' do
78
+ @buyer.should_receive(:capture!).with(:merchantId => @merchant_id, :orderId => @options[:order_id])
79
+ @gateway.purchase 100, @card, @options
80
+ end
81
+
82
+ it "should return the message from the last operation" do
83
+ @gateway.purchase(100, @card, @options).message.should eql("Transaction Successful 2")
84
+ end
85
+
86
+ it "should return the authorization code" do
87
+ @gateway.purchase(100, @card, @options).authorization.should eql("1234")
88
+ end
89
+
90
+ it "should return the transaction id as a param" do
91
+ @gateway.purchase(100, @card, @options).params['transaction_id'].should eql("x123")
92
+ end
93
+ end
94
+
95
+ context "with wrong data" do
96
+ before :each do
97
+ @buyer.stub(:authorize!).and_return 'message' => "Transaction Successful", 'status' => "2"
98
+ end
99
+
100
+ it 'should return a response with error' do
101
+ @gateway.purchase(100, @card, @options).should_not be_success
102
+ end
103
+
104
+ it 'should not capture the payment' do
105
+ @buyer.should_not_receive(:capture!).with(:merchantId => @merchant_id, :orderId => @options[:order_id])
106
+ @gateway.purchase 100, @card, @options
107
+ end
108
+ end
109
+
110
+ context "with invalid data" do
111
+ before :each do
112
+ @buyer.stub(:authorize!).and_return 'message' => "Transaction Successful", 'status' => nil
113
+ end
114
+
115
+ it 'should return a response with error' do
116
+ @gateway.purchase(100, @card, @options).should_not be_success
117
+ end
118
+
119
+ it 'should not capture the payment' do
120
+ @buyer.should_not_receive(:capture!).with(:merchantId => @merchant_id, :orderId => @options[:order_id])
121
+ @gateway.purchase 100, @card, @options
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,32 @@
1
+ unless defined? SPREE_ROOT
2
+ ENV["RAILS_ENV"] = "test"
3
+ case
4
+ when ENV["SPREE_ENV_FILE"]
5
+ require ENV["SPREE_ENV_FILE"]
6
+ when File.dirname(__FILE__) =~ %r{vendor/SPREE/vendor/extensions}
7
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../../")}/config/environment"
8
+ else
9
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
10
+ end
11
+ end
12
+
13
+ require 'spec'
14
+ require 'spec/rails'
15
+
16
+ Spec::Runner.configure do |config|
17
+ # config.use_transactional_fixtures = true
18
+ # config.use_instantiated_fixtures = false
19
+ # config.fixture_path = Rails.root + '/spec/fixtures'
20
+
21
+ # You can declare fixtures for each behaviour like this:
22
+ # describe "...." do
23
+ # fixtures :table_a, :table_b
24
+ #
25
+ # Alternatively, if you prefer to declare them only once, you can
26
+ # do so here, like so ...
27
+ #
28
+ # config.global_fixtures = :table_a, :table_b
29
+ #
30
+ # If you declare global fixtures, be aware that they will be declared
31
+ # for all of your examples, even those that don't use them.
32
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gonow_braspag
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease: !!null
6
+ platform: ruby
7
+ authors:
8
+ - Gonow
9
+ autorequire: !!null
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-04 00:00:00.000000000 -03:00
13
+ default_executable: !!null
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: spree_core
17
+ requirement: &2152809940 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.60.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2152809940
26
+ description: Extension to use on braspag's service
27
+ email: !!null
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - LICENSE
33
+ - README.md
34
+ - Rakefile
35
+ - Versionfile
36
+ - app/models/gateway/braspag.rb
37
+ - config/routes.rb
38
+ - db/seeds.rb
39
+ - gonow_braspag.gemspec
40
+ - lib/active_merchant/billing/gateways/braspag.rb
41
+ - lib/gonow_braspag.rb
42
+ - lib/gonow_braspag_hooks.rb
43
+ - lib/tasks/gonow_braspag.rake
44
+ - lib/tasks/install.rake
45
+ - spec/models/gateway_braspag_spec.rb
46
+ - spec/spec.opts
47
+ - spec/spec_helper.rb
48
+ has_rdoc: true
49
+ homepage: https://github.com/gonow/spree-braspag
50
+ licenses: []
51
+ post_install_message: !!null
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.8.7
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements:
68
+ - none
69
+ rubyforge_project: !!null
70
+ rubygems_version: 1.5.0
71
+ signing_key: !!null
72
+ specification_version: 3
73
+ summary: Extension to use on braspag's service
74
+ test_files:
75
+ - spec/models/gateway_braspag_spec.rb
76
+ - spec/spec.opts
77
+ - spec/spec_helper.rb