| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- <?php
- /**
- * This file is part of the Klarna Core module
- *
- * (c) Klarna Bank AB (publ)
- *
- * For the full copyright and license information, please view the NOTICE
- * and LICENSE files that were distributed with this source code.
- */
- namespace Klarna\Core\Helper;
- use Magento\Customer\Api\AddressRepositoryInterface;
- use Magento\Customer\Api\CustomerRepositoryInterface;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\App\Helper\AbstractHelper;
- use Magento\Framework\App\Helper\Context;
- use Magento\Framework\Locale\Resolver;
- use Magento\Store\Api\Data\StoreInterface;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Store\Model\Store;
- /**
- * Class ConfigHelper
- *
- * @package Klarna\Core\Helper
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ConfigHelper extends AbstractHelper
- {
- /**
- * Payment method
- *
- * @var string
- */
- private $code = '';
- /**
- * Observer event prefix
- *
- * @var string
- */
- private $eventPrefix = '';
- /**
- * @var Resolver
- */
- private $resolver;
- /**
- * @var CustomerRepositoryInterface
- */
- private $customerRepository;
- /**
- * @var AddressRepositoryInterface
- */
- private $addressRepository;
- const KP_METHOD_CODE = 'klarna_kp';
- const KCO_METHOD_CODE = 'klarna_kco';
- /**
- * ConfigHelper constructor.
- *
- * @param Context $context
- * @param Resolver $resolver
- * @param CustomerRepositoryInterface $customerRepository
- * @param AddressRepositoryInterface $addressRepository
- * @param string $code
- * @param string $eventPrefix
- */
- public function __construct(
- Context $context,
- Resolver $resolver,
- CustomerRepositoryInterface $customerRepository,
- AddressRepositoryInterface $addressRepository,
- $code = 'klarna_kp',
- $eventPrefix = 'kp'
- ) {
- parent::__construct($context);
- $this->resolver = $resolver;
- $this->code = $code;
- $this->eventPrefix = $eventPrefix;
- $this->customerRepository = $customerRepository;
- $this->addressRepository = $addressRepository;
- }
- /**
- * Get the order status that should be set on orders that have been processed by Klarna
- *
- * @param Store $store
- * @param string $paymentMethod
- *
- * @return string
- */
- public function getProcessedOrderStatus($store = null, $paymentMethod = null)
- {
- return $this->getPaymentConfig('order_status', $store, $paymentMethod);
- }
- /**
- * Get payment config value
- *
- * @param string $config
- * @param Store $store
- * @param string $paymentMethod
- *
- * @return mixed
- */
- public function getPaymentConfig($config, $store = null, $paymentMethod = null)
- {
- if (!$paymentMethod) {
- $paymentMethod = $this->code;
- }
- $scope = $this->getScope($store);
- return $this->scopeConfig->getValue(sprintf('payment/' . $paymentMethod . '/%s', $config), $scope, $store);
- }
- /**
- * Prepare float for API call
- *
- * @param float $float
- *
- * @return int
- * @deprecated 4.0.1
- * @see DataConverter::toApiFloat()
- */
- public function toApiFloat($float)
- {
- return round($float * 100);
- }
- /**
- * Get API config value
- *
- * @param string $config
- * @param Store $store
- *
- * @return mixed
- */
- public function getApiConfig($config, $store = null)
- {
- $scope = $this->getScope($store);
- return $this->scopeConfig->getValue(sprintf('klarna/api/%s', $config), $scope, $store);
- }
- /**
- * Get the current locale code
- *
- * @return string
- */
- public function getLocaleCode()
- {
- return $this->resolver->getLocale();
- }
- /**
- * Get checkout design config value
- *
- * @param Store $store
- *
- * @return mixed
- */
- public function getCheckoutDesignConfig($store = null)
- {
- $scope = $this->getScope($store);
- $designOptions = $this->scopeConfig->getValue('checkout/' . $this->code . '_design', $scope, $store);
- return is_array($designOptions) ? $designOptions : [];
- }
- /**
- * Get base currencey for store
- *
- * @param Store $store
- *
- * @return mixed
- */
- public function getBaseCurrencyCode($store = null)
- {
- $scope = $this->getScope($store);
- return $this->scopeConfig->getValue('currency/options/base', $scope, $store);
- }
- /**
- * Get payment config value
- *
- * @param string $config
- * @param Store $store
- * @param string $paymentMethod
- *
- * @return bool
- */
- public function isPaymentConfigFlag($config, $store = null, $paymentMethod = null)
- {
- if (null === $paymentMethod) {
- $paymentMethod = $this->code;
- }
- $scope = $this->getScope($store);
- return $this->scopeConfig->isSetFlag(sprintf('payment/' . $paymentMethod . '/%s', $config), $scope, $store);
- }
- /**
- * Get API config value
- *
- * @param string $config
- * @param Store $store
- *
- * @return bool
- */
- public function isApiConfigFlag($config, $store = null)
- {
- $scope = $this->getScope($store);
- return $this->scopeConfig->isSetFlag(sprintf('klarna/api/%s', $config), $scope, $store);
- }
- /**
- * check if b2b mode is enabled in setting
- *
- * @param StoreInterface $store
- * @return bool
- */
- public function isB2bEnabled($store = null)
- {
- return $this->isCheckoutConfigFlag('enable_b2b', $store, $this->code);
- }
- /**
- * Get checkout config value
- *
- * @param string $config
- * @param Store $store
- * @param string $paymentMethod
- *
- * @return bool
- */
- public function isCheckoutConfigFlag($config, $store = null, $paymentMethod = null)
- {
- if (null === $paymentMethod) {
- $paymentMethod = $this->code;
- }
- $scope = $this->getScope($store);
- return $this->scopeConfig->isSetFlag(sprintf('checkout/%s/%s', $paymentMethod, $config), $scope, $store);
- }
- /**
- * check if this customer is a business customer
- *
- * @param int $customerId
- * @param Store $store
- * @return bool
- * @throws \Magento\Framework\Exception\LocalizedException
- * @throws \Magento\Framework\Exception\NoSuchEntityException
- */
- public function isB2bCustomer($customerId, $store)
- {
- if ($customerId) {
- $businessIdValue = $this->getBusinessIdAttributeValue($customerId, $store);
- $businessNameValue = $this->getCompanyNameFromAddress($customerId);
- if (!empty($businessIdValue) || !empty($businessNameValue)) {
- return true;
- }
- }
- return false;
- }
- /**
- * get organization id value
- *
- * @param int $customerId
- * @param store $store
- * @return bool|string
- * @throws \Magento\Framework\Exception\LocalizedException
- * @throws \Magento\Framework\Exception\NoSuchEntityException
- */
- public function getBusinessIdAttributeValue($customerId, $store)
- {
- $customerObj = $this->customerRepository->getById($customerId);
- $businessIdValue = $customerObj->getCustomAttribute($this->getBusinessIdAttribute($store));
- if ($businessIdValue) {
- return $businessIdValue->getValue();
- }
- return false;
- }
- /**
- * get the code for custom attribute for recording organization id
- *
- * @param Store $store
- * @return mixed
- */
- public function getBusinessIdAttribute($store = null)
- {
- return $this->getCheckoutConfig('business_id_attribute', $store);
- }
- /**
- * Get checkout config value
- *
- * @param string $config
- * @param Store $store
- *
- * @return mixed
- */
- public function getCheckoutConfig($config, $store = null)
- {
- $scope = $this->getScope($store);
- return $this->scopeConfig->getValue(sprintf('checkout/%s/%s', $this->code, $config), $scope, $store);
- }
- /**
- * check if customer's default billing address contain company name
- *
- * @param int $customerId
- * @return bool|null|string
- * @throws \Magento\Framework\Exception\LocalizedException
- * @throws \Magento\Framework\Exception\NoSuchEntityException
- */
- public function getCompanyNameFromAddress($customerId)
- {
- $customerObj = $this->customerRepository->getById($customerId);
- $billingAddressId = $customerObj->getDefaultBilling();
- if ($billingAddressId) {
- try {
- $defaultBillingAddress = $this->addressRepository->getById($billingAddressId);
- } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
- $this->_logger->info("The default billing address does not exist.");
- return false;
- }
- return $defaultBillingAddress->getCompany();
- }
- return false;
- }
- /**
- * Determine if FPT (Fixed Product Tax) is set to be included in the subtotal
- *
- * @param Store $store
- * @return int
- */
- public function getDisplayInSubtotalFpt($store = null)
- {
- $scope = $this->getScope($store);
- return $this->scopeConfig->getValue('tax/weee/include_in_subtotal', $scope, $store);
- }
- /**
- * Checking if Fixed Product Taxes are enabled
- *
- * @param Store $store
- * @return bool
- */
- public function isFptEnabled($store = null)
- {
- $scope = $this->getScope($store);
- return $this->scopeConfig->getValue('tax/weee/enable', $scope, $store);
- }
- /**
- * Get the scope value of the store
- *
- * @param Store $store
- * @return string
- */
- private function getScope($store = null)
- {
- if ($store === null) {
- return ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
- }
- return ScopeInterface::SCOPE_STORES;
- }
- }
|