class ActiveMerchant::Billing::WompiGateway

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/wompi.rb, line 16
def initialize(options = {})
  ## Sandbox keys have prefix pub_test_ and prv_test_
  ## Production keys have prefix pub_prod_ and prv_prod_
  begin
    requires!(options, :prod_private_key, :prod_public_key)
  rescue ArgumentError
    begin
      requires!(options, :test_private_key, :test_public_key)
    rescue ArgumentError
      raise ArgumentError, 'Gateway requires both test_private_key and test_public_key, or both prod_private_key and prod_public_key'
    end
  end
  super
end

Public Instance Methods

authorize(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 43
def authorize(money, payment, options = {})
  post = {
    public_key: public_key,
    type: 'CARD',
    financial_operation: 'PREAUTHORIZATION'
  }
  add_auth_params(post, money, payment, options)

  commit('authorize', post, '/payment_sources_sync')
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 54
def capture(money, authorization, options = {})
  post = {
    reference: options[:reference] || generate_reference,
    public_key: public_key,
    payment_source_id: authorization.to_i
  }
  add_invoice(post, money, options)
  commit('capture', post, '/transactions_sync')
end
purchase(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 31
def purchase(money, payment, options = {})
  post = {
    reference: options[:reference] || generate_reference,
    public_key: public_key
  }
  add_invoice(post, money, options)
  add_tip_in_cents(post, options)
  add_card(post, payment, options)

  commit('sale', post, '/transactions_sync')
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 64
def refund(money, authorization, options = {})
  # post = { amount_in_cents: amount(money).to_i, transaction_id: authorization.to_s }
  # commit('refund', post, '/refunds_sync')

  # All refunds will instead be voided. This is temporary.
  void(authorization, options, money)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 81
def scrub(transcript)
  transcript.gsub(/(Bearer )\w+/, '\1[REDACTED]').
    gsub(/(\\\"number\\\":\\\")\d+/, '\1[REDACTED]').
    gsub(/(\\\"cvc\\\":\\\")\d+/, '\1[REDACTED]').
    gsub(/(\\\"phone_number\\\":\\\")\+?\d+/, '\1[REDACTED]').
    gsub(/(\\\"email\\\":\\\")\S+\\\",/, '\1[REDACTED]\",').
    gsub(/(\\\"legal_id\\\":\\\")\d+/, '\1[REDACTED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 77
def supports_scrubbing?
  true
end
void(authorization, options = {}, money = nil) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 72
def void(authorization, options = {}, money = nil)
  post = money ? { amount_in_cents: amount(money).to_i } : {}
  commit('void', post, "/transactions/#{authorization}/void_sync")
end

Private Instance Methods

add_auth_params(post, money, card, options) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 124
def add_auth_params(post, money, card, options)
  data = {
    amount_in_cents: amount(money).to_i,
    currency: (options[:currency] || currency(money))
  }
  add_basic_card_info(data, card, options)
  post[:data] = data
end
add_basic_card_info(post, card, options) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 133
def add_basic_card_info(post, card, options)
  installments = options[:installments] ? options[:installments].to_i : 1
  cvc = card.verification_value || nil

  post[:number] = card.number
  post[:exp_month] = card.month.to_s.rjust(2, '0')
  post[:exp_year] = card.year.to_s[2..3]
  post[:installments] = installments
  post[:card_holder] = card.name
  post[:cvc] = cvc if cvc && !cvc.empty?
end
add_card(post, card, options) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 116
def add_card(post, card, options)
  payment_method = {
    type: 'CARD'
  }
  add_basic_card_info(payment_method, card, options)
  post[:payment_method] = payment_method
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 111
def add_invoice(post, money, options)
  post[:amount_in_cents] = amount(money).to_i
  post[:currency] = (options[:currency] || currency(money))
end
add_tip_in_cents(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 145
def add_tip_in_cents(post, options)
  post[:tip_in_cents] = options[:tip_in_cents].to_i if options[:tip_in_cents]
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 189
def authorization_from(response)
  response.dig('data', 'transaction_id') || response.dig('data', 'id') || response.dig('data', 'transaction', 'id')
end
commit(action, parameters, endpoint) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 153
def commit(action, parameters, endpoint)
  url = (test? ? test_url : live_url) + endpoint
  response = parse(ssl_post(url, post_data(action, parameters), headers))
  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(response),
    avs_result: nil,
    cvv_result: nil,
    test: test?,
    error_code: error_code_from(response)
  )
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 197
def error_code_from(response)
  response.dig('error', 'type') unless success_from(response)
end
generate_reference() click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 99
def generate_reference
  SecureRandom.alphanumeric(12)
end
handle_response(response) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 168
def handle_response(response)
  case response.code.to_i
  when 200...300, 401, 404, 422
    response.body
  else
    raise ResponseError.new(response)
  end
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 92
def headers
  {
    'Authorization' => "Bearer #{private_key}",
    'Content-Type' => 'application/json'
  }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 185
def message_from(response)
  response.dig('data', 'status_message') || response.dig('error', 'reason') || response.dig('error', 'messages').to_json
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 149
def parse(body)
  JSON.parse(body)
end
post_data(action, parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 193
def post_data(action, parameters = {})
  parameters.to_json
end
private_key() click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 103
def private_key
  test? ? options[:test_private_key] : options[:prod_private_key]
end
public_key() click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 107
def public_key
  test? ? options[:test_public_key] : options[:prod_public_key]
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 177
def success_from(response)
  success_statuses.include? response.dig('data', 'status')
end
success_statuses() click to toggle source
# File lib/active_merchant/billing/gateways/wompi.rb, line 181
def success_statuses
  %w(APPROVED AVAILABLE)
end