Class: OffsitePayments::Integrations::PagSeguro::Helper

Inherits:
Helper
  • Object
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, #test?

Constructor Details

#initialize(order_id, account, options) ⇒ 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, , options)
  super
  @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


118
119
120
121
122
123
124
125
126
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 118

def area_code_and_number(phone)
  return if phone.nil?
  phone.gsub!(/[^\d]/, '')

  ddd    = phone.slice(0..1)
  number = phone.slice(2..12)

  [ddd, number]
end

#check_for_errors(response, xml) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 128

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 ActiveUtils::ResponseError, response
  end
end

#customer(params = {}) ⇒ Object


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 87

def customer(params = {})
  full_name = remove_excessive_whitespace("#{params[:first_name]} #{params[:last_name]}")

  if phone = area_code_and_number(params[:phone])
    add_field("senderAreaCode", phone[0])
    add_field("senderPhone", phone[1])
  end

  add_field("senderEmail", params[:email])
  add_field('senderName', full_name)
end

#extract_token(xml) ⇒ Object


141
142
143
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 141

def extract_token(xml)
  xml.css("code").text
end

#fetch_tokenObject


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 99

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)

  extract_token(xml)
rescue Timeout::Error, Errno::ECONNRESET, Errno::ETIMEDOUT => e
  raise ActionViewHelperError, "Erro ao conectar-se ao PagSeguro. Por favor, tente novamente."
end

#form_fieldsObject


77
78
79
80
81
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 77

def form_fields
  invoice_id = fetch_token

  {"code" => invoice_id}
end

#humanize_errors(xml) ⇒ Object


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 145

def humanize_errors(xml)
  # reference: https://pagseguro.uol.com.br/v2/guia-de-integracao/codigos-de-erro.html

  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


162
163
164
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 162

def remove_excessive_whitespace(text)
  text.gsub(/\s{2,}/, ' ').strip
end

#shipping(value) ⇒ Object


83
84
85
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 83

def shipping(value)
  add_field("shippingCost", sprintf("%0.02f", value))
end

#shipping_address(params = {}) ⇒ Object


69
70
71
72
73
74
75
# File 'lib/offsite_payments/integrations/pag_seguro.rb', line 69

def shipping_address(params = {})
  add_field('shippingAddressCity',       params[:city].slice(0, 60))     if params[:city]
  add_field('shippingAddressStreet',     params[:address1].slice(0, 80)) if params[:address1]
  add_field('shippingAddressComplement', params[:address2].slice(0, 40)) if params[:address2]
  add_field('shippingAddressState',      params[:state])
  add_field('shippingAddressPostalCode', params[:zip].delete("^0-9").slice(0, 8)) if params[:zip]
end