pag_seguro 0.1.6 → 0.1.7

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/.gitignore CHANGED
@@ -3,4 +3,4 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  coverage/*
6
- spec/pag_seguro/integration/config.yml
6
+ spec/pag_seguro/integration/config_example.yml
data/README.md CHANGED
@@ -18,16 +18,17 @@ Além disso, é necessário que tenha uma conta no pag seguro, e que habilite as
18
18
  Em Integrações -> Pagamentos via API é necessário ativar a opção "Quero receber somente pagamentos via API."
19
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
20
 
21
- ## Documentação da API e dos attributos
21
+ ## Documentação
22
+ ### Classes e Atributos
22
23
 
23
- A nomenclatura dos atributos esperados pelo PagSeguro foram mantidas porém no padrão underscore (que é o padrão de ruby, ao invés do camelcase utilizado pelo pagseguro). Seguem os links das documentações dos attributos no pagseguro:
24
+ 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:
24
25
 
25
26
  * [API de pagamentos](https://pagseguro.uol.com.br/v2/guia-de-integracao/api-de-pagamentos.html#v2-item-api-de-pagamentos-parametros-api)
26
27
  * [API de notificação](https://pagseguro.uol.com.br/v2/guia-de-integracao/api-de-notificacoes.html)
27
28
 
28
- ## Documentação
29
+ ### API de Pagamento
29
30
 
30
- Segue um exemplo de uso desta gem (para mais exemplos, olhe o arquivo spec/integration/checkout_spec.rb):
31
+ Segue um exemplo de uso para criação de um pagamento no PagSeguro:
31
32
 
32
33
  payment = PagSeguro::Payment.new(EMAIL, TOKEN)
33
34
 
@@ -38,7 +39,40 @@ Segue um exemplo de uso desta gem (para mais exemplos, olhe o arquivo spec/integ
38
39
 
39
40
  redirect_to_url = payment.checkout_payment_url
40
41
 
41
- Além dos items presentes no exemplo acima, é possível configurar `payment.sender` (com informações do usuário que está efetuando a compra) e `payment.shipping` ( com as informações de endereço ).
42
+ O método checkout_payment_url envia as informações de `payment` ao PagSeguro e em caso de sucesso gera a url do PagSeguro para qual o comprador deverá ser redirecionado para efetuar a compra.
43
+
44
+ Além dos items presentes no exemplo acima, é possível configurar `payment.sender` (com informações do usuário que está efetuando a compra), `payment.shipping` ( com as informações de endereço ), entre outras opções (para mais exemplos, olhe o arquivo spec/integration/checkout_spec.rb). Em especial, o attributo `payment.id` deve ser utilizado para referenciar um pagamento único no seu sistema.
45
+
46
+ Com exceção do atributo response (que é utilizado para armazenar a resposta enviada pelo PagSeguro), todos os outros atributos podem ser inicialidos em formato hash na inicialização do `PagSeguro::Payment`:
47
+
48
+ payment = PagSeguro::Payment.new(EMAIL, TOKEN, id: 2, items: [ PagSeguro::Item.new(id: 17, description: "A pipe", amount: "3.00", quantity: "89") ], extra_amount: '1.00' )
49
+
50
+ ### API de Notificação
51
+
52
+ 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.
53
+
54
+ O código da notificação é enviado pelo PagSeguro através do parâmentro `notificationCode` em uma requisição do tipo POST ( lembre-se de adicionar uma rota post respectiva ). Segue um exemplo de uso da notificação em uma aplicação rails:
55
+
56
+ class PagSeguroNotificationController < ApplicationController
57
+ def parse_notification
58
+ EMAIL = "seu_email_cadastrado@nopagseguro.com.br"
59
+ TOKEN = "SEU_TOKEN_GERADO_NO_PAG_SEGURO"
60
+ NOTIFICATION_CODE = params(:notificationCode)
61
+
62
+ notification = PagSeguro::Notification.new(EMAIL, TOKEN, NOTIFICATION_CODE)
63
+
64
+ if notification.approved?
65
+ # Idealmente faça alguns testes de sanidade, como notification.gross_amount, notification.item_count, etc
66
+ # notification.id referencia o id do payment, caso tenha sido configurado
67
+ # transacation_id identifica o código da transação no pag seguro
68
+ Invoice.find(notification.id).approve!(notification.transaction_id)
69
+ end
70
+
71
+ if notification.cancelled? || notification.returned?
72
+ Invoice.find(notification.id).void!
73
+ end
74
+ end
75
+ end
42
76
 
43
77
  ## Validações
44
78
 
@@ -46,7 +80,7 @@ Os modelos utilizados nesta gem utilizam as validações do ActiveModel (semelha
46
80
 
47
81
  ## Testes
48
82
 
49
- Esta gem possui testes extensivos utilizando Rspec. Para rodar os estes, altere o arquivo spec/pag_seguro/integration/config.yml com seus dados no pag_seguro, entre na pasta onde a gem está instalada e execute:
83
+ Esta gem possui testes extensivos utilizando Rspec. Para rodar os testes, altere o arquivo spec/pag_seguro/integration/config.yml com seus dados no pag_seguro, entre na pasta onde a gem está instalada e execute:
50
84
 
51
85
  bundle
52
86
  guard
@@ -39,7 +39,7 @@
39
39
 
40
40
  - if shipping.present?
41
41
  %shipping
42
- - if shipping.type.present?
42
+ - if !shipping.type.zero?
43
43
  %type= shipping.type
44
44
  %address
45
45
  %country BRA
@@ -1,10 +1,25 @@
1
+ # encoding: utf-8
1
2
  require 'net/https'
2
3
 
3
4
  module PagSeguro
4
5
  class Notification
5
6
  attr_accessor :data
6
- PAGSEGURO_APPROVED = 3
7
- PAGSEGURO_AVAILABLE = 4
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
8
23
 
9
24
  def initialize(email = nil, token = nil, notification_code=nil)
10
25
  raise "Needs a notification code" if notification_code.blank?
@@ -13,25 +28,45 @@ module PagSeguro
13
28
  @data = Nokogiri::XML(notification_data(email, token, notification_code))
14
29
  end
15
30
 
16
- def status
17
- @data.css("status").first.content.to_i
18
- end
19
-
20
- def approved?
21
- PAGSEGURO_APPROVED == status
31
+ def id
32
+ @data.css("reference").first.content
22
33
  end
23
34
 
24
- def available?
25
- PAGSEGURO_AVAILABLE == status
35
+ def gross_amount
36
+ @data.css("grossAmount").first.content
26
37
  end
27
-
28
- def id
29
- @data.css("reference").first.content
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
30
61
  end
31
62
 
32
63
  def transaction_id
33
64
  @data.css("code").first.content
34
65
  end
66
+
67
+ def date
68
+ @data.css("date").first.content
69
+ end
35
70
 
36
71
  def items
37
72
  @data.css("items item").map do |i|
@@ -39,6 +74,10 @@ module PagSeguro
39
74
  end
40
75
  end
41
76
 
77
+ def payment_method
78
+ pm = PaymentMethod.new(code: parse_css("paymentMethod code"), type: parse_css("paymentMethod type"))
79
+ end
80
+
42
81
  def sender
43
82
  sn = Sender.new
44
83
  sn.name = parse_css("sender name")
@@ -62,6 +101,62 @@ module PagSeguro
62
101
  sh
63
102
  end
64
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
+
65
160
  private
66
161
  def notification_data(email, token, notification_code)
67
162
  RestClient.get("https://ws.pagseguro.uol.com.br/v2/transactions/notifications/#{notification_code}?email=#{email}&token=#{token}")
@@ -63,7 +63,7 @@ module PagSeguro
63
63
  end
64
64
 
65
65
  def send_checkout
66
- RestClient.post(checkout_url_with_params, checkout_xml, content_type: "application/xml"){|response, request, result| response }
66
+ RestClient.post(checkout_url_with_params, checkout_xml, content_type: "application/xml"){|resp, request, result| resp }
67
67
  end
68
68
 
69
69
  def parse_checkout_response
@@ -76,7 +76,7 @@ module PagSeguro
76
76
  raise Errors::InvalidData.new(response.body)
77
77
  else
78
78
  raise Errors::UnknownError.new(response)
79
- end
79
+ end
80
80
  end
81
81
 
82
82
  def parse_date
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ module PagSeguro
3
+ class PaymentMethod
4
+ attr_accessor :code, :type
5
+
6
+ # Payment Method types
7
+ CREDIT_CARD = 1
8
+ BANK_BILL = 2
9
+ ONLINE_DEBIT = 3
10
+ PAG_SEGURO_BALANCE = 4
11
+ OI_PAGGO = 5
12
+
13
+ def initialize(options = {})
14
+ @code = options[:code]
15
+ @type = options[:type]
16
+ end
17
+
18
+ def code
19
+ @code.to_i
20
+ end
21
+
22
+ def type
23
+ @type.to_i
24
+ end
25
+
26
+ def credit_card?
27
+ CREDIT_CARD == type
28
+ end
29
+
30
+ def bank_bill?
31
+ BANK_BILL == type
32
+ end
33
+
34
+ def online_debit?
35
+ ONLINE_DEBIT == type
36
+ end
37
+
38
+ def pag_seguro_balance?
39
+ PAG_SEGURO_BALANCE == type
40
+ end
41
+
42
+ def oi_paggo?
43
+ OI_PAGGO == type
44
+ end
45
+
46
+ def name
47
+ case code
48
+ when 101 then "Cartão de crédito Visa"
49
+ when 102 then "Cartão de crédito MasterCard"
50
+ when 103 then "Cartão de crédito American Express"
51
+ when 104 then "Cartão de crédito Diners"
52
+ when 105 then "Cartão de crédito Hipercard"
53
+ when 106 then "Cartão de crédito Aura"
54
+ when 107 then "Cartão de crédito Elo"
55
+ when 108 then "Cartão de crédito PLENOCard"
56
+ when 109 then "Cartão de crédito PersonalCard"
57
+ when 201 then "Boleto Bradesco"
58
+ when 202 then "Boleto Santander"
59
+ when 301 then "Débito online Bradesco"
60
+ when 302 then "Débito online Itaú"
61
+ when 303 then "Débito online Unibanco"
62
+ when 304 then "Débito online Banco do Brasil"
63
+ when 305 then "Débito online Banco Real"
64
+ when 306 then "Débito online Banrisul"
65
+ when 307 then "Débito online HSBC"
66
+ when 401 then "Saldo PagSeguro"
67
+ when 501 then "Oi Paggo"
68
+ else "Desconhecido"
69
+ end
70
+ end
71
+ end
72
+ end
@@ -4,7 +4,7 @@ module PagSeguro
4
4
 
5
5
  PAC = 1
6
6
  SEDEX = 2
7
- NOT_IDENTIFIED = 3
7
+ UNIDENTIFIED = 3
8
8
 
9
9
  validates_format_of :postal_code, with: /^\d{8}$/, message: " must be an integer with 8 digits", allow_blank: true
10
10
 
@@ -25,5 +25,21 @@ module PagSeguro
25
25
  def postal_code
26
26
  @postal_code if @postal_code.present? && @postal_code.to_s.size == 8
27
27
  end
28
+
29
+ def type
30
+ @type.to_i
31
+ end
32
+
33
+ def pac?
34
+ PAC == type
35
+ end
36
+
37
+ def sedex?
38
+ SEDEX == type
39
+ end
40
+
41
+ def unidentified?
42
+ UNIDENTIFIED == type
43
+ end
28
44
  end
29
45
  end
@@ -1,3 +1,3 @@
1
1
  module PagSeguro
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
data/lib/pag_seguro.rb CHANGED
@@ -9,6 +9,7 @@ require "rest-client"
9
9
  # PagSeguro classes
10
10
  require "item"
11
11
  require "payment"
12
+ require "payment_method"
12
13
  require "sender"
13
14
  require "shipping"
14
15
  require "notification"
@@ -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>
@@ -102,7 +102,7 @@ describe PagSeguro::Payment do
102
102
 
103
103
  it "should have shipping type" do
104
104
  @xml_without_shipping_info.css("checkout shipping type").should be_empty
105
- @xml.css("checkout shipping type").first.content.to_i.should == PagSeguro::Shipping::SEDEX
105
+ @xml.css("checkout shipping type").first.content.to_i.should == 2
106
106
  end
107
107
 
108
108
  it "should have state" do
@@ -1,10 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
- config = YAML.load_file(File.dirname(__FILE__) + "/config.yml")
5
- EMAIL = config["email"]
6
- TOKEN = config["token"]
7
-
8
4
  def create_valid_payment
9
5
  payment = PagSeguro::Payment.new(EMAIL, TOKEN)
10
6
  payment.items = [
@@ -18,63 +14,73 @@ def create_valid_payment
18
14
  payment
19
15
  end
20
16
 
21
- describe "PagSeguro::Payment.code" do
22
- it "should send a request to pagseguro" do
23
- payment = create_valid_payment
24
- payment.code.size.should == 32
17
+ describe PagSeguro::Payment do
18
+ before do
19
+ if EMAIL == "seu_email_cadastrado@nopagseguro.com.br"
20
+ pending "You need to set your email for your PagSeguro account in spec/pag_seguro/integration/config.yml in order to run this spec"
21
+ elsif TOKEN == "SEU_TOKEN_GERADO_NO_PAG_SEGURO"
22
+ pending "You need to set your token for your PagSeguro account in spec/pag_seguro/integration/config.yml in order to run this spec"
23
+ end
25
24
  end
26
25
 
27
- it "should be valid even without a sender and shipping information" do
28
- payment = create_valid_payment
29
- payment.sender = nil
30
- payment.shipping = nil
31
- payment.code.size.should == 32
32
- end
26
+ describe "#code" do
27
+ it "should send a request to pagseguro" do
28
+ payment = create_valid_payment
29
+ payment.code.size.should == 32
30
+ end
31
+
32
+ it "should be valid even without a sender and shipping information" do
33
+ payment = create_valid_payment
34
+ payment.sender = nil
35
+ payment.shipping = nil
36
+ payment.code.size.should == 32
37
+ end
33
38
 
34
- it "should tell me when the email and token are invalid" do
35
- payment = PagSeguro::Payment.new("not_a_user@not_an_email.com", "NOTATOKEN7F048A09A8AEFDD1E5A7B91")
36
- lambda { payment.code }.should raise_error(PagSeguro::Errors::Unauthorized)
37
- end
39
+ it "should tell me when the email and token are invalid" do
40
+ payment = PagSeguro::Payment.new("not_a_user@not_an_email.com", "NOTATOKEN7F048A09A8AEFDD1E5A7B91")
41
+ lambda { payment.code }.should raise_error(PagSeguro::Errors::Unauthorized)
42
+ end
38
43
 
39
- it "should list errors given by pagseguro" do
40
- payment = PagSeguro::Payment.new(EMAIL, TOKEN)
41
- lambda { payment.code }.should raise_error(PagSeguro::Errors::InvalidData)
42
- end
44
+ it "should list errors given by pagseguro" do
45
+ payment = PagSeguro::Payment.new(EMAIL, TOKEN)
46
+ lambda { payment.code }.should raise_error(PagSeguro::Errors::InvalidData)
47
+ end
43
48
 
44
- it "should give a response code of 200 for the user pagseguro url" do
45
- payment = create_valid_payment
46
- RestClient.get(payment.checkout_payment_url).code.should == 200
49
+ it "should give a response code of 200 for the user pagseguro url" do
50
+ payment = create_valid_payment
51
+ RestClient.get(payment.checkout_payment_url).code.should == 200
52
+ end
47
53
  end
48
- end
49
54
 
50
- describe "PagSeguro::Payment.date" do
51
- it "should send a request to pagseguro" do
52
- payment = create_valid_payment
53
- payment.date.should match(/^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[-+]\d{2}:\d{2}$/)
55
+ describe "#date" do
56
+ it "should send a request to pagseguro" do
57
+ payment = create_valid_payment
58
+ payment.date.should match(/^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[-+]\d{2}:\d{2}$/)
59
+ end
54
60
  end
55
- end
56
61
 
57
- describe "PagSeguro::Payment parse_checkout_response" do
58
- before do
59
- @payment = create_valid_payment
60
- @payment.stub(:parse_code)
61
- @payment.stub(:parse_date)
62
- @payment.stub(:parse_checkout_response){ "some response" }
63
- end
62
+ describe "#parse_checkout_response" do
63
+ before do
64
+ @payment = create_valid_payment
65
+ @payment.stub(:parse_code)
66
+ @payment.stub(:parse_date)
67
+ @payment.stub(:parse_checkout_response){ "some response" }
68
+ end
64
69
 
65
- it "should not make a request to pagseguro more than once" do
66
- @payment.should_receive(:parse_checkout_response).exactly(1).times
70
+ it "should not make a request to pagseguro more than once" do
71
+ @payment.should_receive(:parse_checkout_response).exactly(1).times
67
72
 
68
- @payment.code
69
- @payment.code
70
- @payment.date
71
- end
73
+ @payment.code
74
+ @payment.code
75
+ @payment.date
76
+ end
72
77
 
73
- it "should be make more than one request to pag seguro if the payment is reset" do
74
- @payment.should_receive(:parse_checkout_response).exactly(2).times
78
+ it "should make more than one request to pag seguro if the payment is reset" do
79
+ @payment.should_receive(:parse_checkout_response).exactly(2).times
75
80
 
76
- @payment.code
77
- @payment.reset!
78
- @payment.date
81
+ @payment.code
82
+ @payment.reset!
83
+ @payment.date
84
+ end
79
85
  end
80
86
  end
@@ -1,2 +1,3 @@
1
1
  email: seu_email_cadastrado@nopagseguro.com.br
2
- token: SEU_TOKEN_GERADO_NO_PAG_SEGURO
2
+ token: SEU_TOKEN_GERADO_NO_PAG_SEGURO
3
+ notification_code: SEU_CODIGO_DE_NOTIFICACAO
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe PagSeguro::Notification 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 NOTIFICATION_CODE == "SEU_CODIGO_DE_NOTIFICACAO"
11
+ pending "You need to set your notification token for your PagSeguro account in spec/pag_seguro/integration/config.yml in order to run this spec"
12
+ else
13
+ @notification = PagSeguro::Notification.new(EMAIL, TOKEN, NOTIFICATION_CODE)
14
+ end
15
+ end
16
+
17
+ it { @notification.transaction_id.should be_present }
18
+ it { @notification.date.should be_present }
19
+ it { @notification.id.should be_present }
20
+ it { @notification.type.should be_present }
21
+ it { @notification.status.should be_present }
22
+ it { @notification.payment_method.type.should be_present }
23
+ it { @notification.payment_method.code.should be_present }
24
+ it { @notification.gross_amount.should be_present }
25
+ it { @notification.discount_amount.should be_present }
26
+ it { @notification.fee_amount.should be_present }
27
+ it { @notification.net_amount.should be_present }
28
+ it { @notification.extra_amount.should be_present }
29
+ it { @notification.installment_count.should be_present }
30
+ it { @notification.item_count.should be_present }
31
+ it { @notification.items.should be_present }
32
+
33
+ it "should have all required item attributes" do
34
+ @notification.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 { @notification.sender.email.should be_present }
43
+ it { @notification.shipping.type.should be_present }
44
+ end
@@ -1,10 +1,11 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require_relative 'notification_data_mock'
5
4
 
6
5
  describe PagSeguro::Notification do
7
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 }
8
9
  @notification = PagSeguro::Notification.new("mail", "token", "not_code")
9
10
  end
10
11
 
@@ -16,6 +17,42 @@ describe PagSeguro::Notification do
16
17
  @notification.transaction_id.should == "9E884542-81B3-4419-9A75-BCC6FB495EF1"
17
18
  end
18
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
24
+
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
+
19
56
  it "should be approved in this case" do
20
57
  @notification.should be_approved
21
58
  end
@@ -28,9 +65,14 @@ describe PagSeguro::Notification do
28
65
  @sender.phone_number == "56273440"
29
66
  end
30
67
 
68
+ it "should have a date" do
69
+ @notification.date.should be_present
70
+ @notification.date.should match(/^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[-+]\d{2}:\d{2}$/)
71
+ end
72
+
31
73
  it "should have a shipping" do
32
74
  @shipping = @notification.shipping
33
- @shipping.type.should == "1"
75
+ @shipping.type.should == 1
34
76
  @shipping.cost.should == "21.50"
35
77
  @shipping.state.should == "SP"
36
78
  @shipping.city.should == "Sao Paulo"
@@ -41,6 +83,12 @@ describe PagSeguro::Notification do
41
83
  @shipping.complement.should == "5o andar"
42
84
  end
43
85
 
86
+ it "should have a payment method" do
87
+ @payment_method = @notification.payment_method
88
+ @payment_method.code.should == 101
89
+ @payment_method.type.should == 1
90
+ end
91
+
44
92
  it "should have items" do
45
93
  @items = @notification.items
46
94
  @items.size.should == 2
@@ -56,13 +104,80 @@ describe PagSeguro::Notification do
56
104
  @items[1].amount.should == "25600.00"
57
105
  end
58
106
 
59
- it "should be_available if its status is 4" do
60
- @notification.stub(:status){ 4 }
61
- @notification.should be_available
107
+ describe "status" do
108
+ it "should have a status" do
109
+ @notification.status.should == 3
110
+ end
111
+
112
+ it "should be processing if its status is 1" do
113
+ @notification.stub(:status){ 1 }
114
+ @notification.should be_processing
115
+ end
116
+
117
+ it "should be in analysis if its status is 2" do
118
+ @notification.stub(:status){ 2 }
119
+ @notification.should be_in_analysis
120
+ end
121
+
122
+ it "should be approved if its status is 3" do
123
+ @notification.stub(:status){ 3 }
124
+ @notification.should be_approved
125
+ end
126
+
127
+ it "should be available if its status is 4" do
128
+ @notification.stub(:status){ 4 }
129
+ @notification.should be_available
130
+ end
131
+
132
+ it "should be disputed if its status is 5" do
133
+ @notification.stub(:status){ 5 }
134
+ @notification.should be_disputed
135
+ end
136
+
137
+ it "should be disputed if its status is 5" do
138
+ @notification.stub(:status){ 5 }
139
+ @notification.should be_disputed
140
+ end
141
+
142
+ it "should be returned if its status is 6" do
143
+ @notification.stub(:status){ 6 }
144
+ @notification.should be_returned
145
+ end
146
+
147
+ it "should be cancelled if its status is 7" do
148
+ @notification.stub(:status){ 7 }
149
+ @notification.should be_cancelled
150
+ end
62
151
  end
63
152
 
64
- it "should be_approved if its status is 3" do
65
- @notification.stub(:status){ 3 }
66
- @notification.should be_approved
153
+ describe "type" do
154
+ it "should have a type" do
155
+ @notification.type.should == 1
156
+ end
157
+
158
+ it "should be payment if type is 1" do
159
+ @notification.stub(:type){ 1 }
160
+ @notification.should be_payment
161
+ end
162
+
163
+ it "should be transfer if type is 2" do
164
+ @notification.stub(:type){ 2 }
165
+ @notification.should be_transfer
166
+ end
167
+
168
+ it "should be addition of funds if type is 3" do
169
+ @notification.stub(:type){ 3 }
170
+ @notification.should be_addition_of_funds
171
+ end
172
+
173
+ it "should be charge if type is 4" do
174
+ @notification.stub(:type){ 4 }
175
+ @notification.should be_charge
176
+ end
177
+
178
+ it "should be bonus if type is 5" do
179
+ @notification.stub(:type){ 5 }
180
+ @notification.should be_bonus
181
+ end
67
182
  end
68
183
  end
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe PagSeguro::PaymentMethod do
5
+ context "instance" do
6
+ context "accessors" do
7
+ before { @payment_method = PagSeguro::PaymentMethod.new }
8
+
9
+ it { @payment_method.should have_attribute_accessor(:code) }
10
+ it { @payment_method.should have_attribute_accessor(:type) }
11
+ end
12
+
13
+ it "should be able to initialize with all attributes" do
14
+ payment_method = PagSeguro::PaymentMethod.new(code: "101", type: "1")
15
+ payment_method.code.should == 101
16
+ payment_method.type.should == 1
17
+ end
18
+
19
+ describe "types" do
20
+ before { @payment_method = PagSeguro::PaymentMethod.new }
21
+
22
+ it "should be credit card if type is 1" do
23
+ @payment_method.stub(:type){ 1 }
24
+ @payment_method.should be_credit_card
25
+ end
26
+
27
+ it "should be bank bill if type is 2" do
28
+ @payment_method.stub(:type){ 2 }
29
+ @payment_method.should be_bank_bill
30
+ end
31
+
32
+ it "should be online debit if type is 3" do
33
+ @payment_method.stub(:type){ 3 }
34
+ @payment_method.should be_online_debit
35
+ end
36
+
37
+ it "should be PagSeguro balance if type is 4" do
38
+ @payment_method.stub(:type){ 4 }
39
+ @payment_method.should be_pag_seguro_balance
40
+ end
41
+
42
+ it "should be oi paggo if type is 5" do
43
+ @payment_method.stub(:type){ 5 }
44
+ @payment_method.should be_oi_paggo
45
+ end
46
+ end
47
+
48
+ describe "codes" do
49
+ def should_have_meaning_for_code(meaning, code)
50
+ PagSeguro::PaymentMethod.new(code: code).name.should == meaning
51
+ end
52
+
53
+ it { should_have_meaning_for_code("Cartão de crédito Visa", 101) }
54
+ it { should_have_meaning_for_code("Cartão de crédito MasterCard", 102) }
55
+ it { should_have_meaning_for_code("Cartão de crédito American Express", 103) }
56
+ it { should_have_meaning_for_code("Cartão de crédito Diners", 104) }
57
+ it { should_have_meaning_for_code("Cartão de crédito Hipercard", 105) }
58
+ it { should_have_meaning_for_code("Cartão de crédito Aura", 106) }
59
+ it { should_have_meaning_for_code("Cartão de crédito Elo", 107) }
60
+ it { should_have_meaning_for_code("Cartão de crédito PLENOCard", 108) }
61
+ it { should_have_meaning_for_code("Cartão de crédito PersonalCard", 109) }
62
+ it { should_have_meaning_for_code("Boleto Bradesco", 201) }
63
+ it { should_have_meaning_for_code("Boleto Santander", 202) }
64
+ it { should_have_meaning_for_code("Débito online Bradesco", 301) }
65
+ it { should_have_meaning_for_code("Débito online Itaú", 302) }
66
+ it { should_have_meaning_for_code("Débito online Unibanco", 303) }
67
+ it { should_have_meaning_for_code("Débito online Banco do Brasil", 304) }
68
+ it { should_have_meaning_for_code("Débito online Banco Real", 305) }
69
+ it { should_have_meaning_for_code("Débito online Banrisul", 306) }
70
+ it { should_have_meaning_for_code("Débito online HSBC", 307) }
71
+ it { should_have_meaning_for_code("Saldo PagSeguro", 401) }
72
+ it { should_have_meaning_for_code("Oi Paggo", 501) }
73
+ it { should_have_meaning_for_code("Desconhecido", 0) }
74
+ end
75
+ end
76
+ end
@@ -107,29 +107,37 @@ describe PagSeguro::Payment do
107
107
  describe "parse_checkout_response" do
108
108
  before do
109
109
  @payment = PagSeguro::Payment.new("mymail", "mytoken")
110
- @response = double("response")
111
- @payment.stub(:send_checkout){ @response }
110
+ end
111
+
112
+ it "should not raise errors if response code is 200" do
113
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 200 }
114
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ "some body info" }
115
+ lambda { @payment.send(:parse_checkout_response) }.should_not raise_error
112
116
  end
113
117
 
114
118
  it "should raise PagSeguro::Errors::InvalidData if response code is 400" do
115
- @response.stub(code: 400, body: "some error description")
119
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 400 }
120
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ "some error description" }
116
121
  lambda { @payment.send(:parse_checkout_response) }.should raise_error(PagSeguro::Errors::InvalidData)
117
122
  end
118
123
 
119
124
  it "should raise PagSeguro::Errors::Unauthorized if response code is 400" do
120
- @response.stub(code: 401)
125
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 401 }
121
126
  lambda { @payment.send(:parse_checkout_response) }.should raise_error(PagSeguro::Errors::Unauthorized)
122
127
  end
123
-
124
- it "should not raise errors if response code is 200" do
125
- @response.stub(code: 200, body: "some response body")
126
- lambda { @payment.send(:parse_checkout_response) }.should_not raise_error
127
- end
128
-
128
+
129
129
  it "should raise PagSeguro::Errors::UnknownError if response code is not 200, 400 or 401" do
130
- @response.stub(code: 300, body: "some response body")
130
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 300 }
131
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ "some response body" }
131
132
  lambda { @payment.send(:parse_checkout_response) }.should raise_error(PagSeguro::Errors::UnknownError)
132
133
  end
134
+
135
+ it "should be able to reset response" do
136
+ @payment.response = "something"
137
+ @payment.response.should be_present
138
+ @payment.reset!
139
+ @payment.response.should be_nil
140
+ end
133
141
  end
134
142
 
135
143
  end
@@ -23,6 +23,23 @@ describe PagSeguro::Shipping do
23
23
  it { @shipping.should have_attribute_accessor(:street) }
24
24
  it { @shipping.should have_attribute_accessor(:number) }
25
25
  it { @shipping.should have_attribute_accessor(:complement) }
26
+
27
+ describe "types" do
28
+ it "should be pac if type is 1" do
29
+ @shipping.stub( :type ){ 1 }
30
+ @shipping.should be_pac
31
+ end
32
+
33
+ it "should be sedex if type is 2" do
34
+ @shipping.stub( :type ){ 2 }
35
+ @shipping.should be_sedex
36
+ end
37
+
38
+ it "should be unidentified if type is 3" do
39
+ @shipping.stub( :type ){ 3 }
40
+ @shipping.should be_unidentified
41
+ end
42
+ end
26
43
  end
27
44
 
28
45
  it "should be able to initialize all attributes" do
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,11 @@ end
6
6
  require 'yaml'
7
7
  require File.dirname(__FILE__) + "/../lib/pag_seguro"
8
8
 
9
+ config = YAML.load_file(File.dirname(__FILE__) + "/pag_seguro/integration/config.yml")
10
+ EMAIL = config["email"]
11
+ TOKEN = config["token"]
12
+ NOTIFICATION_CODE = config["notification_code"]
13
+
9
14
  class HaveAttributeAccessor
10
15
  def initialize(attribute)
11
16
  @attribute = attribute
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.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &2168863380 !ruby/object:Gem::Requirement
16
+ requirement: &2152303520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2168863380
24
+ version_requirements: *2152303520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: haml
27
- requirement: &2168862900 !ruby/object:Gem::Requirement
27
+ requirement: &2152302620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2168862900
35
+ version_requirements: *2152302620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &2168862140 !ruby/object:Gem::Requirement
38
+ requirement: &2152302080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2168862140
46
+ version_requirements: *2152302080
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rest-client
49
- requirement: &2168861420 !ruby/object:Gem::Requirement
49
+ requirement: &2152300820 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.6.7
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2168861420
57
+ version_requirements: *2152300820
58
58
  description:
59
59
  email:
60
60
  - stefano.diem@gmail.com
@@ -76,19 +76,22 @@ files:
76
76
  - lib/pag_seguro/item.rb
77
77
  - lib/pag_seguro/notification.rb
78
78
  - lib/pag_seguro/payment.rb
79
+ - lib/pag_seguro/payment_method.rb
79
80
  - lib/pag_seguro/sender.rb
80
81
  - lib/pag_seguro/shipping.rb
81
82
  - lib/pag_seguro/version.rb
82
83
  - pag_seguro.gemspec
84
+ - spec/fixtures/notification.xml
83
85
  - spec/pag_seguro/checkout_xml_spec.rb
84
86
  - spec/pag_seguro/errors/invalid_data_spec.rb
85
87
  - spec/pag_seguro/errors/unauthorized_spec.rb
86
88
  - spec/pag_seguro/errors/unknown_error_spec.rb
87
89
  - spec/pag_seguro/integration/checkout_spec.rb
88
90
  - spec/pag_seguro/integration/config.yml
91
+ - spec/pag_seguro/integration/notification_spec.rb
89
92
  - spec/pag_seguro/item_spec.rb
90
- - spec/pag_seguro/notification_data_mock.rb
91
93
  - spec/pag_seguro/notification_spec.rb
94
+ - spec/pag_seguro/payment_method_spec.rb
92
95
  - spec/pag_seguro/payment_spec.rb
93
96
  - spec/pag_seguro/sender_spec.rb
94
97
  - spec/pag_seguro/shipping_spec.rb
@@ -119,15 +122,17 @@ signing_key:
119
122
  specification_version: 3
120
123
  summary: A ruby gem to handle PagSeguro's API version 2
121
124
  test_files:
125
+ - spec/fixtures/notification.xml
122
126
  - spec/pag_seguro/checkout_xml_spec.rb
123
127
  - spec/pag_seguro/errors/invalid_data_spec.rb
124
128
  - spec/pag_seguro/errors/unauthorized_spec.rb
125
129
  - spec/pag_seguro/errors/unknown_error_spec.rb
126
130
  - spec/pag_seguro/integration/checkout_spec.rb
127
131
  - spec/pag_seguro/integration/config.yml
132
+ - spec/pag_seguro/integration/notification_spec.rb
128
133
  - spec/pag_seguro/item_spec.rb
129
- - spec/pag_seguro/notification_data_mock.rb
130
134
  - spec/pag_seguro/notification_spec.rb
135
+ - spec/pag_seguro/payment_method_spec.rb
131
136
  - spec/pag_seguro/payment_spec.rb
132
137
  - spec/pag_seguro/sender_spec.rb
133
138
  - spec/pag_seguro/shipping_spec.rb
@@ -1,64 +0,0 @@
1
- # encoding: utf-8
2
- module PagSeguro
3
- class Notification
4
- def notification_data(email, token, notification_code)
5
- <<-XML
6
- <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
7
- <transaction>
8
- <date>2011-02-10T16:13:41.000-03:00</date>
9
- <code>9E884542-81B3-4419-9A75-BCC6FB495EF1</code>
10
- <reference>REF1234</reference>
11
- <type>1</type>
12
- <status>3</status>
13
- <paymentMethod>
14
- <type>1</type>
15
- <code>101</code>
16
- </paymentMethod>
17
- <grossAmount>49900.00</grossAmount>
18
- <discountAmount>0.00</discountAmount>
19
- <feeAmount>0.00</feeAmount>
20
- <netAmount>49900.00</netAmount>
21
- <extraAmount>0.00</extraAmount>
22
- <installmentCount>1</installmentCount>
23
- <itemCount>2</itemCount>
24
- <items>
25
- <item>
26
- <id>0001</id>
27
- <description>Notebook Prata</description>
28
- <quantity>1</quantity>
29
- <amount>24300.00</amount>
30
- </item>
31
- <item>
32
- <id>0002</id>
33
- <description>Notebook Rosa</description>
34
- <quantity>1</quantity>
35
- <amount>25600.00</amount>
36
- </item>
37
- </items>
38
- <sender>
39
- <name>José Comprador</name>
40
- <email>comprador@uol.com.br</email>
41
- <phone>
42
- <areaCode>11</areaCode>
43
- <number>56273440</number>
44
- </phone>
45
- </sender>
46
- <shipping>
47
- <address>
48
- <street>Av. Brig. Faria Lima</street>
49
- <number>1384</number>
50
- <complement>5o andar</complement>
51
- <district>Jardim Paulistano</district>
52
- <postalCode>01452002</postalCode>
53
- <city>Sao Paulo</city>
54
- <state>SP</state>
55
- <country>BRA</country>
56
- </address>
57
- <type>1</type>
58
- <cost>21.50</cost>
59
- </shipping>
60
- </transaction>
61
- XML
62
- end
63
- end
64
- end