Skip to content

Commit

Permalink
Merge pull request #27 from monofone/master
Browse files Browse the repository at this point in the history
painFormat should be overwritable
  • Loading branch information
ianare committed Dec 29, 2013
2 parents 455bec4 + fb3948a commit f37fe2b
Show file tree
Hide file tree
Showing 30 changed files with 1,511 additions and 236 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
}
],
"autoload": {
"psr-0": { "Digitick\\Sepa": "lib/" }
"psr-0": {
"Digitick\\Sepa": "lib/"
}
},
"require": {
"php": ">=5.3.2"
Expand Down
38 changes: 25 additions & 13 deletions lib/Digitick/Sepa/DomBuilder/BaseDomBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

abstract class BaseDomBuilder implements DomBuilderInterface
{
const INITIAL_STRING = '<?xml version="1.0" encoding="UTF-8"?><Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:%s"></Document>';

protected $doc;

Expand All @@ -43,12 +42,13 @@ abstract class BaseDomBuilder implements DomBuilderInterface
*/
protected $currentPayment = null;

function __construct($painFormat) {
function __construct($painFormat)
{
$this->doc = new \DOMDocument('1.0', 'UTF-8');
$this->doc->formatOutput = true;
$this->root = $this->doc->createElement('Document');
$this->root->setAttribute('xmlns', sprintf("urn:iso:std:iso:20022:tech:xsd:%s", $painFormat));
$this->root->setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance");
$this->root->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
$this->doc->appendChild($this->root);
}

Expand All @@ -57,10 +57,12 @@ function __construct($painFormat) {
* @param null $value
* @return \DOMElement
*/
protected function createElement($name, $value = null) {
if($value){
protected function createElement($name, $value = null)
{
if ($value) {
$elm = $this->doc->createElement($name);
$elm->appendChild($this->doc->createTextNode($value));

return $elm;
} else {
return $this->doc->createElement($name);
Expand All @@ -70,7 +72,8 @@ protected function createElement($name, $value = null) {
/**
* @return string
*/
public function asXml(){
public function asXml()
{
return $this->doc->saveXML();
}

Expand All @@ -88,16 +91,22 @@ protected function intToCurrency($amount)
* @param GroupHeader $groupHeader
* @return mixed
*/
public function visitGroupHeader(GroupHeader $groupHeader) {
public function visitGroupHeader(GroupHeader $groupHeader)
{
$groupHeaderTag = $this->doc->createElement('GrpHdr');
$messageId = $this->createElement('MsgId', $groupHeader->getMessageIdentification());
$groupHeaderTag->appendChild($messageId);
$creationDateTime = $this->createElement('CreDtTm', $groupHeader->getCreationDateTime()->format('Y-m-d\TH:i:s\Z'));
$creationDateTime = $this->createElement(
'CreDtTm',
$groupHeader->getCreationDateTime()->format('Y-m-d\TH:i:s\Z')
);
$groupHeaderTag->appendChild($creationDateTime);
$groupHeaderTag->appendChild($this->createElement('NbOfTxs', $groupHeader->getNumberOfTransactions()));
$groupHeaderTag->appendChild($this->createElement('CtrlSum', $this->intToCurrency($groupHeader->getControlSumCents())));
$groupHeaderTag->appendChild(
$this->createElement('CtrlSum', $this->intToCurrency($groupHeader->getControlSumCents()))
);
$initiatingParty = $this->createElement('InitgPty');
$initiatingPartyName = $this->createElement('Nm', $groupHeader->getInitiatingPartyName());
$initiatingPartyName = $this->createElement('Nm', $groupHeader->getInitiatingPartyName());
$initiatingParty->appendChild($initiatingPartyName);
if ($groupHeader->getInitiatingPartyId() !== null) {
$id = $this->createElement('Id', $groupHeader->getInitiatingPartyId());
Expand All @@ -112,7 +121,8 @@ public function visitGroupHeader(GroupHeader $groupHeader) {
* @param string $bic
* @return \DOMElement
*/
protected function getFinancialInstitutionElement($bic) {
protected function getFinancialInstitutionElement($bic)
{
$finInstitution = $this->createElement('FinInstnId');
$finInstitution->appendChild($this->createElement('BIC', $bic));

Expand All @@ -124,7 +134,8 @@ protected function getFinancialInstitutionElement($bic) {
* @param string $iban
* @return \DOMElement
*/
public function getIbanElement($iban) {
public function getIbanElement($iban)
{
$id = $this->createElement('Id');
$id->appendChild($this->createElement('IBAN', $iban));

Expand All @@ -136,7 +147,8 @@ public function getIbanElement($iban) {
* @param string $remittenceInformation
* @return \DOMElement
*/
public function getRemittenceElement($remittenceInformation) {
public function getRemittenceElement($remittenceInformation)
{
$remittanceInformation = $this->createElement('RmtInf');
$remittanceInformation->appendChild($this->createElement('Ustrd', $remittenceInformation));

Expand Down
31 changes: 21 additions & 10 deletions lib/Digitick/Sepa/DomBuilder/CustomerCreditTransferDomBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

class CustomerCreditTransferDomBuilder extends BaseDomBuilder
{
function __construct() {
parent::__construct(CustomerCreditTransferFile::PAIN_FORMAT);
function __construct($painFormat = 'pain.001.002.03')
{
parent::__construct($painFormat);
}


Expand All @@ -42,7 +43,8 @@ function __construct() {
* @param TransferFileInterface $transferFile
* @return mixed
*/
public function visitTransferFile(TransferFileInterface $transferFile) {
public function visitTransferFile(TransferFileInterface $transferFile)
{
$this->currentTransfer = $this->doc->createElement('CstmrCdtTrfInitn');
$this->root->appendChild($this->currentTransfer);
}
Expand All @@ -53,7 +55,8 @@ public function visitTransferFile(TransferFileInterface $transferFile) {
* @param PaymentInformation $paymentInformation
* @return mixed
*/
public function visitPaymentInformation(PaymentInformation $paymentInformation) {
public function visitPaymentInformation(PaymentInformation $paymentInformation)
{
$this->currentPayment = $this->createElement('PmtInf');
$this->currentPayment->appendChild($this->createElement('PmtInfId', $paymentInformation->getId()));
if ($paymentInformation->getCategoryPurposeCode()) {
Expand All @@ -62,9 +65,13 @@ public function visitPaymentInformation(PaymentInformation $paymentInformation)
$this->currentPayment->appendChild($categoryPurpose);
}
$this->currentPayment->appendChild($this->createElement('PmtMtd', $paymentInformation->getPaymentMethod()));
$this->currentPayment->appendChild($this->createElement('NbOfTxs', $paymentInformation->getNumberOfTransactions()));
$this->currentPayment->appendChild(
$this->createElement('NbOfTxs', $paymentInformation->getNumberOfTransactions())
);

$this->currentPayment->appendChild($this->createElement('CtrlSum', $this->intToCurrency($paymentInformation->getControlSumCents())));
$this->currentPayment->appendChild(
$this->createElement('CtrlSum', $this->intToCurrency($paymentInformation->getControlSumCents()))
);

$paymentTypeInformation = $this->createElement('PmtTpInf');
$serviceLevel = $this->createElement('SvcLvl');
Expand All @@ -86,7 +93,7 @@ public function visitPaymentInformation(PaymentInformation $paymentInformation)
$id = $this->createElement('Id');
$id->appendChild($this->createElement('IBAN', $paymentInformation->getOriginAccountIBAN()));
$debtorAccount->appendChild($id);
if($paymentInformation->getOriginAccountCurrency()) {
if ($paymentInformation->getOriginAccountCurrency()) {
$debtorAccount->appendChild($this->createElement('Ccy', $paymentInformation->getOriginAccountCurrency()));
}
$this->currentPayment->appendChild($debtorAccount);
Expand All @@ -97,7 +104,7 @@ public function visitPaymentInformation(PaymentInformation $paymentInformation)
$debtorAgent->appendChild($financialInstitutionId);
$this->currentPayment->appendChild($debtorAgent);

$this->currentPayment->appendChild($this->createElement('ChrgBr','SLEV'));
$this->currentPayment->appendChild($this->createElement('ChrgBr', 'SLEV'));
$this->currentTransfer->appendChild($this->currentPayment);
}

Expand All @@ -107,7 +114,8 @@ public function visitPaymentInformation(PaymentInformation $paymentInformation)
* @param TransferInformationInterface $transactionInformation
* @return mixed
*/
public function visitTransferInformation(TransferInformationInterface $transactionInformation) {
public function visitTransferInformation(TransferInformationInterface $transactionInformation)
{
/** @var $transactionInformation CustomerCreditTransferInformation */
$CdtTrfTxInf = $this->createElement('CdtTrfTxInf');

Expand All @@ -118,7 +126,10 @@ public function visitTransferInformation(TransferInformationInterface $transacti

// Amount 2.42
$amount = $this->createElement('Amt');
$instructedAmount = $this->createElement('InstdAmt', $this->intToCurrency($transactionInformation->getTransferAmount()));
$instructedAmount = $this->createElement(
'InstdAmt',
$this->intToCurrency($transactionInformation->getTransferAmount())
);
$instructedAmount->setAttribute('Ccy', $transactionInformation->getCurrency());
$amount->appendChild($instructedAmount);
$CdtTrfTxInf->appendChild($amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
class CustomerDirectDebitTransferDomBuilder extends BaseDomBuilder
{

public function __construct() {
parent::__construct(CustomerDirectDebitTransferFile::PAIN_FORMAT);
public function __construct($painFormat = 'pain.008.002.02')
{
parent::__construct($painFormat);
}

/**
Expand All @@ -43,7 +44,8 @@ public function __construct() {
* @param TransferFileInterface $transferFile
* @return mixed
*/
public function visitTransferFile(TransferFileInterface $transferFile) {
public function visitTransferFile(TransferFileInterface $transferFile)
{
$this->currentTransfer = $this->doc->createElement('CstmrDrctDbtInitn');
$this->root->appendChild($this->currentTransfer);
}
Expand All @@ -54,13 +56,18 @@ public function visitTransferFile(TransferFileInterface $transferFile) {
* @param PaymentInformation $paymentInformation
* @return mixed
*/
public function visitPaymentInformation(PaymentInformation $paymentInformation) {
public function visitPaymentInformation(PaymentInformation $paymentInformation)
{
$this->currentPayment = $this->createElement('PmtInf');
$this->currentPayment->appendChild($this->createElement('PmtInfId', $paymentInformation->getId()));
$this->currentPayment->appendChild($this->createElement('PmtMtd', $paymentInformation->getPaymentMethod()));
$this->currentPayment->appendChild($this->createElement('NbOfTxs', $paymentInformation->getNumberOfTransactions()));
$this->currentPayment->appendChild(
$this->createElement('NbOfTxs', $paymentInformation->getNumberOfTransactions())
);

$this->currentPayment->appendChild($this->createElement('CtrlSum', $this->intToCurrency($paymentInformation->getControlSumCents())));
$this->currentPayment->appendChild(
$this->createElement('CtrlSum', $this->intToCurrency($paymentInformation->getControlSumCents()))
);

$paymentTypeInformation = $this->createElement('PmtTpInf');
$serviceLevel = $this->createElement('SvcLvl');
Expand Down Expand Up @@ -117,23 +124,33 @@ public function visitPaymentInformation(PaymentInformation $paymentInformation)
* @param TransferInformationInterface $transactionInformation
* @return mixed
*/
public function visitTransferInformation(TransferInformationInterface $transactionInformation) {
public function visitTransferInformation(TransferInformationInterface $transactionInformation)
{
/** @var $transactionInformation CustomerDirectDebitTransferInformation */
$directDebitTransactionInformation = $this->createElement('DrctDbtTxInf');

$paymentId = $this->createElement('PmtId');
$paymentId->appendChild($this->createElement('EndToEndId', $transactionInformation->getEndToEndIdentification()));
$paymentId->appendChild(
$this->createElement('EndToEndId', $transactionInformation->getEndToEndIdentification())
);
$directDebitTransactionInformation->appendChild($paymentId);

$instructedAmount = $this->createElement('InstdAmt', $this->intToCurrency($transactionInformation->getTransferAmount()));
$instructedAmount = $this->createElement(
'InstdAmt',
$this->intToCurrency($transactionInformation->getTransferAmount())
);
$instructedAmount->setAttribute('Ccy', $transactionInformation->getCurrency());
$directDebitTransactionInformation->appendChild($instructedAmount);

$directDebitTransaction = $this->createElement('DrctDbtTx');
$mandateRelatedInformation = $this->createElement('MndtRltdInf');
$directDebitTransaction->appendChild($mandateRelatedInformation);
$mandateRelatedInformation->appendChild($this->createElement('MndtId', $transactionInformation->getMandateId()));
$mandateRelatedInformation->appendChild($this->createElement('DtOfSgntr', $transactionInformation->getMandateSignDate()->format('Y-m-d')));
$mandateRelatedInformation->appendChild(
$this->createElement('MndtId', $transactionInformation->getMandateId())
);
$mandateRelatedInformation->appendChild(
$this->createElement('DtOfSgntr', $transactionInformation->getMandateSignDate()->format('Y-m-d'))
);
$directDebitTransactionInformation->appendChild($directDebitTransaction);

// TODO add the possibility to add CreditorSchemeId on transfer level
Expand All @@ -150,7 +167,9 @@ public function visitTransferInformation(TransferInformationInterface $transacti
$debtorAccount->appendChild($this->getIbanElement($transactionInformation->getIban()));
$directDebitTransactionInformation->appendChild($debtorAccount);

$directDebitTransactionInformation->appendChild($this->getRemittenceElement($transactionInformation->getRemittanceInformation()));
$directDebitTransactionInformation->appendChild(
$this->getRemittenceElement($transactionInformation->getRemittanceInformation())
);
$this->currentPayment->appendChild($directDebitTransactionInformation);

}
Expand Down
8 changes: 5 additions & 3 deletions lib/Digitick/Sepa/DomBuilder/DomBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
use Digitick\Sepa\Exception\InvalidArgumentException;
use Digitick\Sepa\TransferFile\TransferFileInterface;

class DomBuilderFactory {
class DomBuilderFactory
{

public static function createDomBuilder(TransferFileInterface $transferFile) {
public static function createDomBuilder(TransferFileInterface $transferFile)
{
$transferFileClass = get_class($transferFile);
switch($transferFileClass) {
switch ($transferFileClass) {
case 'Digitick\Sepa\TransferFile\CustomerCreditTransferFile':
$domBuilder = new CustomerCreditTransferDomBuilder();
$transferFile->accept($domBuilder);
Expand Down
3 changes: 2 additions & 1 deletion lib/Digitick/Sepa/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class Exception extends \Exception {
class Exception extends \Exception
{

}
3 changes: 2 additions & 1 deletion lib/Digitick/Sepa/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class InvalidArgumentException extends Exception{
class InvalidArgumentException extends Exception
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class InvalidPaymentMethodException extends Exception {
class InvalidPaymentMethodException extends Exception
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class InvalidTransferFileConfiguration extends Exception {
class InvalidTransferFileConfiguration extends Exception
{

}
3 changes: 2 additions & 1 deletion lib/Digitick/Sepa/Exception/InvalidTransferTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class InvalidTransferTypeException extends Exception {
class InvalidTransferTypeException extends Exception
{

}
Loading

0 comments on commit f37fe2b

Please sign in to comment.