1111use Fhp \Segment \Common \Kti ;
1212use Fhp \Segment \DME \HIDMESv1 ;
1313use Fhp \Segment \DME \HIDMESv2 ;
14- use Fhp \Segment \DME \HIDXES ;
15- use Fhp \Segment \DME \HKDMEv1 ;
16- use Fhp \Segment \DME \HKDMEv2 ;
1714use Fhp \Segment \DSE \HIDSESv2 ;
18- use Fhp \Segment \DSE \HKDSEv1 ;
19- use Fhp \Segment \DSE \HKDSEv2 ;
15+ use Fhp \Segment \DSE \HIDXES ;
2016use Fhp \Segment \SPA \HISPAS ;
2117use Fhp \Syntax \Bin ;
2218use Fhp \UnsupportedException ;
@@ -44,6 +40,9 @@ class SendSEPADirectDebit extends BaseAction
4440 /** @var bool */
4541 protected $ tryToUseControlSumForSingleTransactions = false ;
4642
43+ /** @var string */
44+ private $ coreType ;
45+
4746 public static function create (SEPAAccount $ account , string $ painMessage , bool $ tryToUseControlSumForSingleTransactions = false ): SendSEPADirectDebit
4847 {
4948 if (preg_match ('/xmlns="(?<namespace>[^"]+)"/s ' , $ painMessage , $ matches ) === 1 ) {
@@ -56,10 +55,16 @@ public static function create(SEPAAccount $account, string $painMessage, bool $t
5655 $ nbOfTxs = substr_count ($ painMessage , '<DrctDbtTxInf> ' );
5756 $ ctrlSum = null ;
5857
59- if (preg_match ('/ <GrpHdr>.*<CtrlSum>(?<ctrlsum>[.0-9]+)<\ /CtrlSum>.*<\ /GrpHdr>/ s ' , $ painMessage , $ matches ) === 1 ) {
58+ if (preg_match ('@ <GrpHdr>.*<CtrlSum>(?<ctrlsum>[.0-9]+)</CtrlSum>.*</GrpHdr>@ s ' , $ painMessage , $ matches ) === 1 ) {
6059 $ ctrlSum = $ matches ['ctrlsum ' ];
6160 }
6261
62+ if (preg_match ('@<PmtTpInf>.*<LclInstrm>.*<Cd>(?<coretype>CORE|COR1|B2B)</Cd>.*</LclInstrm>.*</PmtTpInf>@s ' , $ painMessage , $ matches ) === 1 ) {
63+ $ coreType = $ matches ['coretype ' ];
64+ } else {
65+ throw new \InvalidArgumentException ('The type CORE/COR1/B2B is missing in PAIN message ' );
66+ }
67+
6368 if ($ nbOfTxs > 1 && is_null ($ ctrlSum )) {
6469 throw new \InvalidArgumentException ('The control sum aka "<GrpHdr><CtrlSum>xx</CtrlSum></GrpHdr>" is missing in PAIN message ' );
6570 }
@@ -69,6 +74,7 @@ public static function create(SEPAAccount $account, string $painMessage, bool $t
6974 $ result ->painMessage = $ painMessage ;
7075 $ result ->painNamespace = $ painNamespace ;
7176 $ result ->ctrlSum = $ ctrlSum ;
77+ $ result ->coreType = $ coreType ;
7278
7379 $ result ->singleDirectDebit = $ nbOfTxs === 1 ;
7480
@@ -86,8 +92,8 @@ protected function createRequest(BPD $bpd, ?UPD $upd)
8692 $ useSingleDirectDebit = false ;
8793 }
8894
89- /** @var HIDXES|BaseSegment $hidxes */
90- $ hidxes = $ bpd ->requireLatestSupportedParameters ($ useSingleDirectDebit ? ' HIDSES ' : ' HIDMES ' );
95+ /* @var HIDXES|BaseSegment $hidxes */
96+ $ hidxes = $ bpd ->requireLatestSupportedParameters (GetSEPADirectDebitParameters:: getHixxesSegmentName ( $ this -> coreType , $ useSingleDirectDebit ) );
9197
9298 $ supportedPainNamespaces = null ;
9399
@@ -108,19 +114,8 @@ protected function createRequest(BPD $bpd, ?UPD $upd)
108114 . implode (', ' , $ supportedPainNamespaces ));
109115 }
110116
111- /** @var HKDMEv1|HKDSEv1 $hkdxe */
112- $ hkdxe = null ;
113- switch ($ hidxes ->getVersion ()) {
114- case 1 :
115- $ hkdxe = $ useSingleDirectDebit ? HKDSEv1::createEmpty () : HKDMEv1::createEmpty ();
116- break ;
117- case 2 :
118- $ hkdxe = $ useSingleDirectDebit ? HKDSEv2::createEmpty () : HKDMEv2::createEmpty ();
119- break ;
120- default :
121- throw new UnsupportedException ('Unsupported HKDME or HKDSE version: ' . $ hidxes ->getVersion ());
122- }
123-
117+ /** @var mixed $hkdxe */ // TODO Put a new interface type here.
118+ $ hkdxe = $ hidxes ->createRequestSegment ();
124119 $ hkdxe ->kontoverbindungInternational = Kti::fromAccount ($ this ->account );
125120 $ hkdxe ->sepaDescriptor = $ this ->painNamespace ;
126121 $ hkdxe ->sepaPainMessage = new Bin ($ this ->painMessage );
0 commit comments