class ActiveMerchant::Billing::KushkiGateway

Constants

ENDPOINT

Public Class Methods

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

Public Instance Methods

purchase(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 20
def purchase(amount, payment_method, options={})
  MultiResponse.run() do |r|
    r.process { tokenize(amount, payment_method, options) }
    r.process { charge(amount, r.authorization, options) }
  end
end
refund(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 27
def refund(amount, authorization, options={})
  action = 'refund'

  post = {}
  post[:ticketNumber] = authorization

  commit(action, post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 49
def scrub(transcript)
  transcript.
    gsub(%r((Private-Merchant-Id: )\d+), '\1[FILTERED]').
    gsub(%r((\"card\\\":{\\\"number\\\":\\\")\d+), '\1[FILTERED]').
    gsub(%r((\"cvv\\\":\\\")\d+), '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 45
def supports_scrubbing?
  true
end
void(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 36
def void(authorization, options={})
  action = 'void'

  post = {}
  post[:ticketNumber] = authorization

  commit(action, post)
end

Private Instance Methods

add_amount_by_country(sum, options) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 102
def add_amount_by_country(sum, options)
  if amount = options[:amount]
    sum[:subtotalIva] = amount[:subtotal_iva].to_f if amount[:subtotal_iva]
    sum[:iva] = amount[:iva].to_f if amount[:iva]
    sum[:subtotalIva0] = amount[:subtotal_iva_0].to_f if amount[:subtotal_iva_0]
    sum[:ice] = amount[:ice].to_f if amount[:ice]
    if (extra_taxes = amount[:extra_taxes]) && sum[:currency] == 'COP'
      sum[:extraTaxes] ||= Hash.new
      sum[:extraTaxes][:propina] = extra_taxes[:propina].to_f if extra_taxes[:propina]
      sum[:extraTaxes][:tasaAeroportuaria] = extra_taxes[:tasa_aeroportuaria].to_f if extra_taxes[:tasa_aeroportuaria]
      sum[:extraTaxes][:agenciaDeViaje] = extra_taxes[:agencia_de_viaje].to_f if extra_taxes[:agencia_de_viaje]
      sum[:extraTaxes][:iac] = extra_taxes[:iac].to_f if extra_taxes[:iac]
    end
  end
end
add_amount_defaults(sum, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 92
def add_amount_defaults(sum, money, options)
  sum[:subtotalIva] = amount(money).to_f
  sum[:iva] = 0
  sum[:subtotalIva0] = 0

  if sum[:currency] != 'COP'
    sum[:ice] = 0
  end
end
add_invoice(action, post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 78
def add_invoice(action, post, money, options)
  if action == 'tokenize'
    post[:totalAmount] = amount(money).to_f
    post[:currency] = options[:currency] || currency(money)
    post[:isDeferred] = false
  else
    sum = {}
    sum[:currency] = options[:currency] || currency(money)
    add_amount_defaults(sum, money, options)
    add_amount_by_country(sum, options)
    post[:amount] = sum
  end
end
add_payment_method(post, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 118
def add_payment_method(post, payment_method, options)
  card = {}
  card[:number] = payment_method.number
  card[:cvv] = payment_method.verification_value
  card[:expiryMonth] = format(payment_method.month, :two_digits)
  card[:expiryYear] = format(payment_method.year, :two_digits)
  card[:name] = payment_method.name
  post[:card] = card
end
add_reference(post, authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 128
def add_reference(post, authorization, options)
  post[:token] = authorization
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 210
def authorization_from(response)
  response['token'] || response['ticketNumber']
end
charge(amount, authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 68
def charge(amount, authorization, options)
  action = 'charge'

  post = {}
  add_reference(post, authorization, options)
  add_invoice(action, post, amount, options)

  commit(action, post)
end
commit(action, params) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 139
def commit(action, params)
  response = begin
    parse(ssl_invoke(action, params))
  rescue ResponseError => e
    parse(e.response.body)
  end

  success = success_from(response)

  Response.new(
    success,
    message_from(success, response),
    response,
    authorization: success ? authorization_from(response) : nil,
    error_code: success ? nil : error_from(response),
    test: test?
  )
end
error_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 214
def error_from(response)
  response['code']
end
headers(action) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 166
def headers(action)
  hfields = {}
  hfields['Public-Merchant-Id'] = @options[:public_merchant_id] if action == 'tokenize'
  hfields['Private-Merchant-Id'] = @options[:private_merchant_id] unless action == 'tokenize'
  hfields['Content-Type'] = 'application/json'
  hfields
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 202
def message_from(succeeded, response)
  if succeeded
    'Succeeded'
  else
    response['message']
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 188
def parse(body)
  JSON.parse(body)
rescue JSON::ParserError
  message = 'Invalid JSON response received from KushkiGateway. Please contact KushkiGateway if you continue to receive this message.'
  message += " (The raw response returned by the API was #{body.inspect})"
  {
    'message' => message
  }
end
post_data(params) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 174
def post_data(params)
  params.to_json
end
ssl_invoke(action, params) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 158
def ssl_invoke(action, params)
  if ['void', 'refund'].include?(action)
    ssl_request(:delete, url(action, params), nil, headers(action))
  else
    ssl_post(url(action, params), post_data(params), headers(action))
  end
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 198
def success_from(response)
  return true if response['token'] || response['ticketNumber'] || response['code'] == 'K000'
end
tokenize(amount, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 58
def tokenize(amount, payment_method, options)
  action = 'tokenize'

  post = {}
  add_invoice(action, post, amount, options)
  add_payment_method(post, payment_method, options)

  commit(action, post)
end
url(action, params) click to toggle source
# File lib/active_merchant/billing/gateways/kushki.rb, line 178
def url(action, params)
  base_url = test? ? test_url : live_url

  if ['void', 'refund'].include?(action)
    base_url + ENDPOINT[action] + '/' + params[:ticketNumber].to_s
  else
    base_url + ENDPOINT[action]
  end
end