class ActiveMerchant::Billing::MundipaggGateway

Constants

STANDARD_ERROR_CODE_MAPPING
STANDARD_ERROR_MESSAGE_MAPPING

Public Class Methods

new(options={}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 31
def initialize(options={})
  requires!(options, :api_key)
  super
end

Public Instance Methods

authorize(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 46
def authorize(money, payment, options={})
  post = {}
  add_invoice(post, money, options)
  add_customer_data(post, options) unless payment.is_a?(String)
  add_shipping_address(post, options)
  add_payment(post, payment, options)
  add_capture_flag(post, payment)
  commit('authonly', post)
end
capture(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 56
def capture(money, authorization, options={})
  post = {}
  post[:code] = authorization
  add_invoice(post, money, options)
  commit('capture', post, authorization)
end
purchase(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 36
def purchase(money, payment, options={})
  post = {}
  add_invoice(post, money, options)
  add_customer_data(post, options) unless payment.is_a?(String)
  add_shipping_address(post, options)
  add_payment(post, payment, options)

  commit('sale', post)
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 63
def refund(money, authorization, options={})
  add_invoice(post={}, money, options)
  commit('refund', post, authorization)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 91
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r(("cvv\\":\\")\d*), '\1[FILTERED]').
    gsub(%r((card\\":{\\"number\\":\\")\d*), '\1[FILTERED]')
end
store(payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 72
def store(payment, options={})
  post = {}
  options.update(name: payment.name)
  options = add_customer(options) unless options[:customer_id]
  add_payment(post, payment, options)
  commit('store', post, options[:customer_id])
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 87
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 80
def verify(credit_card, options={})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(100, credit_card, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 68
def void(authorization, options={})
  commit('void', nil, authorization)
end

Private Instance Methods

add_billing_address(post, type, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 112
def add_billing_address(post, type, options)
  if address = (options[:billing_address] || options[:address])
    billing = {}
    address = options[:billing_address] || options[:address]
    billing[:street] = address[:address1].match(/\D+/)[0].strip if address[:address1]
    billing[:number] = address[:address1].match(/\d+/)[0] if address[:address1]
    billing[:compliment] = address[:address2] if address[:address2]
    billing[:city] = address[:city] if address[:city]
    billing[:state] = address[:state] if address[:state]
    billing[:country] = address[:country] if address[:country]
    billing[:zip_code] = address[:zip] if address[:zip]
    billing[:neighborhood] = address[:neighborhood]
    post[:payment][type.to_sym][:card][:billing_address] = billing
  end
end
add_capture_flag(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 146
def add_capture_flag(post, payment)
  if voucher?(payment)
    post[:payment][:voucher][:capture] = false
  else
    post[:payment][:credit_card][:capture] = false
  end
end
add_credit_card(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 168
def add_credit_card(post, payment, options)
  post[:payment][:payment_method] = 'credit_card'
  post[:payment][:credit_card] = {}
  if payment.is_a?(String)
    post[:payment][:credit_card][:card_id] = parse_auth(payment)[1]
  else
    post[:payment][:credit_card][:card] = {}
    post[:payment][:credit_card][:card][:number] = payment.number
    post[:payment][:credit_card][:card][:holder_name] = payment.name
    post[:payment][:credit_card][:card][:exp_month] = payment.month
    post[:payment][:credit_card][:card][:exp_year] = payment.year
    post[:payment][:credit_card][:card][:cvv] = payment.verification_value
    post[:payment][:credit_card][:card][:holder_document] = options[:holder_document] if options[:holder_document]
    add_billing_address(post, 'credit_card', options)
  end
end
add_customer(options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 100
def add_customer(options)
  post = {}
  post[:name] = options[:name]
  customer = commit('customer', post)
  options.update(customer_id: customer.authorization)
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 107
def add_customer_data(post, options)
  post[:customer] = {}
  post[:customer][:email] = options[:email]
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 141
def add_invoice(post, money, options)
  post[:amount] = money
  post[:currency] = (options[:currency] || currency(money))
end
add_payment(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 154
def add_payment(post, payment, options)
  post[:customer][:name] = payment.name if post[:customer]
  post[:customer_id] = parse_auth(payment)[0] if payment.is_a?(String)
  post[:payment] = {}
  affiliation = options[:gateway_affiliation_id] || @options[:gateway_id]
  post[:payment][:gateway_affiliation_id] = affiliation if affiliation
  post[:payment][:metadata] = { mundipagg_payment_method_code: '1' } if test?
  if voucher?(payment)
    add_voucher(post, payment, options)
  else
    add_credit_card(post, payment, options)
  end
end
add_shipping_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 128
def add_shipping_address(post, options)
  if address = options[:shipping_address]
    post[:address] = {}
    post[:address][:street] = address[:address1].match(/\D+/)[0].strip if address[:address1]&.match(/\D+/)
    post[:address][:number] = address[:address1].match(/\d+/)[0] if address[:address1]&.match(/\d+/)
    post[:address][:compliment] = address[:address2] if address[:address2]
    post[:address][:city] = address[:city] if address[:city]
    post[:address][:state] = address[:state] if address[:state]
    post[:address][:country] = address[:country] if address[:country]
    post[:address][:zip_code] = address[:zip] if address[:zip]
  end
end
add_voucher(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 185
def add_voucher(post, payment, options)
  post[:currency] = 'BRL'
  post[:payment][:payment_method] = 'voucher'
  post[:payment][:voucher] = {}
  post[:payment][:voucher][:card] = {}
  post[:payment][:voucher][:card][:number] = payment.number
  post[:payment][:voucher][:card][:holder_name] = payment.name
  post[:payment][:voucher][:card][:holder_document] = options[:holder_document]
  post[:payment][:voucher][:card][:exp_month] = payment.month
  post[:payment][:voucher][:card][:exp_year] = payment.year
  post[:payment][:voucher][:card][:cvv] = payment.verification_value
  add_billing_address(post, 'voucher', options)
end
authorization_from(response, action) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 275
def authorization_from(response, action)
  return "#{response['customer']['id']}|#{response['id']}" if action == 'store'
  response['id']
end
commit(action, parameters, auth = nil) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 232
def commit(action, parameters, auth = nil)
  url = url_for(action, auth)
  parameters.merge!(parameters[:payment][:credit_card].delete(:card)).delete(:payment) if action == 'store'
  response = if %w[refund void].include? action
               parse(ssl_request(:delete, url, post_data(parameters), headers))
             else
               parse(ssl_post(url, post_data(parameters), headers))
             end

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(response, action),
    avs_result: AVSResult.new(code: response['some_avs_response_key']),
    cvv_result: CVVResult.new(response['some_cvv_response_key']),
    test: test?,
    error_code: error_code_from(response)
  )
rescue ResponseError => e
  message = get_error_message(e)
  return Response.new(
    false,
    "#{STANDARD_ERROR_MESSAGE_MAPPING[e.response.code]} #{message}",
    parse(e.response.body),
    test: test?,
    error_code: STANDARD_ERROR_CODE_MAPPING[e.response.code]
  )
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 288
def error_code_from(response)
  STANDARD_ERROR_CODE[:processing_error] unless success_from(response)
end
get_error_message(error) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 266
def get_error_message(error)
  JSON.parse(error.response.body)['message']
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 204
def headers
  {
    'Authorization' => 'Basic ' + Base64.strict_encode64("#{@options[:api_key]}:"),
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 270
def message_from(response)
  return response['message'] if response['message']
  return response['last_transaction']['acquirer_message'] if response['last_transaction']
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 212
def parse(body)
  JSON.parse(body)
end
parse_auth(auth) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 280
def parse_auth(auth)
  auth.split('|')
end
post_data(parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 284
def post_data(parameters = {})
  parameters.to_json
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 262
def success_from(response)
  %w[pending paid processing canceled active].include? response['status']
end
url_for(action, auth = nil) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 216
def url_for(action, auth = nil)
  url = live_url
  case action
  when 'store'
    "#{url}/customers/#{auth}/cards/"
  when 'customer'
    "#{url}/customers/"
  when 'refund', 'void'
    "#{url}/charges/#{auth}/"
  when 'capture'
    "#{url}/charges/#{auth}/capture/"
  else
    "#{url}/charges/"
  end
end
voucher?(payment) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 199
def voucher?(payment)
  return false if payment.is_a?(String)
  %w[sodexo vr].include? card_brand(payment)
end