Skip to content

Instantly share code, notes, and snippets.

@gonzalo-bulnes
Last active December 17, 2015 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonzalo-bulnes/5571666 to your computer and use it in GitHub Desktop.
Save gonzalo-bulnes/5571666 to your computer and use it in GitHub Desktop.
A logger for the TBK gem. This logger doesn't calculate the `TBK_MAC` value but allows you to set it manually instead. See https://github.com/gonzalo-bulnes/tbk/issues/2 for context. See also https://github.com/kiel-cristian/tbk-rails .
# encoding: utf-8
# lib/tbk/webpay/logger.rb
# Copyright (c) 2013 Acid Ltda <www.acid.cl>
# Copyright (c) 2013 Gonzalo Bulnes Guilpain <gonzalo@acid.cl>
# Copyright (c) 2013 Cristián Carreño <kiel@acid.cl>
#
# This code is distributed under the same license than TBK-Rails,
# see https://github.com/kiel-cristian/tbk-rails for details.
module TBK
module Webpay
module Logger
# Public: Log a Webpay transaction, create logfile if necessary
#
# hash - The transaction parameters as a Hash
# tbk_codigo_commercio - The commerce ID String
# tbk_mac - The transaction pseudo MAC String
#
# Exemple:
#
# hash = { 'TBK_ORDEN_COMPRA'=>'221356', 'TBK_TIPO_TRANSACCION'=>'TR_NORMAL', \
# 'TBK_RESPUESTA'=>'-1', 'TBK_MONTO'=>'565200', \
# 'TBK_CODIGO_AUTORIZACION'=>'000000', \
# 'TBK_FINAL_NUMERO_TARJETA'=>'3265', 'TBK_FECHA_CONTABLE'=>'0306', \
# 'TBK_FECHA_TRANSACCION'=>'0306', 'TBK_HORA_TRANSACCION'=>'165028', \
# 'TBK_ID_SESION'=>'238831', 'TBK_ID_TRANSACCION'=>'129613424593', \
# 'TBK_TIPO_PAGO'=>'VN', 'TBK_NUMERO_CUOTAS'=>'3'}
# tbk_codigo_commercio = 59702734271
# tbk_mac = '76d37677633e4a095669d51'
# log_transaction hash, tbk_codigo_comercio, tbk_mac
#
# Returns nothing.
def log_transaction hash, tbk_codigo_comercio, tbk_mac
# Note: since the file names are defined automatically,
# dir_path MUST be a directory path (end with a slash '/')
#
# See the Transbank Manual de Integración KCC 6.0, section 9.2
if defined? Rails
dir_path = Rails.root.join 'log', 'webpay'
else
raise 'Not yet implemented.'
#dir_path = 'logs/webpay/'
end
filename = "TBK_#{ hash['TBK_TIPO_TRANSACCION'] }_#{ hash['TBK_FECHA_TRANSACCION'] }.log"
path = dir_path.join filename
log = generate_log hash, tbk_codigo_comercio, tbk_mac
unless File.exists? path
# Ensure the log directory exists
FileUtils.mkdir_p dir_path
File.new path, 'w+'
end
File.open path, 'a+' do |file|
unless File.zero?(path)
file.write "\n"
end
file.write log
end
end
private
# Public: Create a Webpay transaction log entry
#
# hash - The transaction parameters as a Hash
# tbk_codigo_commercio - The commerce ID String
# tbk_mac - The transaction pseudo MAC String
#
# Exemple:
#
# hash = { 'TBK_ORDEN_COMPRA'=>'221356', 'TBK_TIPO_TRANSACCION'=>'TR_NORMAL', \
# 'TBK_RESPUESTA'=>'-1', 'TBK_MONTO'=>'565200', \
# 'TBK_CODIGO_AUTORIZACION'=>'000000', \
# 'TBK_FINAL_NUMERO_TARJETA'=>'3265', 'TBK_FECHA_CONTABLE'=>'0306', \
# 'TBK_FECHA_TRANSACCION'=>'0306', 'TBK_HORA_TRANSACCION'=>'165028', \
# 'TBK_ID_SESION'=>'238831', 'TBK_ID_TRANSACCION'=>'129613424593', \
# 'TBK_TIPO_PAGO'=>'VN', 'TBK_NUMERO_CUOTAS'=>'3'}
# tbk_codigo_commercio = 59702734271
# tbk_mac = '76d37677633e4a095669d51'
# log_transaction hash, tbk_codigo_comercio, tbk_mac
#
# Returns the log entry String.
def generate_log hash, tbk_codigo_comercio, tbk_mac
response = []
response << (hash['TBK_RESPUESTA'] == '0' ? 'ACK' : 'ERR')
response << "TBK_ORDEN_COMPRA=#{hash['TBK_ORDEN_COMPRA']}"
response << "TBK_CODIGO_COMERCIO=#{tbk_codigo_comercio}"
response << "TBK_TIPO_TRANSACCION=#{hash['TBK_TIPO_TRANSACCION']}"
response << "TBK_RESPUESTA=#{hash['TBK_RESPUESTA']}"
response << "TBK_MONTO=#{hash['TBK_MONTO']}"
response << "TBK_CODIGO_AUTORIZACION=#{hash['TBK_CODIGO_AUTORIZACION']}"
response << "TBK_FINAL_NUMERO_TARJETA=#{hash['TBK_FINAL_NUMERO_TARJETA']}"
response << "TBK_FECHA_CONTABLE=#{hash['TBK_FECHA_CONTABLE']}"
response << "TBK_FECHA_TRANSACCION=#{hash['TBK_FECHA_TRANSACCION']}"
response << "TBK_HORA_TRANSACCION=#{hash['TBK_HORA_TRANSACCION']}"
response << "TBK_ID_SESION=#{hash['TBK_ID_SESION']}"
response << "TBK_ID_TRANSACCION=#{hash['TBK_ID_TRANSACCION']}"
response << "TBK_TIPO_PAGO=#{hash['TBK_TIPO_PAGO']}"
response << "TBK_NUMERO_CUOTAS=#{hash['TBK_NUMERO_CUOTAS']}"
if hash['TBK_TIPO_PAGO'] == 'CI'
if hash['TBK_TASA_INTERES_MAX']
response << "TBK_TASA_INTERES_MAX=#{hash['TBK_TASA_INTERES_MAX']}"
end
if hash['TBK_MONTO_CUOTA']
response << "TBK_MONTO_CUOTA=#{hash['TBK_MONTO_CUOTA']}"
end
end
response << "TBK_MAC=#{tbk_mac}"
response = response.join('; ')
response
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment