Class: PagseguroV2::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pagseguro_v2/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, token) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
# File 'lib/pagseguro_v2/client.rb', line 15

def initialize(email, token)
  super()
  self.email = email
  self.token = token
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



12
13
14
# File 'lib/pagseguro_v2/client.rb', line 12

def email
  @email
end

#tokenObject

Returns the value of attribute token.



13
14
15
# File 'lib/pagseguro_v2/client.rb', line 13

def token
  @token
end

Instance Method Details

#checkout(attributes) ⇒ Object



21
22
23
24
25
# File 'lib/pagseguro_v2/client.rb', line 21

def checkout(attributes)
  checkout = Checkout.new(attributes)
  checkout.client = self
  checkout
end

#get(path, options = {}) ⇒ Object



77
78
79
# File 'lib/pagseguro_v2/client.rb', line 77

def get(path, options = {})
  self.class.get(path, options)
end

#inquiry(attributes) ⇒ Object



33
34
35
36
37
# File 'lib/pagseguro_v2/client.rb', line 33

def inquiry(attributes)
  inquiry = Inquiry.new(attributes)
  inquiry.client = self
  inquiry
end

#inquiry_transaction(inquiry) ⇒ Object



49
50
51
52
53
# File 'lib/pagseguro_v2/client.rb', line 49

def inquiry_transaction(inquiry)
  path = PagseguroV2::Config::INQUIRY_PATH.gsub 'ID', inquiry.transaction_id
  options = { query: {email: email, token: token} }
  parse_get_response(path, options)
end

#notification(attributes) ⇒ Object



27
28
29
30
31
# File 'lib/pagseguro_v2/client.rb', line 27

def notification(attributes)
  notification = Notification.new(attributes)
  notification.client = self
  notification
end

#parse_get_response(path, options) ⇒ Object



60
61
62
63
# File 'lib/pagseguro_v2/client.rb', line 60

def parse_get_response(path, options)
  response = get(path, options)
  parse_response(response)
end

#parse_post_response(path, object) ⇒ Object



55
56
57
58
# File 'lib/pagseguro_v2/client.rb', line 55

def parse_post_response(path, object)
  response = post(path, object)
  parse_response(response)
end

#parse_response(response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pagseguro_v2/client.rb', line 65

def parse_response(response)
  if response.code == 200
    response
  elsif response.code == 401
    raise PagseguroV2::Errors::Unauthorized
  elsif response.code == 400
    raise PagseguroV2::Errors::InvalidData.new(response.body)
  else
    raise PagseguroV2::Errors::UnknownError.new(response)
  end
end

#post(path, object) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/pagseguro_v2/client.rb', line 81

def post(path, object)
  object_xml = object.to_xml
  header = {"Content-Type" => "application/xml; charset=UTF-8"}
  query = { :email => self.email, :token => self.token }
  options = {query: query, body: object_xml, headers: header}
  self.class.post(path, options)
end

#proceed_checkout(checkout) ⇒ Object



39
40
41
# File 'lib/pagseguro_v2/client.rb', line 39

def proceed_checkout(checkout)
  self.parse_post_response(PagseguroV2::Config::CHECKOUT_PATH, checkout)
end

#query_transaction(notification) ⇒ Object



43
44
45
46
47
# File 'lib/pagseguro_v2/client.rb', line 43

def query_transaction(notification)
  path = PagseguroV2::Config::NOTIFICATION_PATH.gsub 'ID', notification.code
  options = { query: {email: email, token: token} }
  parse_get_response(path, options)
end