@@ -40,7 +40,7 @@ class FinTsNew
40
40
private $ logger ;
41
41
42
42
// The TAN mode and medium to be used for business transactions that require a TAN.
43
- /** @var VerfahrensparameterZweiSchrittVerfahrenV6|null Note that this is a sub-type of {@link TanMode} */
43
+ /** @var VerfahrensparameterZweiSchrittVerfahrenV6|int| null Note that this is a sub-type of {@link TanMode} */
44
44
private $ selectedTanMode ;
45
45
/** @var string|null This is a {@link TanMedium#getName()}, but we don't have the {@link TanMedium} instance. */
46
46
private $ selectedTanMedium ;
@@ -169,7 +169,7 @@ public function login()
169
169
$ this ->requireTanMode ();
170
170
$ this ->ensureSynchronized ();
171
171
$ this ->messageNumber = 1 ;
172
- $ login = new DialogInitialization ($ this ->options , $ this ->credentials , $ this ->selectedTanMode ,
172
+ $ login = new DialogInitialization ($ this ->options , $ this ->credentials , $ this ->getSelectedTanMode () ,
173
173
$ this ->selectedTanMedium , $ this ->kundensystemId );
174
174
$ this ->execute ($ login );
175
175
return $ login ;
@@ -210,7 +210,7 @@ public function execute($action)
210
210
if ($ this ->bpd ->tanRequiredForRequest ($ requestSegments )) {
211
211
$ message ->add (HKTANv6::createProzessvariante2Step1 ($ this ->requireTanMode (), $ this ->selectedTanMedium ));
212
212
}
213
- $ request = $ this ->buildMessage ($ message );
213
+ $ request = $ this ->buildMessage ($ message, $ this -> getSelectedTanMode () );
214
214
$ action ->setRequestSegmentNumbers (array_map (function ($ segment ) {
215
215
/* @var BaseSegment $segment */
216
216
return $ segment ->getSegmentNumber ();
@@ -300,9 +300,10 @@ public function submitTan($action, $tan)
300
300
}
301
301
302
302
// Construct the request.
303
+ $ tanMode = $ this ->requireTanMode ();
303
304
$ message = MessageBuilder::create ()
304
- ->add (HKTANv6::createProzessvariante2Step2 ($ this -> requireTanMode () , $ tanRequest ->getProcessId ()));
305
- $ request = $ this ->buildMessage ($ message , $ tan );
305
+ ->add (HKTANv6::createProzessvariante2Step2 ($ tanMode , $ tanRequest ->getProcessId ()));
306
+ $ request = $ this ->buildMessage ($ message , $ tanMode , $ tan );
306
307
307
308
// Execute the request.
308
309
$ response = $ this ->sendRequestForAction ($ action , $ request );
@@ -390,7 +391,7 @@ public function getTanMedia($tanMode)
390
391
// Execute the GetTanMedia request with the $tanMode swapped in temporarily.
391
392
$ oldTanMode = $ this ->selectedTanMode ;
392
393
$ oldTanMedium = $ this ->selectedTanMedium ;
393
- $ this ->selectedTanMode = $ this -> resolveTanMode ( $ tanMode) ;
394
+ $ this ->selectedTanMode = $ tanMode instanceof TanMode ? $ tanMode -> getId () : $ tanMode ;
394
395
$ this ->selectedTanMedium = '' ;
395
396
try {
396
397
$ this ->executeWeakDialogInitialization ('HKTAB ' );
@@ -408,37 +409,15 @@ public function getTanMedia($tanMode)
408
409
}
409
410
410
411
/**
411
- * Note: While the selected TAN mode is not immediately sent to the bank server by this function, but only used in
412
- * subsequent requests in {@link #execute()}, this function may need to contact the server in order to retrieve the
413
- * BPD and validate the $tanMode.
414
412
* @param TanMode|int $tanMode Either a {@link TanMode} instance obtained from {@link #getTanModes()} or its ID.
415
413
* @param TanMedium|string|null $tanMedium If the $tanMode has {@link TanMode#needsTanMedium()} set to true, this
416
414
* must be the value returned from {@link TanMedium#getName()} for one of the TAN media supported with that TAN
417
415
* mode. Use {@link #getTanMedia()} to obtain a list of possible TAN media options.
418
- * @throws CurlException When the connection fails in a layer below the FinTS protocol.
419
- * @throws ServerException When the server resopnds with an error.
420
416
*/
421
417
public function selectTanMode ($ tanMode , $ tanMedium = null )
422
418
{
423
- $ this ->selectedTanMode = $ this ->resolveTanMode ($ tanMode );
424
- if (!($ this ->selectedTanMode instanceof VerfahrensparameterZweiSchrittVerfahrenV6)) {
425
- throw new UnsupportedException ('Only supports VerfahrensparameterZweiSchrittVerfahrenV6 ' );
426
- }
427
- if ($ this ->selectedTanMode ->tanProzess !== VerfahrensparameterZweiSchrittVerfahrenV6::PROZESSVARIANTE_2 ) {
428
- throw new UnsupportedException ('Only supports Prozessvariante 2 ' );
429
- }
430
-
431
- if ($ this ->selectedTanMode ->needsTanMedium ()) {
432
- if ($ tanMedium === null ) {
433
- throw new \InvalidArgumentException ('tanMedium is mandatory for this tanMode ' );
434
- }
435
- $ this ->selectedTanMedium = is_string ($ tanMedium ) ? $ tanMedium : $ tanMedium ->getName ();
436
- } else {
437
- if ($ tanMedium !== null ) {
438
- throw new \InvalidArgumentException ('tanMedium not allowed for this tanMode ' );
439
- }
440
- $ this ->selectedTanMedium = null ;
441
- }
419
+ $ this ->selectedTanMode = $ tanMode instanceof TanMode ? $ tanMode ->getId () : $ tanMode ;
420
+ $ this ->selectedTanMedium = $ tanMedium instanceof TanMedium ? $ tanMedium ->getName () : $ tanMedium ;
442
421
}
443
422
444
423
// ------------------------------------------------- IMPLEMENTATION ------------------------------------------------
@@ -526,33 +505,54 @@ private function ensureSynchronized()
526
505
}
527
506
528
507
/**
529
- * @param TanMode|int $tanMode A {@link TanMode} or its ID received from the user.
530
- * @return TanMode The corresponding fresh {@link TanMode} from the BPD.
508
+ * If the selected TAN mode was provided as an int, resolves it to a full {@link TanMode} instance, which may
509
+ * involve a request to the server to retrieve the BPD. Then returns it.
510
+ * @return VerfahrensparameterZweiSchrittVerfahrenV6|null The current TAN mode, null if none was selected, never an int.
531
511
* @throws CurlException When the connection fails in a layer below the FinTS protocol.
532
- * @throws ServerException When the server resopnds with an error.
512
+ * @throws ServerException When the server resopnds with an error during the BPD fetch .
533
513
*/
534
- private function resolveTanMode ( $ tanMode )
514
+ private function getSelectedTanMode ( )
535
515
{
536
- if (!is_int ($ tanMode )) {
537
- $ tanMode = $ tanMode ->getId ();
538
- }
539
- $ this ->ensureBpdAvailable ();
540
- if (!array_key_exists ($ tanMode , $ this ->bpd ->allTanModes )) {
541
- throw new \InvalidArgumentException ("Unknown TAN mode: $ tanMode " );
516
+ if (is_int ($ this ->selectedTanMode )) {
517
+ $ this ->ensureBpdAvailable ();
518
+ if (!array_key_exists ($ this ->selectedTanMode , $ this ->bpd ->allTanModes )) {
519
+ throw new \InvalidArgumentException ("Unknown TAN mode: $ this ->selectedTanMode " );
520
+ }
521
+ $ this ->selectedTanMode = $ this ->bpd ->allTanModes [$ this ->selectedTanMode ];
522
+ if (!($ this ->selectedTanMode instanceof VerfahrensparameterZweiSchrittVerfahrenV6)) {
523
+ throw new UnsupportedException ('Only supports VerfahrensparameterZweiSchrittVerfahrenV6 ' );
524
+ }
525
+ if ($ this ->selectedTanMode ->tanProzess !== VerfahrensparameterZweiSchrittVerfahrenV6::PROZESSVARIANTE_2 ) {
526
+ throw new UnsupportedException ('Only supports Prozessvariante 2 ' );
527
+ }
528
+
529
+ if ($ this ->selectedTanMode ->needsTanMedium ()) {
530
+ if ($ this ->selectedTanMedium === null ) {
531
+ throw new \InvalidArgumentException ('tanMedium is mandatory for this tanMode ' );
532
+ }
533
+ } else {
534
+ if ($ this ->selectedTanMedium !== null ) {
535
+ throw new \InvalidArgumentException ('tanMedium not allowed for this tanMode ' );
536
+ }
537
+ }
542
538
}
543
- return $ this ->bpd -> allTanModes [ $ tanMode ] ;
539
+ return $ this ->selectedTanMode ;
544
540
}
545
541
546
542
/**
543
+ * Like {@link #getSelectedTanMode()}, but throws an exception if none was selected.
547
544
* @return VerfahrensparameterZweiSchrittVerfahrenV6 The current TAN mode.
548
545
* @throws \RuntimeException If no TAN mode has been selected.
546
+ * @throws CurlException When the connection fails in a layer below the FinTS protocol.
547
+ * @throws ServerException When the server resopnds with an error during the BPD fetch.
549
548
*/
550
549
private function requireTanMode ()
551
550
{
552
- if ($ this ->selectedTanMode === null ) {
551
+ $ tanMode = $ this ->getSelectedTanMode ();
552
+ if ($ tanMode === null ) {
553
553
throw new \RuntimeException ('selectTanMode() must be called before executing business transactions ' );
554
554
}
555
- return $ this -> selectedTanMode ;
555
+ return $ tanMode ;
556
556
}
557
557
558
558
/**
@@ -645,8 +645,8 @@ private function executeWeakDialogInitialization($hktanRef)
645
645
}
646
646
647
647
$ this ->messageNumber = 1 ;
648
- $ dialogInitialization = new DialogInitialization ($ this ->options , $ this ->credentials , $ this -> selectedTanMode ,
649
- $ this ->selectedTanMedium , $ this ->kundensystemId , $ hktanRef );
648
+ $ dialogInitialization = new DialogInitialization ($ this ->options , $ this ->credentials ,
649
+ $ this ->getSelectedTanMode (), $ this -> selectedTanMedium , $ this ->kundensystemId , $ hktanRef );
650
650
$ this ->execute ($ dialogInitialization );
651
651
try {
652
652
$ dialogInitialization ->ensureSuccess ();
@@ -690,7 +690,9 @@ protected function endDialog($isAnonymous = false)
690
690
try {
691
691
if ($ this ->dialogId !== null ) {
692
692
$ message = MessageBuilder::create ()->add (HKENDv1::create ($ this ->dialogId ));
693
- $ request = $ isAnonymous ? Message::createPlainMessage ($ message ) : $ this ->buildMessage ($ message );
693
+ $ request = $ isAnonymous
694
+ ? Message::createPlainMessage ($ message )
695
+ : $ this ->buildMessage ($ message , $ this ->getSelectedTanMode ());
694
696
$ response = $ this ->sendMessage ($ request );
695
697
if ($ response ->findRueckmeldung (Rueckmeldungscode::BEENDET ) === null ) {
696
698
throw new UnexpectedResponseException (
@@ -715,17 +717,18 @@ protected function endDialog($isAnonymous = false)
715
717
/**
716
718
* Injects FinTsOptions/BPD/UPD/Credentials information into the message.
717
719
* @param MessageBuilder $message The message to be built.
720
+ * @param TanMode|null $tanMode Optionally a TAN mode that will be used when sending this message.
718
721
* @param string|null Optionally a TAN to sign this message with.
719
722
* @return Message The built message.
720
723
*/
721
- private function buildMessage ($ message , $ tan = null )
724
+ private function buildMessage ($ message , $ tanMode = null , $ tan = null )
722
725
{
723
726
return Message::createWrappedMessage (
724
727
$ message ,
725
728
$ this ->options ,
726
729
$ this ->kundensystemId === null ? '0 ' : $ this ->kundensystemId ,
727
730
$ this ->credentials ,
728
- $ this -> selectedTanMode ,
731
+ $ tanMode ,
729
732
$ tan
730
733
);
731
734
}
@@ -741,7 +744,7 @@ private function buildMessage($message, $tan = null)
741
744
private function sendMessage ($ request )
742
745
{
743
746
if ($ request instanceof MessageBuilder) {
744
- $ request = $ this ->buildMessage ($ request );
747
+ $ request = $ this ->buildMessage ($ request, $ this -> getSelectedTanMode () );
745
748
}
746
749
747
750
$ request ->header ->dialogId = $ this ->dialogId === null ? '0 ' : $ this ->dialogId ;
0 commit comments