Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Vault.php
Go to the documentation of this file.
1 <?php
7 
17 use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
24 
34 class Vault implements VaultPaymentInterface
35 {
39  const TOKEN_METADATA_KEY = 'token_metadata';
40 
44  private static $activeKey = 'active';
45 
49  private static $titleKey = 'title';
50 
54  private $configFactory;
55 
59  private $config;
60 
64  private $vaultProvider;
65 
69  private $objectManager;
70 
74  private $storeId;
75 
79  private $valueHandlerPool;
80 
84  private $eventManager;
85 
89  private $commandManagerPool;
90 
94  private $tokenManagement;
95 
99  private $paymentExtensionFactory;
100 
104  private $code;
105 
122  public function __construct(
123  ConfigInterface $config,
124  ConfigFactoryInterface $configFactory,
125  ObjectManagerInterface $objectManager,
126  MethodInterface $vaultProvider,
127  ManagerInterface $eventManager,
128  ValueHandlerPoolInterface $valueHandlerPool,
129  Command\CommandManagerPoolInterface $commandManagerPool,
130  PaymentTokenManagementInterface $tokenManagement,
131  OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
132  $code
133  ) {
134  $this->config = $config;
135  $this->configFactory = $configFactory;
136  $this->objectManager = $objectManager;
137  $this->valueHandlerPool = $valueHandlerPool;
138  $this->vaultProvider = $vaultProvider;
139  $this->eventManager = $eventManager;
140  $this->commandManagerPool = $commandManagerPool;
141  $this->tokenManagement = $tokenManagement;
142  $this->paymentExtensionFactory = $paymentExtensionFactory;
143  $this->code = $code;
144  }
145 
149  private function getVaultProvider()
150  {
151  return $this->vaultProvider;
152  }
153 
161  private function getConfiguredValue($field, $storeId = null)
162  {
163  $handler = $this->valueHandlerPool->get($field);
164  $subject = ['field' => $field];
165 
166  return $handler->handle($subject, $storeId ?: $this->getStore());
167  }
168 
173  public function getCode()
174  {
175  return $this->code;
176  }
177 
182  public function getFormBlockType()
183  {
184  return Form::class;
185  }
186 
191  public function getTitle()
192  {
193  return $this->getConfiguredValue(self::$titleKey);
194  }
195 
200  public function setStore($storeId)
201  {
202  $this->storeId = (int)$storeId;
203  }
204 
209  public function getStore()
210  {
211  return $this->storeId;
212  }
213 
218  public function canOrder()
219  {
220  return false;
221  }
222 
227  public function canAuthorize()
228  {
229  return $this->getVaultProvider()->canAuthorize()
230  && $this->getVaultProvider()->getConfigData(static::CAN_AUTHORIZE);
231  }
232 
237  public function canCapture()
238  {
239  return $this->getVaultProvider()->canCapture()
240  && $this->getVaultProvider()->getConfigData(static::CAN_CAPTURE);
241  }
242 
247  public function canCapturePartial()
248  {
249  return false;
250  }
251 
256  public function canCaptureOnce()
257  {
258  return $this->getVaultProvider()->canCaptureOnce();
259  }
260 
265  public function canRefund()
266  {
267  return false;
268  }
269 
274  public function canRefundPartialPerInvoice()
275  {
276  return false;
277  }
278 
283  public function canVoid()
284  {
285  return false;
286  }
287 
292  public function canUseInternal()
293  {
294  $isInternalAllowed = $this->getConfiguredValue('can_use_internal');
295  // if config has't been specified for Vault, need to check payment provider option
296  if ($isInternalAllowed === null) {
297  return $this->getVaultProvider()->canUseInternal();
298  }
299  return (bool) $isInternalAllowed;
300  }
301 
306  public function canUseCheckout()
307  {
308  return $this->getVaultProvider()->canUseCheckout();
309  }
310 
315  public function canEdit()
316  {
317  return $this->getVaultProvider()->canEdit();
318  }
319 
324  public function canFetchTransactionInfo()
325  {
326  return false;
327  }
328 
333  public function fetchTransactionInfo(InfoInterface $payment, $transactionId)
334  {
335  throw new \DomainException("Not implemented");
336  }
337 
342  public function isGateway()
343  {
344  return $this->getVaultProvider()->isGateway();
345  }
346 
351  public function isOffline()
352  {
353  return $this->getVaultProvider()->isOffline();
354  }
355 
360  public function isInitializeNeeded()
361  {
362  return $this->getVaultProvider()->isInitializeNeeded();
363  }
364 
369  public function canUseForCountry($country)
370  {
371  return $this->getVaultProvider()->canUseForCountry($country);
372  }
373 
378  public function canUseForCurrency($currencyCode)
379  {
380  return $this->getVaultProvider()->canUseForCurrency($currencyCode);
381  }
382 
387  public function getInfoBlockType()
388  {
389  return $this->getVaultProvider()->getInfoBlockType();
390  }
391 
396  public function getInfoInstance()
397  {
398  return $this->getVaultProvider()->getInfoInstance();
399  }
400 
406  {
407  $this->getVaultProvider()->setInfoInstance($info);
408  }
409 
414  public function validate()
415  {
416  return $this->getVaultProvider()->validate();
417  }
418 
424  {
425  throw new \DomainException("Not implemented");
426  }
427 
432  public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
433  {
434  if (!$payment instanceof OrderPaymentInterface) {
435  throw new \DomainException('Not implemented');
436  }
439  $this->attachTokenExtensionAttribute($payment);
440 
441  $commandExecutor = $this->commandManagerPool->get(
442  $this->getVaultProvider()->getCode()
443  );
444 
445  $commandExecutor->executeByCode(
447  $payment,
448  [
449  'amount' => $amount
450  ]
451  );
452 
453  $payment->setMethod($this->getVaultProvider()->getCode());
454 
455  return $this;
456  }
457 
462  public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
463  {
464  if (!$payment instanceof OrderPaymentInterface) {
465  throw new \DomainException('Not implemented');
466  }
469  if ($payment->getAuthorizationTransaction()) {
470  throw new \DomainException('Capture can not be performed through vault');
471  }
472 
473  $this->attachTokenExtensionAttribute($payment);
474 
475  $commandExecutor = $this->commandManagerPool->get(
476  $this->getVaultProvider()->getCode()
477  );
478 
479  $commandExecutor->executeByCode(
481  $payment,
482  [
483  'amount' => $amount
484  ]
485  );
486 
487  $payment->setMethod($this->getVaultProvider()->getCode());
488  }
489 
494  private function attachTokenExtensionAttribute(OrderPaymentInterface $orderPayment)
495  {
496  $additionalInformation = $orderPayment->getAdditionalInformation();
497  if (empty($additionalInformation[PaymentTokenInterface::PUBLIC_HASH])) {
498  throw new \LogicException('Public hash should be defined');
499  }
500 
501  $customerId = isset($additionalInformation[PaymentTokenInterface::CUSTOMER_ID]) ?
502  $additionalInformation[PaymentTokenInterface::CUSTOMER_ID] : null;
503 
504  $publicHash = $additionalInformation[PaymentTokenInterface::PUBLIC_HASH];
505 
506  $paymentToken = $this->tokenManagement->getByPublicHash($publicHash, $customerId);
507 
508  if ($paymentToken === null) {
509  throw new \LogicException("No token found");
510  }
511 
512  $extensionAttributes = $this->getPaymentExtensionAttributes($orderPayment);
513  $extensionAttributes->setVaultPaymentToken($paymentToken);
514  }
515 
520  private function getPaymentExtensionAttributes(OrderPaymentInterface $payment)
521  {
522  $extensionAttributes = $payment->getExtensionAttributes();
523  if ($extensionAttributes === null) {
524  $extensionAttributes = $this->paymentExtensionFactory->create();
525  $payment->setExtensionAttributes($extensionAttributes);
526  }
527 
528  return $extensionAttributes;
529  }
530 
536  {
537  throw new \DomainException("Not implemented");
538  }
539 
544  public function cancel(\Magento\Payment\Model\InfoInterface $payment)
545  {
546  throw new \DomainException("Not implemented");
547  }
548 
553  public function void(\Magento\Payment\Model\InfoInterface $payment)
554  {
555  throw new \DomainException("Not implemented");
556  }
557 
562  public function canReviewPayment()
563  {
564  throw new \DomainException("Not implemented");
565  }
566 
572  {
573  throw new \DomainException("Not implemented");
574  }
575 
581  {
582  throw new \DomainException("Not implemented");
583  }
584 
589  public function getConfigData($field, $storeId = null)
590  {
591  return $this->getConfiguredValue($field, $storeId);
592  }
593 
598  public function assignData(\Magento\Framework\DataObject $data)
599  {
600  $this->eventManager->dispatch(
601  'payment_method_assign_data_vault',
602  [
606  ]
607  );
608 
609  $this->eventManager->dispatch(
610  'payment_method_assign_data_vault_' . $this->getProviderCode(),
611  [
615  ]
616  );
617 
618  return $this->getVaultProvider()->assignData($data);
619  }
620 
625  public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
626  {
627  return $this->getVaultProvider()->isAvailable($quote)
628  && $this->config->getValue(self::$activeKey, $this->getStore() ?: ($quote ? $quote->getStoreId() : null));
629  }
630 
635  public function isActive($storeId = null)
636  {
637  return $this->getVaultProvider()->isActive($storeId)
638  && $this->config->getValue(self::$activeKey, $this->getStore() ?: $storeId);
639  }
640 
645  public function initialize($paymentAction, $stateObject)
646  {
647  throw new \DomainException("Not implemented");
648  }
649 
654  public function getConfigPaymentAction()
655  {
656  return $this->getVaultProvider()->getConfigPaymentAction();
657  }
658 
663  public function getProviderCode()
664  {
665  return $this->getVaultProvider()->getCode();
666  }
667 }
getConfigData($field, $storeId=null)
Definition: Vault.php:589
fetchTransactionInfo(InfoInterface $payment, $transactionId)
Definition: Vault.php:333
$objectManager
Definition: bootstrap.php:17
$config
Definition: fraud_order.php:17
cancel(\Magento\Payment\Model\InfoInterface $payment)
Definition: Vault.php:544
$quote
refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
Definition: Vault.php:535
order(\Magento\Payment\Model\InfoInterface $payment, $amount)
Definition: Vault.php:423
__construct(ConfigInterface $config, ConfigFactoryInterface $configFactory, ObjectManagerInterface $objectManager, MethodInterface $vaultProvider, ManagerInterface $eventManager, ValueHandlerPoolInterface $valueHandlerPool, Command\CommandManagerPoolInterface $commandManagerPool, PaymentTokenManagementInterface $tokenManagement, OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory, $code)
Definition: Vault.php:122
void(\Magento\Payment\Model\InfoInterface $payment)
Definition: Vault.php:553
$amount
Definition: order.php:14
$payment
Definition: order.php:17
assignData(\Magento\Framework\DataObject $data)
Definition: Vault.php:598
setInfoInstance(InfoInterface $info)
Definition: Vault.php:405
canUseForCurrency($currencyCode)
Definition: Vault.php:378
$extensionAttributes
Definition: payment.php:22
denyPayment(InfoInterface $payment)
Definition: Vault.php:580
$configFactory
Definition: config_data.php:43
isAvailable(\Magento\Quote\Api\Data\CartInterface $quote=null)
Definition: Vault.php:625
initialize($paymentAction, $stateObject)
Definition: Vault.php:645
acceptPayment(InfoInterface $payment)
Definition: Vault.php:571
$paymentExtensionFactory
Definition: payment.php:21
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
catch(\Exception $e) $handler
Definition: index.php:30