Class: ActiveMerchant::Billing::Integrations::PagSeguro::Helper
- Inherits:
-
Helper
- Object
- Helper
- ActiveMerchant::Billing::Integrations::PagSeguro::Helper
show all
- Defined in:
- lib/active_merchant/billing/integrations/pag_seguro/helper.rb
Instance Attribute Summary
Attributes inherited from Helper
#fields
Instance Method Summary
collapse
Methods inherited from Helper
#add_field, #add_fields, #add_raw_html_field, #billing_address, #form_method, inherited, mapping, #raw_html_fields, #shipping_address, #test?
Constructor Details
#initialize(order_id, account, options) ⇒ Helper
Returns a new instance of Helper.
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 9
def initialize(order_id, account, options)
super
@account = account
add_field('itemAmount1', sprintf("%0.02f", options[:amount]))
add_field('itemId1', '1')
add_field('itemQuantity1', '1')
add_field('shippingType', '3')
add_field('currency', 'BRL')
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class ActiveMerchant::Billing::Integrations::Helper
Instance Method Details
#area_code_and_number(phone) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 74
def area_code_and_number(phone)
phone.gsub!(/[^\d]/, '')
ddd = phone.slice(0..1)
number = phone.slice(2..12)
[ddd, number]
end
|
#check_for_errors(response, xml) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 83
def check_for_errors(response, xml)
return if response.code == "200"
case response.code
when "400"
raise StandardError, humanize_errors(xml)
when "401"
raise StandardError, "Token do PagSeguro inválido."
else
raise ActiveMerchant::ResponseError, response
end
end
|
#customer(params = {}) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 46
def customer(params = {})
phone = area_code_and_number(params[:phone])
add_field("senderAreaCode", phone[0])
add_field("senderPhone", phone[1])
add_field("senderEmail", params[:email])
add_field('senderName', "#{params[:first_name]} #{params[:last_name]}")
end
|
96
97
98
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 96
def (xml)
xml.css("code").text
end
|
#fetch_token ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 55
def fetch_token
uri = URI.parse(PagSeguro.invoicing_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.content_type = "application/x-www-form-urlencoded"
request.set_form_data @fields
response = http.request(request)
xml = Nokogiri::XML.parse(response.body)
check_for_errors(response, xml)
(xml)
rescue Timeout::Error => e
raise StandardError, "Erro ao se conectar ao PagSeguro."
end
|
36
37
38
39
40
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 36
def form_fields
invoice_id = fetch_token
{"code" => invoice_id}
end
|
#humanize_errors(xml) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 100
def humanize_errors(xml)
xml.css("errors").children.map do |error|
case error.css('code').text
when "11013"
"Código de área inválido"
when "11014"
"Número de telefone inválido. Formato esperado: (DD) XXXX-XXXX"
when "11017"
"Código postal (CEP) inválido."
else
error.css('message').text
end
end.join(", ")
end
|
#shipping(value) ⇒ Object
42
43
44
|
# File 'lib/active_merchant/billing/integrations/pag_seguro/helper.rb', line 42
def shipping(value)
add_field("shippingCost", sprintf("%0.02f", value))
end
|