lib/pagseguro_client/order.rb in pagseguro_client-0.1.0 vs lib/pagseguro_client/order.rb in pagseguro_client-0.1.1
- old
+ new
@@ -25,28 +25,55 @@
reference: id
}
products.each_with_index do |item, index|
index += 1
data["itemId#{index}"] = item[:id]
- data["itemDescription#{index}"] = item[:description]
+ data["itemDescription#{index}"] = item[:description].to_s.unpack("U*").pack("C*")
data["itemAmount#{index}"] = item[:amount]
data["itemQuantity#{index}"] = item[:quantity] || 1
end
data
end
+ def parse_response(xml)
+ doc = Nokogiri::XML(xml)
+
+ # Verify if this is an error
+ unless doc.css("error").empty?
+ code = doc.xpath("//code").text
+ message = doc.xpath("//message").text
+ PagseguroError.new(code, message)
+
+ else
+ code = doc.xpath("//code").text
+ {
+ code: code,
+ url: PagseguroClient.payment_url(code)
+ }
+ end
+ end
+
# Send a new payment request to Pagseguro
# Returns the URL to redirect your user to complete the payment in Pagseguro
def send_request
- response = RestClient.post("#{PagseguroClient.base_url}/v2/checkout", data)
+ begin
+ response = RestClient.post(PagseguroClient.checkout_url, data, {
+ :content_type => "application/x-www-form-urlencoded",
+ :charset => "UTF-8"
+ })
- doc = Nokogiri::XML(response.body)
- code = doc.xpath("//code").text
+ parse_response(response.body)
- {
- code: code,
- url: PagseguroClient.payment_url(code)
- }
+ rescue => e
+ raise parse_response(e.response)
+ end
+ end
+ end
+
+ class PagseguroError < StandardError
+ attr_reader :code, :message
+ def initialize(code, message)
+ @code, @message = code, message
end
end
end