ConfigHelper.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * This file is part of the Klarna Core module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Core\Helper;
  11. use Magento\Customer\Api\AddressRepositoryInterface;
  12. use Magento\Customer\Api\CustomerRepositoryInterface;
  13. use Magento\Framework\App\Config\ScopeConfigInterface;
  14. use Magento\Framework\App\Helper\AbstractHelper;
  15. use Magento\Framework\App\Helper\Context;
  16. use Magento\Framework\Locale\Resolver;
  17. use Magento\Store\Api\Data\StoreInterface;
  18. use Magento\Store\Model\ScopeInterface;
  19. use Magento\Store\Model\Store;
  20. /**
  21. * Class ConfigHelper
  22. *
  23. * @package Klarna\Core\Helper
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class ConfigHelper extends AbstractHelper
  27. {
  28. /**
  29. * Payment method
  30. *
  31. * @var string
  32. */
  33. private $code = '';
  34. /**
  35. * Observer event prefix
  36. *
  37. * @var string
  38. */
  39. private $eventPrefix = '';
  40. /**
  41. * @var Resolver
  42. */
  43. private $resolver;
  44. /**
  45. * @var CustomerRepositoryInterface
  46. */
  47. private $customerRepository;
  48. /**
  49. * @var AddressRepositoryInterface
  50. */
  51. private $addressRepository;
  52. const KP_METHOD_CODE = 'klarna_kp';
  53. const KCO_METHOD_CODE = 'klarna_kco';
  54. /**
  55. * ConfigHelper constructor.
  56. *
  57. * @param Context $context
  58. * @param Resolver $resolver
  59. * @param CustomerRepositoryInterface $customerRepository
  60. * @param AddressRepositoryInterface $addressRepository
  61. * @param string $code
  62. * @param string $eventPrefix
  63. */
  64. public function __construct(
  65. Context $context,
  66. Resolver $resolver,
  67. CustomerRepositoryInterface $customerRepository,
  68. AddressRepositoryInterface $addressRepository,
  69. $code = 'klarna_kp',
  70. $eventPrefix = 'kp'
  71. ) {
  72. parent::__construct($context);
  73. $this->resolver = $resolver;
  74. $this->code = $code;
  75. $this->eventPrefix = $eventPrefix;
  76. $this->customerRepository = $customerRepository;
  77. $this->addressRepository = $addressRepository;
  78. }
  79. /**
  80. * Get the order status that should be set on orders that have been processed by Klarna
  81. *
  82. * @param Store $store
  83. * @param string $paymentMethod
  84. *
  85. * @return string
  86. */
  87. public function getProcessedOrderStatus($store = null, $paymentMethod = null)
  88. {
  89. return $this->getPaymentConfig('order_status', $store, $paymentMethod);
  90. }
  91. /**
  92. * Get payment config value
  93. *
  94. * @param string $config
  95. * @param Store $store
  96. * @param string $paymentMethod
  97. *
  98. * @return mixed
  99. */
  100. public function getPaymentConfig($config, $store = null, $paymentMethod = null)
  101. {
  102. if (!$paymentMethod) {
  103. $paymentMethod = $this->code;
  104. }
  105. $scope = $this->getScope($store);
  106. return $this->scopeConfig->getValue(sprintf('payment/' . $paymentMethod . '/%s', $config), $scope, $store);
  107. }
  108. /**
  109. * Prepare float for API call
  110. *
  111. * @param float $float
  112. *
  113. * @return int
  114. * @deprecated 4.0.1
  115. * @see DataConverter::toApiFloat()
  116. */
  117. public function toApiFloat($float)
  118. {
  119. return round($float * 100);
  120. }
  121. /**
  122. * Get API config value
  123. *
  124. * @param string $config
  125. * @param Store $store
  126. *
  127. * @return mixed
  128. */
  129. public function getApiConfig($config, $store = null)
  130. {
  131. $scope = $this->getScope($store);
  132. return $this->scopeConfig->getValue(sprintf('klarna/api/%s', $config), $scope, $store);
  133. }
  134. /**
  135. * Get the current locale code
  136. *
  137. * @return string
  138. */
  139. public function getLocaleCode()
  140. {
  141. return $this->resolver->getLocale();
  142. }
  143. /**
  144. * Get checkout design config value
  145. *
  146. * @param Store $store
  147. *
  148. * @return mixed
  149. */
  150. public function getCheckoutDesignConfig($store = null)
  151. {
  152. $scope = $this->getScope($store);
  153. $designOptions = $this->scopeConfig->getValue('checkout/' . $this->code . '_design', $scope, $store);
  154. return is_array($designOptions) ? $designOptions : [];
  155. }
  156. /**
  157. * Get base currencey for store
  158. *
  159. * @param Store $store
  160. *
  161. * @return mixed
  162. */
  163. public function getBaseCurrencyCode($store = null)
  164. {
  165. $scope = $this->getScope($store);
  166. return $this->scopeConfig->getValue('currency/options/base', $scope, $store);
  167. }
  168. /**
  169. * Get payment config value
  170. *
  171. * @param string $config
  172. * @param Store $store
  173. * @param string $paymentMethod
  174. *
  175. * @return bool
  176. */
  177. public function isPaymentConfigFlag($config, $store = null, $paymentMethod = null)
  178. {
  179. if (null === $paymentMethod) {
  180. $paymentMethod = $this->code;
  181. }
  182. $scope = $this->getScope($store);
  183. return $this->scopeConfig->isSetFlag(sprintf('payment/' . $paymentMethod . '/%s', $config), $scope, $store);
  184. }
  185. /**
  186. * Get API config value
  187. *
  188. * @param string $config
  189. * @param Store $store
  190. *
  191. * @return bool
  192. */
  193. public function isApiConfigFlag($config, $store = null)
  194. {
  195. $scope = $this->getScope($store);
  196. return $this->scopeConfig->isSetFlag(sprintf('klarna/api/%s', $config), $scope, $store);
  197. }
  198. /**
  199. * check if b2b mode is enabled in setting
  200. *
  201. * @param StoreInterface $store
  202. * @return bool
  203. */
  204. public function isB2bEnabled($store = null)
  205. {
  206. return $this->isCheckoutConfigFlag('enable_b2b', $store, $this->code);
  207. }
  208. /**
  209. * Get checkout config value
  210. *
  211. * @param string $config
  212. * @param Store $store
  213. * @param string $paymentMethod
  214. *
  215. * @return bool
  216. */
  217. public function isCheckoutConfigFlag($config, $store = null, $paymentMethod = null)
  218. {
  219. if (null === $paymentMethod) {
  220. $paymentMethod = $this->code;
  221. }
  222. $scope = $this->getScope($store);
  223. return $this->scopeConfig->isSetFlag(sprintf('checkout/%s/%s', $paymentMethod, $config), $scope, $store);
  224. }
  225. /**
  226. * check if this customer is a business customer
  227. *
  228. * @param int $customerId
  229. * @param Store $store
  230. * @return bool
  231. * @throws \Magento\Framework\Exception\LocalizedException
  232. * @throws \Magento\Framework\Exception\NoSuchEntityException
  233. */
  234. public function isB2bCustomer($customerId, $store)
  235. {
  236. if ($customerId) {
  237. $businessIdValue = $this->getBusinessIdAttributeValue($customerId, $store);
  238. $businessNameValue = $this->getCompanyNameFromAddress($customerId);
  239. if (!empty($businessIdValue) || !empty($businessNameValue)) {
  240. return true;
  241. }
  242. }
  243. return false;
  244. }
  245. /**
  246. * get organization id value
  247. *
  248. * @param int $customerId
  249. * @param store $store
  250. * @return bool|string
  251. * @throws \Magento\Framework\Exception\LocalizedException
  252. * @throws \Magento\Framework\Exception\NoSuchEntityException
  253. */
  254. public function getBusinessIdAttributeValue($customerId, $store)
  255. {
  256. $customerObj = $this->customerRepository->getById($customerId);
  257. $businessIdValue = $customerObj->getCustomAttribute($this->getBusinessIdAttribute($store));
  258. if ($businessIdValue) {
  259. return $businessIdValue->getValue();
  260. }
  261. return false;
  262. }
  263. /**
  264. * get the code for custom attribute for recording organization id
  265. *
  266. * @param Store $store
  267. * @return mixed
  268. */
  269. public function getBusinessIdAttribute($store = null)
  270. {
  271. return $this->getCheckoutConfig('business_id_attribute', $store);
  272. }
  273. /**
  274. * Get checkout config value
  275. *
  276. * @param string $config
  277. * @param Store $store
  278. *
  279. * @return mixed
  280. */
  281. public function getCheckoutConfig($config, $store = null)
  282. {
  283. $scope = $this->getScope($store);
  284. return $this->scopeConfig->getValue(sprintf('checkout/%s/%s', $this->code, $config), $scope, $store);
  285. }
  286. /**
  287. * check if customer's default billing address contain company name
  288. *
  289. * @param int $customerId
  290. * @return bool|null|string
  291. * @throws \Magento\Framework\Exception\LocalizedException
  292. * @throws \Magento\Framework\Exception\NoSuchEntityException
  293. */
  294. public function getCompanyNameFromAddress($customerId)
  295. {
  296. $customerObj = $this->customerRepository->getById($customerId);
  297. $billingAddressId = $customerObj->getDefaultBilling();
  298. if ($billingAddressId) {
  299. try {
  300. $defaultBillingAddress = $this->addressRepository->getById($billingAddressId);
  301. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  302. $this->_logger->info("The default billing address does not exist.");
  303. return false;
  304. }
  305. return $defaultBillingAddress->getCompany();
  306. }
  307. return false;
  308. }
  309. /**
  310. * Determine if FPT (Fixed Product Tax) is set to be included in the subtotal
  311. *
  312. * @param Store $store
  313. * @return int
  314. */
  315. public function getDisplayInSubtotalFpt($store = null)
  316. {
  317. $scope = $this->getScope($store);
  318. return $this->scopeConfig->getValue('tax/weee/include_in_subtotal', $scope, $store);
  319. }
  320. /**
  321. * Checking if Fixed Product Taxes are enabled
  322. *
  323. * @param Store $store
  324. * @return bool
  325. */
  326. public function isFptEnabled($store = null)
  327. {
  328. $scope = $this->getScope($store);
  329. return $this->scopeConfig->getValue('tax/weee/enable', $scope, $store);
  330. }
  331. /**
  332. * Get the scope value of the store
  333. *
  334. * @param Store $store
  335. * @return string
  336. */
  337. private function getScope($store = null)
  338. {
  339. if ($store === null) {
  340. return ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  341. }
  342. return ScopeInterface::SCOPE_STORES;
  343. }
  344. }