Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractMethod.php
Go to the documentation of this file.
1 <?php
8 
16 use Magento\Directory\Helper\Data as DirectoryHelper;
17 
33 {
34  const STATUS_UNKNOWN = 'UNKNOWN';
35 
36  const STATUS_APPROVED = 'APPROVED';
37 
38  const STATUS_ERROR = 'ERROR';
39 
40  const STATUS_DECLINED = 'DECLINED';
41 
42  const STATUS_VOID = 'VOID';
43 
44  const STATUS_SUCCESS = 'SUCCESS';
45 
49  protected $_code;
50 
54  protected $_formBlockType = \Magento\Payment\Block\Form::class;
55 
59  protected $_infoBlockType = \Magento\Payment\Block\Info::class;
60 
66  protected $_isGateway = false;
67 
73  protected $_isOffline = false;
74 
80  protected $_canOrder = false;
81 
87  protected $_canAuthorize = false;
88 
94  protected $_canCapture = false;
95 
101  protected $_canCapturePartial = false;
102 
108  protected $_canCaptureOnce = false;
109 
115  protected $_canRefund = false;
116 
122  protected $_canRefundInvoicePartial = false;
123 
129  protected $_canVoid = false;
130 
136  protected $_canUseInternal = true;
137 
143  protected $_canUseCheckout = true;
144 
150  protected $_isInitializeNeeded = false;
151 
157  protected $_canFetchTransactionInfo = false;
158 
164  protected $_canReviewPayment = false;
165 
171  protected $_canCancelInvoice = false;
172 
179 
185  protected $_paymentData;
186 
192  protected $_scopeConfig;
193 
197  protected $logger;
198 
202  private $directory;
203 
218  public function __construct(
219  \Magento\Framework\Model\Context $context,
220  \Magento\Framework\Registry $registry,
221  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
222  \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
223  \Magento\Payment\Helper\Data $paymentData,
224  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
225  \Magento\Payment\Model\Method\Logger $logger,
226  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
227  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
228  array $data = [],
229  DirectoryHelper $directory = null
230  ) {
231  parent::__construct(
232  $context,
233  $registry,
234  $extensionFactory,
236  $resource,
237  $resourceCollection,
238  $data
239  );
240  $this->_paymentData = $paymentData;
241  $this->_scopeConfig = $scopeConfig;
242  $this->logger = $logger;
243  $this->directory = $directory ?: ObjectManager::getInstance()->get(DirectoryHelper::class);
244  $this->initializeData($data);
245  }
246 
253  protected function initializeData($data = [])
254  {
255  if (!empty($data['formBlockType'])) {
256  $this->_formBlockType = $data['formBlockType'];
257  }
258  }
259 
264  public function setStore($storeId)
265  {
266  $this->setData('store', (int)$storeId);
267  }
268 
273  public function getStore()
274  {
275  return $this->getData('store');
276  }
277 
285  public function canOrder()
286  {
287  return $this->_canOrder;
288  }
289 
297  public function canAuthorize()
298  {
299  return $this->_canAuthorize;
300  }
301 
309  public function canCapture()
310  {
311  return $this->_canCapture;
312  }
313 
321  public function canCapturePartial()
322  {
324  }
325 
333  public function canCaptureOnce()
334  {
335  return $this->_canCaptureOnce;
336  }
337 
345  public function canRefund()
346  {
347  return $this->_canRefund;
348  }
349 
357  public function canRefundPartialPerInvoice()
358  {
360  }
361 
369  public function canVoid()
370  {
371  return $this->_canVoid;
372  }
373 
381  public function canUseInternal()
382  {
383  return $this->_canUseInternal;
384  }
385 
392  public function canUseCheckout()
393  {
394  return $this->_canUseCheckout;
395  }
396 
404  public function canEdit()
405  {
406  return true;
407  }
408 
416  public function canFetchTransactionInfo()
417  {
419  }
420 
431  public function fetchTransactionInfo(InfoInterface $payment, $transactionId)
432  {
433  return [];
434  }
435 
443  public function isGateway()
444  {
445  return $this->_isGateway;
446  }
447 
455  public function isOffline()
456  {
457  return $this->_isOffline;
458  }
459 
467  public function isInitializeNeeded()
468  {
470  }
471 
479  public function canUseForCountry($country)
480  {
481  /*
482  for specific country, the flag will set up as 1
483  */
484  if ($this->getConfigData('allowspecific') == 1) {
485  $availableCountries = explode(',', $this->getConfigData('specificcountry'));
486  if (!in_array($country, $availableCountries)) {
487  return false;
488  }
489  }
490  return true;
491  }
492 
501  public function canUseForCurrency($currencyCode)
502  {
503  return true;
504  }
505 
513  public function getCode()
514  {
515  if (empty($this->_code)) {
516  throw new \Magento\Framework\Exception\LocalizedException(
517  __('We cannot retrieve the payment method code.')
518  );
519  }
520  return $this->_code;
521  }
522 
529  public function getFormBlockType()
530  {
531  return $this->_formBlockType;
532  }
533 
541  public function getInfoBlockType()
542  {
543  return $this->_infoBlockType;
544  }
545 
554  public function getInfoInstance()
555  {
556  $instance = $this->getData('info_instance');
557  if (!$instance instanceof InfoInterface) {
558  throw new \Magento\Framework\Exception\LocalizedException(
559  __('We cannot retrieve the payment information object instance.')
560  );
561  }
562  return $instance;
563  }
564 
574  {
575  $this->setData('info_instance', $info);
576  }
577 
586  public function validate()
587  {
591  $paymentInfo = $this->getInfoInstance();
592  if ($paymentInfo instanceof Payment) {
593  $billingCountry = $paymentInfo->getOrder()->getBillingAddress()->getCountryId();
594  } else {
595  $billingCountry = $paymentInfo->getQuote()->getBillingAddress()->getCountryId();
596  }
597  $billingCountry = $billingCountry ?: $this->directory->getDefaultCountry();
598 
599  if (!$this->canUseForCountry($billingCountry)) {
600  throw new \Magento\Framework\Exception\LocalizedException(
601  __('You can\'t use the payment type you selected to make payments to the billing country.')
602  );
603  }
604 
605  return $this;
606  }
607 
620  {
621  if (!$this->canOrder()) {
622  throw new \Magento\Framework\Exception\LocalizedException(__('The order action is not available.'));
623  }
624  return $this;
625  }
626 
639  {
640  if (!$this->canAuthorize()) {
641  throw new \Magento\Framework\Exception\LocalizedException(__('The authorize action is not available.'));
642  }
643  return $this;
644  }
645 
658  {
659  if (!$this->canCapture()) {
660  throw new \Magento\Framework\Exception\LocalizedException(__('The capture action is not available.'));
661  }
662 
663  return $this;
664  }
665 
678  {
679  if (!$this->canRefund()) {
680  throw new \Magento\Framework\Exception\LocalizedException(__('The refund action is not available.'));
681  }
682  return $this;
683  }
684 
694  public function cancel(\Magento\Payment\Model\InfoInterface $payment)
695  {
696  return $this;
697  }
698 
709  public function void(\Magento\Payment\Model\InfoInterface $payment)
710  {
711  if (!$this->canVoid()) {
712  throw new \Magento\Framework\Exception\LocalizedException(__('The void action is not available.'));
713  }
714  return $this;
715  }
716 
723  public function canReviewPayment()
724  {
726  }
727 
739  {
740  if (!$this->canReviewPayment()) {
741  throw new \Magento\Framework\Exception\LocalizedException(__('The payment review action is unavailable.'));
742  }
743  return false;
744  }
745 
757  {
758  if (!$this->canReviewPayment()) {
759  throw new \Magento\Framework\Exception\LocalizedException(__('The payment review action is unavailable.'));
760  }
761  return false;
762  }
763 
770  public function getTitle()
771  {
772  return $this->getConfigData('title');
773  }
774 
784  public function getConfigData($field, $storeId = null)
785  {
786  if ('order_place_redirect_url' === $field) {
787  return $this->getOrderPlaceRedirectUrl();
788  }
789  if (null === $storeId) {
790  $storeId = $this->getStore();
791  }
792  $path = 'payment/' . $this->getCode() . '/' . $field;
793  return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
794  }
795 
805  public function assignData(\Magento\Framework\DataObject $data)
806  {
807  $this->_eventManager->dispatch(
808  'payment_method_assign_data_' . $this->getCode(),
809  [
813  ]
814  );
815 
816  $this->_eventManager->dispatch(
817  'payment_method_assign_data',
818  [
822  ]
823  );
824 
825  return $this;
826  }
827 
835  public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
836  {
837  if (!$this->isActive($quote ? $quote->getStoreId() : null)) {
838  return false;
839  }
840 
841  $checkResult = new DataObject();
842  $checkResult->setData('is_available', true);
843 
844  // for future use in observers
845  $this->_eventManager->dispatch(
846  'payment_method_is_active',
847  [
848  'result' => $checkResult,
849  'method_instance' => $this,
850  'quote' => $quote
851  ]
852  );
853 
854  return $checkResult->getData('is_available');
855  }
856 
864  public function isActive($storeId = null)
865  {
866  return (bool)(int)$this->getConfigData('active', $storeId);
867  }
868 
881  public function initialize($paymentAction, $stateObject)
882  {
883  return $this;
884  }
885 
894  public function getConfigPaymentAction()
895  {
896  return $this->getConfigData('payment_action');
897  }
898 
906  protected function _debug($debugData)
907  {
908  $this->logger->debug(
909  $debugData,
911  $this->getDebugFlag()
912  );
913  }
914 
923  public function getDebugFlag()
924  {
925  return (bool)(int)$this->getConfigData('debug');
926  }
927 
936  public function debugData($debugData)
937  {
938  $this->_debug($debugData);
939  }
940 
948  {
950  }
951 }
cancel(\Magento\Payment\Model\InfoInterface $payment)
authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
order(\Magento\Payment\Model\InfoInterface $payment, $amount)
fetchTransactionInfo(InfoInterface $payment, $transactionId)
$quote
$paymentInfo
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
void(\Magento\Payment\Model\InfoInterface $payment)
$amount
Definition: order.php:14
$payment
Definition: order.php:17
initialize($paymentAction, $stateObject)
isAvailable(\Magento\Quote\Api\Data\CartInterface $quote=null)
refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
assignData(\Magento\Framework\DataObject $data)
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Payment\Helper\Data $paymentData, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Payment\Model\Method\Logger $logger, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], DirectoryHelper $directory=null)