Class: PagSeguro::Notification
- Inherits:
-
Object
- Object
- PagSeguro::Notification
- Defined in:
- lib/pagseguro/notification.rb
Constant Summary collapse
- API_URL =
"https://pagseguro.uol.com.br/Security/NPI/Default.aspx"
- MAPPING =
Map all the attributes from PagSeguro
{ :payment_method => "TipoPagamento", :order_id => "Referencia", :processed_at => "DataTransacao", :status => "StatusTransacao", :transaction_id => "TransacaoID", :shipping_type => "TipoFrete", :shipping => "ValorFrete", :notes => "Anotacao" }
- STATUS =
Map order status from PagSeguro
{ "Completo" => :completed, "Aguardando Pagto" => :pending, "Aprovado" => :approved, "Em Análise" => :verifying, "Cancelado" => :canceled, "Devolvido" => :refunded }
- PAYMENT_METHOD =
Map payment method from PagSeguro
{ "Cartão de Crédito" => :credit_card, "Boleto" => :invoice, "Pagamento" => :pagseguro, "Pagamento online" => :online_transfer }
Instance Attribute Summary collapse
-
#params ⇒ Object
The Rails params hash.
Instance Method Summary collapse
-
#buyer ⇒ Object
Return the buyer info.
-
#initialize(params, token = nil) ⇒ Notification
constructor
Expects the params object from the current request.
-
#mapping_for(name) ⇒ Object
A wrapper to the params hash, sanitizing the return to symbols.
- #method_missing(method, *args) ⇒ Object
-
#normalize(hash) ⇒ Object
Normalize the specified hash converting all data to UTF-8.
-
#payment_method ⇒ Object
Return the payment method Will be mapped to the PAYMENT_METHOD constant.
-
#processed_at ⇒ Object
Parse the processing date to a Ruby object.
-
#products ⇒ Object
Return a list of products sent by PagSeguro.
- #respond_to?(method, include_private = false) ⇒ Boolean
-
#shipping ⇒ Object
Return the shipping fee Will be converted to a float number.
-
#status ⇒ Object
Return the order status Will be mapped to the STATUS constant.
-
#valid?(force = false) ⇒ Boolean
Cache the validation.
Constructor Details
#initialize(params, token = nil) ⇒ Notification
Expects the params object from the current request
40 41 42 43 |
# File 'lib/pagseguro/notification.rb', line 40 def initialize(params, token = nil) @token = token @params = PagSeguro.developer? ? params : normalize(params) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
121 122 123 124 |
# File 'lib/pagseguro/notification.rb', line 121 def method_missing(method, *args) return mapping_for(method) if MAPPING[method] super end |
Instance Attribute Details
#params ⇒ Object
The Rails params hash
37 38 39 |
# File 'lib/pagseguro/notification.rb', line 37 def params @params end |
Instance Method Details
#buyer ⇒ Object
Return the buyer info
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pagseguro/notification.rb', line 101 def buyer @buyer ||= { :name => params["CliNome"], :email => params["CliEmail"], :phone => { :area_code => params["CliTelefone"].to_s.split(" ").first, :number => params["CliTelefone"].to_s.split(" ").last }, :address => { :street => params["CliEndereco"], :number => params["CliNumero"], :complements => params["CliComplemento"], :neighbourhood => params["CliBairro"], :city => params["CliCidade"], :state => params["CliEstado"], :postal_code => params["CliCEP"] } } end |
#mapping_for(name) ⇒ Object
A wrapper to the params hash, sanitizing the return to symbols
133 134 135 |
# File 'lib/pagseguro/notification.rb', line 133 def mapping_for(name) params[MAPPING[name]] end |
#normalize(hash) ⇒ Object
Normalize the specified hash converting all data to UTF-8
46 47 48 49 50 |
# File 'lib/pagseguro/notification.rb', line 46 def normalize(hash) each_value(hash) do |value| Utils.to_utf8(value) end end |
#payment_method ⇒ Object
Return the payment method Will be mapped to the PAYMENT_METHOD constant
88 89 90 |
# File 'lib/pagseguro/notification.rb', line 88 def payment_method @payment_method ||= PAYMENT_METHOD[mapping_for(:payment_method)] end |
#processed_at ⇒ Object
Parse the processing date to a Ruby object
93 94 95 96 97 98 |
# File 'lib/pagseguro/notification.rb', line 93 def processed_at @processed_at ||= begin groups = *mapping_for(:processed_at).match(/(\d{2})\/(\d{2})\/(\d{4}) ([\d:]+)/sm) Time.parse("#{groups[3]}-#{groups[2]}-#{groups[1]} #{groups[4]}") end end |
#products ⇒ Object
Return a list of products sent by PagSeguro. The values will be normalized (e.g. currencies will be converted to cents, quantity will be an integer)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pagseguro/notification.rb', line 55 def products @products ||= begin items = [] for i in (1..params["NumItens"].to_i) items << { :id => params["ProdID_#{i}"], :description => params["ProdDescricao_#{i}"], :quantity => params["ProdQuantidade_#{i}"].to_i, :price => to_price(params["ProdValor_#{i}"]), :shipping => to_price(params["ProdFrete_#{i}"]), :fees => to_price(params["ProdExtras_#{i}"]) } end items end end |
#respond_to?(method, include_private = false) ⇒ Boolean
126 127 128 129 |
# File 'lib/pagseguro/notification.rb', line 126 def respond_to?(method, include_private = false) return true if MAPPING[method] super end |
#shipping ⇒ Object
Return the shipping fee Will be converted to a float number
76 77 78 |
# File 'lib/pagseguro/notification.rb', line 76 def shipping to_price mapping_for(:shipping) end |
#status ⇒ Object
Return the order status Will be mapped to the STATUS constant
82 83 84 |
# File 'lib/pagseguro/notification.rb', line 82 def status @status ||= STATUS[mapping_for(:status)] end |
#valid?(force = false) ⇒ Boolean
Cache the validation. To bypass the cache, just provide an argument that is evaluated as true.
invoice.valid?
invoice.valid?(:nocache)
142 143 144 145 146 |
# File 'lib/pagseguro/notification.rb', line 142 def valid?(force=false) @valid = nil if force @valid = validates? if @valid.nil? @valid end |