AmazonConfig.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Core\Model;
  17. use Magento\Store\Model\ScopeInterface;
  18. use Magento\Framework\App\Config\ScopeConfigInterface;
  19. use Magento\Store\Model\StoreManagerInterface;
  20. class AmazonConfig
  21. {
  22. /**
  23. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  24. */
  25. protected $scopeConfig;
  26. /**
  27. * @var StoreManagerInterface
  28. */
  29. private $storeManager;
  30. /**
  31. * Config constructor.
  32. *
  33. * @param StoreManagerInterface $storeManager
  34. * @param ScopeConfigInterface $scopeConfig
  35. */
  36. public function __construct(
  37. StoreManagerInterface $storeManager,
  38. ScopeConfigInterface $scopeConfig
  39. ) {
  40. $this->storeManager = $storeManager;
  41. $this->scopeConfig = $scopeConfig;
  42. }
  43. /**
  44. * Gets customer's current currency
  45. *
  46. * @param null $store
  47. * @return mixed
  48. * @throws \Magento\Framework\Exception\NoSuchEntityException
  49. */
  50. protected function getCurrentCurrencyCode($store = null)
  51. {
  52. return $this->storeManager->getStore()->getCurrentCurrency()->getCode();
  53. }
  54. /**
  55. * @param string $scope
  56. * @param null $scopeCode
  57. *
  58. * @return mixed
  59. */
  60. public function getPaymentRegion($scope = ScopeInterface::SCOPE_STORE, $scopeCode = null)
  61. {
  62. return $this->scopeConfig->getValue(
  63. 'payment/amazon_payment/payment_region',
  64. $scope,
  65. $scopeCode
  66. );
  67. }
  68. /**
  69. * Checks to see if store's selected region is a multicurrency region.
  70. * @param string $scope
  71. * @param null $scopeCode
  72. * @param null $store
  73. * @return bool
  74. */
  75. public function isMulticurrencyRegion($scope = ScopeInterface::SCOPE_STORE, $scopeCode = null, $store = null)
  76. {
  77. $mcRegions = $this->scopeConfig->getValue(
  78. 'multicurrency/regions',
  79. $scope,
  80. $store
  81. );
  82. if ($mcRegions) {
  83. $allowedRegions = explode(',', $mcRegions);
  84. if (in_array($this->getPaymentRegion(), $allowedRegions)) {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. /**
  91. * Check to see if multicurrency is enabled and if it's available for given endpoint/region
  92. *
  93. * @param string $scope
  94. * @param null $scopeCode
  95. * @param null $store
  96. *
  97. * @return bool
  98. */
  99. public function multiCurrencyEnabled($scope = ScopeInterface::SCOPE_STORE, $scopeCode = null, $store = null)
  100. {
  101. $enabled = $this->scopeConfig->getValue(
  102. 'payment/amazon_payment/multicurrency',
  103. $scope,
  104. $scopeCode
  105. );
  106. if ($enabled) {
  107. return $this->isMulticurrencyRegion($scope, $scopeCode, $store);
  108. }
  109. return false;
  110. }
  111. /**
  112. * Only certain currency codes are allowed to be used with multi-currency
  113. *
  114. * @param null $store
  115. * @return bool
  116. */
  117. public function useMultiCurrency($store = null)
  118. {
  119. if ($this->multiCurrencyEnabled()) {
  120. // get allowed presentment currencies from config.xml
  121. $currencies = $this->scopeConfig->getValue(
  122. 'multicurrency/currencies',
  123. ScopeInterface::SCOPE_STORE,
  124. $store
  125. );
  126. if ($currencies) {
  127. $allowedCurrencies = explode(',', $currencies);
  128. if (in_array($this->getCurrentCurrencyCode(), $allowedCurrencies)) {
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. /*
  136. * @return string
  137. */
  138. public function getPresentmentCurrency()
  139. {
  140. return $this->getCurrentCurrencyCode();
  141. }
  142. /**
  143. * Retrieves the base currency of the store.
  144. *
  145. * @param null $store
  146. * @return mixed
  147. */
  148. public function getBaseCurrencyCode($store = null)
  149. {
  150. return $this->scopeConfig->getValue(
  151. 'currency/options/base',
  152. ScopeInterface::SCOPE_STORE,
  153. $store
  154. );
  155. }
  156. }