KlarnaConfig.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 Klarna\Core\Api\VersionInterface;
  12. use Klarna\Core\Api\VersionInterfaceFactory;
  13. use Klarna\Core\Exception as KlarnaException;
  14. use Magento\Framework\App\Config\ScopeConfigInterface;
  15. use Magento\Framework\App\Helper\AbstractHelper;
  16. use Magento\Framework\App\Helper\Context;
  17. use Magento\Framework\Config\DataInterface;
  18. use Magento\Framework\DataObject;
  19. use Magento\Framework\DataObjectFactory;
  20. use Magento\Store\Model\ScopeInterface;
  21. use Magento\Store\Model\Store;
  22. /**
  23. * Class KlarnaConfig
  24. *
  25. * @package Klarna\Core\Helper
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class KlarnaConfig extends AbstractHelper
  29. {
  30. /**
  31. * Observer event prefix
  32. *
  33. * @var string
  34. */
  35. private $eventPrefix = '';
  36. /**
  37. * Configuration cache for api versions
  38. *
  39. * @var array
  40. */
  41. private $versionConfigCache = [];
  42. /**
  43. * @var DataInterface
  44. */
  45. private $config;
  46. /**
  47. * @var VersionInterfaceFactory
  48. */
  49. private $versionFactory;
  50. /**
  51. * @var DataObjectFactory
  52. */
  53. private $dataObjectFactory;
  54. /**
  55. * ConfigHelper constructor.
  56. *
  57. * @param Context $context
  58. * @param DataInterface $config
  59. * @param VersionInterfaceFactory $versionFactory
  60. * @param DataObjectFactory $dataObjectFactory
  61. * @param string $eventPrefix
  62. */
  63. public function __construct(
  64. Context $context,
  65. DataInterface $config,
  66. VersionInterfaceFactory $versionFactory,
  67. DataObjectFactory $dataObjectFactory,
  68. $eventPrefix = 'kp'
  69. ) {
  70. parent::__construct($context);
  71. $this->config = $config;
  72. $this->eventPrefix = $eventPrefix;
  73. $this->versionFactory = $versionFactory;
  74. $this->dataObjectFactory = $dataObjectFactory;
  75. }
  76. /**
  77. * Get the current checkout api type code
  78. *
  79. * @param Store $store
  80. *
  81. * @return string
  82. * @throws KlarnaException
  83. */
  84. public function getCheckoutType($store = null)
  85. {
  86. return $this->getVersionConfig($store)->getType();
  87. }
  88. /**
  89. * Get configuration parameters for API version
  90. *
  91. * @param string $version
  92. * @return VersionInterface
  93. * @throws KlarnaException
  94. */
  95. public function getVersionConfig($store)
  96. {
  97. $scope = ($store === null ? ScopeConfigInterface::SCOPE_TYPE_DEFAULT : ScopeInterface::SCOPE_STORES);
  98. $version = $this->scopeConfig->getValue('klarna/api/api_version', $scope, $store);
  99. if ($version === null) {
  100. throw new KlarnaException(__('Invalid Api Version: ' . $version));
  101. }
  102. if (!array_key_exists($version, $this->versionConfigCache)) {
  103. $this->versionConfigCache[$version] = $this->getCheckoutVersionDetails($version);
  104. }
  105. return $this->versionConfigCache[$version];
  106. }
  107. /**
  108. * Get api version details
  109. *
  110. * @param string $code
  111. *
  112. * @return VersionInterface
  113. * @throws KlarnaException
  114. */
  115. public function getCheckoutVersionDetails($code)
  116. {
  117. $options = $this->getConfig(sprintf('api_versions/%s', $code));
  118. if ($options === null) {
  119. $options = [];
  120. }
  121. if (!is_array($options)) {
  122. $options = [$options];
  123. }
  124. if (isset($options['options'])) {
  125. $options = array_merge($options, $options['options']);
  126. unset($options['options']);
  127. }
  128. $options['code'] = $code;
  129. $apiTypeConfig = $this->getApiTypeConfig($options['type']);
  130. $apiTypeOptions = $apiTypeConfig->getOptions();
  131. $apiTypeOptions['ordermanagement'] = $apiTypeConfig->getOrdermanagement();
  132. $options = array_merge($apiTypeOptions, $options);
  133. /** @var VersionInterface $optionsObject */
  134. $optionsObject = $this->versionFactory->create(['data' => $options]);
  135. return $this->fireEvent($this->eventPrefix . '_load_version_details', 'options', $optionsObject);
  136. }
  137. /**
  138. * Get Klarna config value for $key
  139. *
  140. * @param $key
  141. * @return mixed
  142. * @throws \RuntimeException
  143. */
  144. private function getConfig($key)
  145. {
  146. return $this->config->get($key);
  147. }
  148. /**
  149. * Get api type configuration
  150. *
  151. * @param string $code
  152. *
  153. * @return DataObject
  154. * @throws KlarnaException
  155. */
  156. public function getApiTypeConfig($code)
  157. {
  158. $typeConfig = $this->getConfig(sprintf('api_types/%s', $code));
  159. if (!$typeConfig) {
  160. throw new KlarnaException(__('Invalid API version selected!'));
  161. }
  162. return $this->fireEvent($this->eventPrefix . '_load_api_config', 'options', $typeConfig);
  163. }
  164. /**
  165. * @param string $eventName
  166. * @param string $objectName
  167. * @param array|\stdClass $objectData
  168. * @return mixed
  169. */
  170. private function fireEvent($eventName, $objectName, $dataObject)
  171. {
  172. if (is_array($dataObject)) {
  173. $dataObject = $this->dataObjectFactory->create(['data' => $dataObject]);
  174. }
  175. $eventData = [
  176. $objectName => $dataObject
  177. ];
  178. $this->_eventManager->dispatch($eventName, $eventData);
  179. return $dataObject;
  180. }
  181. /**
  182. * Get order line times from klarna.xml file
  183. *
  184. * @param string $checkoutType
  185. * @return string[][]
  186. */
  187. public function getOrderlines($checkoutType)
  188. {
  189. return $this->getConfig(sprintf('order_lines/%s', $checkoutType));
  190. }
  191. /**
  192. * Get merchant checkbox method configuration details
  193. *
  194. * @param string $code
  195. *
  196. * @return DataObject
  197. */
  198. public function getMerchantCheckboxMethodConfig($code)
  199. {
  200. $options = $this->getConfig(sprintf('merchant_checkbox/%s', $code));
  201. if ($options === null) {
  202. $options = [];
  203. }
  204. if (!is_array($options)) {
  205. $options = [$options];
  206. }
  207. $options['code'] = $code;
  208. return $this->dataObjectFactory->create(['data' => $options]);
  209. }
  210. /**
  211. * Determine if current store allows shipping callbacks
  212. *
  213. * @param Store $store
  214. *
  215. * @return bool
  216. * @throws KlarnaException
  217. */
  218. public function isShippingCallbackSupport($store = null)
  219. {
  220. return $this->getVersionConfig($store)->isShippingCallbackSupport();
  221. }
  222. /**
  223. * Determine if current store supports the use of the merchant checkbox feature
  224. *
  225. * @param Store $store
  226. *
  227. * @return bool
  228. * @throws KlarnaException
  229. */
  230. public function isMerchantCheckboxSupport($store = null)
  231. {
  232. return $this->getVersionConfig($store)->isMerchantCheckboxSupport();
  233. }
  234. /**
  235. * Determine if current store supports the use of date of birth mandatory
  236. *
  237. * @param Store $store
  238. *
  239. * @return bool
  240. * @throws KlarnaException
  241. */
  242. public function isDateOfBirthMandatorySupport($store = null)
  243. {
  244. return $this->getVersionConfig($store)->isDateOfBirthMandatorySupport();
  245. }
  246. /**
  247. * Determine if current store supports the use of phone mandatory
  248. *
  249. * @param Store $store
  250. *
  251. * @return bool
  252. * @throws KlarnaException
  253. */
  254. public function isPhoneMandatorySupport($store = null)
  255. {
  256. return $this->getVersionConfig($store)->isPhoneMandatorySupport();
  257. }
  258. /**
  259. * Determine if current store supports the use of phone mandatory
  260. *
  261. * @param Store $store
  262. *
  263. * @return string
  264. * @throws KlarnaException
  265. */
  266. public function getOrderMangagementClass($store = null)
  267. {
  268. return $this->getVersionConfig($store)->getOrdermanagement();
  269. }
  270. /**
  271. * Determine if current store supports the use of title mandatory
  272. *
  273. * @param Store $store
  274. *
  275. * @return bool
  276. * @throws KlarnaException
  277. */
  278. public function isTitleMandatorySupport($store = null)
  279. {
  280. return $this->getVersionConfig($store)->isTitleMandatorySupport();
  281. }
  282. /**
  283. * Determine if current store has a delayed push notification from Klarna
  284. *
  285. * @param Store $store
  286. *
  287. * @return bool
  288. * @throws KlarnaException
  289. */
  290. public function isDelayedPushNotification($store = null)
  291. {
  292. return $this->getVersionConfig($store)->isDelayedPushNotification();
  293. }
  294. /**
  295. * Determine if current store supports the use of partial captures and refunds
  296. *
  297. * @param Store $store
  298. *
  299. * @return bool
  300. * @throws KlarnaException
  301. */
  302. public function isPartialPaymentSupport($store = null)
  303. {
  304. return !$this->getVersionConfig($store)->isPartialPaymentDisabled();
  305. }
  306. /**
  307. * Return Builder Type to use in OM requests
  308. *
  309. * @param VersionInterface $versionConfig
  310. * @param string $methodCode
  311. * @return null|string
  312. * @SuppressWarnings(PMD.UnusedFormalParameter)
  313. */
  314. public function getOmBuilderType(VersionInterface $versionConfig, $methodCode = 'klarna_kp')
  315. {
  316. // It is expected that this method will have plugins added by other modules.
  317. // $versionConfig and $methodCode are required in those cases.
  318. return null;
  319. }
  320. /**
  321. * @param $store
  322. * @return bool
  323. * @throws KlarnaException
  324. */
  325. public function isSeparateTaxLine($store)
  326. {
  327. return $this->getVersionConfig($store)->isSeparateTaxLine();
  328. }
  329. /**
  330. * @param $store
  331. * @return bool
  332. * @throws KlarnaException
  333. * @deprecated 4.4.0
  334. * @see \Klarna\Kco\Helper\Checkout::isShippingInIframe
  335. */
  336. public function isShippingInIframe($store)
  337. {
  338. return $this->getVersionConfig($store)->isShippingInIframe();
  339. }
  340. /**
  341. * @param string $code
  342. * @return mixed
  343. */
  344. public function getExternalPaymentOptions($code)
  345. {
  346. return $this->getConfig(sprintf('external_payment_methods/%s', $code));
  347. }
  348. }