|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace EbicsApi\Ebics\Builders\Document; |
| 4 | + |
| 5 | +use DateTime; |
| 6 | +use EbicsApi\Ebics\Contracts\PostalAddressInterface; |
| 7 | +use EbicsApi\Ebics\Models\CustomerCreditTransfer; |
| 8 | +use InvalidArgumentException; |
| 9 | + |
| 10 | +/** |
| 11 | + * CustomerCreditTransferBuilder |
| 12 | + * Corresponds to EbicsApi\Ebics\Builders\CustomerCreditTransfer\CustomerCreditTransferBuilder but with namespaces |
| 13 | + * |
| 14 | + * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 15 | + * @author Jan-Philipp Georg (moori) |
| 16 | + */ |
| 17 | +final class CustomerCreditTransferBuilder extends DocumentBuilder |
| 18 | +{ |
| 19 | + public function createInstance( |
| 20 | + string $debitorFinInstBIC, |
| 21 | + string $debitorIBAN, |
| 22 | + string $debitorName, |
| 23 | + ?DateTime $executionDate = null, |
| 24 | + bool $batchBooking = true, |
| 25 | + ?string $msgId = null, |
| 26 | + ?string $paymentReference = null, |
| 27 | + ?string $chargeBearer = null |
| 28 | + ): self { |
| 29 | + $this->instance = new CustomerCreditTransfer(); |
| 30 | + $now = new DateTime(); |
| 31 | + |
| 32 | + $doc = $this->el('Document'); |
| 33 | + $doc->setAttributeNS( |
| 34 | + 'http://www.w3.org/2000/xmlns/', |
| 35 | + 'xmlns:xsi', |
| 36 | + 'http://www.w3.org/2001/XMLSchema-instance' |
| 37 | + ); |
| 38 | + $doc->setAttributeNS( |
| 39 | + 'http://www.w3.org/2001/XMLSchema-instance', |
| 40 | + 'xsi:schemaLocation', |
| 41 | + $this->schemaLocation() |
| 42 | + ); |
| 43 | + $this->instance->appendChild($doc); |
| 44 | + |
| 45 | + $ccti = $this->el('CstmrCdtTrfInitn'); |
| 46 | + $doc->appendChild($ccti); |
| 47 | + |
| 48 | + $grpHdr = $this->el('GrpHdr'); |
| 49 | + $ccti->appendChild($grpHdr); |
| 50 | + $grpHdr->appendChild( |
| 51 | + $this->el('MsgId', $msgId ?: $this->randomService->uniqueIdWithDate('msg')) |
| 52 | + ); |
| 53 | + $grpHdr->appendChild($this->el('CreDtTm', $now->format('Y-m-d\TH:i:s\Z'))); |
| 54 | + $grpHdr->appendChild($this->el('NbOfTxs', '0')); |
| 55 | + $grpHdr->appendChild($this->el('CtrlSum', '0')); |
| 56 | + |
| 57 | + $initg = $this->el('InitgPty'); |
| 58 | + $initg->appendChild($this->el('Nm', $debitorName)); |
| 59 | + $grpHdr->appendChild($initg); |
| 60 | + |
| 61 | + $pmtInf = $this->el('PmtInf'); |
| 62 | + $ccti->appendChild($pmtInf); |
| 63 | + |
| 64 | + $pmtInf->appendChild( |
| 65 | + $this->el('PmtInfId', $paymentReference ?: $this->randomService->uniqueIdWithDate('pmt')) |
| 66 | + ); |
| 67 | + $pmtInf->appendChild($this->el('PmtMtd', 'TRF')); |
| 68 | + $pmtInf->appendChild($this->el('BtchBookg', $batchBooking ? 'true' : 'false')); |
| 69 | + $pmtInf->appendChild($this->el('NbOfTxs', '0')); |
| 70 | + $pmtInf->appendChild($this->el('CtrlSum', '0')); |
| 71 | + |
| 72 | + $pmtTpInf = $this->el('PmtTpInf'); |
| 73 | + $svcLvl = $this->el('SvcLvl'); |
| 74 | + $svcLvl->appendChild($this->el('Cd', 'SEPA')); |
| 75 | + $pmtTpInf->appendChild($svcLvl); |
| 76 | + $pmtInf->appendChild($pmtTpInf); |
| 77 | + |
| 78 | + $pmtInf->appendChild($this->buildReqdExctnDt($executionDate ?: $now)); |
| 79 | + |
| 80 | + $dbtr = $this->el('Dbtr'); |
| 81 | + $dbtr->appendChild($this->el('Nm', $debitorName)); |
| 82 | + $pmtInf->appendChild($dbtr); |
| 83 | + |
| 84 | + $dbtrAcct = $this->el('DbtrAcct'); |
| 85 | + $id = $this->el('Id'); |
| 86 | + $id->appendChild($this->el('IBAN', $debitorIBAN)); |
| 87 | + $dbtrAcct->appendChild($id); |
| 88 | + $pmtInf->appendChild($dbtrAcct); |
| 89 | + |
| 90 | + $dbtrAgt = $this->el('DbtrAgt'); |
| 91 | + $fin = $this->el('FinInstnId'); |
| 92 | + $fin->appendChild($this->el('BICFI', $debitorFinInstBIC)); |
| 93 | + $dbtrAgt->appendChild($fin); |
| 94 | + $pmtInf->appendChild($dbtrAgt); |
| 95 | + |
| 96 | + $pmtInf->appendChild($this->el('ChrgBr', $chargeBearer ?: 'SLEV')); |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + public function addBankTransaction( |
| 102 | + string $creditorIBAN, |
| 103 | + string $creditorName, |
| 104 | + ?PostalAddressInterface $postalAddress, |
| 105 | + float $amount, |
| 106 | + string $currency, |
| 107 | + ?string $purposeText = null, |
| 108 | + ?string $endToEndId = null, |
| 109 | + ?string $purposeCode = null |
| 110 | + ): self { |
| 111 | + if ($currency !== 'EUR') { |
| 112 | + throw new InvalidArgumentException('The SEPA transaction is restricted to EUR currency.'); |
| 113 | + } |
| 114 | + |
| 115 | + $tx = $this->createCreditTransferTransactionElement($amount, null, $endToEndId); |
| 116 | + $this->addAmountElement($tx, $amount, $currency); |
| 117 | + $this->addCreditor( |
| 118 | + $tx, |
| 119 | + null, |
| 120 | + $creditorIBAN, |
| 121 | + $creditorName, |
| 122 | + $postalAddress, |
| 123 | + $purposeText, |
| 124 | + $purposeCode |
| 125 | + ); |
| 126 | + |
| 127 | + return $this; |
| 128 | + } |
| 129 | + |
| 130 | + public function addSEPATransaction( |
| 131 | + string $creditorFinInstBIC, |
| 132 | + string $creditorIBAN, |
| 133 | + string $creditorName, |
| 134 | + ?PostalAddressInterface $postalAddress, |
| 135 | + float $amount, |
| 136 | + string $currency, |
| 137 | + ?string $purposeText = null, |
| 138 | + ?string $chargeBearer = null, |
| 139 | + ?string $endToEndId = null, |
| 140 | + ?string $purposeCode = null |
| 141 | + ): self { |
| 142 | + if ($currency !== 'EUR') { |
| 143 | + throw new InvalidArgumentException('The SEPA transaction is restricted to EUR currency.'); |
| 144 | + } |
| 145 | + |
| 146 | + $tx = $this->createCreditTransferTransactionElement($amount, null, $endToEndId); |
| 147 | + |
| 148 | + $this->addAmountElement($tx, $amount, $currency); |
| 149 | + |
| 150 | + if ($chargeBearer !== null) { |
| 151 | + $tx->appendChild($this->el('ChrgBr', $chargeBearer)); |
| 152 | + } |
| 153 | + |
| 154 | + $this->addCreditor( |
| 155 | + $tx, |
| 156 | + $creditorFinInstBIC, |
| 157 | + $creditorIBAN, |
| 158 | + $creditorName, |
| 159 | + $postalAddress, |
| 160 | + $purposeText, |
| 161 | + $purposeCode |
| 162 | + ); |
| 163 | + |
| 164 | + return $this; |
| 165 | + } |
| 166 | + |
| 167 | + public function addForeignTransaction( |
| 168 | + string $creditorFinInstBIC, |
| 169 | + string $creditorIBAN, |
| 170 | + string $creditorName, |
| 171 | + ?PostalAddressInterface $postalAddress, |
| 172 | + float $amount, |
| 173 | + string $currency, |
| 174 | + ?string $purposeText = null, |
| 175 | + ?string $endToEndId = null, |
| 176 | + ?string $purposeCode = null |
| 177 | + ): self { |
| 178 | + $tx = $this->createCreditTransferTransactionElement($amount, null, $endToEndId); |
| 179 | + $this->addAmountElement($tx, $amount, $currency); |
| 180 | + $this->addCreditor( |
| 181 | + $tx, |
| 182 | + $creditorFinInstBIC, |
| 183 | + $creditorIBAN, |
| 184 | + $creditorName, |
| 185 | + $postalAddress, |
| 186 | + $purposeText, |
| 187 | + $purposeCode |
| 188 | + ); |
| 189 | + |
| 190 | + return $this; |
| 191 | + } |
| 192 | +} |
0 commit comments