Skip to content

Commit 2017bb2

Browse files
committed
Added support for Rails 3.0.0.
1 parent d897ea2 commit 2017bb2

File tree

96 files changed

+1252
-8651
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1252
-8651
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spec/support/tmp/**/*
2+
pkg

.rspec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--color --format documentation

Gemfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source :rubygems
2+
3+
gem "rails", "3.0.0"
4+
gem "fakeweb"
5+
gem "rspec-rails", "2.0.0.beta.20"
6+
gem "faker"
7+
gem "ruby-debug19", :require => false
8+
gem "nokogiri"

Gemfile.lock

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
abstract (1.0.0)
5+
actionmailer (3.0.0)
6+
actionpack (= 3.0.0)
7+
mail (~> 2.2.5)
8+
actionpack (3.0.0)
9+
activemodel (= 3.0.0)
10+
activesupport (= 3.0.0)
11+
builder (~> 2.1.2)
12+
erubis (~> 2.6.6)
13+
i18n (~> 0.4.1)
14+
rack (~> 1.2.1)
15+
rack-mount (~> 0.6.12)
16+
rack-test (~> 0.5.4)
17+
tzinfo (~> 0.3.23)
18+
activemodel (3.0.0)
19+
activesupport (= 3.0.0)
20+
builder (~> 2.1.2)
21+
i18n (~> 0.4.1)
22+
activerecord (3.0.0)
23+
activemodel (= 3.0.0)
24+
activesupport (= 3.0.0)
25+
arel (~> 1.0.0)
26+
tzinfo (~> 0.3.23)
27+
activeresource (3.0.0)
28+
activemodel (= 3.0.0)
29+
activesupport (= 3.0.0)
30+
activesupport (3.0.0)
31+
archive-tar-minitar (0.5.2)
32+
arel (1.0.1)
33+
activesupport (~> 3.0.0)
34+
builder (2.1.2)
35+
columnize (0.3.1)
36+
diff-lcs (1.1.2)
37+
erubis (2.6.6)
38+
abstract (>= 1.0.0)
39+
faker (0.3.1)
40+
fakeweb (1.3.0)
41+
i18n (0.4.1)
42+
linecache19 (0.5.11)
43+
ruby_core_source (>= 0.1.4)
44+
mail (2.2.5)
45+
activesupport (>= 2.3.6)
46+
mime-types
47+
treetop (>= 1.4.5)
48+
mime-types (1.16)
49+
nokogiri (1.4.3.1)
50+
polyglot (0.3.1)
51+
rack (1.2.1)
52+
rack-mount (0.6.13)
53+
rack (>= 1.0.0)
54+
rack-test (0.5.4)
55+
rack (>= 1.0)
56+
rails (3.0.0)
57+
actionmailer (= 3.0.0)
58+
actionpack (= 3.0.0)
59+
activerecord (= 3.0.0)
60+
activeresource (= 3.0.0)
61+
activesupport (= 3.0.0)
62+
bundler (~> 1.0.0)
63+
railties (= 3.0.0)
64+
railties (3.0.0)
65+
actionpack (= 3.0.0)
66+
activesupport (= 3.0.0)
67+
rake (>= 0.8.4)
68+
thor (~> 0.14.0)
69+
rake (0.8.7)
70+
rspec (2.0.0.beta.20)
71+
rspec-core (= 2.0.0.beta.20)
72+
rspec-expectations (= 2.0.0.beta.20)
73+
rspec-mocks (= 2.0.0.beta.20)
74+
rspec-core (2.0.0.beta.20)
75+
rspec-expectations (2.0.0.beta.20)
76+
diff-lcs (>= 1.1.2)
77+
rspec-mocks (2.0.0.beta.20)
78+
rspec-rails (2.0.0.beta.20)
79+
rspec (= 2.0.0.beta.20)
80+
ruby-debug-base19 (0.11.24)
81+
columnize (>= 0.3.1)
82+
linecache19 (>= 0.5.11)
83+
ruby_core_source (>= 0.1.4)
84+
ruby-debug19 (0.11.6)
85+
columnize (>= 0.3.1)
86+
linecache19 (>= 0.5.11)
87+
ruby-debug-base19 (>= 0.11.19)
88+
ruby_core_source (0.1.4)
89+
archive-tar-minitar (>= 0.5.2)
90+
thor (0.14.0)
91+
treetop (1.4.8)
92+
polyglot (>= 0.3.1)
93+
tzinfo (0.3.23)
94+
95+
PLATFORMS
96+
ruby
97+
98+
DEPENDENCIES
99+
faker
100+
fakeweb
101+
nokogiri
102+
rails (= 3.0.0)
103+
rspec-rails (= 2.0.0.beta.20)
104+
ruby-debug19

README.markdown

+15-8
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@ Após o processo de compra e pagamento, o usuário é enviado de volta a seu sit
1616

1717
Antes de enviar o usuário para essa URL, o robô do PagSeguro faz um POST para ela, em segundo plano, com os dados e status da transação. Lendo esse POST, você pode obter o status do pedido. Se o pagamento entrou em análise, ou se o usuário pagou usando boleto bancário, o status será "Aguardando Pagamento" ou "Em Análise". Nesses casos, quando a transação for confirmada (o que pode acontecer alguns dias depois) a loja receberá outro POST, informando o novo status. **Cada vez que a transação muda de status, um POST é enviado.**
1818

19+
REQUISITOS
20+
----------
21+
22+
A versão atual que está sendo mantida suporta Rails 3.0.0 ou superior.
23+
24+
Se você quiser esta biblioteca em versão mais antigas do Rails (2.3, por exemplo) deverá usar o [branch legacy](http://github.com/fnando/pagseguro/tree/legacy). Note que esta versão não será mais atualizada com novas funcionalidades; apenas correções de bugs serão aplicadas.
25+
1926
COMO USAR
2027
---------
2128

2229
### Configuração
2330

24-
O primeiro passo é instalar o plugin. Para isso, basta executar o comando abaixo na raíz de seu projeto.
31+
O primeiro passo é instalar a biblioteca. Para isso, basta executar o comando
2532

26-
script/plugin install git://github.com/fnando/pagseguro.git
33+
gem install pagseguro
2734

2835
Se for utilizar o modo de desenvolvimento também precisará da gem Faker:
2936

30-
sudo gem install faker
37+
gem install faker
3138

32-
Depois de instalar o plugin, você precisará executar a rake abaixo; ela irá gerar o arquivo `config/pagseguro.yml`.
39+
Depois de instalar a biblioteca, você precisará executar gerar o arquivo de configuração, que deve residir em `config/pagseguro.yml`. Para gerar um arquivo de modelo execute
3340

34-
rake pagseguro:setup
41+
rails generate pagseguro:install
3542

3643
O arquivo de configuração gerado será parecido com isto:
3744

@@ -92,7 +99,7 @@ Por padrão, o formulário é enviado para o email no arquivo de configuração.
9299

93100
### Recebendo notificações
94101

95-
Toda vez que o status de pagamento for alterado, o [PagSeguro](https://pagseguro.uol.com.br/?ind=689659) irá notificar sua URL de retorno com diversos dados. Você pode interceptar estas notificações com o método `pagseguro_notification`. O bloco receberá um objeto da class `PagSeguro::Notification` e só será executado se for uma notificação verificada junto ao [PagSeguro](https://pagseguro.uol.com.br/?ind=689659).
102+
Toda vez que o status de pagamento for alterado, o [PagSeguro](https://pagseguro.uol.com.br/?ind=689659) irá notificar sua URL de retorno com diversos dados. Você pode interceptar estas notificações com o método `pagseguro_notification`. O bloco receberá um objeto da classe `PagSeguro::Notification` e só será executado se for uma notificação verificada junto ao [PagSeguro](https://pagseguro.uol.com.br/?ind=689659).
96103

97104
class CartController < ApplicationController
98105
skip_before_filter :verify_authenticity_token
@@ -141,15 +148,15 @@ O objeto `notification` possui os seguintes métodos:
141148

142149
### Utilizando modo de desenvolvimento
143150

144-
Toda vez que você enviar o formulário no modo de desenvolvimento, um arquivo YAML será criado em `tmp/pagseguro-#{RAILS_ENV}.yml`. Esse arquivo conterá todos os pedidos enviados.
151+
Toda vez que você enviar o formulário no modo de desenvolvimento, um arquivo YAML será criado em `tmp/pagseguro-#{Rails.env}.yml`. Esse arquivo conterá todos os pedidos enviados.
145152

146153
Depois, você será redirecionado para a URL de retorno que você configurou no arquivo `config/pagseguro.yml`. Para simular o envio de notificações, você deve utilizar a rake `pagseguro:notify`.
147154

148155
$ rake pagseguro:notify ID=<id do pedido>
149156

150157
O ID do pedido deve ser o mesmo que foi informado quando você instanciou a class `PagSeguro::Order`. Por padrão, o status do pedido será `completed` e o tipo de pagamento `credit_card`. Você pode especificar esses parâmetros como o exemplo abaixo.
151158

152-
$ rake pagamento:notify ID=1 PAYMENT_METHOD=invoice STATUS=canceled NOTE="Enviar por motoboy" NAME="José da Silva"
159+
$ rake pagamento:notify ID=1 PAYMENT_METHOD=invoice STATUS=canceled NOTE="Enviar por motoboy" NAME="José da Silva" EMAIL="[email protected]"
153160

154161
#### PAYMENT_METHOD
155162

Rakefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "rspec/core/rake_task"
2+
require File.dirname(__FILE__) + "/lib/pagseguro/version"
3+
4+
RSpec::Core::RakeTask.new
5+
6+
begin
7+
require "jeweler"
8+
9+
JEWEL = Jeweler::Tasks.new do |gem|
10+
gem.name = "pagseguro"
11+
gem.version = PagSeguro::Version::STRING
12+
gem.summary = "A wrapper for the PagSeguro payment gateway."
13+
gem.description = ""
14+
gem.authors = ["Nando Vieira"]
15+
gem.email = "[email protected]"
16+
gem.homepage = "http://github.com/fnando/pagseguro"
17+
gem.has_rdoc = false
18+
gem.files = FileList["{.rspec,Gemfile,Gemfile.lock,Rakefile,README.markdown,pagseguro.gemspec}", "{lib,spec,templates,test}/**/*"]
19+
gem.add_development_dependency "rspec", ">= 2.0.0"
20+
end
21+
22+
Jeweler::GemcutterTasks.new
23+
rescue LoadError => e
24+
puts "You don't have Jeweler installed, so you won't be able to build gems."
25+
end

init.rb

-10
This file was deleted.

lib/pagseguro.rb

+37-79
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,65 @@
22
require "uri"
33
require "time"
44

5-
%w(notification order).each do |f|
6-
require File.join(File.dirname(__FILE__), "pagseguro", f)
7-
end
5+
require "pagseguro/rake"
6+
require "pagseguro/railtie"
7+
require "pagseguro/notification"
8+
require "pagseguro/order"
9+
require "pagseguro/action_controller"
10+
require "pagseguro/helper"
811

912
module PagSeguro
1013
extend self
11-
12-
# The path to the configuration file
13-
if defined?(Rails)
14-
CONFIG_FILE = File.join(Rails.root, "config", "pagseguro.yml")
15-
else
16-
CONFIG_FILE = "config/pagseguro.yml"
17-
end
18-
14+
1915
# PagSeguro receives all invoices in this URL. If developer mode is enabled,
2016
# then the URL will be /pagseguro_developer/invoice
2117
GATEWAY_URL = "https://pagseguro.uol.com.br/security/webpagamentos/webpagto.aspx"
22-
18+
2319
# Hold the config/pagseguro.yml contents
2420
@@config = nil
25-
26-
# Initialize the developer mode if `developer`
27-
# configuration is set
28-
def init!
29-
# check if configuration file is already created
30-
unless File.exist?(CONFIG_FILE)
31-
puts "=> [PagSeguro] The configuration could not be found at #{CONFIG_FILE.inspect}"
32-
return
21+
22+
# The path to the configuration file
23+
def config_file
24+
Rails.root.join("config/pagseguro.yml")
25+
end
26+
27+
# Check if configuration file exists.
28+
def config?
29+
File.exist?(config_file)
30+
end
31+
32+
# Load configuration file.
33+
def config
34+
raise MissingConfigurationException, "file not found on #{config_file.inspect}" unless config?
35+
36+
# load file if is not loaded yet
37+
@@config ||= YAML.load_file(config_file)
38+
39+
# raise an exception if the environment hasn't been set
40+
# or if file is empty
41+
if @@config == false || !@@config[Rails.env]
42+
raise MissingEnvironmentException, ":#{Rails.env} environment not set on #{config_file.inspect}"
3343
end
34-
35-
# The developer mode is enabled? So install it!
36-
developer_mode_install! if developer?
44+
45+
# retrieve the environment settings
46+
@@config[Rails.env]
3747
end
38-
48+
3949
# The gateway URL will point to a local URL is
4050
# app is running in developer mode
4151
def gateway_url
4252
if developer?
43-
File.join "/pagseguro_developer/create"
53+
"/pagseguro_developer"
4454
else
4555
GATEWAY_URL
4656
end
4757
end
48-
58+
4959
# Reader for the `developer` configuration
5060
def developer?
51-
config["developer"] == true
52-
end
53-
54-
# The developer mode install will add a controller to the
55-
# load path and set the URL routing to /pagseguro_developer/:action/:id
56-
# For now, there are only 1 action available: create
57-
def developer_mode_install!
58-
controller_path = File.dirname(__FILE__) + "/pagseguro/controllers"
59-
60-
$LOAD_PATH << controller_path
61-
62-
if defined?(ActiveSupport::Dependencies)
63-
ActiveSupport::Dependencies.load_paths << controller_path
64-
elsif defined?(Dependencies.load_paths)
65-
Dependencies.load_paths << controller_path
66-
else
67-
puts "=> [PagSeguro] Rails version too old for developer mode to work" and return
68-
end
69-
70-
::ActionController::Routing::RouteSet.class_eval do
71-
next if defined?(draw_with_pagseguro_map)
72-
73-
def draw_with_pagseguro_map
74-
draw_without_pagseguro_map do |map|
75-
map.named_route "pagseguro_developer",
76-
"/pagseguro_developer/:action/:id",
77-
:controller => "pagseguro_developer"
78-
79-
yield map
80-
end
81-
end
82-
83-
alias_method_chain :draw, :pagseguro_map
84-
end
61+
config? && config["developer"] == true
8562
end
86-
87-
def config
88-
raise MissingConfigurationException, "file not found on #{CONFIG_FILE.inspect}" unless File.exist?(CONFIG_FILE)
89-
90-
# load file if is not loaded yet
91-
@@config ||= YAML.load_file(CONFIG_FILE)
92-
93-
# raise an exception if the environment hasn't been set
94-
# or if file is empty
95-
if @@config == false || !@@config[RAILS_ENV]
96-
raise MissingEnvironmentException, ":#{RAILS_ENV} environment not set on #{CONFIG_FILE.inspect}"
97-
end
9863

99-
# retrieve the environment settings
100-
@@config[RAILS_ENV]
101-
end
102-
103-
# exceptions
10464
class MissingEnvironmentException < StandardError; end
10565
class MissingConfigurationException < StandardError; end
10666
end
107-
108-
PagSeguro.init!

lib/pagseguro/action_controller.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module PagSeguro
2+
module ActionController
3+
private
4+
def pagseguro_notification(token = nil, &block)
5+
return unless request.post?
6+
7+
notification = PagSeguro::Notification.new(params, token)
8+
yield notification if notification.valid?
9+
end
10+
end
11+
end

lib/pagseguro/action_controller_ext.rb

-11
This file was deleted.

0 commit comments

Comments
 (0)