pagamento_digital 0.1.2beta

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.
@@ -0,0 +1,106 @@
1
+ # encoding: utf-8
2
+ module PagamentoDigital
3
+ class Order
4
+ include ActionView::Helpers::NumberHelper
5
+ # Map all billing attributes that will be added as form inputs.
6
+ BILLING_MAPPING = {
7
+ :nome => "nome",
8
+ :cpf => "cpf",
9
+ :sexo => "sexo",
10
+ :data_nasc => 'data_nascimento',
11
+ :email => "email",
12
+ :telefone => "telefone",
13
+ :celular => "celular",
14
+ :endereco => "endereco",
15
+ :complemento => "complemento",
16
+ :bairro => "bairro",
17
+ :cidade => "cidade",
18
+ :estado => "estado",
19
+ :cep => "cep",
20
+ :free => "free",
21
+ :tipo_frete => "tipo_frete",
22
+ :desconto => "desconto",
23
+ :acrescimo => "acrescimo",
24
+ :razao_social => 'cliente_razao_social',
25
+ :cnpj => "cliente_cnpj",
26
+ :rg => 'rg',
27
+ :hash => "hash",
28
+ }
29
+
30
+ # The list of products added to the order
31
+ attr_accessor :products
32
+
33
+ # The billing info that will be sent to PagamentoDigital.
34
+ attr_accessor :billing
35
+
36
+
37
+ def frete= value
38
+ @frete = convert_unit(value, 2)
39
+ end
40
+ def frete
41
+ @frete
42
+ end
43
+
44
+
45
+
46
+ # Define the shipping type.
47
+ # Can be EN (PAC) or SD (Sedex)
48
+ #attr_accessor :tipo_frete
49
+
50
+ def initialize(order_id = nil)
51
+ reset!
52
+ self.id = order_id
53
+ self.billing = {}
54
+ end
55
+
56
+ # Set the order identifier. Should be a unique
57
+ # value to identify this order on your own application
58
+ def id=(identifier)
59
+ @id = identifier
60
+ end
61
+
62
+ # Get the order identifier
63
+ def id
64
+ @id
65
+ end
66
+
67
+ # Remove all products from this order
68
+ def reset!
69
+ @products = []
70
+ end
71
+
72
+ # Add a new product to the PagamentoDigital order
73
+ # The allowed values are:
74
+ # - weight (Optional. If float, will be multiplied by 1000g)
75
+ # - shipping (Optional. If float, will be multiplied by 100 cents)
76
+ # - quantity (Optional. Defaults to 1)
77
+ # - price (Required. If float, will be multiplied by 100 cents)
78
+ # - description (Required. Identifies the product)
79
+ # - id (Required. Should match the product on your database)
80
+ # - fees (Optional. If float, will be multiplied by 100 cents)
81
+ def <<(options)
82
+ options = {
83
+ :preco => nil,
84
+ :qtde => 1
85
+ }.merge(options)
86
+
87
+ # convert shipping to cents
88
+ #options[:frete] = convert_unit(options[:frete], 100)
89
+ # convert price to cents
90
+ options[:preco] = convert_unit(options[:preco], 2)
91
+ products.push(options)
92
+ end
93
+
94
+ def add(options)
95
+ self << options
96
+ end
97
+
98
+ private
99
+ def convert_unit(number, unit)
100
+
101
+ number = number.to_f.round(unit) rescue 0
102
+ number = number_with_precision number, :precision => unit, :separator => '.', :delimiter => ' '
103
+ number
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,26 @@
1
+ module PagamentoDigital
2
+ class Railtie < Rails::Railtie
3
+ # generators do
4
+ # require "pagamento_digital/generator"
5
+ # end
6
+
7
+ initializer :add_routing_paths do |app|
8
+ if PagSeguro.developer?
9
+ app.routes_reloader.paths.unshift(File.dirname(__FILE__) + "/routes.rb")
10
+ end
11
+ end
12
+
13
+ rake_tasks do
14
+ load File.dirname(__FILE__) + "/../tasks/pagamento_digital.rake"
15
+ end
16
+
17
+ initializer "pagamento_digital.initialize" do |app|
18
+ ::ActionView::Base.send(:include, PagamentoDigital::Helper)
19
+ ::ActionController::Base.send(:include, PagamentoDigital::ActionController)
20
+ end
21
+
22
+ config.after_initialize do
23
+ require "pagamento_digital/developer_controller" if PagamentoDigital.developer?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,134 @@
1
+ # encoding: utf-8
2
+ module PagamentoDigital
3
+ module Rake
4
+ extend self
5
+ def run
6
+ require "digest/md5"
7
+
8
+ env = ENV.inject({}) do |buffer, (name, value)|
9
+ value = value.respond_to?(:force_encoding) ? value.dup.force_encoding("UTF-8") : value
10
+ buffer.merge(name => value)
11
+ end
12
+
13
+ # Not running in developer mode? Exit!
14
+ unless PagamentoDigital.developer?
15
+ puts "=> [PagamentoDigital] Can only notify development URLs"
16
+ puts "=> [PagamentoDigital] Double check your config/pagseguro.yml file"
17
+ exit 1
18
+ end
19
+
20
+ # There's no orders file! Exit!
21
+ unless File.exist?(PagamentoDigital::DeveloperController::ORDERS_FILE)
22
+ puts "=> [PagamentoDigital] No orders added. Exiting now!"
23
+ exit 1
24
+ end
25
+
26
+ # Load the orders file
27
+ orders = YAML.load_file(PagamentoDigital::DeveloperController::ORDERS_FILE)
28
+
29
+ # Ops! No orders added! Exit!
30
+ unless orders
31
+ puts "=> [PagamentoDigital] No invoices created. Exiting now!"
32
+ exit 1
33
+ end
34
+
35
+ # Get the specified order
36
+ order = orders[env["ID"]]
37
+
38
+ # Not again! No order! Exit!
39
+ unless order
40
+ puts "=> [PagamentoDigital] The order #{env['ID'].inspect} could not be found. Exiting now!"
41
+ exit 1
42
+ end
43
+
44
+ # Set the client's info
45
+ name = env.fetch("NAME", Faker.name)
46
+ email = env.fetch("EMAIL", Faker.email)
47
+
48
+ order["cliente_nome"] = name
49
+ order["cliente_email"] = email
50
+ order["cliente_endereco"] = Faker.street_name + ", Nº #{rand(1000)}"
51
+
52
+ order["cliente_complemento"] = Faker.secondary_address
53
+ order["cliente_bairro"] = Faker.city
54
+ order["cliente_cidade"] = Faker.city
55
+ order["cliente_estado"] = Faker.state
56
+
57
+ order["cliente_cep"] = Faker.zipcode
58
+ order["cliente_telefone"] = Faker.phone_number
59
+ order["cliente_celular"] = Faker.phone_number
60
+
61
+ # Set the transaction date
62
+ order["data_transacao"] = Time.now.strftime("%d/%m/%Y %H:%M:%S")
63
+
64
+ # Replace the order id to the correct name
65
+ order["id_pedido"] = order.delete("id_pedido")
66
+
67
+
68
+ to_price = proc do |price|
69
+ if price.to_s =~ /^(.*?)[,\.](.*?)$/
70
+ "#{$1}.#{$2}".to_f.round
71
+ else
72
+ "0.00".to_f.round
73
+ end
74
+ end
75
+ sum_valor_produtos = 0
76
+ index = 0
77
+ loop do
78
+ index+=1
79
+ break unless order.has_key?("produto_valor_#{index}")
80
+ order["produto_codigo_#{index}"] = order.delete("produto_codigo_#{index}")
81
+ order["produto_descricao_#{index}"] = order.delete("produto_descricao_#{index}")
82
+ order["produto_valor_#{index}"] = to_price.call(order.delete("produto_valor_#{index}"))
83
+ order["produto_qtde_#{index}"] = order.delete("produto_qtde_#{index}")
84
+ #soma produtos
85
+ sum_valor_produtos += order["produto_valor_#{index}"].to_f * order["produto_qtde_#{index}"].to_f
86
+ end
87
+
88
+ order['frete'] = to_price.call(order.delete "frete")
89
+
90
+ order['valor_original'] = sum_valor_produtos + order['frete'].to_f
91
+ order['valor_loja'] = order['valor_original']
92
+ order['valor_total'] = order['valor_loja'] + 1.5#taxa só para sacanear
93
+
94
+ # Retrieve the specified status or default to :completed
95
+ status = env.fetch("COD_STATUS", :aprovada).to_sym
96
+
97
+ # Retrieve the specified payment method or default to :credit_card
98
+ payment_method = env.fetch("PAYMENT_METHOD", :credicard_visa).to_sym
99
+
100
+ # Set a random transaction id
101
+ order["id_transacao"] = Digest::MD5.hexdigest(Time.now.to_s)
102
+
103
+ # Set note
104
+ order["free"] = env["NOTE"].to_s
105
+
106
+
107
+
108
+
109
+ # Retrieve index
110
+ index = proc do |hash, value|
111
+ if hash.respond_to?(:key)
112
+ hash.key(value)
113
+ else
114
+ hash.index(value)
115
+ end
116
+ end
117
+
118
+ # Set payment method and status
119
+ order["tipo_pagamento"] = index[PagamentoDigital::Notification::PAYMENT_METHOD, payment_method]
120
+ order["cod_status"] = index[PagamentoDigital::Notification::COD_STATUS, status]
121
+
122
+ # Finally, ping the configured return URL
123
+
124
+ uri = URI.parse File.join(PagamentoDigital.config["base"], PagamentoDigital.config["return_to"])
125
+ response = Net::HTTP.post_form uri, order
126
+ basedir = "#{Rails.root}/tmp/pagamento_digital"
127
+ FileUtils.makedirs basedir
128
+ tmp = File.open("#{basedir}/#{order['id_pedido']}.html",'w'){|out| out << PagamentoDigital::Utils.to_utf8(response.body)}
129
+ puts "saida salva em #{tmp.path}"
130
+
131
+
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ get "pagamento_digital_developer/confirm", :to => "pagamento_digital/developer#confirm"
3
+ post "pagamento_digital_developer", :to => "pagamento_digital/developer#create"
4
+ end
@@ -0,0 +1,13 @@
1
+ module PagamentoDigital
2
+ module Utils
3
+ extend self
4
+
5
+ def to_utf8(string)
6
+ string.to_s.unpack("C*").pack("U*")
7
+ end
8
+
9
+ def to_iso8859(string)
10
+ string.to_s.unpack("U*").pack("C*")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module PagamentoDigital
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 2
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}beta"
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ namespace :pagamento_digital do
2
+ desc "Send notification to the URL specified in your config/pagamento_digital.rb file"
3
+ task :notify => :environment do
4
+ PagamentoDigital::Rake.run
5
+ end
6
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ file.reference.pagamento_digital-app=/home/reinaldo/desenvolvimento/ruby/pagamento_digital/app
2
+ file.reference.pagamento_digital-lib=/home/reinaldo/desenvolvimento/ruby/pagamento_digital/lib
3
+ platform.active=Ruby_0
File without changes
@@ -0,0 +1,9 @@
1
+ file.reference.pagamento_digital-app=app
2
+ file.reference.pagamento_digital-lib=lib
3
+ javac.classpath=
4
+ lib.dir=${file.reference.pagamento_digital-lib}
5
+ main.file=
6
+ platform.active=Ruby_0
7
+ source.encoding=UTF-8
8
+ src.dir=${file.reference.pagamento_digital-app}
9
+ src.templates.dir=templates
@@ -0,0 +1,15 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://www.netbeans.org/ns/project/1">
3
+ <type>org.netbeans.modules.ruby.rubyproject</type>
4
+ <configuration>
5
+ <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
6
+ <name>pagamento_digital</name>
7
+ <source-roots>
8
+ <root id="src.templates.dir"/>
9
+ <root id="src.dir"/>
10
+ <root id="lib.dir"/>
11
+ </source-roots>
12
+ <test-roots/>
13
+ </data>
14
+ </configuration>
15
+ </project>
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "pagamento_digital/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "pagamento_digital"
7
+ s.version = PagamentoDigital::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Reinaldo Mendes"]
10
+ s.email = ["reinaldombox-blog@yahoo.com.br"]
11
+ s.homepage = "http://rubygems.org/gems/pagamento_digital"
12
+ s.summary = "Pagamento digital alpha, usar por sua conta"
13
+ s.description = s.summary
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "rails" , "~> 3.1"
21
+ s.add_development_dependency "rake" , "~> 0.9"
22
+
23
+
24
+
25
+
26
+ end
@@ -0,0 +1,43 @@
1
+ ################################################################
2
+ #######só para exemplo utilizar da forma que melhor entender
3
+ ################################################################
4
+ _pagamento = {
5
+ }
6
+ def pagamento.config
7
+ {
8
+ :email => "xxx@xxx.com",
9
+ :token => "xxxxxxxxxxxx"
10
+ }
11
+ end
12
+ def pagamento.reload
13
+ self
14
+ end
15
+
16
+
17
+ ################################################################################
18
+ #_pagamento = FormaPagamento.find_by_chave('pagamento_digital')
19
+ lambda_retorno = lambda{Rails.application.routes.url_helpers.send :pagamento_digital_retorno_url }
20
+
21
+ #redirect após pagamento
22
+ return_to lambda_retorno
23
+ #notificaçoes retorno automático
24
+ url_aviso lambda_retorno
25
+ #email da loja
26
+ email lambda{_pagamento.reload.config[:email]}
27
+ #token gerado para sua conta
28
+ authenticity_token lambda{_pagamento.config[:token]}
29
+
30
+
31
+ development do
32
+ developer true #modo de desenvolvimento
33
+ base "http://localhost:3000" #url base do servidor só necessário para developer
34
+ end
35
+
36
+
37
+ #production do
38
+ # #developer false
39
+ ## email lambda{_pagamento.reload.config[:email]}
40
+ ## authenticity_token lambda{_pagamento.reload.config[:token]}
41
+ #end
42
+
43
+
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pagamento_digital
3
+ version: !ruby/object:Gem::Version
4
+ hash: 699835116
5
+ prerelease: 5
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ - beta
11
+ version: 0.1.2beta
12
+ platform: ruby
13
+ authors:
14
+ - Reinaldo Mendes
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-03-26 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 3
32
+ - 1
33
+ version: "3.1"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 25
45
+ segments:
46
+ - 0
47
+ - 9
48
+ version: "0.9"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Pagamento digital alpha, usar por sua conta
52
+ email:
53
+ - reinaldombox-blog@yahoo.com.br
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - README.markdown
64
+ - Rakefile
65
+ - app/views/pagamento_digital/_form.erb
66
+ - lib/pagamento_digital.rb
67
+ - lib/pagamento_digital/action_controller.rb
68
+ - lib/pagamento_digital/base.rb
69
+ - lib/pagamento_digital/cacert.pem
70
+ - lib/pagamento_digital/developer_controller.rb
71
+ - lib/pagamento_digital/engine.rb
72
+ - lib/pagamento_digital/faker.rb
73
+ - lib/pagamento_digital/generator.rb
74
+ - lib/pagamento_digital/helper.rb
75
+ - lib/pagamento_digital/notification.rb
76
+ - lib/pagamento_digital/order.rb
77
+ - lib/pagamento_digital/railtie.rb
78
+ - lib/pagamento_digital/rake.rb
79
+ - lib/pagamento_digital/routes.rb
80
+ - lib/pagamento_digital/utils.rb
81
+ - lib/pagamento_digital/version.rb
82
+ - lib/tasks/pagamento_digital.rake
83
+ - nbproject/private/config.properties
84
+ - nbproject/private/private.properties
85
+ - nbproject/private/rake-d.txt
86
+ - nbproject/project.properties
87
+ - nbproject/project.xml
88
+ - pagamento_digital.gemspec
89
+ - templates/config.rb
90
+ homepage: http://rubygems.org/gems/pagamento_digital
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">"
111
+ - !ruby/object:Gem::Version
112
+ hash: 25
113
+ segments:
114
+ - 1
115
+ - 3
116
+ - 1
117
+ version: 1.3.1
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.15
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Pagamento digital alpha, usar por sua conta
125
+ test_files: []
126
+