pag_seguro 0.2.3 → 0.3.0

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.
data/README.md CHANGED
@@ -11,26 +11,28 @@ Esta gem foi desenvolvida para utilizar Ruby 1.9.2 ou superior, e não têm comp
11
11
  Adicione a `gem "pag_seguro"` ao seu Gemfile:
12
12
 
13
13
  gem 'pag_seguro'
14
-
14
+
15
15
  Além disso, é necessário que tenha uma conta no pag seguro, e que habilite as seguintes configurações:
16
16
 
17
- Em Integrações -> Token de segurança clique em Gerar novo token e guarde esta informação em local seguro
18
- Em Integrações -> Pagamentos via API é necessário ativar a opção "Quero receber somente pagamentos via API."
19
- Em Integrações -> Notificação de transações é necessário ativar a notificação de transações e definir a url de retorno
20
-
17
+ Em [Integrações -> Token](https://pagseguro.uol.com.br/integracao/token-de-seguranca.jhtml) de segurança clique em Gerar novo token e guarde esta informação em local seguro
18
+ Em [Integrações -> Pagamentos via API](https://pagseguro.uol.com.br/integracao/pagamentos-via-api.jhtml) é necessário ativar a opção "Quero receber somente pagamentos via API."
19
+ Em [Integrações -> Notificação de transações](https://pagseguro.uol.com.br/integracao/notificacao-de-transacoes.jhtml) é necessário ativar a notificação de transações e definir a url de retorno
20
+
21
21
  ## Documentação
22
+
22
23
  ### Classes e Atributos
23
24
 
24
25
  A nomenclatura dos atributos e recursos (classes) esperados pelo PagSeguro foram mantidas porém usando o padrão de nomenclatura do ruby (ao invés do camelcase utilizado pelo pagseguro). Seguem os links das documentações dos atributos no pagseguro:
25
26
 
26
27
  * [API de pagamentos](https://pagseguro.uol.com.br/v2/guia-de-integracao/api-de-pagamentos.html#v2-item-api-de-pagamentos-parametros-api)
27
28
  * [API de notificação](https://pagseguro.uol.com.br/v2/guia-de-integracao/api-de-notificacoes.html)
29
+ * [API de transações](https://pagseguro.uol.com.br/v2/guia-de-integracao/consulta-de-transacoes-por-codigo.html)
28
30
 
29
31
  ### API de Pagamento
30
32
 
31
33
  Segue um exemplo de uso para criação de um pagamento no PagSeguro:
32
34
 
33
- payment = PagSeguro::Payment.new(EMAIL, TOKEN)
35
+ payment = PagSeguro::Payment.new(email, token, id: invoice.id)
34
36
 
35
37
  payment.items = [
36
38
  PagSeguro::Item.new(id: 25, description: "A Bic Pen", amount: "1.50", quantity: "4", shipping_cost: "1.00", weight: 10),
@@ -45,7 +47,7 @@ Além dos items presentes no exemplo acima, é possível configurar `payment.sen
45
47
 
46
48
  Segue um exemplo mais completo do uso da api de pagamentos:
47
49
 
48
- payment = PagSeguro::Payment.new(EMAIL, TOKEN)
50
+ payment = PagSeguro::Payment.new(email, token, id: invoice.id)
49
51
 
50
52
  payment.items = [
51
53
  PagSeguro::Item.new(id: 25, description: "A Bic Pen", amount: "1.50", quantity: "4", shipping_cost: "1.00", weight: 10),
@@ -71,21 +73,21 @@ Com exceção do atributo response (que é utilizado para armazenar a resposta e
71
73
 
72
74
  ### API de Notificação
73
75
 
74
- As notificações de alteração no status da compra no PagSeguro serão enviadas para a URL que tiver configurado na Notificação de transações (vide Instalação). Obs.: Até o momento o PagSeguro não permite configurar uma url dinâmica para envio das notificação ( e apenas permite uma url por conta ), então provavelemente será necessário que crie uma conta diferente no PagSeguro para cada sistema que desenvolver.
76
+ As notificações de alteração no status da compra no PagSeguro serão enviadas para a URL que tiver configurado na [Notificação de transações](https://pagseguro.uol.com.br/v2/guia-de-integracao/consulta-de-transacoes-por-codigo.html). Obs.: Até o momento o PagSeguro não permite configurar uma url dinâmica para envio das notificação ( e apenas permite uma url por conta ), então provavelemente será necessário que crie uma conta diferente no PagSeguro para cada sistema que desenvolver.
75
77
 
76
78
  O código da notificação é enviado pelo PagSeguro através do parâmentro `notificationCode` em uma requisição do tipo POST. Segue um exemplo de uso da notificação em uma aplicação rails (este exemplo supõe a existência de um `resources :notifications` em suas rotas, e um modelo `Invoice` responsável pelos pagamentos):
77
79
 
78
80
  class NotificationsController < ApplicationController
79
81
  def create
80
- EMAIL = "seu_email_cadastrado@nopagseguro.com.br"
81
- TOKEN = "SEU_TOKEN_GERADO_NO_PAG_SEGURO"
82
- NOTIFICATION_CODE = params(:notificationCode)
82
+ email = "seu_email_cadastrado@nopagseguro.com.br"
83
+ token = "SEU_TOKEN_GERADO_NO_PAG_SEGURO"
84
+ notification_code = params(:notificationCode)
83
85
 
84
- notification = PagSeguro::Notification.new(EMAIL, TOKEN, NOTIFICATION_CODE)
86
+ notification = PagSeguro::Notification.new(email, token, notification_code)
85
87
 
86
88
  if notification.approved?
87
89
  # Idealmente faça alguns testes de sanidade, como notification.gross_amount, notification.item_count, etc
88
- # notification.id referencia o id do payment, caso tenha sido configurado
90
+ # notification.id referencia o id do payment/invoice, caso tenha sido configurado
89
91
  # transacation_id identifica o código da transação no pag seguro
90
92
  Invoice.find(notification.id).approve!(notification.transaction_id)
91
93
  end
@@ -96,6 +98,26 @@ O código da notificação é enviado pelo PagSeguro através do parâmentro `no
96
98
  end
97
99
  end
98
100
 
101
+ Para este exemplo, o url configurada na [Notificação de transações](https://pagseguro.uol.com.br/v2/guia-de-integracao/consulta-de-transacoes-por-codigo.html) poderia ser algo como `http://lojamodelo.com.br/notifications`
102
+
103
+ ### Consulta de Transações
104
+
105
+ Para realizar a consulta de uma transação é preciso obter o código da transação. Este código é enviado nas Notificações de Transações do PagSeguro (de forma assíncrona), através do método `notification.transaction_id` ou de forma síncrona assim que o usuário retorna à loja após ter concluído a compra.
106
+
107
+ Para buscar informações da transação de forma síncrona, é necessário que acesse sua conta no PagSeguro, e clique em [Integrações > Página de redirecionamento](https://pagseguro.uol.com.br/integracao/pagina-de-redirecionamento.jhtml) e ative o redirecionamento com o código da transação, definindo o nome do parâmetro que será enviado para sua aplicação (e.g.: http://lojamodelo.com.br/checkout?transaction_id=E884542-81B3-4419-9A75-BCC6FB495EF1 ). O redirecionamento para esta página é executado através de uma requisição GET.
108
+
109
+ Caso queira utilizar uma URL dinâmica de retorno, é necessário ativar a página de redirecionamento dinâmico em [Integrações > Página de redirecionamento](https://pagseguro.uol.com.br/integracao/pagina-de-redirecionamento.jhtml), e passar o argumento `redirect_url` para o objeto PagSeguro::Payment:
110
+
111
+ PagSeguro::Payment.new(email, token, id: invoice.id, redirect_url: "http://lojamodelo.com.br/checkout")
112
+
113
+ Você pode consultar as informações da transação através do `PagSeguro::Query`, que possui os mesmos attributos e métodos que `PagSeguro::Notification` para consulta da transação:
114
+
115
+ query = PagSeguro::Query.new(email, token, "E884542-81B3-4419-9A75-BCC6FB495EF1")
116
+
117
+ if query.approved?
118
+ # ...
119
+ end
120
+
99
121
  ## Validações
100
122
 
101
123
  Os modelos utilizados nesta gem utilizam as validações do ActiveModel (semelhantes às presentes em ActiveRecord/Rails) e incluem diversas validações, permitindo que se verifique a validade (utilizando object.valid?) dos dados antes de enviá-los ao PagSeguro. A gem não bloqueia o envio das informações caso os dados estejam inválidos, deixando este passo a cargo da sua aplicação, mas levanta erros caso o pag seguro retorne algum erro relativo às informações enviadas.
@@ -107,14 +129,18 @@ Esta gem possui testes extensivos utilizando Rspec. Para rodar os testes, altere
107
129
  bundle
108
130
  guard
109
131
 
132
+ ## Todo
133
+
134
+ Adicionar código para realizar consultas ao [Histórico de Transações](https://pagseguro.uol.com.br/v2/guia-de-integracao/consulta-de-transacoes-por-intervalo-de-datas.html)
135
+
110
136
  ## Contribuindo
111
137
 
112
138
  Caso queira contribuir, faça um fork desta gem no [github](https://github.com/heavenstudio/pag_seguro), escreva os testes respectivos ao bug/feature desejados e faça um merge request.
113
139
 
114
- ## TODO
140
+ ## Sobre
115
141
 
116
- Permitir realizar [consultas de transações](https://pagseguro.uol.com.br/v2/guia-de-integracao/consultas.html)
142
+ Desenvolvida por [Stefano Diem Benatti](mailto:stefano@heavenstudio.com.br)
117
143
 
118
- ## Sobre
144
+ ## Colaboradores
119
145
 
120
- Desenvolvida por [Stefano Diem Benatti](mailto:stefano@heavenstudio.com.br)
146
+ Rafael Castilho (<http://github.com/castilhor>)
@@ -1,174 +1,16 @@
1
- # encoding: utf-8
2
- require 'net/https'
3
-
4
1
  module PagSeguro
5
- class Notification
6
- attr_accessor :data
7
-
8
- # possible status values
9
- PAGSEGURO_PROCESSING = 1
10
- PAGSEGURO_IN_ANALYSIS = 2
11
- PAGSEGURO_APPROVED = 3
12
- PAGSEGURO_AVAILABLE = 4
13
- PAGSEGURO_DISPUTED = 5
14
- PAGSEGURO_RETURNED = 6
15
- PAGSEGURO_CANCELLED = 7
16
-
17
- # possible type values
18
- PAGSEGURO_PAYMENT = 1
19
- PAGSEGURO_TRANSFER = 2
20
- PAGSEGURO_ADDITION_OF_FUNDS = 3
21
- PAGSEGURO_CHARGE = 4
22
- PAGSEGURO_BONUS = 5
2
+ class Notification < Transaction
23
3
 
24
4
  def initialize(email = nil, token = nil, notification_code=nil)
25
5
  raise "Needs a notification code" if notification_code.blank?
26
6
  raise "Needs an email" if email.blank?
27
7
  raise "Needs a token" if token.blank?
28
- @data = Nokogiri::XML(notification_data(email, token, notification_code))
8
+ @data = transaction_data(email, token, notification_code)
29
9
  end
30
10
 
31
- def id
32
- @data.css("reference").first.content
33
- end
34
-
35
- def gross_amount
36
- @data.css("grossAmount").first.content
37
- end
38
-
39
- def discount_amount
40
- @data.css("discountAmount").first.content
41
- end
42
-
43
- def fee_amount
44
- @data.css("feeAmount").first.content
45
- end
46
-
47
- def net_amount
48
- @data.css("feeAmount").first.content
49
- end
50
-
51
- def extra_amount
52
- @data.css("feeAmount").first.content
53
- end
54
-
55
- def installment_count
56
- @data.css("itemCount").first.content.to_i
57
- end
58
-
59
- def item_count
60
- @data.css("itemCount").first.content.to_i
61
- end
62
-
63
- def transaction_id
64
- @data.css("code").first.content
65
- end
66
-
67
- def date
68
- DateTime.iso8601( @data.css("date").first.content )
69
- end
70
-
71
- def items
72
- @data.css("items item").map do |i|
73
- Item.new(id: parse_item(i, "id"), description: parse_item(i, "description"), quantity: parse_item(i, "quantity"), amount: parse_item(i, "amount"))
74
- end
75
- end
76
-
77
- def payment_method
78
- pm = PaymentMethod.new(code: parse_css("paymentMethod code"), type: parse_css("paymentMethod type"))
79
- end
80
-
81
- def sender
82
- sn = Sender.new
83
- sn.name = parse_css("sender name")
84
- sn.email = parse_css("sender email")
85
- sn.phone_ddd = parse_css("sender phone areaCode")
86
- sn.phone_number = parse_css("sender phone number")
87
- sn
88
- end
89
-
90
- def shipping
91
- sh = Shipping.new
92
- sh.type = parse_css("shipping type")
93
- sh.cost = parse_css("shipping cost")
94
- sh.state = parse_css("shipping address state")
95
- sh.city = parse_css("shipping address city")
96
- sh.postal_code = parse_css("shipping address postalCode")
97
- sh.district = parse_css("shipping address district")
98
- sh.street = parse_css("shipping address street")
99
- sh.number = parse_css("shipping address number")
100
- sh.complement = parse_css("shipping address complement")
101
- sh
102
- end
103
-
104
- def status
105
- @data.css("status").first.content.to_i
106
- end
107
-
108
- def type
109
- @data.css("type").first.content.to_i
110
- end
111
-
112
- def processing?
113
- PAGSEGURO_PROCESSING == status
114
- end
115
-
116
- def in_analysis?
117
- PAGSEGURO_IN_ANALYSIS == status
118
- end
119
-
120
- def approved?
121
- PAGSEGURO_APPROVED == status
122
- end
123
-
124
- def available?
125
- PAGSEGURO_AVAILABLE == status
126
- end
127
-
128
- def disputed?
129
- PAGSEGURO_DISPUTED == status
130
- end
131
-
132
- def returned?
133
- PAGSEGURO_RETURNED == status
134
- end
135
-
136
- def cancelled?
137
- PAGSEGURO_CANCELLED == status
138
- end
139
-
140
- def payment?
141
- PAGSEGURO_PAYMENT == type
142
- end
143
-
144
- def transfer?
145
- PAGSEGURO_TRANSFER == type
146
- end
147
-
148
- def addition_of_funds?
149
- PAGSEGURO_ADDITION_OF_FUNDS == type
150
- end
151
-
152
- def charge?
153
- PAGSEGURO_CHARGE == type
154
- end
155
-
156
- def bonus?
157
- PAGSEGURO_BONUS == type
158
- end
159
-
160
11
  private
161
- def notification_data(email, token, notification_code)
162
- RestClient.get("https://ws.pagseguro.uol.com.br/v2/transactions/notifications/#{notification_code}?email=#{email}&token=#{token}")
12
+ def transaction_data(email, token, notification_code)
13
+ super(RestClient.get("#{PAGSEGURO_TRANSACTIONS_URL}/notifications/#{notification_code}?email=#{email}&token=#{token}"))
163
14
  end
164
-
165
- def parse_item(data, attribute)
166
- data.css(attribute).first.content
167
- end
168
-
169
- def parse_css(selector)
170
- value = @data.css(selector).first
171
- value.nil? ? nil : value.content
172
- end
173
15
  end
174
- end
16
+ end
@@ -0,0 +1,16 @@
1
+ module PagSeguro
2
+ class Query < Transaction
3
+
4
+ def initialize(email = nil, token = nil, transaction_code=nil)
5
+ raise "Needs a transaction code" if transaction_code.blank?
6
+ raise "Needs an email" if email.blank?
7
+ raise "Needs a token" if token.blank?
8
+ @data = transaction_data(email, token, transaction_code)
9
+ end
10
+
11
+ private
12
+ def transaction_data(email, token, transaction_code)
13
+ super(RestClient.get("#{PAGSEGURO_TRANSACTIONS_URL}/#{transaction_code}?email=#{email}&token=#{token}"))
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,173 @@
1
+ # encoding: utf-8
2
+ require 'net/https'
3
+
4
+ module PagSeguro
5
+ class Transaction
6
+ attr_accessor :data
7
+
8
+ PAGSEGURO_TRANSACTIONS_URL = "https://ws.pagseguro.uol.com.br/v2/transactions"
9
+
10
+ # possible status values
11
+ PAGSEGURO_PROCESSING = 1
12
+ PAGSEGURO_IN_ANALYSIS = 2
13
+ PAGSEGURO_APPROVED = 3
14
+ PAGSEGURO_AVAILABLE = 4
15
+ PAGSEGURO_DISPUTED = 5
16
+ PAGSEGURO_RETURNED = 6
17
+ PAGSEGURO_CANCELLED = 7
18
+
19
+ # possible type values
20
+ PAGSEGURO_PAYMENT = 1
21
+ PAGSEGURO_TRANSFER = 2
22
+ PAGSEGURO_ADDITION_OF_FUNDS = 3
23
+ PAGSEGURO_CHARGE = 4
24
+ PAGSEGURO_BONUS = 5
25
+
26
+ def initialize(transaction_xml)
27
+ @data = transaction_data(transaction_xml)
28
+ end
29
+
30
+ def id
31
+ @data.css("reference").first.content
32
+ end
33
+
34
+ def gross_amount
35
+ @data.css("grossAmount").first.content
36
+ end
37
+
38
+ def discount_amount
39
+ @data.css("discountAmount").first.content
40
+ end
41
+
42
+ def fee_amount
43
+ @data.css("feeAmount").first.content
44
+ end
45
+
46
+ def net_amount
47
+ @data.css("feeAmount").first.content
48
+ end
49
+
50
+ def extra_amount
51
+ @data.css("feeAmount").first.content
52
+ end
53
+
54
+ def installment_count
55
+ @data.css("itemCount").first.content.to_i
56
+ end
57
+
58
+ def item_count
59
+ @data.css("itemCount").first.content.to_i
60
+ end
61
+
62
+ def transaction_id
63
+ @data.css("code").first.content
64
+ end
65
+
66
+ def date
67
+ DateTime.iso8601( @data.css("date").first.content )
68
+ end
69
+
70
+ def items
71
+ @data.css("items item").map do |i|
72
+ Item.new(id: parse_item(i, "id"), description: parse_item(i, "description"), quantity: parse_item(i, "quantity"), amount: parse_item(i, "amount"))
73
+ end
74
+ end
75
+
76
+ def payment_method
77
+ pm = PaymentMethod.new(code: parse_css("paymentMethod code"), type: parse_css("paymentMethod type"))
78
+ end
79
+
80
+ def sender
81
+ sn = Sender.new
82
+ sn.name = parse_css("sender name")
83
+ sn.email = parse_css("sender email")
84
+ sn.phone_ddd = parse_css("sender phone areaCode")
85
+ sn.phone_number = parse_css("sender phone number")
86
+ sn
87
+ end
88
+
89
+ def shipping
90
+ sh = Shipping.new
91
+ sh.type = parse_css("shipping type")
92
+ sh.cost = parse_css("shipping cost")
93
+ sh.state = parse_css("shipping address state")
94
+ sh.city = parse_css("shipping address city")
95
+ sh.postal_code = parse_css("shipping address postalCode")
96
+ sh.district = parse_css("shipping address district")
97
+ sh.street = parse_css("shipping address street")
98
+ sh.number = parse_css("shipping address number")
99
+ sh.complement = parse_css("shipping address complement")
100
+ sh
101
+ end
102
+
103
+ def status
104
+ @data.css("status").first.content.to_i
105
+ end
106
+
107
+ def type
108
+ @data.css("type").first.content.to_i
109
+ end
110
+
111
+ def processing?
112
+ PAGSEGURO_PROCESSING == status
113
+ end
114
+
115
+ def in_analysis?
116
+ PAGSEGURO_IN_ANALYSIS == status
117
+ end
118
+
119
+ def approved?
120
+ PAGSEGURO_APPROVED == status
121
+ end
122
+
123
+ def available?
124
+ PAGSEGURO_AVAILABLE == status
125
+ end
126
+
127
+ def disputed?
128
+ PAGSEGURO_DISPUTED == status
129
+ end
130
+
131
+ def returned?
132
+ PAGSEGURO_RETURNED == status
133
+ end
134
+
135
+ def cancelled?
136
+ PAGSEGURO_CANCELLED == status
137
+ end
138
+
139
+ def payment?
140
+ PAGSEGURO_PAYMENT == type
141
+ end
142
+
143
+ def transfer?
144
+ PAGSEGURO_TRANSFER == type
145
+ end
146
+
147
+ def addition_of_funds?
148
+ PAGSEGURO_ADDITION_OF_FUNDS == type
149
+ end
150
+
151
+ def charge?
152
+ PAGSEGURO_CHARGE == type
153
+ end
154
+
155
+ def bonus?
156
+ PAGSEGURO_BONUS == type
157
+ end
158
+
159
+ protected
160
+ def transaction_data(transaction_xml)
161
+ Nokogiri::XML(transaction_xml)
162
+ end
163
+
164
+ def parse_item(data, attribute)
165
+ data.css(attribute).first.content
166
+ end
167
+
168
+ def parse_css(selector)
169
+ value = @data.css(selector).first
170
+ value.nil? ? nil : value.content
171
+ end
172
+ end
173
+ end
@@ -1,3 +1,3 @@
1
1
  module PagSeguro
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/pag_seguro.rb CHANGED
@@ -14,7 +14,9 @@ require "payment"
14
14
  require "payment_method"
15
15
  require "sender"
16
16
  require "shipping"
17
+ require "transaction"
17
18
  require "notification"
19
+ require "query"
18
20
 
19
21
  # Error classes
20
22
  require "errors/unauthorized"
File without changes
@@ -1,3 +1,4 @@
1
1
  email: seu_email_cadastrado@nopagseguro.com.br
2
2
  token: SEU_TOKEN_GERADO_NO_PAG_SEGURO
3
3
  notification_code: SEU_CODIGO_DE_NOTIFICACAO
4
+ transaction_id: UM_CODIGO_DE_TRANSACAO
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe PagSeguro::Query do
5
+ before :all do
6
+ if EMAIL == "seu_email_cadastrado@nopagseguro.com.br"
7
+ pending "You need to set your email for your PagSeguro account in spec/pag_seguro/integration/config.yml in order to run this spec"
8
+ elsif TOKEN == "SEU_TOKEN_GERADO_NO_PAG_SEGURO"
9
+ pending "You need to set your token for your PagSeguro account in spec/pag_seguro/integration/config.yml in order to run this spec"
10
+ elsif TRANSACTION_ID == "UM_CODIGO_DE_TRANSACAO"
11
+ pending "You need to set one transaction id for your PagSeguro account in spec/pag_seguro/integration/config.yml in order to run this spec"
12
+ else
13
+ @query = PagSeguro::Query.new(EMAIL, TOKEN, TRANSACTION_ID)
14
+ end
15
+ end
16
+
17
+ it { @query.transaction_id.should be_present }
18
+ it { @query.date.should be_present }
19
+ it { @query.id.should be_present }
20
+ it { @query.type.should be_present }
21
+ it { @query.status.should be_present }
22
+ it { @query.payment_method.type.should be_present }
23
+ it { @query.payment_method.code.should be_present }
24
+ it { @query.gross_amount.should be_present }
25
+ it { @query.discount_amount.should be_present }
26
+ it { @query.fee_amount.should be_present }
27
+ it { @query.net_amount.should be_present }
28
+ it { @query.extra_amount.should be_present }
29
+ it { @query.installment_count.should be_present }
30
+ it { @query.item_count.should be_present }
31
+ it { @query.items.should be_present }
32
+
33
+ it "should have all required item attributes" do
34
+ @query.items.each do |item|
35
+ item.id.should be_present
36
+ item.description.should be_present
37
+ item.amount.should be_present
38
+ item.quantity.should be_present
39
+ end
40
+ end
41
+
42
+ it { @query.sender.email.should be_present }
43
+ it { @query.shipping.type.should be_present }
44
+ end
@@ -3,184 +3,8 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe PagSeguro::Notification do
6
- before do
7
- notification_xml_mock = File.open( File.expand_path( File.dirname(__FILE__) + '/../fixtures/notification.xml') )
8
- PagSeguro::Notification.any_instance.stub(:notification_data){ notification_xml_mock }
9
- @notification = PagSeguro::Notification.new("mail", "token", "not_code")
10
- end
11
-
12
- it "should have an id" do
13
- @notification.id.should == "REF1234"
14
- end
15
-
16
- it "should have a transaction id" do
17
- @notification.transaction_id.should == "9E884542-81B3-4419-9A75-BCC6FB495EF1"
18
- end
19
-
20
- it "should have a gross amount" do
21
- @notification.gross_amount.should be_present
22
- @notification.gross_amount.should match(/^\d+\.\d{2}$/)
23
- end
6
+ before { PagSeguro::Notification.any_instance.stub(transaction_data: transaction_data) }
7
+ let(:transaction){ PagSeguro::Notification.new("mail", "token", "not_code") }
24
8
 
25
- it "should have a discount amount" do
26
- @notification.discount_amount.should be_present
27
- @notification.discount_amount.should match(/^\d+\.\d{2}$/)
28
- end
29
-
30
- it "should have a fee amount" do
31
- @notification.fee_amount.should be_present
32
- @notification.fee_amount.should match(/^\d+\.\d{2}$/)
33
- end
34
-
35
- it "should have a net amount" do
36
- @notification.net_amount.should be_present
37
- @notification.net_amount.should match(/^\d+\.\d{2}$/)
38
- end
39
-
40
- it "should have an extra amount" do
41
- @notification.extra_amount.should be_present
42
- @notification.extra_amount.should match(/^\d+\.\d{2}$/)
43
- end
44
-
45
- it "should have an installment count" do
46
- @notification.installment_count.should be_present
47
- @notification.installment_count.should be_an_integer
48
- end
49
-
50
- it "should have an item count" do
51
- @notification.item_count.should be_present
52
- @notification.item_count.should be_an_integer
53
- @notification.item_count.should == @notification.items.count
54
- end
55
-
56
- it "should be approved in this case" do
57
- @notification.should be_approved
58
- end
59
-
60
- it "should have a sender" do
61
- @sender = @notification.sender
62
- @sender.name.should == "José Comprador"
63
- @sender.email.should == "comprador@uol.com.br"
64
- @sender.phone_ddd.should == "11"
65
- @sender.phone_number == "56273440"
66
- end
67
-
68
- it "should have a date" do
69
- @notification.date.should be_present
70
- @notification.date.should be_an_instance_of(DateTime)
71
- @notification.date.year.should == 2011
72
- @notification.date.month.should == 2
73
- @notification.date.day.should == 10
74
- end
75
-
76
- it "should have a shipping" do
77
- @shipping = @notification.shipping
78
- @shipping.type.should == 1
79
- @shipping.cost.should == "21.50"
80
- @shipping.state.should == "SP"
81
- @shipping.city.should == "Sao Paulo"
82
- @shipping.postal_code.should == "01452002"
83
- @shipping.district.should == "Jardim Paulistano"
84
- @shipping.street.should == "Av. Brig. Faria Lima"
85
- @shipping.number.should == "1384"
86
- @shipping.complement.should == "5o andar"
87
- end
88
-
89
- it "should have a payment method" do
90
- @payment_method = @notification.payment_method
91
- @payment_method.code.should == 101
92
- @payment_method.type.should == 1
93
- end
94
-
95
- it "should have items" do
96
- @items = @notification.items
97
- @items.size.should == 2
98
-
99
- @items[0].id.should == "0001"
100
- @items[0].description.should == "Notebook Prata"
101
- @items[0].quantity.should == "1"
102
- @items[0].amount.should == "24300.00"
103
-
104
- @items[1].id.should == "0002"
105
- @items[1].description.should == "Notebook Rosa"
106
- @items[1].quantity.should == "1"
107
- @items[1].amount.should == "25600.00"
108
- end
109
-
110
- describe "status" do
111
- it "should have a status" do
112
- @notification.status.should == 3
113
- end
114
-
115
- it "should be processing if its status is 1" do
116
- @notification.stub(:status){ 1 }
117
- @notification.should be_processing
118
- end
119
-
120
- it "should be in analysis if its status is 2" do
121
- @notification.stub(:status){ 2 }
122
- @notification.should be_in_analysis
123
- end
124
-
125
- it "should be approved if its status is 3" do
126
- @notification.stub(:status){ 3 }
127
- @notification.should be_approved
128
- end
129
-
130
- it "should be available if its status is 4" do
131
- @notification.stub(:status){ 4 }
132
- @notification.should be_available
133
- end
134
-
135
- it "should be disputed if its status is 5" do
136
- @notification.stub(:status){ 5 }
137
- @notification.should be_disputed
138
- end
139
-
140
- it "should be disputed if its status is 5" do
141
- @notification.stub(:status){ 5 }
142
- @notification.should be_disputed
143
- end
144
-
145
- it "should be returned if its status is 6" do
146
- @notification.stub(:status){ 6 }
147
- @notification.should be_returned
148
- end
149
-
150
- it "should be cancelled if its status is 7" do
151
- @notification.stub(:status){ 7 }
152
- @notification.should be_cancelled
153
- end
154
- end
155
-
156
- describe "type" do
157
- it "should have a type" do
158
- @notification.type.should == 1
159
- end
160
-
161
- it "should be payment if type is 1" do
162
- @notification.stub(:type){ 1 }
163
- @notification.should be_payment
164
- end
165
-
166
- it "should be transfer if type is 2" do
167
- @notification.stub(:type){ 2 }
168
- @notification.should be_transfer
169
- end
170
-
171
- it "should be addition of funds if type is 3" do
172
- @notification.stub(:type){ 3 }
173
- @notification.should be_addition_of_funds
174
- end
175
-
176
- it "should be charge if type is 4" do
177
- @notification.stub(:type){ 4 }
178
- @notification.should be_charge
179
- end
180
-
181
- it "should be bonus if type is 5" do
182
- @notification.stub(:type){ 5 }
183
- @notification.should be_bonus
184
- end
185
- end
186
- end
9
+ it_behaves_like "a transaction"
10
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe PagSeguro::Query do
6
+ before { PagSeguro::Query.any_instance.stub(transaction_data: transaction_data) }
7
+ let(:transaction){ PagSeguro::Query.new("mail", "token", "trans_code") }
8
+
9
+ it_behaves_like "a transaction"
10
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe PagSeguro::Transaction do
6
+ let(:transaction){ PagSeguro::Transaction.new(transaction_xml) }
7
+
8
+ it_behaves_like "a transaction"
9
+ end
data/spec/spec_helper.rb CHANGED
@@ -5,11 +5,13 @@ end
5
5
 
6
6
  require 'yaml'
7
7
  require File.dirname(__FILE__) + "/../lib/pag_seguro"
8
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
8
9
 
9
10
  config = YAML.load_file(File.dirname(__FILE__) + "/pag_seguro/integration/config.yml")
10
11
  EMAIL = config["email"]
11
12
  TOKEN = config["token"]
12
13
  NOTIFICATION_CODE = config["notification_code"]
14
+ TRANSACTION_ID = config["transaction_id"]
13
15
 
14
16
  class HaveAttributeAccessor
15
17
  def initialize(attribute)
@@ -0,0 +1,180 @@
1
+ # encoding: UTF-8
2
+ shared_examples_for "a transaction" do
3
+ let(:transaction_xml){ File.open( File.expand_path( File.dirname(__FILE__) + '/../fixtures/transaction.xml') ) }
4
+ let(:transaction_data){ Nokogiri::XML(transaction_xml) }
5
+
6
+ it "should have an id" do
7
+ transaction.id.should == "REF1234"
8
+ end
9
+
10
+ it "should have a transaction id" do
11
+ transaction.transaction_id.should == "9E884542-81B3-4419-9A75-BCC6FB495EF1"
12
+ end
13
+
14
+ it "should have a gross amount" do
15
+ transaction.gross_amount.should be_present
16
+ transaction.gross_amount.should match(/^\d+\.\d{2}$/)
17
+ end
18
+
19
+ it "should have a discount amount" do
20
+ transaction.discount_amount.should be_present
21
+ transaction.discount_amount.should match(/^\d+\.\d{2}$/)
22
+ end
23
+
24
+ it "should have a fee amount" do
25
+ transaction.fee_amount.should be_present
26
+ transaction.fee_amount.should match(/^\d+\.\d{2}$/)
27
+ end
28
+
29
+ it "should have a net amount" do
30
+ transaction.net_amount.should be_present
31
+ transaction.net_amount.should match(/^\d+\.\d{2}$/)
32
+ end
33
+
34
+ it "should have an extra amount" do
35
+ transaction.extra_amount.should be_present
36
+ transaction.extra_amount.should match(/^\d+\.\d{2}$/)
37
+ end
38
+
39
+ it "should have an installment count" do
40
+ transaction.installment_count.should be_present
41
+ transaction.installment_count.should be_an_integer
42
+ end
43
+
44
+ it "should have an item count" do
45
+ transaction.item_count.should be_present
46
+ transaction.item_count.should be_an_integer
47
+ transaction.item_count.should == transaction.items.count
48
+ end
49
+
50
+ it "should be approved in this case" do
51
+ transaction.should be_approved
52
+ end
53
+
54
+ it "should have a sender" do
55
+ @sender = transaction.sender
56
+ @sender.name.should == "José Comprador"
57
+ @sender.email.should == "comprador@uol.com.br"
58
+ @sender.phone_ddd.should == "11"
59
+ @sender.phone_number == "56273440"
60
+ end
61
+
62
+ it "should have a date" do
63
+ transaction.date.should be_present
64
+ transaction.date.should be_an_instance_of(DateTime)
65
+ transaction.date.year.should == 2011
66
+ transaction.date.month.should == 2
67
+ transaction.date.day.should == 10
68
+ end
69
+
70
+ it "should have a shipping" do
71
+ @shipping = transaction.shipping
72
+ @shipping.type.should == 1
73
+ @shipping.cost.should == "21.50"
74
+ @shipping.state.should == "SP"
75
+ @shipping.city.should == "Sao Paulo"
76
+ @shipping.postal_code.should == "01452002"
77
+ @shipping.district.should == "Jardim Paulistano"
78
+ @shipping.street.should == "Av. Brig. Faria Lima"
79
+ @shipping.number.should == "1384"
80
+ @shipping.complement.should == "5o andar"
81
+ end
82
+
83
+ it "should have a payment method" do
84
+ @payment_method = transaction.payment_method
85
+ @payment_method.code.should == 101
86
+ @payment_method.type.should == 1
87
+ end
88
+
89
+ it "should have items" do
90
+ @items = transaction.items
91
+ @items.size.should == 2
92
+
93
+ @items[0].id.should == "0001"
94
+ @items[0].description.should == "Notebook Prata"
95
+ @items[0].quantity.should == "1"
96
+ @items[0].amount.should == "24300.00"
97
+
98
+ @items[1].id.should == "0002"
99
+ @items[1].description.should == "Notebook Rosa"
100
+ @items[1].quantity.should == "1"
101
+ @items[1].amount.should == "25600.00"
102
+ end
103
+
104
+ describe "status" do
105
+ it "should have a status" do
106
+ transaction.status.should == 3
107
+ end
108
+
109
+ it "should be processing if its status is 1" do
110
+ transaction.stub(:status){ 1 }
111
+ transaction.should be_processing
112
+ end
113
+
114
+ it "should be in analysis if its status is 2" do
115
+ transaction.stub(:status){ 2 }
116
+ transaction.should be_in_analysis
117
+ end
118
+
119
+ it "should be approved if its status is 3" do
120
+ transaction.stub(:status){ 3 }
121
+ transaction.should be_approved
122
+ end
123
+
124
+ it "should be available if its status is 4" do
125
+ transaction.stub(:status){ 4 }
126
+ transaction.should be_available
127
+ end
128
+
129
+ it "should be disputed if its status is 5" do
130
+ transaction.stub(:status){ 5 }
131
+ transaction.should be_disputed
132
+ end
133
+
134
+ it "should be disputed if its status is 5" do
135
+ transaction.stub(:status){ 5 }
136
+ transaction.should be_disputed
137
+ end
138
+
139
+ it "should be returned if its status is 6" do
140
+ transaction.stub(:status){ 6 }
141
+ transaction.should be_returned
142
+ end
143
+
144
+ it "should be cancelled if its status is 7" do
145
+ transaction.stub(:status){ 7 }
146
+ transaction.should be_cancelled
147
+ end
148
+ end
149
+
150
+ describe "type" do
151
+ it "should have a type" do
152
+ transaction.type.should == 1
153
+ end
154
+
155
+ it "should be payment if type is 1" do
156
+ transaction.stub(:type){ 1 }
157
+ transaction.should be_payment
158
+ end
159
+
160
+ it "should be transfer if type is 2" do
161
+ transaction.stub(:type){ 2 }
162
+ transaction.should be_transfer
163
+ end
164
+
165
+ it "should be addition of funds if type is 3" do
166
+ transaction.stub(:type){ 3 }
167
+ transaction.should be_addition_of_funds
168
+ end
169
+
170
+ it "should be charge if type is 4" do
171
+ transaction.stub(:type){ 4 }
172
+ transaction.should be_charge
173
+ end
174
+
175
+ it "should be bonus if type is 5" do
176
+ transaction.stub(:type){ 5 }
177
+ transaction.should be_bonus
178
+ end
179
+ end
180
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pag_seguro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-15 00:00:00.000000000 Z
12
+ date: 2012-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -97,11 +97,13 @@ files:
97
97
  - lib/pag_seguro/notification.rb
98
98
  - lib/pag_seguro/payment.rb
99
99
  - lib/pag_seguro/payment_method.rb
100
+ - lib/pag_seguro/query.rb
100
101
  - lib/pag_seguro/sender.rb
101
102
  - lib/pag_seguro/shipping.rb
103
+ - lib/pag_seguro/transaction.rb
102
104
  - lib/pag_seguro/version.rb
103
105
  - pag_seguro.gemspec
104
- - spec/fixtures/notification.xml
106
+ - spec/fixtures/transaction.xml
105
107
  - spec/pag_seguro/checkout_xml_spec.rb
106
108
  - spec/pag_seguro/errors/invalid_data_spec.rb
107
109
  - spec/pag_seguro/errors/unauthorized_spec.rb
@@ -109,14 +111,18 @@ files:
109
111
  - spec/pag_seguro/integration/checkout_spec.rb
110
112
  - spec/pag_seguro/integration/config.yml
111
113
  - spec/pag_seguro/integration/notification_spec.rb
114
+ - spec/pag_seguro/integration/query_spec.rb
112
115
  - spec/pag_seguro/item_spec.rb
113
116
  - spec/pag_seguro/notification_spec.rb
114
117
  - spec/pag_seguro/payment_method_spec.rb
115
118
  - spec/pag_seguro/payment_spec.rb
119
+ - spec/pag_seguro/query_spec.rb
116
120
  - spec/pag_seguro/sender_spec.rb
117
121
  - spec/pag_seguro/shipping_spec.rb
122
+ - spec/pag_seguro/transaction_spec.rb
118
123
  - spec/pag_seguro/version_spec.rb
119
124
  - spec/spec_helper.rb
125
+ - spec/support/transaction_shared_examples.rb
120
126
  homepage: http://github.com/heavenstudio/pag_seguro
121
127
  licenses: []
122
128
  post_install_message:
@@ -142,7 +148,7 @@ signing_key:
142
148
  specification_version: 3
143
149
  summary: A ruby gem to handle PagSeguro's API version 2
144
150
  test_files:
145
- - spec/fixtures/notification.xml
151
+ - spec/fixtures/transaction.xml
146
152
  - spec/pag_seguro/checkout_xml_spec.rb
147
153
  - spec/pag_seguro/errors/invalid_data_spec.rb
148
154
  - spec/pag_seguro/errors/unauthorized_spec.rb
@@ -150,11 +156,16 @@ test_files:
150
156
  - spec/pag_seguro/integration/checkout_spec.rb
151
157
  - spec/pag_seguro/integration/config.yml
152
158
  - spec/pag_seguro/integration/notification_spec.rb
159
+ - spec/pag_seguro/integration/query_spec.rb
153
160
  - spec/pag_seguro/item_spec.rb
154
161
  - spec/pag_seguro/notification_spec.rb
155
162
  - spec/pag_seguro/payment_method_spec.rb
156
163
  - spec/pag_seguro/payment_spec.rb
164
+ - spec/pag_seguro/query_spec.rb
157
165
  - spec/pag_seguro/sender_spec.rb
158
166
  - spec/pag_seguro/shipping_spec.rb
167
+ - spec/pag_seguro/transaction_spec.rb
159
168
  - spec/pag_seguro/version_spec.rb
160
169
  - spec/spec_helper.rb
170
+ - spec/support/transaction_shared_examples.rb
171
+ has_rdoc: false