show all api keys, generate btn on website level
This commit is contained in:
parent
e0766419b3
commit
cbdb424102
89
Block/Adminhtml/Form/Field/ApiKeys.php
Normal file
89
Block/Adminhtml/Form/Field/ApiKeys.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Storefront\BTCPay\Block\Adminhtml\Form\Field;
|
||||
|
||||
|
||||
use Magento\Backend\Block\Template\Context;
|
||||
use Magento\Framework\Data\Form\Element\AbstractElement;
|
||||
use Magento\Framework\View\Helper\SecureHtmlRenderer;
|
||||
use Storefront\BTCPay\Helper\Data;
|
||||
|
||||
class ApiKeys extends \Magento\Config\Block\System\Config\Form\Field
|
||||
{
|
||||
/**
|
||||
* @var Data $helper
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
public function __construct(Context $context, Data $helper, array $data = [], ?SecureHtmlRenderer $secureRenderer = null)
|
||||
{
|
||||
parent::__construct($context, $data, $secureRenderer);
|
||||
$this->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 = '<tr>
|
||||
<td class="label">
|
||||
<label><span>' . $this->escapeHtml(__('Api Keys')) . '</span></label>
|
||||
</td>
|
||||
<td class="value">' . $html . '
|
||||
</td>
|
||||
<td class="">
|
||||
|
||||
</td>
|
||||
</tr>';
|
||||
|
||||
|
||||
return $r;
|
||||
|
||||
}
|
||||
|
||||
private function getApiKeyInfoPerStore()
|
||||
{
|
||||
$isBaseUrlSet = $this->helper->isBtcPayBaseUrlSet();
|
||||
|
||||
if(!$isBaseUrlSet){
|
||||
return __('Save the BTCPay Base Url first.');
|
||||
}
|
||||
|
||||
|
||||
$html ='<table>
|
||||
<tr>
|
||||
<th style="text-align: left; width: 60px">Store</th>
|
||||
<th style="text-align: left">API Key</th>
|
||||
<th></th>
|
||||
</tr>';
|
||||
|
||||
|
||||
|
||||
|
||||
$magentoStoreViewsWithApiKeyInfo = $this->helper->getStoreViewsWithApiKeyInfo();
|
||||
|
||||
foreach ($magentoStoreViewsWithApiKeyInfo as $store=>$info){
|
||||
|
||||
|
||||
$html=$html.'<tr>
|
||||
<td>'.$store.'</td>
|
||||
<td>'.$info['api_key'].'</td>
|
||||
<td><a target="_blank" href="'.$info['generate_url'].'\">'. __('Generate API Key') .'</a></td>
|
||||
</tr>';
|
||||
}
|
||||
|
||||
return $html.'</table>';
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,20 +1,19 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Storefront\BTCPay\Helper;
|
||||
|
||||
|
||||
use Magento\Framework\App\Config;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Api\StoreRepositoryInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Storefront\BTCPay\Model\BTCPay\Exception\CannotCreateWebhook;
|
||||
use Storefront\BTCPay\Model\BTCPay\Exception\ForbiddenException;
|
||||
|
||||
class Data
|
||||
{
|
||||
|
||||
const REQUIRED_API_PERMISSIONS = [
|
||||
'btcpay.store.canviewinvoices',
|
||||
'btcpay.store.cancreateinvoice',
|
||||
@ -22,7 +21,6 @@ class Data
|
||||
'btcpay.store.canviewstoresettings'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @var \Storefront\BTCPay\Model\BTCPay\BTCPayService
|
||||
*/
|
||||
@ -42,12 +40,24 @@ class Data
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
public function __construct(\Storefront\BTCPay\Model\BTCPay\BTCPayService $BTCPayService, ScopeConfigInterface $scopeConfig, \Magento\Framework\App\CacheInterface $cache, LoggerInterface $logger)
|
||||
/**
|
||||
* @var StoreRepositoryInterface $storeRepository
|
||||
*/
|
||||
private $storeRepository;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface $storeManager
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
public function __construct(\Storefront\BTCPay\Model\BTCPay\BTCPayService $BTCPayService, ScopeConfigInterface $scopeConfig, \Magento\Framework\App\CacheInterface $cache, LoggerInterface $logger, StoreRepositoryInterface $storeRepository, StoreManagerInterface $storeManager)
|
||||
{
|
||||
$this->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 = '<span style="color: red">' . __('No API key generated for this store yet.') . '</span>';
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,11 +29,18 @@
|
||||
<comment>This secret is used by the BTCPay Server Webhook. No need to worry about it. Webhooks are installed automatically.</comment>
|
||||
</field>
|
||||
|
||||
<field id="api_keys" translate="label" sortOrder="57" showInDefault="1" showInWebsite="0" showInStore="0">
|
||||
<label>API Keys</label>
|
||||
<frontend_model>Storefront\BTCPay\Block\Adminhtml\Form\Field\ApiKeys</frontend_model>
|
||||
</field>
|
||||
|
||||
<!--
|
||||
<field id="api_key" translate="label" type="text" sortOrder="60" showInDefault="0"
|
||||
showInWebsite="0" showInStore="1">
|
||||
showInWebsite="0" showInStore="0">
|
||||
<label>API Key</label>
|
||||
<comment model="Storefront\BTCPay\Model\Config\ApiKeyComment" />
|
||||
</field>
|
||||
</field>-->
|
||||
|
||||
|
||||
<field id="btcpay_store_id" translate="label" type="select" sortOrder="70" showInDefault="0"
|
||||
showInWebsite="0" showInStore="1">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user