pagseguro_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rbenv-gemsets ADDED
@@ -0,0 +1 @@
1
+ pagseguro_client
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,123 @@
1
+ # PAGSEGURO Client
2
+
3
+ Este é um plugin do Ruby on Rails que permite utilizar o [PagSeguro](https://pagseguro.uol.com.br), gateway de pagamentos do [UOL](http://uol.com.br).
4
+
5
+ ## Changelog
6
+ * Implementação parcial da [API de pagamentos](https://pagseguro.uol.com.br/v2/guia-de-integracao/api-de-pagamentos.html)
7
+ * Implementação parcial da [API de notificações](https://pagseguro.uol.com.br/v2/guia-de-integracao/notificacoes.html)
8
+
9
+ ## Como usar
10
+
11
+ ### Instalação
12
+
13
+ Adicione a biblioteca ao arquivo Gemfile:
14
+
15
+ ```ruby
16
+ gem 'pagseguro_client', git: "git://github.com/matheustardivo/pagseguro_client.git"
17
+ ```
18
+
19
+ E crie o arquivo de configuração em `config/pagseguro.yml`:
20
+
21
+ ```yaml
22
+ development: &development
23
+ base_url: "http://localhost:4000"
24
+ email: matheustardivo@gmail.com
25
+ token: "a1951ac04115012fabb660334b97cc6e"
26
+
27
+ test:
28
+ <<: *development
29
+
30
+ production:
31
+ base_url: "https://ws.pagseguro.uol.com.br"
32
+ email: matheustardivo@gmail.com
33
+ token: "tokenGeradoPeloPagseguro"
34
+ ```
35
+
36
+ Para realizar os seus testes você pode usar um servidor de testes do Pagseguro que ainda está em fase de desenvolvimento, mas já pode ser acessado através do link: [Pagseguro Server](http://pss.tardivo.info)
37
+
38
+ ### Gerando a sua ordem de pagamento
39
+
40
+ ```ruby
41
+ @order = PagseguroClient::Order.new(id) # Seu identificador da ordem de pagamento
42
+ @order.add(
43
+ id: "1", # Seu identificador do produto
44
+ description: produto.descricao,
45
+ amount: produto.preco)
46
+ @response = order.send_request
47
+ ```
48
+
49
+ ### Exemplo de resposta da ordem de pagamento
50
+
51
+ ```ruby
52
+ # Hash
53
+ {
54
+ code: "8CF4BE7DCECEF0F004A6DFA0A8243412",
55
+ url: "https://ws.pagseguro.uol.com.br/v2/checkout/payment.html?code=8CF4BE7DCECEF0F004A6DFA0A8243412"
56
+ }
57
+ ```
58
+
59
+ Agora basta usar a url retornada para enviar o usuário para efetuar o pagamento no site do Pagseguro.
60
+
61
+ ### Obtendo as notificações
62
+ ```ruby
63
+ # No seu controller
64
+ def notificacao
65
+ return unless request.post?
66
+
67
+ @notification = PagseguroClient::Notification.retrieve(params[:notificationCode])
68
+ # Seu código para utilizar a notificação
69
+
70
+ render nothing: true
71
+ end
72
+ ```
73
+
74
+ O objeto `notification` possui os seguintes métodos:
75
+
76
+ * `PagseguroClient::Notification#code`: Código da notificação
77
+ * `PagseguroClient::Notification#order_id`: Código da sua ordem de pagamento
78
+ * `PagseguroClient::Notification#order_code`: Código do Pagseguro para sua ordem de pagamento
79
+ * `PagseguroClient::Notification#status`: Status da ordem de pagamento atual
80
+ * `PagseguroClient::Notification#payment_method`: Método utilizado para o pagamento
81
+
82
+ #### Métodos de pagamento
83
+
84
+ * `credit_card`: Cartão de crédito
85
+ * `invoice`: Boleto
86
+ * `online_transfer`: Pagamento online
87
+ * `pagseguro`: Transferência entre contas do PagSeguro
88
+ * `oi_paggo`: Oi Paggo :)
89
+
90
+ #### Status
91
+
92
+ * `pending`: Aguardando pagamento
93
+ * `verifying`: Em análise
94
+ * `approved`: Aprovado
95
+ * `available`: Disponível
96
+ * `refunded`: Devolvido
97
+ * `canceled`: Cancelado
98
+
99
+ ## Autor
100
+ Matheus Tardivo (<http://matheustardivo.com>)
101
+
102
+ ## Licença:
103
+
104
+ (The MIT License)
105
+
106
+ Permission is hereby granted, free of charge, to any person obtaining
107
+ a copy of this software and associated documentation files (the
108
+ 'Software'), to deal in the Software without restriction, including
109
+ without limitation the rights to use, copy, modify, merge, publish,
110
+ distribute, sublicense, and/or sell copies of the Software, and to
111
+ permit persons to whom the Software is furnished to do so, subject to
112
+ the following conditions:
113
+
114
+ The above copyright notice and this permission notice shall be
115
+ included in all copies or substantial portions of the Software.
116
+
117
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
118
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
119
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
120
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
121
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
122
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
123
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,40 @@
1
+ module PagseguroClient
2
+ extend self
3
+
4
+ @@config = nil
5
+
6
+ def config_file
7
+ Rails.root.join("config/pagseguro.yml")
8
+ end
9
+
10
+ def config?
11
+ File.exist?(config_file)
12
+ end
13
+
14
+ def config
15
+ raise MissingConfigurationError, "file not found on #{config_file.inspect}" unless config?
16
+
17
+ @@config ||= YAML.load_file(config_file)
18
+
19
+ if @@config == false || !@@config[Rails.env]
20
+ raise MissingEnvironmentError, ":#{Rails.env} environment not set on #{config_file.inspect}"
21
+ end
22
+
23
+ @@config[Rails.env]
24
+ end
25
+
26
+ def base_url
27
+ config["base_url"]
28
+ end
29
+
30
+ def token
31
+ config["token"]
32
+ end
33
+
34
+ def email
35
+ config["email"]
36
+ end
37
+
38
+ class MissingEnvironmentError < StandardError; end
39
+ class MissingConfigurationError < StandardError; end
40
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ module PagseguroClient
4
+ class Notification
5
+ PAYMENT_METHOD = {
6
+ 1 => :credit_card,
7
+ 2 => :invoice,
8
+ 3 => :online_transfer,
9
+ 4 => :pagseguro,
10
+ 5 => :oi_paggo
11
+ }
12
+
13
+ STATUS = {
14
+ 1 => :pending,
15
+ 2 => :verifying,
16
+ 3 => :approved,
17
+ 4 => :available,
18
+ 6 => :refunded,
19
+ 7 => :canceled
20
+ }
21
+
22
+ attr_accessor :code, :order_id, :order_code, :status, :payment_method
23
+
24
+ def initialize(attributes = {})
25
+ attributes.each do |name, value|
26
+ send("#{name}=", value)
27
+ end
28
+ end
29
+
30
+ def self.retrieve(code)
31
+ response = RestClient.get("#{PagseguroClient.base_url}/v2/transactions/notifications/#{code}",
32
+ {
33
+ params: {
34
+ email: PagseguroClient.email,
35
+ token: PagseguroClient.token
36
+ }
37
+ }
38
+ )
39
+
40
+ doc = Nokogiri::XML(response.body)
41
+ order_id = doc.xpath("//reference").text
42
+ order_code = doc.xpath("//code").text
43
+ status = doc.xpath("//status").text.to_i
44
+ payment_method = doc.xpath("//paymentMethod/type").text.to_i
45
+
46
+ Notification.new(
47
+ code: code,
48
+ order_id: order_id,
49
+ order_code: order_code,
50
+ status: STATUS[status],
51
+ payment_method: PAYMENT_METHOD[payment_method]
52
+ )
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,52 @@
1
+ module PagseguroClient
2
+ class Order
3
+
4
+ attr_accessor :id, :products
5
+
6
+ def initialize(order_id)
7
+ self.id = order_id
8
+ self.products = []
9
+ end
10
+
11
+ # The allowed values are:
12
+ # - id (Required. Should match the product in your database)
13
+ # - description (Required. Identifies the product)
14
+ # - amount (Required. If float, will be multiplied by 100 cents)
15
+ # - quantity (Optional. If not supplied, use 1)
16
+ def add(options)
17
+ products.push(options)
18
+ end
19
+
20
+ def data
21
+ data = {
22
+ email: PagseguroClient.email,
23
+ token: PagseguroClient.token,
24
+ currency: "BRL",
25
+ reference: id
26
+ }
27
+ products.each_with_index do |item, index|
28
+ index += 1
29
+ data["itemId#{index}"] = item[:id]
30
+ data["itemDescription#{index}"] = item[:description]
31
+ data["itemAmount#{index}"] = item[:amount]
32
+ data["itemQuantity#{index}"] = item[:quantity] || 1
33
+ end
34
+
35
+ data
36
+ end
37
+
38
+ # Send a new payment request to Pagseguro
39
+ # Returns the URL to redirect your user to complete the payment in Pagseguro
40
+ def send_request
41
+ response = RestClient.post("#{PagseguroClient.base_url}/v2/checkout", data)
42
+
43
+ doc = Nokogiri::XML(response.body)
44
+ code = doc.xpath("//code").text
45
+
46
+ {
47
+ code: code,
48
+ url: "#{PagseguroClient.base_url}/v2/checkout/payment.html?code=#{code}"
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module PagseguroClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "nokogiri"
2
+ require "rest-client"
3
+
4
+ require "pagseguro_client/base"
5
+ require "pagseguro_client/notification"
6
+ require "pagseguro_client/order"
7
+ require "pagseguro_client/version"
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "pagseguro_client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "pagseguro_client"
7
+ s.version = PagseguroClient::VERSION
8
+ s.authors = ["Matheus Tardivo"]
9
+ s.email = ["matheustardivo@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{The library to access the new services from Pagseguro (v2)}
12
+ s.description = s.summary
13
+
14
+ s.rubyforge_project = "pagseguro_client"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec-rails" , "~> 2.7"
22
+ s.add_development_dependency "rails" , "~> 3.2"
23
+ s.add_development_dependency "rake" , "~> 0.9"
24
+ s.add_development_dependency "sqlite3" , "~> 1.3"
25
+ s.add_runtime_dependency "nokogiri" , "~> 1.5"
26
+ s.add_runtime_dependency "rest-client" , "~> 1.6"
27
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe PagseguroClient::Notification do
4
+
5
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe PagseguroClient::Order do
4
+
5
+ it "should set order id when instantiating object" do
6
+ order = PagseguroClient::Order.new("ABCDEF")
7
+ order.id.should == "ABCDEF"
8
+ end
9
+
10
+ it "should add product" do
11
+ order = PagseguroClient::Order.new("John Doe")
12
+ order.add(id: "1", description: "Description", amount: 199)
13
+ order.products.should_not be_empty
14
+ order.products.size.should == 1
15
+ order.products.first[:id].should == "1"
16
+ end
17
+
18
+ it "should set order product data" do
19
+ order = PagseguroClient::Order.new("John Doe")
20
+ order.add(id: "1", description: "Description", amount: 199)
21
+ order.add(id: "2", description: "Description", amount: 199)
22
+ order.add(id: "3", description: "Description", amount: 199)
23
+
24
+ data = order.data
25
+ data.keys.include?("itemId1").should be_true
26
+ data.keys.include?("itemId2").should be_true
27
+ data.keys.include?("itemId3").should be_true
28
+
29
+ data["itemId1"].should == "1"
30
+ data["itemId2"].should == "2"
31
+ data["itemId3"].should == "3"
32
+ end
33
+
34
+ end
@@ -0,0 +1,6 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require "rails"
4
+ require "pagseguro_client"
5
+ require "support/config/boot"
6
+ require "rspec/rails"
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Account < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ authentication
3
+ end
File without changes
File without changes
@@ -0,0 +1,14 @@
1
+ ENV["BUNDLE_GEMFILE"] = File.dirname(__FILE__) + "/../../../Gemfile"
2
+ require "bundler"
3
+ Bundler.setup
4
+ require "rails/all"
5
+ Bundler.require(:default)
6
+
7
+ module PagSeguroClient
8
+ class Application < Rails::Application
9
+ config.root = File.dirname(__FILE__) + "/.."
10
+ config.active_support.deprecation = :log
11
+ end
12
+ end
13
+
14
+ PagSeguroClient::Application.initialize!
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -0,0 +1,10 @@
1
+ development: &development
2
+ base_url: "http://localhost:4000"
3
+ email: matheustardivo@gmail.com
4
+ token: "a1951ac04115012fabb660334b97cc6e"
5
+
6
+ test:
7
+ <<: *development
8
+
9
+ production:
10
+ <<: *development
@@ -0,0 +1,4 @@
1
+ PagSeguroClient::Application.routes.draw do
2
+ get "dashboard", :to => "dashboard#index"
3
+ get "login", :to => "session#new"
4
+ end
File without changes
File without changes
@@ -0,0 +1,55 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
2
+ <transaction>
3
+ <date>2011-02-10T16:13:41.000-03:00</date>
4
+ <code>9E884542-81B3-4419-9A75-BCC6FB495EF1</code>
5
+ <reference>REF1234</reference>
6
+ <type>1</type>
7
+ <status>3</status>
8
+ <paymentMethod>
9
+ <type>1</type>
10
+ <code>101</code>
11
+ </paymentMethod>
12
+ <grossAmount>49900.00</grossAmount>
13
+ <discountAmount>0.00</discountAmount>
14
+ <feeAmount>0.00</feeAmount>
15
+ <netAmount>49900.00</netAmount>
16
+ <extraAmount>0.00</extraAmount>
17
+ <installmentCount>1</installmentCount>
18
+ <itemCount>2</itemCount>
19
+ <items>
20
+ <item>
21
+ <id>0001</id>
22
+ <description>Notebook Prata</description>
23
+ <quantity>1</quantity>
24
+ <amount>24300.00</amount>
25
+ </item>
26
+ <item>
27
+ <id>0002</id>
28
+ <description>Notebook Rosa</description>
29
+ <quantity>1</quantity>
30
+ <amount>25600.00</amount>
31
+ </item>
32
+ </items>
33
+ <sender>
34
+ <name>José Comprador</name>
35
+ <email>comprador@uol.com.br</email>
36
+ <phone>
37
+ <areaCode>11</areaCode>
38
+ <number>56273440</number>
39
+ </phone>
40
+ </sender>
41
+ <shipping>
42
+ <address>
43
+ <street>Av. Brig. Faria Lima</street>
44
+ <number>1384</number>
45
+ <complement>5o andar</complement>
46
+ <district>Jardim Paulistano</district>
47
+ <postalCode>01452002</postalCode>
48
+ <city>Sao Paulo</city>
49
+ <state>SP</state>
50
+ <country>BRA</country>
51
+ </address>
52
+ <type>1</type>
53
+ <cost>21.50</cost>
54
+ </shipping>
55
+ </transaction>
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pagseguro_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matheus Tardivo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-rails
16
+ requirement: &70144902931820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70144902931820
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails
27
+ requirement: &70144902931020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.2'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70144902931020
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70144902930560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.9'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70144902930560
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: &70144902930100 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70144902930100
58
+ - !ruby/object:Gem::Dependency
59
+ name: nokogiri
60
+ requirement: &70144902945860 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '1.5'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70144902945860
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: &70144902945020 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '1.6'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70144902945020
80
+ description: The library to access the new services from Pagseguro (v2)
81
+ email:
82
+ - matheustardivo@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rbenv-gemsets
89
+ - .rspec
90
+ - Gemfile
91
+ - README.markdown
92
+ - Rakefile
93
+ - lib/pagseguro_client.rb
94
+ - lib/pagseguro_client/base.rb
95
+ - lib/pagseguro_client/notification.rb
96
+ - lib/pagseguro_client/order.rb
97
+ - lib/pagseguro_client/version.rb
98
+ - pagseguro_client.gemspec
99
+ - spec/pagseguro_client/notification_spec.rb
100
+ - spec/pagseguro_client/order_spec.rb
101
+ - spec/spec_helper.rb
102
+ - spec/support/app/controllers/application_controller.rb
103
+ - spec/support/app/models/account.rb
104
+ - spec/support/app/models/user.rb
105
+ - spec/support/app/views/dashboard/index.erb
106
+ - spec/support/app/views/session/new.erb
107
+ - spec/support/config/boot.rb
108
+ - spec/support/config/database.yml
109
+ - spec/support/config/pagseguro.yml
110
+ - spec/support/config/routes.rb
111
+ - spec/support/log/.gitkeep
112
+ - spec/support/log/test.log
113
+ - spec/support/notification.xml
114
+ homepage: ''
115
+ licenses: []
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project: pagseguro_client
134
+ rubygems_version: 1.8.11
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: The library to access the new services from Pagseguro (v2)
138
+ test_files:
139
+ - spec/pagseguro_client/notification_spec.rb
140
+ - spec/pagseguro_client/order_spec.rb
141
+ - spec/spec_helper.rb
142
+ - spec/support/app/controllers/application_controller.rb
143
+ - spec/support/app/models/account.rb
144
+ - spec/support/app/models/user.rb
145
+ - spec/support/app/views/dashboard/index.erb
146
+ - spec/support/app/views/session/new.erb
147
+ - spec/support/config/boot.rb
148
+ - spec/support/config/database.yml
149
+ - spec/support/config/pagseguro.yml
150
+ - spec/support/config/routes.rb
151
+ - spec/support/log/.gitkeep
152
+ - spec/support/log/test.log
153
+ - spec/support/notification.xml