Info.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Block;
  7. /**
  8. * Base payment information block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Info extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Payment rendered specific information
  17. *
  18. * @var \Magento\Framework\DataObject
  19. */
  20. protected $_paymentSpecificInformation;
  21. /**
  22. * @var string
  23. */
  24. protected $_template = 'Magento_Payment::info/default.phtml';
  25. /**
  26. * Retrieve info model
  27. *
  28. * @return \Magento\Payment\Model\InfoInterface
  29. * @throws \Magento\Framework\Exception\LocalizedException
  30. */
  31. public function getInfo()
  32. {
  33. $info = $this->getData('info');
  34. if (!$info instanceof \Magento\Payment\Model\InfoInterface) {
  35. throw new \Magento\Framework\Exception\LocalizedException(
  36. __('We cannot retrieve the payment info model object.')
  37. );
  38. }
  39. return $info;
  40. }
  41. /**
  42. * Retrieve payment method model
  43. *
  44. * @return \Magento\Payment\Model\MethodInterface
  45. */
  46. public function getMethod()
  47. {
  48. return $this->getInfo()->getMethodInstance();
  49. }
  50. /**
  51. * Render as PDF
  52. * @return string
  53. */
  54. public function toPdf()
  55. {
  56. $this->setTemplate('Magento_Payment::info/pdf/default.phtml');
  57. return $this->toHtml();
  58. }
  59. /**
  60. * Getter for children PDF, as array. Analogue of $this->getChildHtml()
  61. *
  62. * Children must have toPdf() callable
  63. * Known issue: not sorted
  64. * @return array
  65. */
  66. public function getChildPdfAsArray()
  67. {
  68. $result = [];
  69. foreach ($this->getLayout()->getChildBlocks($this->getNameInLayout()) as $child) {
  70. if (method_exists($child, 'toPdf') && is_callable([$child, 'toPdf'])) {
  71. $result[] = $child->toPdf();
  72. }
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Get some specific information in format of array($label => $value)
  78. *
  79. * @return array
  80. */
  81. public function getSpecificInformation()
  82. {
  83. return $this->_prepareSpecificInformation()->getData();
  84. }
  85. /**
  86. * Render the value as an array
  87. *
  88. * @param mixed $value
  89. * @param bool $escapeHtml
  90. * @return array
  91. */
  92. public function getValueAsArray($value, $escapeHtml = false)
  93. {
  94. if (empty($value)) {
  95. return [];
  96. }
  97. if (!is_array($value)) {
  98. $value = [$value];
  99. }
  100. if ($escapeHtml) {
  101. foreach ($value as $_key => $_val) {
  102. $value[$_key] = $this->escapeHtml($_val);
  103. }
  104. }
  105. return $value;
  106. }
  107. /**
  108. * Check whether payment information should show up in secure mode
  109. * true => only "public" payment information may be shown
  110. * false => full information may be shown
  111. *
  112. * @return bool
  113. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  114. */
  115. public function getIsSecureMode()
  116. {
  117. if ($this->hasIsSecureMode()) {
  118. return (bool)(int)$this->_getData('is_secure_mode');
  119. }
  120. $method = $this->getMethod();
  121. if (!$method) {
  122. return true;
  123. }
  124. $store = $method->getStore();
  125. if (!$store) {
  126. return false;
  127. }
  128. $methodStore = $this->_storeManager->getStore($store);
  129. return $methodStore->getCode() != \Magento\Store\Model\Store::ADMIN_CODE;
  130. }
  131. /**
  132. * Prepare information specific to current payment method
  133. *
  134. * @param null|\Magento\Framework\DataObject|array $transport
  135. * @return \Magento\Framework\DataObject
  136. */
  137. protected function _prepareSpecificInformation($transport = null)
  138. {
  139. if (null === $this->_paymentSpecificInformation) {
  140. if (null === $transport) {
  141. $transport = new \Magento\Framework\DataObject();
  142. } elseif (is_array($transport)) {
  143. $transport = new \Magento\Framework\DataObject($transport);
  144. }
  145. $this->_paymentSpecificInformation = $transport;
  146. }
  147. return $this->_paymentSpecificInformation;
  148. }
  149. }