Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Adapter.php
Go to the documentation of this file.
1 <?php
8 
22 use Psr\Log\LoggerInterface;
23 
33 class Adapter implements MethodInterface
34 {
38  private $valueHandlerPool;
39 
43  private $validatorPool;
44 
48  private $commandPool;
49 
53  private $storeId;
54 
58  private $formBlockType;
59 
63  private $infoBlockType;
64 
68  private $infoInstance;
69 
73  private $code;
74 
78  private $eventManager;
79 
83  private $paymentDataObjectFactory;
84 
88  private $commandExecutor;
89 
95  private $logger;
96 
110  public function __construct(
111  ManagerInterface $eventManager,
112  ValueHandlerPoolInterface $valueHandlerPool,
113  PaymentDataObjectFactory $paymentDataObjectFactory,
114  $code,
115  $formBlockType,
116  $infoBlockType,
117  CommandPoolInterface $commandPool = null,
118  ValidatorPoolInterface $validatorPool = null,
119  CommandManagerInterface $commandExecutor = null,
120  LoggerInterface $logger = null
121  ) {
122  $this->valueHandlerPool = $valueHandlerPool;
123  $this->validatorPool = $validatorPool;
124  $this->commandPool = $commandPool;
125  $this->code = $code;
126  $this->infoBlockType = $infoBlockType;
127  $this->formBlockType = $formBlockType;
128  $this->eventManager = $eventManager;
129  $this->paymentDataObjectFactory = $paymentDataObjectFactory;
130  $this->commandExecutor = $commandExecutor;
131  $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
132  }
133 
140  public function getValidatorPool()
141  {
142  if ($this->validatorPool === null) {
143  throw new \DomainException('Validator pool is not configured for use.');
144  }
145  return $this->validatorPool;
146  }
147 
151  public function canOrder()
152  {
153  return $this->canPerformCommand('order');
154  }
155 
159  public function canAuthorize()
160  {
161  return $this->canPerformCommand('authorize');
162  }
163 
167  public function canCapture()
168  {
169  return $this->canPerformCommand('capture');
170  }
171 
175  public function canCapturePartial()
176  {
177  return $this->canPerformCommand('capture_partial');
178  }
179 
183  public function canCaptureOnce()
184  {
185  return $this->canPerformCommand('capture_once');
186  }
187 
191  public function canRefund()
192  {
193  return $this->canPerformCommand('refund');
194  }
195 
199  public function canRefundPartialPerInvoice()
200  {
201  return $this->canPerformCommand('refund_partial_per_invoice');
202  }
203 
207  public function canVoid()
208  {
209  return $this->canPerformCommand('void');
210  }
211 
215  public function canUseInternal()
216  {
217  return (bool)$this->getConfiguredValue('can_use_internal');
218  }
219 
223  public function canUseCheckout()
224  {
225  return (bool)$this->getConfiguredValue('can_use_checkout');
226  }
227 
231  public function canEdit()
232  {
233  return (bool)$this->getConfiguredValue('can_edit');
234  }
235 
239  public function canFetchTransactionInfo()
240  {
241  return $this->canPerformCommand('fetch_transaction_info');
242  }
243 
247  public function canReviewPayment()
248  {
249  return $this->canPerformCommand('review_payment');
250  }
251 
255  public function isGateway()
256  {
257  return (bool)$this->getConfiguredValue('is_gateway');
258  }
259 
263  public function isOffline()
264  {
265  return (bool)$this->getConfiguredValue('is_offline');
266  }
267 
271  public function isInitializeNeeded()
272  {
273  return (bool)(int)$this->getConfiguredValue('can_initialize');
274  }
275 
279  public function isAvailable(CartInterface $quote = null)
280  {
281  if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
282  return false;
283  }
284 
285  $checkResult = new DataObject();
286  $checkResult->setData('is_available', true);
287  try {
288  $infoInstance = $this->getInfoInstance();
289  if ($infoInstance !== null) {
290  $validator = $this->getValidatorPool()->get('availability');
291  $result = $validator->validate(
292  [
293  'payment' => $this->paymentDataObjectFactory->create($infoInstance)
294  ]
295  );
296 
297  $checkResult->setData('is_available', $result->isValid());
298  }
299  } catch (\Exception $e) {
300  // pass
301  }
302 
303  // for future use in observers
304  $this->eventManager->dispatch(
305  'payment_method_is_active',
306  [
307  'result' => $checkResult,
308  'method_instance' => $this,
309  'quote' => $quote
310  ]
311  );
312 
313  return $checkResult->getData('is_available');
314  }
315 
319  public function isActive($storeId = null)
320  {
321  return (bool)$this->getConfiguredValue('active', $storeId);
322  }
323 
327  public function canUseForCountry($country)
328  {
329  try {
330  $validator = $this->getValidatorPool()->get('country');
331  } catch (\Exception $e) {
332  return true;
333  }
334 
335  $result = $validator->validate(['country' => $country, 'storeId' => $this->getStore()]);
336  return $result->isValid();
337  }
338 
342  public function canUseForCurrency($currencyCode)
343  {
344  try {
345  $validator = $this->getValidatorPool()->get('currency');
346  } catch (\Exception $e) {
347  return true;
348  }
349 
350  $result = $validator->validate(['currency' => $currencyCode, 'storeId' => $this->getStore()]);
351  return $result->isValid();
352  }
353 
360  private function canPerformCommand($commandCode)
361  {
362  return (bool)$this->getConfiguredValue('can_' . $commandCode);
363  }
364 
372  private function getConfiguredValue($field, $storeId = null)
373  {
374  $handler = $this->valueHandlerPool->get($field);
375  $subject = [
376  'field' => $field
377  ];
378 
379  if ($this->getInfoInstance()) {
380  $subject['payment'] = $this->paymentDataObjectFactory->create($this->getInfoInstance());
381  }
382 
383  return $handler->handle($subject, $storeId ?: $this->getStore());
384  }
385 
389  public function getConfigData($field, $storeId = null)
390  {
391  return $this->getConfiguredValue($field, $storeId);
392  }
393 
397  public function validate()
398  {
399  try {
400  $validator = $this->getValidatorPool()->get('global');
401  } catch (\Exception $e) {
402  return $this;
403  }
404 
405  $result = $validator->validate(
406  ['payment' => $this->getInfoInstance(), 'storeId' => $this->getStore()]
407  );
408 
409  if (!$result->isValid()) {
410  throw new LocalizedException(
411  __(implode("\n", $result->getFailsDescription()))
412  );
413  }
414 
415  return $this;
416  }
417 
421  public function fetchTransactionInfo(InfoInterface $payment, $transactionId)
422  {
423  return $this->executeCommand(
424  'fetch_transaction_information',
425  ['payment' => $payment, 'transactionId' => $transactionId]
426  );
427  }
428 
433  {
434  $this->executeCommand(
435  'order',
436  ['payment' => $payment, 'amount' => $amount]
437  );
438 
439  return $this;
440  }
441 
446  {
447  $this->executeCommand(
448  'authorize',
449  ['payment' => $payment, 'amount' => $amount]
450  );
451 
452  return $this;
453  }
454 
459  {
460  $this->executeCommand(
461  'capture',
462  ['payment' => $payment, 'amount' => $amount]
463  );
464 
465  return $this;
466  }
467 
472  {
473  $this->executeCommand(
474  'refund',
475  ['payment' => $payment, 'amount' => $amount]
476  );
477 
478  return $this;
479  }
480 
484  public function cancel(InfoInterface $payment)
485  {
486  $this->executeCommand('cancel', ['payment' => $payment]);
487 
488  return $this;
489  }
490 
494  public function void(InfoInterface $payment)
495  {
496  $this->executeCommand('void', ['payment' => $payment]);
497 
498  return $this;
499  }
500 
505  {
506  $this->executeCommand('accept_payment', ['payment' => $payment]);
507 
508  return $this;
509  }
510 
515  {
516  $this->executeCommand('deny_payment', ['payment' => $payment]);
517 
518  return $this;
519  }
520 
524  private function executeCommand($commandCode, array $arguments = [])
525  {
526  if (!$this->canPerformCommand($commandCode)) {
527  return null;
528  }
529 
531  $payment = null;
532  if (isset($arguments['payment']) && $arguments['payment'] instanceof InfoInterface) {
533  $payment = $arguments['payment'];
534  $arguments['payment'] = $this->paymentDataObjectFactory->create($arguments['payment']);
535  }
536 
537  if ($this->commandExecutor !== null) {
538  return $this->commandExecutor->executeByCode($commandCode, $payment, $arguments);
539  }
540 
541  if ($this->commandPool === null) {
542  throw new \DomainException("The command pool isn't configured for use.");
543  }
544 
545  $command = $this->commandPool->get($commandCode);
546 
547  return $command->execute($arguments);
548  }
549 
553  public function getCode()
554  {
555  return $this->code;
556  }
557 
561  public function getTitle()
562  {
563  return $this->getConfiguredValue('title');
564  }
565 
569  public function setStore($storeId)
570  {
571  $this->storeId = (int)$storeId;
572  }
573 
577  public function getStore()
578  {
579  return $this->storeId;
580  }
581 
585  public function getFormBlockType()
586  {
587  return $this->formBlockType;
588  }
589 
593  public function getInfoBlockType()
594  {
595  return $this->infoBlockType;
596  }
597 
601  public function getInfoInstance()
602  {
603  return $this->infoInstance;
604  }
605 
610  {
611  $this->infoInstance = $info;
612  }
613 
619  public function assignData(\Magento\Framework\DataObject $data)
620  {
621  $this->eventManager->dispatch(
622  'payment_method_assign_data_' . $this->getCode(),
623  [
627  ]
628  );
629 
630  $this->eventManager->dispatch(
631  'payment_method_assign_data',
632  [
636  ]
637  );
638 
639  return $this;
640  }
641 
646  public function initialize($paymentAction, $stateObject)
647  {
648  $this->executeCommand(
649  'initialize',
650  [
651  'payment' => $this->getInfoInstance(),
652  'paymentAction' => $paymentAction,
653  'stateObject' => $stateObject
654  ]
655  );
656  return $this;
657  }
658 
662  public function getConfigPaymentAction()
663  {
664  return $this->getConfiguredValue('payment_action');
665  }
666 }
acceptPayment(InfoInterface $payment)
Definition: Adapter.php:504
getConfigData($field, $storeId=null)
Definition: Adapter.php:389
capture(InfoInterface $payment, $amount)
Definition: Adapter.php:458
void(InfoInterface $payment)
Definition: Adapter.php:494
$quote
cancel(InfoInterface $payment)
Definition: Adapter.php:484
__()
Definition: __.php:13
isAvailable(CartInterface $quote=null)
Definition: Adapter.php:279
setInfoInstance(InfoInterface $info)
Definition: Adapter.php:609
initialize($paymentAction, $stateObject)
Definition: Adapter.php:646
denyPayment(InfoInterface $payment)
Definition: Adapter.php:514
$amount
Definition: order.php:14
$payment
Definition: order.php:17
__construct(ManagerInterface $eventManager, ValueHandlerPoolInterface $valueHandlerPool, PaymentDataObjectFactory $paymentDataObjectFactory, $code, $formBlockType, $infoBlockType, CommandPoolInterface $commandPool=null, ValidatorPoolInterface $validatorPool=null, CommandManagerInterface $commandExecutor=null, LoggerInterface $logger=null)
Definition: Adapter.php:110
$arguments
fetchTransactionInfo(InfoInterface $payment, $transactionId)
Definition: Adapter.php:421
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
order(InfoInterface $payment, $amount)
Definition: Adapter.php:432
authorize(InfoInterface $payment, $amount)
Definition: Adapter.php:445
refund(InfoInterface $payment, $amount)
Definition: Adapter.php:471
catch(\Exception $e) $handler
Definition: index.php:30
assignData(\Magento\Framework\DataObject $data)
Definition: Adapter.php:619