pag_seguro 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  module PagSeguro
2
2
  module Errors
3
- class UnknownError
3
+ class UnknownError < Exception
4
4
  def initialize(response)
5
- super("Unknown response code (#{response.code}):\n#{reponse.body}")
5
+ super("Unknown response code (#{response.code}):\n#{response.body}")
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module PagSeguro
2
2
  class Payment
3
3
  include ActiveModel::Validations
4
4
 
5
- attr_accessor :id, :email, :token, :items, :sender, :shipping, :extra_amount, :redirect_url, :max_uses, :max_age
5
+ attr_accessor :id, :email, :token, :items, :sender, :shipping, :extra_amount, :redirect_url, :max_uses, :max_age, :response
6
6
 
7
7
  validates_presence_of :email, :token
8
8
  validates_format_of :extra_amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot", allow_blank: true
@@ -36,16 +36,17 @@ module PagSeguro
36
36
  end
37
37
 
38
38
  def code
39
- response = send_checkout
40
- if response.code == 200
41
- Nokogiri::XML(response.body).css("checkout code").first.content
42
- elsif response.code == 401
43
- raise Errors::Unauthorized
44
- elsif response.code == 400
45
- raise Errors::InvalidData.new(response.body)
46
- else
47
- raise Errors::UnknownError.new(response)
48
- end
39
+ @response ||= parse_checkout_response
40
+ parse_code
41
+ end
42
+
43
+ def date
44
+ @response ||= parse_checkout_response
45
+ parse_date
46
+ end
47
+
48
+ def reset!
49
+ @response = nil
49
50
  end
50
51
 
51
52
  protected
@@ -60,5 +61,26 @@ module PagSeguro
60
61
  def send_checkout
61
62
  RestClient.post(checkout_url_with_params, checkout_xml, content_type: "application/xml"){|response, request, result| response }
62
63
  end
64
+
65
+ def parse_checkout_response
66
+ response = send_checkout
67
+ if response.code == 200
68
+ response.body
69
+ elsif response.code == 401
70
+ raise Errors::Unauthorized
71
+ elsif response.code == 400
72
+ raise Errors::InvalidData.new(response.body)
73
+ else
74
+ raise Errors::UnknownError.new(response)
75
+ end
76
+ end
77
+
78
+ def parse_date
79
+ Nokogiri::XML(@response.body).css("checkout date").first.content
80
+ end
81
+
82
+ def parse_code
83
+ Nokogiri::XML(@response.body).css("checkout code").first.content
84
+ end
63
85
  end
64
86
  end
@@ -1,3 +1,3 @@
1
1
  module PagSeguro
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ def invalid_data_xml
4
+ <<XML
5
+ <errors>
6
+ <error>
7
+ <code>404</code>
8
+ <message>Not Found</message>
9
+ </error>
10
+ <error>
11
+ <code>422</code>
12
+ <message>Unauthorized</message>
13
+ </error>
14
+ </errors>
15
+ XML
16
+ end
17
+
18
+ describe PagSeguro::Errors::InvalidData do
19
+ it "should be able to parse an error xml and raise the error codes" do
20
+ lambda { raise PagSeguro::Errors::InvalidData.new(invalid_data_xml) }.should raise_error(PagSeguro::Errors::InvalidData, "404: Not Found\n422: Unauthorized\n")
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Errors::Unauthorized do
4
+ it "should be able to raise an unauthorized error" do
5
+ lambda { raise PagSeguro::Errors::Unauthorized.new }.should raise_error(PagSeguro::Errors::Unauthorized, "Credentials provided (e-mail and token) failed to authenticate")
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ class MockResponse
4
+ def code
5
+ 10000
6
+ end
7
+
8
+ def body
9
+ " error description"
10
+ end
11
+ end
12
+
13
+ describe PagSeguro::Errors::UnknownError do
14
+ it "should be able to raise an unknown error" do
15
+ lambda { raise PagSeguro::Errors::UnknownError.new(MockResponse.new) }.should raise_error(PagSeguro::Errors::UnknownError, "Unknown response code (10000):\n error description")
16
+ end
17
+ end
@@ -23,7 +23,7 @@ describe "PagSeguro::Payment.code" do
23
23
  payment = create_valid_payment
24
24
  payment.code.size.should == 32
25
25
  end
26
-
26
+
27
27
  it "should tell me when the email and token are invalid" do
28
28
  payment = PagSeguro::Payment.new("not_a_user@not_an_email.com", "NOTATOKEN7F048A09A8AEFDD1E5A7B91")
29
29
  lambda { payment.code }.should raise_error(PagSeguro::Errors::Unauthorized)
@@ -38,4 +38,36 @@ describe "PagSeguro::Payment.code" do
38
38
  payment = create_valid_payment
39
39
  RestClient.get(payment.checkout_payment_url).code.should == 200
40
40
  end
41
+ end
42
+
43
+ describe "PagSeguro::Payment.date" do
44
+ it "should send a request to pagseguro" do
45
+ payment = create_valid_payment
46
+ payment.date.should match(/^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[-+]\d{2}:\d{2}$/)
47
+ end
48
+ end
49
+
50
+ describe "PagSeguro::Payment parse_checkout_response" do
51
+ before do
52
+ @payment = create_valid_payment
53
+ @payment.stub(:parse_code)
54
+ @payment.stub(:parse_date)
55
+ @payment.stub(:parse_checkout_response){ "some response" }
56
+ end
57
+
58
+ it "should not make a request to pagseguro more than once" do
59
+ @payment.should_receive(:parse_checkout_response).exactly(1).times
60
+
61
+ @payment.code
62
+ @payment.code
63
+ @payment.date
64
+ end
65
+
66
+ it "should be make more than one request to pag seguro if the payment is reset" do
67
+ @payment.should_receive(:parse_checkout_response).exactly(2).times
68
+
69
+ @payment.code
70
+ @payment.reset!
71
+ @payment.date
72
+ end
41
73
  end
@@ -55,4 +55,14 @@ describe PagSeguro::Notification do
55
55
  @items[1].quantity.should == "1"
56
56
  @items[1].amount.should == "25600.00"
57
57
  end
58
+
59
+ it "should be_available if its status is 4" do
60
+ @notification.stub(:status){ 4 }
61
+ @notification.should be_available
62
+ end
63
+
64
+ it "should be_approved if its status is 3" do
65
+ @notification.stub(:status){ 3 }
66
+ @notification.should be_approved
67
+ end
58
68
  end
@@ -15,6 +15,15 @@ describe PagSeguro::Payment do
15
15
  it { @payment.should have_attribute_accessor(:redirect_url) }
16
16
  it { @payment.should have_attribute_accessor(:max_uses) }
17
17
  it { @payment.should have_attribute_accessor(:max_age) }
18
+ it { @payment.should have_attribute_accessor(:response) }
19
+
20
+ it "should respond to :code" do
21
+ @payment.respond_to?(:code).should be_true
22
+ end
23
+
24
+ it "should respond to :date" do
25
+ @payment.respond_to?(:date).should be_true
26
+ end
18
27
 
19
28
  it "should have items" do
20
29
  @payment.items.should be_instance_of(Array)
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.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000 Z
12
+ date: 2012-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &70199441235740 !ruby/object:Gem::Requirement
16
+ requirement: &2152283540 !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: *70199441235740
24
+ version_requirements: *2152283540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: haml
27
- requirement: &70199441234400 !ruby/object:Gem::Requirement
27
+ requirement: &2152283060 !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: *70199441234400
35
+ version_requirements: *2152283060
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &70199441233240 !ruby/object:Gem::Requirement
38
+ requirement: &2152282300 !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: *70199441233240
46
+ version_requirements: *2152282300
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rest-client
49
- requirement: &70199441231700 !ruby/object:Gem::Requirement
49
+ requirement: &2152281580 !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: *70199441231700
57
+ version_requirements: *2152281580
58
58
  description:
59
59
  email:
60
60
  - stefano.diem@gmail.com
@@ -81,6 +81,9 @@ files:
81
81
  - lib/pag_seguro/version.rb
82
82
  - pag_seguro.gemspec
83
83
  - spec/pag_seguro/checkout_xml_spec.rb
84
+ - spec/pag_seguro/errors/invalid_data_spec.rb
85
+ - spec/pag_seguro/errors/unauthorized_spec.rb
86
+ - spec/pag_seguro/errors/unknown_error_spec.rb
84
87
  - spec/pag_seguro/integration/checkout_spec.rb
85
88
  - spec/pag_seguro/integration/config.yml
86
89
  - spec/pag_seguro/item_spec.rb
@@ -117,6 +120,9 @@ specification_version: 3
117
120
  summary: A ruby gem to handle PagSeguro's API version 2
118
121
  test_files:
119
122
  - spec/pag_seguro/checkout_xml_spec.rb
123
+ - spec/pag_seguro/errors/invalid_data_spec.rb
124
+ - spec/pag_seguro/errors/unauthorized_spec.rb
125
+ - spec/pag_seguro/errors/unknown_error_spec.rb
120
126
  - spec/pag_seguro/integration/checkout_spec.rb
121
127
  - spec/pag_seguro/integration/config.yml
122
128
  - spec/pag_seguro/item_spec.rb