55 */
66namespace Magento \Vault \Model \Method ;
77
8+ use Exception ;
89use Magento \Framework \Event \ManagerInterface ;
910use Magento \Framework \ObjectManagerInterface ;
1011use Magento \Payment \Gateway \Command ;
2122use Magento \Vault \Api \PaymentTokenManagementInterface ;
2223use Magento \Vault \Block \Form ;
2324use Magento \Vault \Model \VaultPaymentInterface ;
25+ use Magento \Framework \Serialize \Serializer \Json ;
2426
2527/**
2628 * Class Vault
@@ -103,6 +105,11 @@ class Vault implements VaultPaymentInterface
103105 */
104106 private $ code ;
105107
108+ /**
109+ * @var Json
110+ */
111+ private $ jsonSerializer ;
112+
106113 /**
107114 * Constructor
108115 *
@@ -116,6 +123,7 @@ class Vault implements VaultPaymentInterface
116123 * @param PaymentTokenManagementInterface $tokenManagement
117124 * @param OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory
118125 * @param string $code
126+ * @param Json|null $jsonSerializer
119127 *
120128 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
121129 */
@@ -129,7 +137,8 @@ public function __construct(
129137 Command \CommandManagerPoolInterface $ commandManagerPool ,
130138 PaymentTokenManagementInterface $ tokenManagement ,
131139 OrderPaymentExtensionInterfaceFactory $ paymentExtensionFactory ,
132- $ code
140+ $ code ,
141+ Json $ jsonSerializer = null
133142 ) {
134143 $ this ->config = $ config ;
135144 $ this ->configFactory = $ configFactory ;
@@ -141,21 +150,14 @@ public function __construct(
141150 $ this ->tokenManagement = $ tokenManagement ;
142151 $ this ->paymentExtensionFactory = $ paymentExtensionFactory ;
143152 $ this ->code = $ code ;
144- }
145-
146- /**
147- * @return MethodInterface
148- */
149- private function getVaultProvider ()
150- {
151- return $ this ->vaultProvider ;
153+ $ this ->jsonSerializer = $ jsonSerializer ?: $ this ->objectManager ->get (Json::class);
152154 }
153155
154156 /**
155157 * Unifies configured value handling logic
156158 *
157159 * @param string $field
158- * @param null $storeId
160+ * @param int| null $storeId
159161 * @return mixed
160162 */
161163 private function getConfiguredValue ($ field , $ storeId = null )
@@ -226,8 +228,8 @@ public function canOrder()
226228 */
227229 public function canAuthorize ()
228230 {
229- return $ this ->getVaultProvider () ->canAuthorize ()
230- && $ this ->getVaultProvider () ->getConfigData (static ::CAN_AUTHORIZE );
231+ return $ this ->vaultProvider ->canAuthorize ()
232+ && $ this ->vaultProvider ->getConfigData (static ::CAN_AUTHORIZE );
231233 }
232234
233235 /**
@@ -236,8 +238,8 @@ public function canAuthorize()
236238 */
237239 public function canCapture ()
238240 {
239- return $ this ->getVaultProvider () ->canCapture ()
240- && $ this ->getVaultProvider () ->getConfigData (static ::CAN_CAPTURE );
241+ return $ this ->vaultProvider ->canCapture ()
242+ && $ this ->vaultProvider ->getConfigData (static ::CAN_CAPTURE );
241243 }
242244
243245 /**
@@ -255,7 +257,7 @@ public function canCapturePartial()
255257 */
256258 public function canCaptureOnce ()
257259 {
258- return $ this ->getVaultProvider () ->canCaptureOnce ();
260+ return $ this ->vaultProvider ->canCaptureOnce ();
259261 }
260262
261263 /**
@@ -294,7 +296,7 @@ public function canUseInternal()
294296 $ isInternalAllowed = $ this ->getConfiguredValue ('can_use_internal ' );
295297 // if config has't been specified for Vault, need to check payment provider option
296298 if ($ isInternalAllowed === null ) {
297- return $ this ->getVaultProvider () ->canUseInternal ();
299+ return $ this ->vaultProvider ->canUseInternal ();
298300 }
299301 return (bool ) $ isInternalAllowed ;
300302 }
@@ -305,7 +307,7 @@ public function canUseInternal()
305307 */
306308 public function canUseCheckout ()
307309 {
308- return $ this ->getVaultProvider () ->canUseCheckout ();
310+ return $ this ->vaultProvider ->canUseCheckout ();
309311 }
310312
311313 /**
@@ -314,7 +316,7 @@ public function canUseCheckout()
314316 */
315317 public function canEdit ()
316318 {
317- return $ this ->getVaultProvider () ->canEdit ();
319+ return $ this ->vaultProvider ->canEdit ();
318320 }
319321
320322 /**
@@ -341,7 +343,7 @@ public function fetchTransactionInfo(InfoInterface $payment, $transactionId)
341343 */
342344 public function isGateway ()
343345 {
344- return $ this ->getVaultProvider () ->isGateway ();
346+ return $ this ->vaultProvider ->isGateway ();
345347 }
346348
347349 /**
@@ -350,7 +352,7 @@ public function isGateway()
350352 */
351353 public function isOffline ()
352354 {
353- return $ this ->getVaultProvider () ->isOffline ();
355+ return $ this ->vaultProvider ->isOffline ();
354356 }
355357
356358 /**
@@ -359,7 +361,7 @@ public function isOffline()
359361 */
360362 public function isInitializeNeeded ()
361363 {
362- return $ this ->getVaultProvider () ->isInitializeNeeded ();
364+ return $ this ->vaultProvider ->isInitializeNeeded ();
363365 }
364366
365367 /**
@@ -368,7 +370,7 @@ public function isInitializeNeeded()
368370 */
369371 public function canUseForCountry ($ country )
370372 {
371- return $ this ->getVaultProvider () ->canUseForCountry ($ country );
373+ return $ this ->vaultProvider ->canUseForCountry ($ country );
372374 }
373375
374376 /**
@@ -377,7 +379,7 @@ public function canUseForCountry($country)
377379 */
378380 public function canUseForCurrency ($ currencyCode )
379381 {
380- return $ this ->getVaultProvider () ->canUseForCurrency ($ currencyCode );
382+ return $ this ->vaultProvider ->canUseForCurrency ($ currencyCode );
381383 }
382384
383385 /**
@@ -386,7 +388,7 @@ public function canUseForCurrency($currencyCode)
386388 */
387389 public function getInfoBlockType ()
388390 {
389- return $ this ->getVaultProvider () ->getInfoBlockType ();
391+ return $ this ->vaultProvider ->getInfoBlockType ();
390392 }
391393
392394 /**
@@ -395,7 +397,7 @@ public function getInfoBlockType()
395397 */
396398 public function getInfoInstance ()
397399 {
398- return $ this ->getVaultProvider () ->getInfoInstance ();
400+ return $ this ->vaultProvider ->getInfoInstance ();
399401 }
400402
401403 /**
@@ -404,7 +406,7 @@ public function getInfoInstance()
404406 */
405407 public function setInfoInstance (InfoInterface $ info )
406408 {
407- $ this ->getVaultProvider () ->setInfoInstance ($ info );
409+ $ this ->vaultProvider ->setInfoInstance ($ info );
408410 }
409411
410412 /**
@@ -413,7 +415,7 @@ public function setInfoInstance(InfoInterface $info)
413415 */
414416 public function validate ()
415417 {
416- return $ this ->getVaultProvider () ->validate ();
418+ return $ this ->vaultProvider ->validate ();
417419 }
418420
419421 /**
@@ -437,9 +439,10 @@ public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount
437439 /** @var $payment OrderPaymentInterface */
438440
439441 $ this ->attachTokenExtensionAttribute ($ payment );
442+ $ this ->attachCreditCardInfo ($ payment );
440443
441444 $ commandExecutor = $ this ->commandManagerPool ->get (
442- $ this ->getVaultProvider () ->getCode ()
445+ $ this ->vaultProvider ->getCode ()
443446 );
444447
445448 $ commandExecutor ->executeByCode (
@@ -450,7 +453,7 @@ public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount
450453 ]
451454 );
452455
453- $ payment ->setMethod ($ this ->getVaultProvider () ->getCode ());
456+ $ payment ->setMethod ($ this ->vaultProvider ->getCode ());
454457
455458 return $ this ;
456459 }
@@ -473,7 +476,7 @@ public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
473476 $ this ->attachTokenExtensionAttribute ($ payment );
474477
475478 $ commandExecutor = $ this ->commandManagerPool ->get (
476- $ this ->getVaultProvider () ->getCode ()
479+ $ this ->vaultProvider ->getCode ()
477480 );
478481
479482 $ commandExecutor ->executeByCode (
@@ -484,10 +487,12 @@ public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
484487 ]
485488 );
486489
487- $ payment ->setMethod ($ this ->getVaultProvider () ->getCode ());
490+ $ payment ->setMethod ($ this ->vaultProvider ->getCode ());
488491 }
489492
490493 /**
494+ * Attaches token extension attribute.
495+ *
491496 * @param OrderPaymentInterface $orderPayment
492497 * @return void
493498 */
@@ -514,6 +519,8 @@ private function attachTokenExtensionAttribute(OrderPaymentInterface $orderPayme
514519 }
515520
516521 /**
522+ * Returns Payment's extension attributes.
523+ *
517524 * @param OrderPaymentInterface $payment
518525 * @return \Magento\Sales\Api\Data\OrderPaymentExtensionInterface
519526 */
@@ -528,6 +535,27 @@ private function getPaymentExtensionAttributes(OrderPaymentInterface $payment)
528535 return $ extensionAttributes ;
529536 }
530537
538+ /**
539+ * Attaches credit card info.
540+ *
541+ * @param OrderPaymentInterface $payment
542+ * @return void
543+ */
544+ private function attachCreditCardInfo (OrderPaymentInterface $ payment ): void
545+ {
546+ try {
547+ $ paymentToken = $ payment ->getExtensionAttributes ()
548+ ->getVaultPaymentToken ();
549+ if ($ paymentToken ) {
550+ $ tokenDetails = $ this ->jsonSerializer ->unserialize ($ paymentToken ->getTokenDetails ());
551+ if ($ tokenDetails && is_array ($ tokenDetails )) {
552+ $ payment ->addData ($ tokenDetails );
553+ }
554+ }
555+ } catch (Exception $ e ) {
556+ }
557+ }
558+
531559 /**
532560 * @inheritdoc
533561 * @since 100.1.0
@@ -615,7 +643,7 @@ public function assignData(\Magento\Framework\DataObject $data)
615643 ]
616644 );
617645
618- return $ this ->getVaultProvider () ->assignData ($ data );
646+ return $ this ->vaultProvider ->assignData ($ data );
619647 }
620648
621649 /**
@@ -624,7 +652,7 @@ public function assignData(\Magento\Framework\DataObject $data)
624652 */
625653 public function isAvailable (\Magento \Quote \Api \Data \CartInterface $ quote = null )
626654 {
627- return $ this ->getVaultProvider () ->isAvailable ($ quote )
655+ return $ this ->vaultProvider ->isAvailable ($ quote )
628656 && $ this ->config ->getValue (self ::$ activeKey , $ this ->getStore () ?: ($ quote ? $ quote ->getStoreId () : null ));
629657 }
630658
@@ -634,7 +662,7 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
634662 */
635663 public function isActive ($ storeId = null )
636664 {
637- return $ this ->getVaultProvider () ->isActive ($ storeId )
665+ return $ this ->vaultProvider ->isActive ($ storeId )
638666 && $ this ->config ->getValue (self ::$ activeKey , $ this ->getStore () ?: $ storeId );
639667 }
640668
@@ -653,7 +681,7 @@ public function initialize($paymentAction, $stateObject)
653681 */
654682 public function getConfigPaymentAction ()
655683 {
656- return $ this ->getVaultProvider () ->getConfigPaymentAction ();
684+ return $ this ->vaultProvider ->getConfigPaymentAction ();
657685 }
658686
659687 /**
@@ -662,6 +690,6 @@ public function getConfigPaymentAction()
662690 */
663691 public function getProviderCode ()
664692 {
665- return $ this ->getVaultProvider () ->getCode ();
693+ return $ this ->vaultProvider ->getCode ();
666694 }
667695}
0 commit comments