Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Sep 6, 2009
0 parents commit 7817be9
Show file tree
Hide file tree
Showing 70 changed files with 9,274 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
PAGSEGURO
=========

Este é um plugin do Ruby on Rails que permite utilizar o [PagSeguro](http://pagseguro.uol.com.br/), gateway de pagamentos do [UOL](http://uol.com.br).

COMO USAR
---------

### Configuração

[TODO]

### Montando o formulário

[TODO]

### Recebendo notificações

[TODO]

### Enviando notificações

[TODO]

AUTOR:
------

Nando Vieira (<http://simplesideias.com.br>)

Recomendar no [Working With Rails](http://www.workingwithrails.com/person/7846-nando-vieira)

LICENÇA:
--------

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 changes: 10 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.dirname(__FILE__) + "/lib/pagseguro"

if defined?(Rails)
%w(action_controller_ext helper).each do |f|
require File.dirname(__FILE__) + "/lib/pagseguro/#{f}"
end

ActionView::Base.send(:include, PagseguroHelper)
ActionController::Base.send(:include, PagSeguro::ActionController)
end
108 changes: 108 additions & 0 deletions lib/pagseguro.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
require "net/https"
require "uri"
require "time"

%w(notification order).each do |f|
require File.join(File.dirname(__FILE__), "pagseguro", f)
end

module PagSeguro
extend self

# The path to the configuration file
if defined?(Rails)
CONFIG_FILE = File.join(Rails.root, "config", "pagseguro.yml")
else
CONFIG_FILE = "config/pagseguro.yml"
end

# PagSeguro receives all invoices in this URL. If developer mode is enabled,
# then the URL will be /pagseguro_developer/invoice
GATEWAY_URL = "https://pagseguro.uol.com.br/security/webpagamentos/webpagto.aspx"

# Hold the config/pagseguro.yml contents
@@config = nil

# Initialize the developer mode if `developer`
# configuration is set
def init!
# check if configuration file is already created
unless File.exist?(CONFIG_FILE)
puts "=> [PagSeguro] The configuration could not be found at #{CONFIG_FILE.inspect}"
return
end

# The developer mode is enabled? So install it!
developer_mode_install! if developer?
end

# The gateway URL will point to a local URL is
# app is running in developer mode
def gateway_url
if developer?
File.join "/pagseguro_developer/create"
else
GATEWAY_URL
end
end

# Reader for the `developer` configuration
def developer?
config["developer"] == true
end

# The developer mode install will add a controller to the
# load path and set the URL routing to /pagseguro_developer/:action/:id
# For now, there are only 1 action available: create
def developer_mode_install!
controller_path = File.dirname(__FILE__) + "/pagseguro/controllers"

$LOAD_PATH << controller_path

if defined?(ActiveSupport::Dependencies)
ActiveSupport::Dependencies.load_paths << controller_path
elsif defined?(Dependencies.load_paths)
Dependencies.load_paths << controller_path
else
puts "=> [PagSeguro] Rails version too old for developer mode to work" and return
end

::ActionController::Routing::RouteSet.class_eval do
next if defined?(draw_with_pagseguro_map)

def draw_with_pagseguro_map
draw_without_pagseguro_map do |map|
map.named_route "pagseguro_developer",
"/pagseguro_developer/:action/:id",
:controller => "pagseguro_developer"

yield map
end
end

alias_method_chain :draw, :pagseguro_map
end
end

def config
raise MissingConfigurationException, "file not found on #{CONFIG_FILE.inspect}" unless File.exist?(CONFIG_FILE)

# load file if is not loaded yet
@@config ||= YAML.load_file(CONFIG_FILE)

# raise an exception if the environment hasn't been set
# or if file is empty
if @@config == false || !@@config[RAILS_ENV]
raise MissingEnvironmentException, ":#{RAILS_ENV} environment not set on #{CONFIG_FILE.inspect}"
end

# retrieve the environment settings
@@config[RAILS_ENV]
end

# exceptions
class MissingEnvironmentException < StandardError; end
class MissingConfigurationException < StandardError; end
end

PagSeguro.init!
11 changes: 11 additions & 0 deletions lib/pagseguro/action_controller_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module PagSeguro
module ActionController
private
def pagseguro_status(&block)
return unless request.post?

_notification = PagSeguro::Notification.new(params)
yield _notification if _notification.valid?
end
end
end
26 changes: 26 additions & 0 deletions lib/pagseguro/controllers/pagseguro_developer_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class PagseguroDeveloperController < ApplicationController
skip_before_filter :verify_authenticity_token
PAGSEGURO_ORDERS_FILE = File.join(Rails.root, "tmp", "pagseguro-#{RAILS_ENV}.yml")

def create
require "FileUtils" unless defined?(FileUtils)

# create the orders file if doesn't exist
FileUtils.touch(PAGSEGURO_ORDERS_FILE) unless File.exist?(PAGSEGURO_ORDERS_FILE)

# if file is empty, false is returned;
# default to a empty hash is this case
orders = YAML.load_file(PAGSEGURO_ORDERS_FILE) || {}

# add a new order, associating it to the order id
orders[params[:ref_transacao]] = params.except(:controller, :action, :only_path, :authenticity_token)

# save the file
File.open(PAGSEGURO_ORDERS_FILE, "w+") do |f|
f << orders.to_yaml
end

# redirect to the configuration url
redirect_to PagSeguro.config["return_to"]
end
end
12 changes: 12 additions & 0 deletions lib/pagseguro/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module PagseguroHelper
PAGSEGURO_FORM_VIEW = File.expand_path(File.dirname(__FILE__) + "/views/_form.html.erb")

def pagseguro_form(order, options={})
options = {
:encoding => "utf-8",
:submit => "Pagar com PagSeguro"
}.merge(options)

render :file => PAGSEGURO_FORM_VIEW, :locals => {:options => options, :order => order}
end
end
Loading

0 comments on commit 7817be9

Please sign in to comment.