Data.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Helper;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Quote\Model\Quote\Item\AbstractItem;
  9. use Magento\Store\Model\Store;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Sales\Api\PaymentFailuresInterface;
  12. /**
  13. * Checkout default helper
  14. *
  15. * @author Magento Core Team <core@magentocommerce.com>
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  19. {
  20. const XML_PATH_GUEST_CHECKOUT = 'checkout/options/guest_checkout';
  21. const XML_PATH_CUSTOMER_MUST_BE_LOGGED = 'checkout/options/customer_must_be_logged';
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface
  24. */
  25. protected $_storeManager;
  26. /**
  27. * @var \Magento\Checkout\Model\Session
  28. */
  29. protected $_checkoutSession;
  30. /**
  31. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  32. */
  33. protected $_localeDate;
  34. /**
  35. * @var \Magento\Framework\Mail\Template\TransportBuilder
  36. */
  37. protected $_transportBuilder;
  38. /**
  39. * @var \Magento\Framework\Translate\Inline\StateInterface
  40. */
  41. protected $inlineTranslation;
  42. /**
  43. * @var PriceCurrencyInterface
  44. */
  45. protected $priceCurrency;
  46. /**
  47. * @var PaymentFailuresInterface
  48. */
  49. private $paymentFailures;
  50. /**
  51. * Data constructor.
  52. *
  53. * @param \Magento\Framework\App\Helper\Context $context
  54. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  55. * @param \Magento\Checkout\Model\Session $checkoutSession
  56. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  57. * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
  58. * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
  59. * @param PriceCurrencyInterface $priceCurrency
  60. * @param PaymentFailuresInterface|null $paymentFailures
  61. * @codeCoverageIgnore
  62. */
  63. public function __construct(
  64. \Magento\Framework\App\Helper\Context $context,
  65. \Magento\Store\Model\StoreManagerInterface $storeManager,
  66. \Magento\Checkout\Model\Session $checkoutSession,
  67. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  68. \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
  69. \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
  70. PriceCurrencyInterface $priceCurrency,
  71. PaymentFailuresInterface $paymentFailures = null
  72. ) {
  73. $this->_storeManager = $storeManager;
  74. $this->_checkoutSession = $checkoutSession;
  75. $this->_localeDate = $localeDate;
  76. $this->_transportBuilder = $transportBuilder;
  77. $this->inlineTranslation = $inlineTranslation;
  78. $this->priceCurrency = $priceCurrency;
  79. $this->paymentFailures = $paymentFailures ? : \Magento\Framework\App\ObjectManager::getInstance()
  80. ->get(PaymentFailuresInterface::class);
  81. parent::__construct($context);
  82. }
  83. /**
  84. * Retrieve checkout session model
  85. *
  86. * @return \Magento\Checkout\Model\Session
  87. * @codeCoverageIgnore
  88. */
  89. public function getCheckout()
  90. {
  91. return $this->_checkoutSession;
  92. }
  93. /**
  94. * Retrieve checkout quote model object
  95. *
  96. * @return \Magento\Quote\Model\Quote
  97. * @codeCoverageIgnore
  98. */
  99. public function getQuote()
  100. {
  101. return $this->getCheckout()->getQuote();
  102. }
  103. /**
  104. * Format Price
  105. *
  106. * @param float $price
  107. * @return string
  108. */
  109. public function formatPrice($price)
  110. {
  111. return $this->priceCurrency->format(
  112. $price,
  113. true,
  114. PriceCurrencyInterface::DEFAULT_PRECISION,
  115. $this->getQuote()->getStore()
  116. );
  117. }
  118. /**
  119. * Convert Price
  120. *
  121. * @param float $price
  122. * @param bool $format
  123. * @return float
  124. */
  125. public function convertPrice($price, $format = true)
  126. {
  127. return $format
  128. ? $this->priceCurrency->convertAndFormat($price)
  129. : $this->priceCurrency->convert($price);
  130. }
  131. /**
  132. * Get onepage checkout availability
  133. *
  134. * @return bool
  135. */
  136. public function canOnepageCheckout()
  137. {
  138. return $this->scopeConfig->isSetFlag(
  139. 'checkout/options/onepage_checkout_enabled',
  140. ScopeInterface::SCOPE_STORE
  141. );
  142. }
  143. /**
  144. * Get sales item (quote item, order item etc) price including tax based on row total and tax amount
  145. *
  146. * @param \Magento\Framework\DataObject $item
  147. * @return float
  148. */
  149. public function getPriceInclTax($item)
  150. {
  151. if ($item->getPriceInclTax()) {
  152. return $item->getPriceInclTax();
  153. }
  154. $qty = $item->getQty() ? $item->getQty() : ($item->getQtyOrdered() ? $item->getQtyOrdered() : 1);
  155. $taxAmount = $item->getTaxAmount() + $item->getDiscountTaxCompensation();
  156. $price = (float)$qty ? ($item->getRowTotal() + $taxAmount) / $qty : 0;
  157. return $this->priceCurrency->round($price);
  158. }
  159. /**
  160. * Get sales item (quote item, order item etc) row total price including tax
  161. *
  162. * @param \Magento\Framework\DataObject $item
  163. * @return float
  164. */
  165. public function getSubtotalInclTax($item)
  166. {
  167. if ($item->getRowTotalInclTax()) {
  168. return $item->getRowTotalInclTax();
  169. }
  170. $tax = $item->getTaxAmount() + $item->getDiscountTaxCompensation();
  171. return $item->getRowTotal() + $tax;
  172. }
  173. /**
  174. * Get Base Price Incl Tax
  175. *
  176. * @param AbstractItem $item
  177. * @return float
  178. */
  179. public function getBasePriceInclTax($item)
  180. {
  181. $qty = $item->getQty() ? $item->getQty() : ($item->getQtyOrdered() ? $item->getQtyOrdered() : 1);
  182. $taxAmount = $item->getBaseTaxAmount() + $item->getBaseDiscountTaxCompensation();
  183. $price = (float)$qty ? ($item->getBaseRowTotal() + $taxAmount) / $qty : 0;
  184. return $this->priceCurrency->round($price);
  185. }
  186. /**
  187. * Get Base Subtotal Incl Tax
  188. *
  189. * @param AbstractItem $item
  190. * @return float
  191. */
  192. public function getBaseSubtotalInclTax($item)
  193. {
  194. $tax = $item->getBaseTaxAmount() + $item->getBaseDiscountTaxCompensation();
  195. return $item->getBaseRowTotal() + $tax;
  196. }
  197. /**
  198. * Send email id payment was failed
  199. *
  200. * @param \Magento\Quote\Model\Quote $checkout
  201. * @param string $message
  202. * @param string $checkoutType
  203. * @return $this
  204. */
  205. public function sendPaymentFailedEmail(
  206. \Magento\Quote\Model\Quote $checkout,
  207. string $message,
  208. string $checkoutType = 'onepage'
  209. ): Data {
  210. $this->paymentFailures->handle((int)$checkout->getId(), $message, $checkoutType);
  211. return $this;
  212. }
  213. /**
  214. * Get Emails
  215. *
  216. * @param string $configPath
  217. * @param null|string|bool|int|Store $storeId
  218. * @return array|false
  219. */
  220. protected function _getEmails($configPath, $storeId)
  221. {
  222. $data = $this->scopeConfig->getValue(
  223. $configPath,
  224. ScopeInterface::SCOPE_STORE,
  225. $storeId
  226. );
  227. if (!empty($data)) {
  228. return explode(',', $data);
  229. }
  230. return false;
  231. }
  232. /**
  233. * Check is allowed Guest Checkout. Use config settings and observer
  234. *
  235. * @param \Magento\Quote\Model\Quote $quote
  236. * @param int|Store $store
  237. * @return bool
  238. */
  239. public function isAllowedGuestCheckout(\Magento\Quote\Model\Quote $quote, $store = null)
  240. {
  241. if ($store === null) {
  242. $store = $quote->getStoreId();
  243. }
  244. $guestCheckout = $this->scopeConfig->isSetFlag(
  245. self::XML_PATH_GUEST_CHECKOUT,
  246. ScopeInterface::SCOPE_STORE,
  247. $store
  248. );
  249. if ($guestCheckout == true) {
  250. $result = new \Magento\Framework\DataObject();
  251. $result->setIsAllowed($guestCheckout);
  252. $this->_eventManager->dispatch(
  253. 'checkout_allow_guest',
  254. ['quote' => $quote, 'store' => $store, 'result' => $result]
  255. );
  256. $guestCheckout = $result->getIsAllowed();
  257. }
  258. return $guestCheckout;
  259. }
  260. /**
  261. * Check if context is checkout
  262. *
  263. * @return bool
  264. * @codeCoverageIgnore
  265. */
  266. public function isContextCheckout()
  267. {
  268. return $this->_request->getParam('context') == 'checkout';
  269. }
  270. /**
  271. * Check if user must be logged during checkout process
  272. *
  273. * @return boolean
  274. * @codeCoverageIgnore
  275. */
  276. public function isCustomerMustBeLogged()
  277. {
  278. return $this->scopeConfig->isSetFlag(
  279. self::XML_PATH_CUSTOMER_MUST_BE_LOGGED,
  280. ScopeInterface::SCOPE_STORE
  281. );
  282. }
  283. /**
  284. * If display billing address on payment method is available, otherwise should be display on payment page
  285. *
  286. * @return bool
  287. */
  288. public function isDisplayBillingOnPaymentMethodAvailable()
  289. {
  290. return (bool) !$this->scopeConfig->getValue(
  291. 'checkout/options/display_billing_address_on',
  292. ScopeInterface::SCOPE_STORE
  293. );
  294. }
  295. }