diff --git a/Block/Adminhtml/Form/Field/ApiKeys.php b/Block/Adminhtml/Form/Field/ApiKeys.php new file mode 100644 index 0000000..460cee6 --- /dev/null +++ b/Block/Adminhtml/Form/Field/ApiKeys.php @@ -0,0 +1,89 @@ +helper=$helper; + } + + /** + * Render form element as HTML + * + * @param AbstractElement $element + * @return string + */ + public function render(AbstractElement $element) + { + + //TODO: check if base url is configured + + $html = $this->getApiKeyInfoPerStore(); + + $r = ' + + + +' . $html . ' + + + + +'; + + + return $r; + + } + + private function getApiKeyInfoPerStore() + { + $isBaseUrlSet = $this->helper->isBtcPayBaseUrlSet(); + + if(!$isBaseUrlSet){ + return __('Save the BTCPay Base Url first.'); + } + + + $html =' + + + + + '; + + + + + $magentoStoreViewsWithApiKeyInfo = $this->helper->getStoreViewsWithApiKeyInfo(); + + foreach ($magentoStoreViewsWithApiKeyInfo as $store=>$info){ + + + $html=$html.' + + + + '; + } + + return $html.'
StoreAPI Key
'.$store.''.$info['api_key'].''. __('Generate API Key') .'
'; + + } + +} diff --git a/Helper/Data.php b/Helper/Data.php index d93da44..65525a4 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -1,20 +1,19 @@ btcPayService = $BTCPayService; $this->logger = $logger; $this->cache = $cache; $this->scopeConfig = $scopeConfig; + $this->storeRepository = $storeRepository; + $this->storeManager = $storeManager; } public function getWebhookSecret(): ?string @@ -67,10 +77,8 @@ class Data } if ($errors === false) { - $secret = $this->btcPayService->getWebhookSecret($magentoStoreId); - $errors = []; $myPermissions = $this->btcPayService->getApiKeyPermissions($magentoStoreId); @@ -79,7 +87,6 @@ class Data $specificStores = []; if ($myPermissions) { - foreach ($myPermissions as $permission) { $parts = explode($permissionsSeparator, $permission); if (count($parts) === 1) { @@ -117,20 +124,16 @@ class Data $btcPayStoreId = $this->btcPayService->getBtcPayStore($magentoStoreId); if ($btcPayStoreId) { - if ($this->checkWebhook($magentoStoreId, true)) { // There are no errors... // TODO check if the store has any actual payment methods we can use. The store may still be misconfigured (i.e. no wallet is configured). To check this, we need a new API call, but we don't have it yet. - } else { $errors[] = __('Could not install the webhook in BTCPay Server for this Magento installation.'); } - } else { $errors[] = __('Please select a BTCPay Server Store to use.'); } - } else { // You either have too many permissions or too few! $missingPermissions = array_diff($neededPermissions, $myPermissions); @@ -172,7 +175,7 @@ class Data if ($autoCreateIfNeeded) { try { //TODO: create webhook -/* $this->btcPayService->createWebhook($magentoStoreId);*/ + /* $this->btcPayService->createWebhook($magentoStoreId);*/ return true; } catch (CannotCreateWebhook $e) { $this->logger->error($e); @@ -206,10 +209,61 @@ class Data // TODO delete the webhook and create a new one with the required data... return false; - } - } + public function getStoreViewsWithApiKeyInfo() + { + $magentoStoreViews = $this->getAllMagentoStoreViews(); + $magentoStoreViewsWithApiKeyInfo = []; + foreach ($magentoStoreViews as $magentoStoreView) { + $storeId = (int)$magentoStoreView->getId(); + $storeName = $magentoStoreView->getName(); + + $apiKey = $this->btcPayService->getApiKey($storeId); + + if (!$apiKey) { + $apiKey = '' . __('No API key generated for this store yet.') . ''; + } + $magentoStoreViewsWithApiKeyInfo[$storeName]['api_key'] = $apiKey; + + $generateUrl = $this->getGenerateApiKeyUrl($storeId); + + $magentoStoreViewsWithApiKeyInfo[$storeName]['generate_url'] = $generateUrl; + } + + return $magentoStoreViewsWithApiKeyInfo; + } + + public function getAllMagentoStoreViews() + { + $stores = $this->storeManager->getStores(); + return $stores; + } + + public function getGenerateApiKeyUrl(int $magentoStoreId) + { + $magentoRootDomain = $this->scopeConfig->getValue('web/secure/base_url', 'store', 0); + $magentoRootDomain = parse_url($magentoRootDomain, PHP_URL_HOST); + $magentoRootDomain = str_replace(['http://', 'https://'], '', $magentoRootDomain); + $magentoRootDomain = rtrim($magentoRootDomain, '/'); + + $redirectToUrlAfterCreation = $this->btcPayService->getReceiveApikeyUrl($magentoStoreId); + + $applicationIdentifier = 'magento2'; + $baseUrl = $this->btcPayService->getBtcPayServerBaseUrl($magentoStoreId); + + $authorizeUrl = \BTCPayServer\Client\ApiKey::getAuthorizeUrl($baseUrl, \Storefront\BTCPay\Helper\Data::REQUIRED_API_PERMISSIONS, 'Magento 2 @ ' . $magentoRootDomain, true, true, $redirectToUrlAfterCreation, $applicationIdentifier); + + return $authorizeUrl; + } + + public function isBtcPayBaseUrlSet():bool + { + if ($this->btcPayService->getBtcPayServerBaseUrl(0)) { + return true; + } + return false; + } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 8457e3f..3da30c3 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -29,11 +29,18 @@ This secret is used by the BTCPay Server Webhook. No need to worry about it. Webhooks are installed automatically. + + + Storefront\BTCPay\Block\Adminhtml\Form\Field\ApiKeys + + + +