Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Payment\Helper;
7 
17 
26 {
27  const XML_PATH_PAYMENT_METHODS = 'payment';
28 
32  protected $_paymentConfig;
33 
39  protected $_layout;
40 
46  protected $_methodFactory;
47 
53  protected $_appEmulation;
54 
58  protected $_initialConfig;
59 
70  public function __construct(
71  \Magento\Framework\App\Helper\Context $context,
72  LayoutFactory $layoutFactory,
73  \Magento\Payment\Model\Method\Factory $paymentMethodFactory,
74  \Magento\Store\Model\App\Emulation $appEmulation,
75  \Magento\Payment\Model\Config $paymentConfig,
76  \Magento\Framework\App\Config\Initial $initialConfig
77  ) {
78  parent::__construct($context);
79  $this->_layout = $layoutFactory->create();
80  $this->_methodFactory = $paymentMethodFactory;
81  $this->_appEmulation = $appEmulation;
82  $this->_paymentConfig = $paymentConfig;
83  $this->_initialConfig = $initialConfig;
84  }
85 
90  protected function getMethodModelConfigName($code)
91  {
92  return sprintf('%s/%s/model', self::XML_PATH_PAYMENT_METHODS, $code);
93  }
94 
103  public function getMethodInstance($code)
104  {
105  $class = $this->scopeConfig->getValue(
108  );
109 
110  if (!$class) {
111  throw new \UnexpectedValueException('Payment model name is not provided in config!');
112  }
113 
114  return $this->_methodFactory->create($class);
115  }
116 
126  public function getStoreMethods($store = null, $quote = null)
127  {
128  $res = [];
129  $methods = $this->getPaymentMethods();
130 
131  foreach (array_keys($methods) as $code) {
132  $model = $this->scopeConfig->getValue(
133  $this->getMethodModelConfigName($code),
135  $store
136  );
137  if (!$model) {
138  continue;
139  }
140 
142  $methodInstance = $this->_methodFactory->create($model);
143  $methodInstance->setStore($store);
144  if (!$methodInstance->isAvailable($quote)) {
145  /* if the payment method cannot be used at this time */
146  continue;
147  }
148  $res[] = $methodInstance;
149  }
150 
151  @uasort(
152  $res,
153  function (MethodInterface $a, MethodInterface $b) {
154  return (int)$a->getConfigData('sort_order') <=> (int)$b->getConfigData('sort_order');
155  }
156  );
157 
158  return $res;
159  }
160 
169  {
170  $block = $layout->createBlock($method->getFormBlockType(), $method->getCode());
171  $block->setMethod($method);
172  return $block;
173  }
174 
182  public function getInfoBlock(InfoInterface $info, LayoutInterface $layout = null)
183  {
184  $layout = $layout ?: $this->_layout;
185  $blockType = $info->getMethodInstance()->getInfoBlockType();
186  $block = $layout->createBlock($blockType);
187  $block->setInfo($info);
188  return $block;
189  }
190 
200  {
201  $this->_appEmulation->startEnvironmentEmulation($storeId);
202 
203  try {
204  // Retrieve specified view block from appropriate design package (depends on emulated store)
205  $paymentBlock = $this->getInfoBlock($info);
206  $paymentBlock->setArea(\Magento\Framework\App\Area::AREA_FRONTEND)
207  ->setIsSecureMode(true);
208  $paymentBlock->getMethod()
209  ->setStore($storeId);
210  $paymentBlockHtml = $paymentBlock->toHtml();
211  } catch (\Exception $exception) {
212  $this->_appEmulation->stopEnvironmentEmulation();
213  throw $exception;
214  }
215 
216  $this->_appEmulation->stopEnvironmentEmulation();
217 
218  return $paymentBlockHtml;
219  }
220 
226  public function getPaymentMethods()
227  {
228  return $this->_initialConfig->getData('default')[self::XML_PATH_PAYMENT_METHODS];
229  }
230 
255  public function getPaymentMethodList($sorted = true, $asLabelValue = false, $withGroups = false, $store = null)
256  {
257  $methods = [];
258  $groups = [];
259  $groupRelations = [];
260 
261  foreach ($this->getPaymentMethods() as $code => $data) {
262  if (isset($data['title'])) {
263  $methods[$code] = $data['title'];
264  } else {
265  $methods[$code] = $this->getMethodInstance($code)->getConfigData('title', $store);
266  }
267  if ($asLabelValue && $withGroups && isset($data['group'])) {
268  $groupRelations[$code] = $data['group'];
269  }
270  }
271  if ($asLabelValue && $withGroups) {
272  $groups = $this->_paymentConfig->getGroups();
273  foreach ($groups as $code => $title) {
274  $methods[$code] = $title;
275  }
276  }
277  if ($sorted) {
278  asort($methods);
279  }
280  if ($asLabelValue) {
281  $labelValues = [];
282  foreach ($methods as $code => $title) {
283  $labelValues[$code] = [];
284  }
285  foreach ($methods as $code => $title) {
286  if (isset($groups[$code])) {
287  $labelValues[$code]['label'] = $title;
288  if (!isset($labelValues[$code]['value'])) {
289  $labelValues[$code]['value'] = null;
290  }
291  } elseif (isset($groupRelations[$code])) {
292  unset($labelValues[$code]);
293  $labelValues[$groupRelations[$code]]['value'][$code] = ['value' => $code, 'label' => $title];
294  } else {
295  $labelValues[$code] = ['value' => $code, 'label' => $title];
296  }
297  }
298  return $labelValues;
299  }
300 
301  return $methods;
302  }
303 
310  public function isZeroSubTotal($store = null)
311  {
312  return $this->scopeConfig->getValue(
313  \Magento\Payment\Model\Method\Free::XML_PATH_PAYMENT_FREE_ACTIVE,
315  $store
316  );
317  }
318 
325  public function getZeroSubTotalOrderStatus($store = null)
326  {
327  return $this->scopeConfig->getValue(
328  \Magento\Payment\Model\Method\Free::XML_PATH_PAYMENT_FREE_ORDER_STATUS,
330  $store
331  );
332  }
333 
341  {
342  return $this->scopeConfig->getValue(
343  \Magento\Payment\Model\Method\Free::XML_PATH_PAYMENT_FREE_PAYMENT_ACTION,
345  $store
346  );
347  }
348 }
$title
Definition: default.phtml:14
getMethodFormBlock(MethodInterface $method, LayoutInterface $layout)
Definition: Data.php:168
getPaymentMethodList($sorted=true, $asLabelValue=false, $withGroups=false, $store=null)
Definition: Data.php:255
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\App\Helper\Context $context, LayoutFactory $layoutFactory, \Magento\Payment\Model\Method\Factory $paymentMethodFactory, \Magento\Store\Model\App\Emulation $appEmulation, \Magento\Payment\Model\Config $paymentConfig, \Magento\Framework\App\Config\Initial $initialConfig)
Definition: Data.php:70
$quote
$block
Definition: block.php:8
createBlock($type, $name='', array $arguments=[])
getZeroSubTotalOrderStatus($store=null)
Definition: Data.php:325
$methods
Definition: billing.phtml:71
$_option $_optionId $class
Definition: date.phtml:13
const XML_PATH_PAYMENT_METHODS
Definition: Data.php:27
getMethodModelConfigName($code)
Definition: Data.php:90
isZeroSubTotal($store=null)
Definition: Data.php:310
getInfoBlockHtml(InfoInterface $info, $storeId)
Definition: Data.php:199
$method
Definition: info.phtml:13
getZeroSubTotalPaymentAutomaticInvoice($store=null)
Definition: Data.php:340
getInfoBlock(InfoInterface $info, LayoutInterface $layout=null)
Definition: Data.php:182
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$code
Definition: info.phtml:12