Skip to content

Instantly share code, notes, and snippets.

@djmaze
Created January 17, 2010 19:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save djmaze/279513 to your computer and use it in GitHub Desktop.
Save djmaze/279513 to your computer and use it in GitHub Desktop.
class BankAccount < ActiveRecord::Base
# Umsätze von start_date bis end_date abrufen
# * passport_type, passphrase, pin und file kommen in dieser Implementation aus der zugrunde liegenden Tabelle.
# * Wenn passport_type = "PinTan" ist, wird die pin verwendet.
# * Wenn passport_type = "RDHNew" ist, wird die Schlüsseldatei aus filename verwendet und mit der passphrase entschlüsselt.
def get_transactions(start_date, end_date)
HBCIUtils.setParam("client.passport.#{passport_type}.filename", filename)
HBCIUtils.setParam("client.passport.#{passport_type}.init", '1')
passport = AbstractHBCIPassport.getInstance(passport_type, [passphrase, pin])
handle = HBCIHandler.new(passport.getHBCIVersion, passport)
job = handle.newJob('KUmsAll')
my_account = passport.getAccount(self.number)
job.setParam('my', my_account)
ruby_startdate = start_date || (Date.today - 1)
job.setParam('startdate', JavaUtil::Date.new(ruby_startdate.year-1900, ruby_startdate.month-1, ruby_startdate.day))
ruby_enddate = end_date || (Date.today - 1)
job.setParam('enddate', JavaUtil::Date.new(ruby_enddate.year-1900, ruby_enddate.month-1, ruby_enddate.day))
job.addToQueue
status = handle.execute
handle.close
if status.isOK
result = job.getJobResult
result.getFlatData
else
puts "Fehler: " + status.getErrorString
end
end
end
require 'java'
require File.join(RAILS_ROOT,'vendor/hbci4java.jar')
import 'org.kapott.hbci.manager.HBCIUtils'
import 'org.kapott.hbci.manager.HBCIUtilsInternal'
import 'org.kapott.hbci.passport.AbstractHBCIPassport'
import 'org.kapott.hbci.callback.HBCICallbackConsole'
import 'org.kapott.hbci.manager.HBCIHandler'
class HBCIRunnable
include java.lang.Runnable
def initialize(&block)
@block = block
end
def run
RAILS_DEFAULT_LOGGER.info "Starte HBCI-Job"
@block.call
RAILS_DEFAULT_LOGGER.info "HBCI-Job beendet!"
end
end
# Wrapper für HBCI-Aufrufe
def hbci(&block)
target = HBCIRunnable.new(&block)
thread = java.lang.Thread.new($hbci_thread_group, target)
thread.start
thread.join
end
# ThreadGroup mit eindeutigem Namen für jeden Rails-Thread erzeugen!
$hbci_thread_group = java.lang.ThreadGroup.new("HBCI_#{java.lang.Thread.currentThread.getId}")
hbci do
HBCIUtils.init(nil, nil, MyHBCICallback.new)
end
# Callback für HBCI-Aufrufe
class MyHBCICallback < HBCICallbackConsole
@@status_names = {}
constants.each { |c| @@status_names[const_get(c)] = c }
def callback(passport, reason, msg, dataType, retData)
passphrase, pin, tan = passport.getClientData('init')
case reason
when NEED_PASSPHRASE_LOAD then retData.replace(0, retData.length, passphrase)
# when NEED_PASSPHRASE_SAVE then retData.replace(0, retData.length, passphrase)
when NEED_PT_PIN then retData.replace(0, retData.length, pin)
when NEED_PT_TAN then retData.replace(0, retData.length, tan)
when NEED_CONNECTION, CLOSE_CONNECTION then nil
else super
end
end
def log(msg, level, date, trace)
# Alle HBCI-Statusmeldungen einfach an der Konsole ausgeben
puts msg
end
def status(passport, statusTag, o)
puts @@status_names[statusTag]
end
end
class TransactionsController < ActionController::Base
def index
account = BankAccount.first
hbci do
@umsaetze = account.get_transactions(Date.today - 1, Date.today - 1)
end
end
end
@djmaze
Copy link
Author

djmaze commented May 2, 2010

Dieser Gist zeigt, wie man mit JRuby on Rails und der HBCI4Java-Bibliothek per PIN-TAN- bzw. RDH-Schlüsseldatei-Verfahren die Umsätze eines Bankkontos abfragt.

Mit Hilfe der Wrapper-Methode "hbci" können HBCI-Aufrufe in einer separaten Threadgroup erfolgen, so dass der Applikationsserver nicht warten muss und auch mehrere HBCI-Aktionen parallel durchführen kann.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment