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 47
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)
  add_submerchant(post, options)
  add_auth_key(post, options)
  commit('authonly', post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 59
def capture(money, authorization, options = {})
  post = {}
  post[:code] = authorization
  add_invoice(post, money, options)
  add_auth_key(post, 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)
  add_submerchant(post, options)
  add_auth_key(post, options)
  commit('sale', post)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 67
def refund(money, authorization, options = {})
  add_invoice(post = {}, money, options)
  add_auth_key(post, options)
  commit('refund', post, authorization)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 97
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 77
def store(payment, options = {})
  post = {}
  options.update(name: payment.name)
  options = add_customer(options) unless options[:customer_id]
  add_payment(post, payment, options)
  add_auth_key(post, options)
  commit('store', post, options[:customer_id])
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 93
def supports_scrubbing?
  true
end
verify(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 86
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 73
def void(authorization, options = {})
  commit('void', nil, authorization)
end

Private Instance Methods

add_auth_key(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 236
def add_auth_key(post, options)
  if authorization_secret_key = options[:authorization_secret_key]
    post[:authorization_secret_key] = authorization_secret_key
  end
end
add_billing_address(post, type, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 118
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 152
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 174
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 106
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 113
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 147
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 160
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 134
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_submerchant(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 211
def add_submerchant(post, options)
  if submerchant = options[:submerchant]
    post[:SubMerchant] = {}
    post[:SubMerchant][:Merchant_Category_Code] = submerchant[:merchant_category_code] if submerchant[:merchant_category_code]
    post[:SubMerchant][:Payment_Facilitator_Code] = submerchant[:payment_facilitator_code] if submerchant[:payment_facilitator_code]
    post[:SubMerchant][:Code] = submerchant[:code] if submerchant[:code]
    post[:SubMerchant][:Name] = submerchant[:name] if submerchant[:name]
    post[:SubMerchant][:Document] = submerchant[:document] if submerchant[:document]
    post[:SubMerchant][:Type] = submerchant[:type] if submerchant[:type]
    post[:SubMerchant][:Phone] = {}
    post[:SubMerchant][:Phone][:Country_Code] = submerchant[:phone][:country_code] if submerchant.dig(:phone, :country_code)
    post[:SubMerchant][:Phone][:Number] = submerchant[:phone][:number] if submerchant.dig(:phone, :number)
    post[:SubMerchant][:Phone][:Area_Code] = submerchant[:phone][:area_code] if submerchant.dig(:phone, :area_code)
    post[:SubMerchant][:Address] = {}
    post[:SubMerchant][:Address][:Street] = submerchant[:address][:street] if submerchant.dig(:address, :street)
    post[:SubMerchant][:Address][:Number] = submerchant[:address][:number] if submerchant.dig(:address, :number)
    post[:SubMerchant][:Address][:Complement] = submerchant[:address][:complement] if submerchant.dig(:address, :complement)
    post[:SubMerchant][:Address][:Neighborhood] = submerchant[:address][:neighborhood] if submerchant.dig(:address, :neighborhood)
    post[:SubMerchant][:Address][:City] = submerchant[:address][:city] if submerchant.dig(:address, :city)
    post[:SubMerchant][:Address][:State] = submerchant[:address][:state] if submerchant.dig(:address, :state)
    post[:SubMerchant][:Address][:Country] = submerchant[:address][:country] if submerchant.dig(:address, :country)
    post[:SubMerchant][:Address][:Zip_Code] = submerchant[:address][:zip_code] if submerchant.dig(:address, :zip_code)
  end
end
add_voucher(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 191
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 344
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 271
def commit(action, parameters, auth = nil)
  url = url_for(action, auth)
  authorization_secret_key = parameters[:authorization_secret_key] if parameters
  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(authorization_secret_key)))
             else
               parse(ssl_post(url, post_data(parameters), headers(authorization_secret_key)))
             end

  Response.new(
    success_from(response, action),
    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, action)
  )
rescue ResponseError => e
  message = get_error_messages(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, action) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 358
def error_code_from(response, action)
  return if success_from(response, action)
  return response['last_transaction']['acquirer_return_code'] if response['last_transaction']

  STANDARD_ERROR_CODE[:processing_error]
end
gateway_response_errors(response) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 331
def gateway_response_errors(response)
  error_string = ''

  response['last_transaction']['gateway_response']['errors']&.each do |error|
    error.each do |_key, value|
      error_string += ' | ' unless error_string.blank?
      error_string += value
    end
  end

  error_string
end
gateway_response_errors?(response) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 327
def gateway_response_errors?(response)
  response.try(:[], 'last_transaction').try(:[], 'gateway_response').try(:[], 'errors').present?
end
get_error_messages(error) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 315
def get_error_messages(error)
  parsed_response_body = parse(error.response.body)
  message = parsed_response_body['message']

  parsed_response_body['errors']&.each do |_type, descriptions|
    message += ' | '
    message += descriptions.join(', ')
  end

  message
end
headers(authorization_secret_key = nil) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 242
def headers(authorization_secret_key = nil)
  basic_token = authorization_secret_key || @options[:api_key]
  {
    'Authorization' => 'Basic ' + Base64.strict_encode64("#{basic_token}:"),
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 309
def message_from(response)
  return gateway_response_errors(response) if gateway_response_errors?(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 251
def parse(body)
  JSON.parse(body)
end
parse_auth(auth) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 350
def parse_auth(auth)
  auth.split('|')
end
post_data(parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 354
def post_data(parameters = {})
  parameters.to_json
end
success_from(response, action) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 303
def success_from(response, action)
  success = response.try(:[], 'last_transaction').try(:[], 'success') unless action == 'store'
  success = !response.try(:[], 'id').nil? if action == 'store'
  success
end
url_for(action, auth = nil) click to toggle source
# File lib/active_merchant/billing/gateways/mundipagg.rb, line 255
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 205
def voucher?(payment)
  return false if payment.is_a?(String)

  %w[sodexo vr].include? card_brand(payment)
end