Class: PagseguroClient::Order
- Inherits:
-
Object
- Object
- PagseguroClient::Order
- Defined in:
- lib/pagseguro_client/order.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#products ⇒ Object
Returns the value of attribute products.
-
#redirect_url ⇒ Object
Returns the value of attribute redirect_url.
Instance Method Summary collapse
-
#add(options) ⇒ Object
The allowed values are: - id (Required. Should match the product in your database) - description (Required. Identifies the product) - amount (Required. If float, will be multiplied by 100 cents) - quantity (Optional. If not supplied, use 1).
- #data ⇒ Object
-
#initialize(order_id, redirect_url = nil) ⇒ Order
constructor
A new instance of Order.
- #parse_response(xml) ⇒ Object
-
#send_request ⇒ Object
Send a new payment request to Pagseguro Returns the URL to redirect your user to complete the payment in Pagseguro.
Constructor Details
#initialize(order_id, redirect_url = nil) ⇒ Order
Returns a new instance of Order.
6 7 8 9 10 |
# File 'lib/pagseguro_client/order.rb', line 6 def initialize(order_id, redirect_url = nil) self.id = order_id self.redirect_url = redirect_url self.products = [] end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/pagseguro_client/order.rb', line 4 def id @id end |
#products ⇒ Object
Returns the value of attribute products.
4 5 6 |
# File 'lib/pagseguro_client/order.rb', line 4 def products @products end |
#redirect_url ⇒ Object
Returns the value of attribute redirect_url.
4 5 6 |
# File 'lib/pagseguro_client/order.rb', line 4 def redirect_url @redirect_url end |
Instance Method Details
#add(options) ⇒ Object
The allowed values are:
-
id (Required. Should match the product in your database)
-
description (Required. Identifies the product)
-
amount (Required. If float, will be multiplied by 100 cents)
-
quantity (Optional. If not supplied, use 1)
17 18 19 |
# File 'lib/pagseguro_client/order.rb', line 17 def add() products.push() end |
#data ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pagseguro_client/order.rb', line 21 def data data = { email: PagseguroClient.email, token: PagseguroClient.token, currency: "BRL", reference: id } data["redirectURL"] = redirect_url data["redirectURL"] ||= PagseguroClient.redirect_url if PagseguroClient.redirect? products.each_with_index do |item, index| index += 1 data["itemId#{index}"] = item[:id] 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 |
#parse_response(xml) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pagseguro_client/order.rb', line 37 def parse_response(xml) doc = Nokogiri::XML(xml) # Verify if this is an error unless doc.css("error").empty? code = doc.xpath("//code").text = doc.xpath("//message").text PagseguroError.new(code, ) else code = doc.xpath("//code").text { code: code, url: PagseguroClient.payment_url(code) } end end |
#send_request ⇒ Object
Send a new payment request to Pagseguro Returns the URL to redirect your user to complete the payment in Pagseguro
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pagseguro_client/order.rb', line 57 def send_request begin response = RestClient.post(PagseguroClient.checkout_url, data, { :content_type => "application/x-www-form-urlencoded", :charset => "UTF-8" }) parse_response(response.body) rescue => e raise parse_response(e.response) end end |