Skip to content

Commit

Permalink
Merge pull request #10 from ampaze/camt
Browse files Browse the repository at this point in the history
Möglichkeit der Abfrage der Kontoumsätze als CAMT-XML
  • Loading branch information
nemiah authored Jan 10, 2019
2 parents b95ecc8 + 7414a0e commit 57aace1
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 1 deletion.
93 changes: 92 additions & 1 deletion lib/Fhp/FinTs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Fhp;

use Fhp\FinTsInternal;
use Fhp\DataTypes\Kik;
use Fhp\DataTypes\Kti;
use Fhp\DataTypes\Ktv;
Expand All @@ -17,13 +16,15 @@
use Fhp\Response\GetStatementOfAccount;
use Fhp\Response\GetSEPAStandingOrders;
use Fhp\Response\GetTANRequest;
use Fhp\Response\BankToCustomerAccountReportHICAZ;
use Fhp\Segment\HKKAZ;
use Fhp\Segment\HKSAL;
use Fhp\Segment\HKSPA;
use Fhp\Segment\HKCDB;
use Fhp\Segment\HKTAN;
use Fhp\Segment\HKDSE;
use Fhp\Segment\HKDSC;
use Fhp\Segment\HKCAZ;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Fhp\Dialog\Exception\TANException;
Expand Down Expand Up @@ -326,6 +327,96 @@ protected function createStateOfAccountMessage(
return $message;
}

/**
* Gets Bank To Customer Account Report as camt XML
*
* @param SEPAAccount $account
* @param \DateTime $from
* @param \DateTime $to
* @return string[]
* @throws \Exception
*/
public function getBankToCustomerAccountReportAsRawXML(SEPAAccount $account, \DateTime $from, \DateTime $to)
{
$responses = [];

$this->logger->info('');
$this->logger->info('HKCAZ (statement of accounts) initialize');
$this->logger->info('Start date: ' . $from->format('Y-m-d'));
$this->logger->info('End date : ' . $to->format('Y-m-d'));

$dialog = $this->getDialog();

$message = $this->createHKCAZMessage($dialog, $account, $from, $to, null);
$response = $dialog->sendMessage($message);
$touchdowns = $response->getTouchdowns($message);

$HICAZ = new BankToCustomerAccountReportHICAZ($response->rawResponse);
$responses[] = $HICAZ->getBookedXML();

$touchdownCounter = 1;
while (isset($touchdowns[HKCAZ::NAME])) {
$this->logger->info('Fetching more statement of account results (' . $touchdownCounter++ . ') ...');
$message = $this->createHKCAZMessage(
$dialog,
$account,
$from,
$to,
$this->escapeString($touchdowns[HKCAZ::NAME])
);

$response = $dialog->sendMessage($message);
$touchdowns = $response->getTouchDowns($message);
$HICAZ = new BankToCustomerAccountReportHICAZ($response->rawResponse);
$responses[] = $HICAZ->getBookedXML();
}

$this->logger->info('HKCAZ end');

return $responses;
}

/**
* Helper method to create a "Statement of Account Message".
*
* @param Dialog $dialog
* @param SEPAAccount $account
* @param \DateTime $from
* @param \DateTime $to
* @param string|null $touchdown
* @return Message
* @throws \Exception
*/
protected function createHKCAZMessage(Dialog $dialog, SEPAAccount $account, \DateTime $from, \DateTime $to, $touchdown = null)
{
$kti = new Kti(
$account->getIban(),
$account->getBic(),
$account->getAccountNumber(),
$account->getSubAccount(),
new Kik(280, $account->getBlz())
);

$message = $this->getNewMessage(
$dialog,
array(
new HKCAZ(
1,
3,
$kti,
$this->escapeString(HKCAZ::CAMT_FORMAT_FQ),
HKCAZ::ALL_ACCOUNTS_N,
$from,
$to,
$touchdown
)
),
array(AbstractMessage::OPT_PINTAN_MECH => $this->getUsedPinTanMechanism($dialog))
);

return $message;
}

/**
* Gets the saldo of given SEPAAccount.
*
Expand Down
48 changes: 48 additions & 0 deletions lib/Fhp/Response/BankToCustomerAccountReportHICAZ.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Fhp\Response;

use Fhp\Segment\HKCAZ;

/**
* Class BankToCustomerAccountReportHICAZ.php
* @package Fhp\Response
*/
class BankToCustomerAccountReportHICAZ extends Response
{
const SEG_ACCOUNT_INFORMATION = 'HICAZ';

/**
* Gets the raw MT940 string from response.
*
* @return string
*/
public function getBookedXML()
{
$seg = $this->findSegment(static::SEG_ACCOUNT_INFORMATION);

$parts = $this->splitSegment($seg);

if (count($parts) > 3) {

if ($parts[2] == HKCAZ::CAMT_FORMAT . '.xsd') {
list($empty, $length, $xml) = explode('@', $parts[3], 3);
if ($empty == '' && intval($length) == strlen($xml)) {
return $xml;
}

throw new \Exception('Fehler im XML Payload');

} else {
throw new \Exception('Unerwartetes CAMT XML Format (' . $parts[2] . ')');
}
}

return '';
}

protected function conformToUtf8($string)
{
return $string;
}
}
69 changes: 69 additions & 0 deletions lib/Fhp/Segment/HKCAZ.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Fhp\Segment;

use Fhp\DataTypes\Dat;
use Fhp\DataTypes\Kti;

/**
* Class HKCAZ (Kontoumsätze/Zeitraum (camt))
* Segment type: Geschäftsvorfall
*
* @link: http://www.hbci-zka.de/dokumente/spezifikation_deutsch/fintsv3/FinTS_3.0_Messages_Geschaeftsvorfaelle_2015-08-07_final_version.pdf
* Section: C.2.3.1.1.1
*
* @package Fhp\Segment
*/
class HKCAZ extends AbstractSegment
{
const NAME = 'HKCAZ';
const ALL_ACCOUNTS_N = 'N';
const ALL_ACCOUNTS_Y = 'J';
const CAMT_FORMAT = 'camt.052.001.02';
const CAMT_FORMAT_FQ = 'urn:iso:std:iso:20022:tech:xsd:' . self::CAMT_FORMAT;

/**
* HKCAZ constructor.
* @param int $version
* @param int $segmentNumber
* @param Kti $kti
* @param string $camtFormat
* @param array $allAccounts
* @param \DateTime $from
* @param \DateTime $to
* @param string|null $touchdown
*/
public function __construct(
$version,
$segmentNumber,
$kti,
$camtFormat,
$allAccounts,
\DateTime $from,
\DateTime $to,
$touchdown = null
) {
parent::__construct(
static::NAME,
$segmentNumber,
$version,
array(
$kti,
$camtFormat,
$allAccounts,
new Dat($from),
new Dat($to),
null,
$touchdown
)
);
}

/**
* @return string
*/
public function getName()
{
return static::NAME;
}
}

0 comments on commit 57aace1

Please sign in to comment.