pagseguro_client 0.1.0 → 0.1.1
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.markdown +9 -8
- data/lib/pagseguro_client/base.rb +12 -4
- data/lib/pagseguro_client/order.rb +35 -8
- data/lib/pagseguro_client/version.rb +1 -1
- data/spec/pagseguro_client/base_spec.rb +2 -2
- data/spec/pagseguro_client/order_spec.rb +27 -0
- data/spec/support/config/pagseguro.yml +2 -1
- metadata +1 -1
data/README.markdown
CHANGED
@@ -10,17 +10,18 @@ Este é um plugin do Ruby on Rails que permite utilizar o [PagSeguro](https://pa
|
|
10
10
|
|
11
11
|
### Instalação
|
12
12
|
|
13
|
-
Adicione a biblioteca ao arquivo Gemfile:
|
13
|
+
Adicione a biblioteca ao arquivo Gemfile:
|
14
14
|
|
15
15
|
```ruby
|
16
16
|
gem 'pagseguro_client', git: "git://github.com/matheustardivo/pagseguro_client.git"
|
17
17
|
```
|
18
18
|
|
19
|
-
E crie o arquivo de configuração em `config/pagseguro.yml`:
|
19
|
+
E crie o arquivo de configuração em `config/pagseguro.yml`:
|
20
20
|
|
21
21
|
```yaml
|
22
22
|
development: &development
|
23
|
-
|
23
|
+
ws_url: "http://localhost:4000"
|
24
|
+
ps_url: "http://localhost:4000"
|
24
25
|
email: matheustardivo@gmail.com
|
25
26
|
token: "a1951ac04115012fabb660334b97cc6e"
|
26
27
|
|
@@ -28,7 +29,8 @@ test:
|
|
28
29
|
<<: *development
|
29
30
|
|
30
31
|
production:
|
31
|
-
|
32
|
+
ws_url: "https://ws.pagseguro.uol.com.br"
|
33
|
+
ps_url: "https://pagseguro.uol.com.br"
|
32
34
|
email: matheustardivo@gmail.com
|
33
35
|
token: "tokenGeradoPeloPagseguro"
|
34
36
|
```
|
@@ -41,7 +43,7 @@ Para realizar os seus testes você pode usar um servidor de testes do Pagseguro
|
|
41
43
|
@order = PagseguroClient::Order.new(id) # Seu identificador da ordem de pagamento
|
42
44
|
@order.add(
|
43
45
|
id: "1", # Seu identificador do produto
|
44
|
-
description: produto.descricao,
|
46
|
+
description: produto.descricao,
|
45
47
|
amount: produto.preco)
|
46
48
|
@response = order.send_request
|
47
49
|
```
|
@@ -63,10 +65,10 @@ Agora basta usar a url retornada para enviar o usuário para efetuar o pagamento
|
|
63
65
|
# No seu controller
|
64
66
|
def notificacao
|
65
67
|
return unless request.post?
|
66
|
-
|
68
|
+
|
67
69
|
@notification = PagseguroClient::Notification.retrieve(params[:notificationCode])
|
68
70
|
# Seu código para utilizar a notificação
|
69
|
-
|
71
|
+
|
70
72
|
render nothing: true
|
71
73
|
end
|
72
74
|
```
|
@@ -75,7 +77,6 @@ O objeto `notification` possui os seguintes métodos:
|
|
75
77
|
|
76
78
|
* `PagseguroClient::Notification#code`: Código da notificação
|
77
79
|
* `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
80
|
* `PagseguroClient::Notification#status`: Status da ordem de pagamento atual
|
80
81
|
* `PagseguroClient::Notification#payment_method`: Método utilizado para o pagamento
|
81
82
|
|
@@ -23,8 +23,12 @@ module PagseguroClient
|
|
23
23
|
@@config[Rails.env]
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
config["
|
26
|
+
def ws_url
|
27
|
+
config["ws_url"]
|
28
|
+
end
|
29
|
+
|
30
|
+
def ps_url
|
31
|
+
config["ps_url"]
|
28
32
|
end
|
29
33
|
|
30
34
|
def token
|
@@ -35,12 +39,16 @@ module PagseguroClient
|
|
35
39
|
config["email"]
|
36
40
|
end
|
37
41
|
|
42
|
+
def checkout_url
|
43
|
+
"#{ws_url}/v2/checkout"
|
44
|
+
end
|
45
|
+
|
38
46
|
def payment_url(code)
|
39
|
-
"#{
|
47
|
+
"#{ps_url}/v2/checkout/payment.html?code=#{code}"
|
40
48
|
end
|
41
49
|
|
42
50
|
def notification_url(code)
|
43
|
-
"#{
|
51
|
+
"#{ws_url}/v2/transactions/notifications/#{code}"
|
44
52
|
end
|
45
53
|
|
46
54
|
class MissingEnvironmentError < StandardError; end
|
@@ -27,7 +27,7 @@ module PagseguroClient
|
|
27
27
|
products.each_with_index do |item, index|
|
28
28
|
index += 1
|
29
29
|
data["itemId#{index}"] = item[:id]
|
30
|
-
data["itemDescription#{index}"] = item[:description]
|
30
|
+
data["itemDescription#{index}"] = item[:description].to_s.unpack("U*").pack("C*")
|
31
31
|
data["itemAmount#{index}"] = item[:amount]
|
32
32
|
data["itemQuantity#{index}"] = item[:quantity] || 1
|
33
33
|
end
|
@@ -35,18 +35,45 @@ module PagseguroClient
|
|
35
35
|
data
|
36
36
|
end
|
37
37
|
|
38
|
+
def parse_response(xml)
|
39
|
+
doc = Nokogiri::XML(xml)
|
40
|
+
|
41
|
+
# Verify if this is an error
|
42
|
+
unless doc.css("error").empty?
|
43
|
+
code = doc.xpath("//code").text
|
44
|
+
message = doc.xpath("//message").text
|
45
|
+
PagseguroError.new(code, message)
|
46
|
+
|
47
|
+
else
|
48
|
+
code = doc.xpath("//code").text
|
49
|
+
{
|
50
|
+
code: code,
|
51
|
+
url: PagseguroClient.payment_url(code)
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
38
56
|
# Send a new payment request to Pagseguro
|
39
57
|
# Returns the URL to redirect your user to complete the payment in Pagseguro
|
40
58
|
def send_request
|
41
|
-
|
59
|
+
begin
|
60
|
+
response = RestClient.post(PagseguroClient.checkout_url, data, {
|
61
|
+
:content_type => "application/x-www-form-urlencoded",
|
62
|
+
:charset => "UTF-8"
|
63
|
+
})
|
42
64
|
|
43
|
-
|
44
|
-
code = doc.xpath("//code").text
|
65
|
+
parse_response(response.body)
|
45
66
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
67
|
+
rescue => e
|
68
|
+
raise parse_response(e.response)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class PagseguroError < StandardError
|
74
|
+
attr_reader :code, :message
|
75
|
+
def initialize(code, message)
|
76
|
+
@code, @message = code, message
|
50
77
|
end
|
51
78
|
end
|
52
79
|
end
|
@@ -2,10 +2,10 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe "Base" do
|
4
4
|
it "should return the payment url" do
|
5
|
-
PagseguroClient.payment_url("XPTO").should == "#{PagseguroClient.
|
5
|
+
PagseguroClient.payment_url("XPTO").should == "#{PagseguroClient.ws_url}/v2/checkout/payment.html?code=XPTO"
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should return the notification url" do
|
9
|
-
PagseguroClient.notification_url("XPTO").should == "#{PagseguroClient.
|
9
|
+
PagseguroClient.notification_url("XPTO").should == "#{PagseguroClient.ws_url}/v2/transactions/notifications/XPTO"
|
10
10
|
end
|
11
11
|
end
|
@@ -18,6 +18,19 @@ module PagseguroClient
|
|
18
18
|
order
|
19
19
|
}
|
20
20
|
|
21
|
+
it "should parse success response xml" do
|
22
|
+
xml = <<-XML
|
23
|
+
<?xml version="1.0" encoding="ISO-8859-1"?>
|
24
|
+
<checkout>
|
25
|
+
<code>8CF4BE7DCECEF0F004A6DFA0A8243412</code>
|
26
|
+
<date>2010-12-02T10:11:28.000-02:00</date>
|
27
|
+
</checkout>
|
28
|
+
XML
|
29
|
+
|
30
|
+
hash = subject.parse_response(xml)
|
31
|
+
hash[:code].should == "8CF4BE7DCECEF0F004A6DFA0A8243412"
|
32
|
+
end
|
33
|
+
|
21
34
|
it "should add product" do
|
22
35
|
subject.products.should_not be_empty
|
23
36
|
subject.products.size.should == 1
|
@@ -38,5 +51,19 @@ module PagseguroClient
|
|
38
51
|
data["itemId3"].should == "3"
|
39
52
|
end
|
40
53
|
end
|
54
|
+
|
55
|
+
context "order with invalid product amount" do
|
56
|
+
it "should parse error response xml" do
|
57
|
+
response_xml = <<-XML
|
58
|
+
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?><errors><error><code>11029</code><message>Item amount invalid pattern: 30. Must fit the patern: \\d+.\\d{2} </message></error></errors>
|
59
|
+
XML
|
60
|
+
|
61
|
+
order = Order.new("XPTO")
|
62
|
+
error = order.parse_response(response_xml)
|
63
|
+
error.kind_of?(PagseguroError).should be_true
|
64
|
+
error.code.should == "11029"
|
65
|
+
error.message.should == "Item amount invalid pattern: 30. Must fit the patern: \\d+.\\d{2} "
|
66
|
+
end
|
67
|
+
end
|
41
68
|
end
|
42
69
|
end
|