class ActiveMerchant::Billing::PagarmeGateway

Constants

STANDARD_ERROR_CODE_MAPPING

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/pagarme.rb, line 19
def initialize(options = {})
  requires!(options, :api_key)
  @api_key = options[:api_key]

  super
end

Public Instance Methods

authorize(money, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 35
def authorize(money, payment_method, options = {})
  post = {}
  add_amount(post, money)
  add_payment_method(post, payment_method)
  add_metadata(post, options)

  post[:capture] = false

  commit(:post, 'transactions', post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 46
def capture(money, authorization, options = {})
  return Response.new(false, 'Não é possível capturar uma transação sem uma prévia autorização.') if authorization.nil?

  post = {}
  commit(:post, "transactions/#{authorization}/capture", post)
end
purchase(money, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 26
def purchase(money, payment_method, options = {})
  post = {}
  add_amount(post, money)
  add_payment_method(post, payment_method)
  add_metadata(post, options)

  commit(:post, 'transactions', post)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 53
def refund(money, authorization, options = {})
  return Response.new(false, 'Não é possível estornar uma transação sem uma prévia captura.') if authorization.nil?

  void(authorization, options)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 77
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r((card_number=)\d+), '\1[FILTERED]').
    gsub(%r((card_cvv=)\d+), '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 73
def supports_scrubbing?
  true
end
verify(payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 66
def verify(payment_method, options = {})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(127, payment_method, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 59
def void(authorization, options = {})
  return Response.new(false, 'Não é possível estornar uma transação autorizada sem uma prévia autorização.') if authorization.nil?

  post = {}
  commit(:post, "transactions/#{authorization}/refund", post)
end

Private Instance Methods

add_amount(post, money) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 86
def add_amount(post, money)
  post[:amount] = amount(money)
end
add_credit_card(post, credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 95
def add_credit_card(post, credit_card)
  post[:card_number] = credit_card.number
  post[:card_holder_name] = credit_card.name
  post[:card_expiration_date] = "#{credit_card.month}/#{credit_card.year}"
  post[:card_cvv] = credit_card.verification_value
end
add_metadata(post, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 102
def add_metadata(post, options = {})
  post[:metadata] = {}
  post[:metadata][:order_id] = options[:order_id]
  post[:metadata][:ip] = options[:ip]
  post[:metadata][:customer] = options[:customer]
  post[:metadata][:invoice] = options[:invoice]
  post[:metadata][:merchant] = options[:merchant]
  post[:metadata][:description] = options[:description]
  post[:metadata][:email] = options[:email]
end
add_payment_method(post, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 90
def add_payment_method(post, payment_method)
  post[:payment_method] = 'credit_card'
  add_credit_card(post, payment_method)
end
api_request(method, endpoint, parameters = nil, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 145
def api_request(method, endpoint, parameters = nil, options = {})
  raw_response = response = nil
  begin
    raw_response = ssl_request(method, self.live_url + endpoint, post_data(parameters), headers(options))
    response = parse(raw_response)
  rescue ResponseError => e
    raw_response = e.response.body
    response = response_error(raw_response)
  rescue JSON::ParserError
    response = json_error(raw_response)
  end
  response
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 222
def authorization_from(response)
  response['id'] if success_from(response)
end
commit(method, url, parameters, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 159
def commit(method, url, parameters, options = {})
  response = api_request(method, url, parameters, options)

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(response),
    test: test?,
    error_code: error_code_from(response)
  )
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 230
def error_code_from(response)
  if failure_from(response)
    STANDARD_ERROR_CODE_MAPPING['refused']
  elsif response.key?('errors')
    STANDARD_ERROR_CODE_MAPPING['processing_error']
  end
end
failure_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 196
def failure_from(response)
  response.key?('status') && response['status'] == 'refused'
end
headers(options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 137
def headers(options = {})
  {
    'Authorization' => 'Basic ' + Base64.encode64(@api_key.to_s + ':x').strip,
    'User-Agent' => "Pagar.me/1 ActiveMerchant/#{ActiveMerchant::VERSION}",
    'Accept-Encoding' => 'deflate'
  }
end
json_error(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 178
def json_error(raw_response)
  msg = 'Resposta inválida retornada pela API do Pagar.me. Por favor entre em contato com suporte@pagar.me se você continuar recebendo essa mensagem.'
  msg += "  (A resposta retornada pela API foi #{raw_response.inspect})"
  {
    'errors' => [{
      'message' => msg
    }]
  }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 200
def message_from(response)
  if success_from(response)
    case response['status']
    when 'paid'
      'Transação aprovada'
    when 'authorized'
      'Transação autorizada'
    when 'refunded'
      'Transação estornada'
    else
      "Transação com status '#{response['status']}'"
    end
  elsif failure_from(response)
    'Transação recusada'
  elsif response.key?('errors')
    response['errors'][0]['message']
  else
    msg = json_error(response)
    msg['errors'][0]['message']
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 113
def parse(body)
  JSON.parse(body)
end
post_data(params) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 117
def post_data(params)
  return nil unless params

  params.map do |key, value|
    next if value != false && value.blank?

    if value.is_a?(Hash)
      h = {}
      value.each do |k, v|
        h["#{key}[#{k}]"] = v unless v.blank?
      end
      post_data(h)
    elsif value.is_a?(Array)
      value.map { |v| "#{key}[]=#{CGI.escape(v.to_s)}" }.join('&')
    else
      "#{key}=#{CGI.escape(value.to_s)}"
    end
  end.compact.join('&')
end
response_error(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 172
def response_error(raw_response)
  parse(raw_response)
rescue JSON::ParserError
  json_error(raw_response)
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 188
def success_from(response)
  success_purchase = response.key?('status') && response['status'] == 'paid'
  success_authorize = response.key?('status') && response['status'] == 'authorized'
  success_refund = response.key?('status') && response['status'] == 'refunded'

  success_purchase || success_authorize || success_refund
end
test?() click to toggle source
# File lib/active_merchant/billing/gateways/pagarme.rb, line 226
def test?
  @api_key.start_with?('ak_test')
end