Class: OffsitePayments::Integrations::PagSeguro::Helper
- Inherits:
-
Helper
- Object
- Helper
- OffsitePayments::Integrations::PagSeguro::Helper
show all
- Defined in:
- lib/offsite_payments/integrations/pag_seguro.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.
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 49
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 OffsitePayments::Helper
Instance Method Details
#area_code_and_number(phone) ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 115
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
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 124
def check_for_errors(response, xml)
return if response.code == "200"
case response.code
when "400"
raise ActionViewHelperError, humanize_errors(xml)
when "401"
raise ActionViewHelperError, "Token do PagSeguro inválido."
else
raise ActiveMerchant::ResponseError, response
end
end
|
#customer(params = {}) ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 86
def customer(params = {})
phone = area_code_and_number(params[:phone])
full_name = remove_excessive_whitespace("#{params[:first_name]} #{params[:last_name]}")
add_field("senderAreaCode", phone[0])
add_field("senderPhone", phone[1])
add_field("senderEmail", params[:email])
add_field('senderName', full_name)
end
|
137
138
139
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 137
def (xml)
xml.css("code").text
end
|
#fetch_token ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 96
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, Errno::ECONNRESET => e
raise ActionViewHelperError, "Erro ao conectar-se ao PagSeguro. Por favor, tente novamente."
end
|
76
77
78
79
80
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 76
def form_fields
invoice_id = fetch_token
{"code" => invoice_id}
end
|
#humanize_errors(xml) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 141
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
|
#remove_excessive_whitespace(text) ⇒ Object
158
159
160
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 158
def remove_excessive_whitespace(text)
text.gsub(/\s{2,}/, ' ').strip
end
|
#shipping(value) ⇒ Object
82
83
84
|
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 82
def shipping(value)
add_field("shippingCost", sprintf("%0.02f", value))
end
|