Onboarding.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * This file is part of the Klarna KP 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\Model\System;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\App\ProductMetadata;
  13. use Magento\Framework\App\Request\Http;
  14. use Magento\Store\Model\ScopeInterface;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. /**
  17. * Class Onboarding
  18. *
  19. * @package Klarna\Core\Model\System
  20. */
  21. class Onboarding
  22. {
  23. /** @var ProductMetadata $productMetaData */
  24. private $productMetaData;
  25. /** @var ScopeConfigInterface $scopeConfig */
  26. private $scopeConfig;
  27. /** @var Http $http */
  28. private $http;
  29. /** @var StoreManagerInterface $storeManager */
  30. private $storeManager;
  31. /**
  32. * Onboarding Constructor
  33. *
  34. * @param ProductMetadata $productMetadata
  35. * @param ScopeConfigInterface $scopeConfig
  36. * @param Http $http
  37. * @param StoreManagerInterface $storeManager
  38. */
  39. public function __construct(
  40. ProductMetadata $productMetadata,
  41. ScopeConfigInterface $scopeConfig,
  42. Http $http,
  43. StoreManagerInterface $storeManager
  44. ) {
  45. $this->productMetaData = $productMetadata;
  46. $this->scopeConfig = $scopeConfig;
  47. $this->http = $http;
  48. $this->storeManager = $storeManager;
  49. }
  50. /**
  51. * Get Onboarding URL
  52. *
  53. * @param string $moduleKey
  54. * @param string $moduleVersion
  55. * @return string
  56. * @throws \Magento\Framework\Exception\LocalizedException
  57. */
  58. public function getUrl($moduleKey, $moduleVersion)
  59. {
  60. $platform = 'magento';
  61. $platformVersion = $this->productMetaData->getVersion();
  62. $queryParameter = sprintf(
  63. '?plugin=%s&pluginVersion=%s&platform=%s&platformVersion=%sproducts=%s',
  64. $moduleKey,
  65. $moduleVersion,
  66. $platform,
  67. $platformVersion,
  68. $moduleKey
  69. );
  70. $websiteId = $this->http->getParam('website', 0);
  71. $website = $this->storeManager->getWebsite($websiteId);
  72. $scope = $this->getScope($website);
  73. $country = $this->scopeConfig->getValue('general/store_information/country_id', $scope, $website);
  74. if (!empty($country)) {
  75. $queryParameter .= '&country=' . $country;
  76. }
  77. $url = 'https://eu.portal.klarna.com/signup' . $queryParameter;
  78. if ($country === 'US') {
  79. $url = 'https://us.portal.klarna.com/signup' . $queryParameter;
  80. }
  81. return $url;
  82. }
  83. /**
  84. * Return either website scope or default scope depending on value of $website
  85. *
  86. * @param \Magento\Store\Api\Data\WebsiteInterface $website
  87. * @return string
  88. */
  89. private function getScope($website = null)
  90. {
  91. if ($website === null) {
  92. return ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  93. }
  94. return ScopeInterface::SCOPE_WEBSITES;
  95. }
  96. }