Skip to content

Commit ce233e0

Browse files
author
Ivan Tagil
authored
Merge pull request #24 from Itonomy/feature/CMP-41_42_configure-api-credentials-and-connectivity
CMP-41, CMP-42 - Configure Api Credentials and Connectivity
2 parents 1b19a4f + de246ed commit ce233e0

File tree

18 files changed

+677
-57
lines changed

18 files changed

+677
-57
lines changed

Api/Config/ConfigInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ interface ConfigInterface
1717
*/
1818
public const XML_PATH_GENERAL_ENABLED = 'cm_payments/general/enabled';
1919
public const XML_PATH_GENERAL_CURRENT_VERSION = 'cm_payments/general/current_version';
20-
public const XML_PATH_GENERAL_MERCHANT_KEY = 'cm_payments/general/merchant_key';
21-
public const XML_PATH_GENERAL_MERCHANT_NAME = 'cm_payments/general/merchant_name';
22-
public const XML_PATH_GENERAL_MERCHANT_PASSWORD = 'cm_payments/general/merchant_password';
20+
public const XML_PATH_GENERAL_TEST_MERCHANT_NAME = 'cm_payments/general/test_merchant_name';
21+
public const XML_PATH_GENERAL_TEST_MERCHANT_PASSWORD = 'cm_payments/general/test_merchant_password';
22+
public const XML_PATH_GENERAL_TEST_MERCHANT_KEY = 'cm_payments/general/test_merchant_key';
23+
public const XML_PATH_GENERAL_LIVE_MERCHANT_NAME = 'cm_payments/general/live_merchant_name';
24+
public const XML_PATH_GENERAL_LIVE_MERCHANT_PASSWORD = 'cm_payments/general/live_merchant_password';
25+
public const XML_PATH_GENERAL_LIVE_MERCHANT_KEY = 'cm_payments/general/live_merchant_key';
2326
public const XML_PATH_GENERAL_MODE = 'cm_payments/general/mode';
2427
public const XML_PATH_PAYMENT_PROFILE = 'payment/cm_payments_methods/profile';
2528
public const XML_PATH_PAYMENT_CREDIT_CARD_PROFILE = 'payment/cm_payments_creditcard/profile';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © CM.com. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace CM\Payments\Api\Service;
10+
11+
use GuzzleHttp\Exception\GuzzleException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
14+
interface ApiTestServiceInterface
15+
{
16+
/**
17+
* @return array
18+
* @throws GuzzleException|NoSuchEntityException
19+
*/
20+
public function testApiConnection(): array;
21+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © CM.com. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace CM\Payments\Block\Adminhtml\System\Config\Button;
10+
11+
use CM\Payments\Api\Config\ConfigInterface;
12+
use Exception;
13+
use Magento\Backend\Block\Template\Context;
14+
use Magento\Backend\Block\Widget\Button;
15+
use Magento\Config\Block\System\Config\Form\Field;
16+
use Magento\Framework\Data\Form\Element\AbstractElement;
17+
use Magento\Framework\Exception\NoSuchEntityException;
18+
19+
class CheckApiConnection extends Field
20+
{
21+
/**
22+
* @var string
23+
*/
24+
protected $_template = 'CM_Payments::system/config/button/check_api_connection.phtml';
25+
26+
/**
27+
* @var ConfigInterface
28+
*/
29+
private $config;
30+
31+
/**
32+
* CheckLatestVersion constructor
33+
*
34+
* @param Context $context
35+
* @param ConfigInterface $config
36+
* @param array $data
37+
*/
38+
public function __construct(
39+
Context $context,
40+
ConfigInterface $config,
41+
array $data = []
42+
) {
43+
parent::__construct($context, $data);
44+
45+
$this->config = $config;
46+
}
47+
48+
/**
49+
* @return ?string
50+
* @throws NoSuchEntityException
51+
*/
52+
public function getCurrentVersion(): ?string
53+
{
54+
return $this->config->getCurrentVersion();
55+
}
56+
57+
/**
58+
* @param AbstractElement $element
59+
* @return string
60+
*/
61+
public function render(AbstractElement $element): string
62+
{
63+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
64+
65+
return parent::render($element);
66+
}
67+
68+
/**
69+
* @param AbstractElement $element
70+
* @return string
71+
*/
72+
public function _getElementHtml(AbstractElement $element): string
73+
{
74+
return $this->_toHtml();
75+
}
76+
77+
/**
78+
* @return string
79+
*/
80+
public function getApiConnectionCheckUrl(): string
81+
{
82+
return $this->getUrl('cmpayments/action/checkApiConnection');
83+
}
84+
85+
/**
86+
* @return string
87+
*/
88+
public function getButtonHtml(): string
89+
{
90+
$buttonData = [
91+
'class' => 'cmpayments_button_check_api_connection',
92+
'label' => __('Check Api Connection'),
93+
'data_attribute' => ['bind' => 'click: checkApiConnection']
94+
];
95+
try {
96+
$button = $this->getLayout()->createBlock(
97+
Button::class
98+
)->setData($buttonData);
99+
100+
return $button->toHtml();
101+
} catch (Exception $e) {
102+
return '';
103+
}
104+
}
105+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © CM.com. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace CM\Payments\Block\Adminhtml\System\Config\Fieldset;
10+
11+
use Magento\Config\Block\System\Config\Form\Fieldset;
12+
use Magento\Framework\Data\Form\Element\AbstractElement;
13+
14+
class ApiDetails extends Fieldset
15+
{
16+
/**
17+
* Return header html for fieldset
18+
*
19+
* @param AbstractElement $element
20+
* @return string
21+
*/
22+
protected function _getHeaderHtml($element)
23+
{
24+
return parent::_getHeaderHtml($element) .
25+
'<div class="api-details-extra" ' .
26+
'data-mage-init=\'{"CM_Payments/js/system/action/api-details-config":' .
27+
'{"modeContainerSelector": "#cm_payments_general_api_details_mode"}}\'></div>';
28+
}
29+
}

Client/ApiClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
namespace CM\Payments\Client;
1010

11-
use CM\Payments\Client\Api\ApiClientInterface;
1211
use CM\Payments\Api\Config\ConfigInterface;
12+
use CM\Payments\Client\Api\ApiClientInterface;
1313
use CM\Payments\Client\Api\RequestInterface;
14-
use CM\Payments\Model\AdminHtml\Source\Mode;
14+
use CM\Payments\Model\Adminhtml\Source\Mode;
1515
use GuzzleHttp\Client as HttpClient;
1616
use GuzzleHttp\Exception\GuzzleException;
1717
use Magento\Framework\Exception\NoSuchEntityException;
@@ -50,8 +50,7 @@ public function __construct(
5050
*
5151
* @param RequestInterface $request
5252
* @return array
53-
*
54-
* @throws GuzzleException
53+
* @throws GuzzleException|NoSuchEntityException
5554
*/
5655
public function execute(RequestInterface $request): array
5756
{
@@ -72,6 +71,7 @@ public function execute(RequestInterface $request): array
7271

7372
/**
7473
* @return HttpClient
74+
* @throws NoSuchEntityException
7575
*/
7676
private function getClient(): HttpClient
7777
{
@@ -99,7 +99,7 @@ private function getClient(): HttpClient
9999
*/
100100
private function getBaseApiUrl(): string
101101
{
102-
$url = $this->config->getMode() === Mode::PROD ? self::API_URL : self::API_TEST_URL;
102+
$url = $this->config->getMode() === Mode::LIVE ? self::API_URL : self::API_TEST_URL;
103103

104104
return $url . $this->config->getMerchantKey() . '/';
105105
}

Config/Config.php

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace CM\Payments\Config;
1010

1111
use CM\Payments\Api\Config\ConfigInterface;
12+
use CM\Payments\Model\Adminhtml\Source\Mode;
1213
use CM\Payments\Model\ConfigProvider;
1314
use Magento\Framework\App\Config\ScopeConfigInterface;
1415
use Magento\Store\Model\ScopeInterface;
@@ -68,10 +69,17 @@ public function getCurrentVersion(): ?string
6869
/**
6970
* @inheritDoc
7071
*/
71-
public function getMerchantKey(): ?string
72+
public function getMerchantName(): ?string
7273
{
74+
$mode = $this->getMode();
75+
$configPath = self::XML_PATH_GENERAL_TEST_MERCHANT_NAME;
76+
77+
if ($mode == Mode::LIVE) {
78+
$configPath = self::XML_PATH_GENERAL_LIVE_MERCHANT_NAME;
79+
}
80+
7381
return $this->getConfig(
74-
self::XML_PATH_GENERAL_MERCHANT_KEY,
82+
$configPath,
7583
ScopeInterface::SCOPE_STORES,
7684
(string)$this->storeManager->getStore()->getId()
7785
);
@@ -80,10 +88,17 @@ public function getMerchantKey(): ?string
8088
/**
8189
* @inheritDoc
8290
*/
83-
public function getMerchantName(): ?string
91+
public function getMerchantPassword(): ?string
8492
{
93+
$mode = $this->getMode();
94+
$configPath = self::XML_PATH_GENERAL_TEST_MERCHANT_PASSWORD;
95+
96+
if ($mode == Mode::LIVE) {
97+
$configPath = self::XML_PATH_GENERAL_LIVE_MERCHANT_PASSWORD;
98+
}
99+
85100
return $this->getConfig(
86-
self::XML_PATH_GENERAL_MERCHANT_NAME,
101+
$configPath,
87102
ScopeInterface::SCOPE_STORES,
88103
(string)$this->storeManager->getStore()->getId()
89104
);
@@ -92,10 +107,29 @@ public function getMerchantName(): ?string
92107
/**
93108
* @inheritDoc
94109
*/
95-
public function getMerchantPassword(): ?string
110+
public function getMerchantKey(): ?string
96111
{
112+
$mode = $this->getMode();
113+
$configPath = self::XML_PATH_GENERAL_TEST_MERCHANT_KEY;
114+
115+
if ($mode == Mode::LIVE) {
116+
$configPath = self::XML_PATH_GENERAL_LIVE_MERCHANT_KEY;
117+
}
118+
97119
return $this->getConfig(
98-
self::XML_PATH_GENERAL_MERCHANT_PASSWORD,
120+
$configPath,
121+
ScopeInterface::SCOPE_STORES,
122+
(string)$this->storeManager->getStore()->getId()
123+
);
124+
}
125+
126+
/**
127+
* @inheritDoc
128+
*/
129+
public function getMode(): ?string
130+
{
131+
return $this->getConfig(
132+
self::XML_PATH_GENERAL_MODE,
99133
ScopeInterface::SCOPE_STORES,
100134
(string)$this->storeManager->getStore()->getId()
101135
);
@@ -123,18 +157,6 @@ public function getPaymentProfile(string $paymentMethod): ?string
123157
return $defaultPaymentMethod;
124158
}
125159

126-
/**
127-
* @inheritDoc
128-
*/
129-
public function getMode(): ?string
130-
{
131-
return $this->getConfig(
132-
self::XML_PATH_GENERAL_MODE,
133-
ScopeInterface::SCOPE_STORES,
134-
(string)$this->storeManager->getStore()->getId()
135-
);
136-
}
137-
138160
/**
139161
* @inheritDoc
140162
*/

0 commit comments

Comments
 (0)