Review.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Express;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Quote\Model\Quote\Address\Rate;
  9. /**
  10. * Paypal Express Onepage checkout block
  11. *
  12. * @api
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Review extends \Magento\Framework\View\Element\Template
  17. {
  18. /**
  19. * @var \Magento\Quote\Model\Quote
  20. */
  21. protected $_quote;
  22. /**
  23. * @var \Magento\Quote\Model\Quote\Address
  24. */
  25. protected $_address;
  26. /**
  27. * @var \Magento\Customer\Model\Address\Config
  28. */
  29. protected $_addressConfig;
  30. /**
  31. * Currently selected shipping rate
  32. *
  33. * @var Rate
  34. */
  35. protected $_currentShippingRate = null;
  36. /**
  37. * Paypal controller path
  38. *
  39. * @var string
  40. */
  41. protected $_controllerPath = 'paypal/express';
  42. /**
  43. * @var \Magento\Tax\Helper\Data
  44. */
  45. protected $_taxHelper;
  46. /**
  47. * @var PriceCurrencyInterface
  48. */
  49. protected $priceCurrency;
  50. /**
  51. * @param \Magento\Framework\View\Element\Template\Context $context
  52. * @param \Magento\Tax\Helper\Data $taxHelper
  53. * @param \Magento\Customer\Model\Address\Config $addressConfig
  54. * @param PriceCurrencyInterface $priceCurrency
  55. * @param array $data
  56. */
  57. public function __construct(
  58. \Magento\Framework\View\Element\Template\Context $context,
  59. \Magento\Tax\Helper\Data $taxHelper,
  60. \Magento\Customer\Model\Address\Config $addressConfig,
  61. PriceCurrencyInterface $priceCurrency,
  62. array $data = []
  63. ) {
  64. $this->priceCurrency = $priceCurrency;
  65. $this->_taxHelper = $taxHelper;
  66. $this->_addressConfig = $addressConfig;
  67. parent::__construct($context, $data);
  68. }
  69. /**
  70. * Quote object setter
  71. *
  72. * @param \Magento\Quote\Model\Quote $quote
  73. * @return $this
  74. */
  75. public function setQuote(\Magento\Quote\Model\Quote $quote)
  76. {
  77. $this->_quote = $quote;
  78. return $this;
  79. }
  80. /**
  81. * Return quote billing address
  82. *
  83. * @return \Magento\Quote\Model\Quote\Address
  84. */
  85. public function getBillingAddress()
  86. {
  87. return $this->_quote->getBillingAddress();
  88. }
  89. /**
  90. * Return quote shipping address
  91. *
  92. * @return false|\Magento\Quote\Model\Quote\Address
  93. */
  94. public function getShippingAddress()
  95. {
  96. if ($this->_quote->getIsVirtual()) {
  97. return false;
  98. }
  99. return $this->_quote->getShippingAddress();
  100. }
  101. /**
  102. * Get HTML output for specified address
  103. *
  104. * @param \Magento\Quote\Model\Quote\Address $address
  105. * @return string
  106. */
  107. public function renderAddress($address)
  108. {
  109. /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
  110. $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
  111. $addressData = \Magento\Framework\Convert\ConvertArray::toFlatArray($address->getData());
  112. return $renderer->renderArray($addressData);
  113. }
  114. /**
  115. * Return carrier name from config, base on carrier code
  116. *
  117. * @param string $carrierCode
  118. * @return string
  119. */
  120. public function getCarrierName($carrierCode)
  121. {
  122. if ($name = $this->_scopeConfig->getValue(
  123. "carriers/{$carrierCode}/title",
  124. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  125. )
  126. ) {
  127. return $name;
  128. }
  129. return $carrierCode;
  130. }
  131. /**
  132. * Get either shipping rate code or empty value on error
  133. *
  134. * @param \Magento\Framework\DataObject $rate
  135. * @return string
  136. */
  137. public function renderShippingRateValue(\Magento\Framework\DataObject $rate)
  138. {
  139. if ($rate->getErrorMessage()) {
  140. return '';
  141. }
  142. return $rate->getCode();
  143. }
  144. /**
  145. * Get shipping rate code title and its price or error message
  146. *
  147. * @param \Magento\Framework\DataObject $rate
  148. * @param string $format
  149. * @param string $inclTaxFormat
  150. * @return string
  151. */
  152. public function renderShippingRateOption($rate, $format = '%s - %s%s', $inclTaxFormat = ' (%s %s)')
  153. {
  154. $renderedInclTax = '';
  155. if ($rate->getErrorMessage()) {
  156. $price = $rate->getErrorMessage();
  157. } else {
  158. $price = $this->_getShippingPrice(
  159. $rate->getPrice(),
  160. $this->_taxHelper->displayShippingPriceIncludingTax()
  161. );
  162. $incl = $this->_getShippingPrice($rate->getPrice(), true);
  163. if ($incl != $price && $this->_taxHelper->displayShippingBothPrices()) {
  164. $renderedInclTax = sprintf($inclTaxFormat, $this->escapeHtml(__('Incl. Tax')), $incl);
  165. }
  166. }
  167. return sprintf($format, $this->escapeHtml($rate->getMethodTitle()), $price, $renderedInclTax);
  168. }
  169. /**
  170. * Getter for current shipping rate
  171. *
  172. * @return Rate
  173. */
  174. public function getCurrentShippingRate()
  175. {
  176. return $this->_currentShippingRate;
  177. }
  178. /**
  179. * Whether can edit shipping method
  180. *
  181. * @return bool
  182. */
  183. public function canEditShippingMethod()
  184. {
  185. return $this->getData('can_edit_shipping_method') || !$this->getCurrentShippingRate();
  186. }
  187. /**
  188. * Get quote email
  189. *
  190. * @return string
  191. */
  192. public function getEmail()
  193. {
  194. $billingAddress = $this->getBillingAddress();
  195. return $billingAddress ? $billingAddress->getEmail() : '';
  196. }
  197. /**
  198. * Set controller path
  199. *
  200. * @param string $prefix
  201. * @return void
  202. */
  203. public function setControllerPath($prefix)
  204. {
  205. $this->_controllerPath = $prefix;
  206. }
  207. /**
  208. * Return formatted shipping price
  209. *
  210. * @param float $price
  211. * @param bool $isInclTax
  212. * @return string
  213. */
  214. protected function _getShippingPrice($price, $isInclTax)
  215. {
  216. return $this->_formatPrice($this->_taxHelper->getShippingPrice($price, $isInclTax, $this->_address));
  217. }
  218. /**
  219. * Format price base on store convert price method
  220. *
  221. * @param float $price
  222. * @return string
  223. */
  224. protected function _formatPrice($price)
  225. {
  226. return $this->priceCurrency->convertAndFormat(
  227. $price,
  228. true,
  229. PriceCurrencyInterface::DEFAULT_PRECISION,
  230. $this->_quote->getStore()
  231. );
  232. }
  233. /**
  234. * Retrieve payment method and assign additional template values
  235. *
  236. * @return $this
  237. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  238. */
  239. protected function _beforeToHtml()
  240. {
  241. $methodInstance = $this->_quote->getPayment()->getMethodInstance();
  242. $this->setPaymentMethodTitle($methodInstance->getTitle());
  243. $this->setShippingRateRequired(true);
  244. if ($this->_quote->getIsVirtual()) {
  245. $this->setShippingRateRequired(false);
  246. } else {
  247. // prepare shipping rates
  248. $this->_address = $this->_quote->getShippingAddress();
  249. $groups = $this->_address->getGroupedAllShippingRates();
  250. if ($groups && $this->_address) {
  251. $this->setShippingRateGroups($groups);
  252. // determine current selected code & name
  253. foreach ($groups as $code => $rates) {
  254. foreach ($rates as $rate) {
  255. if ($this->_address->getShippingMethod() == $rate->getCode()) {
  256. $this->_currentShippingRate = $rate;
  257. break 2;
  258. }
  259. }
  260. }
  261. }
  262. $canEditShippingAddress = $this->_quote->getMayEditShippingAddress() && $this->_quote->getPayment()
  263. ->getAdditionalInformation(\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_BUTTON) == 1;
  264. // misc shipping parameters
  265. $this->setShippingMethodSubmitUrl(
  266. $this->getUrl("{$this->_controllerPath}/saveShippingMethod", ['_secure' => true])
  267. )->setCanEditShippingAddress(
  268. $canEditShippingAddress
  269. )->setCanEditShippingMethod(
  270. $this->_quote->getMayEditShippingMethod()
  271. );
  272. }
  273. $this->setEditUrl(
  274. $this->getUrl("{$this->_controllerPath}/edit")
  275. )->setPlaceOrderUrl(
  276. $this->getUrl("{$this->_controllerPath}/placeOrder", ['_secure' => true])
  277. );
  278. return parent::_beforeToHtml();
  279. }
  280. }