Skip to content

Commit

Permalink
Merge pull request #58 from hugofintecture/1.5.0
Browse files Browse the repository at this point in the history
1.5.0
  • Loading branch information
hugofintecture authored Aug 19, 2022
2 parents 5c6b5ec + ec7df9a commit bdf8e4f
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 58 deletions.
48 changes: 45 additions & 3 deletions Block/System/Config/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,55 @@

namespace Fintecture\Payment\Block\System\Config;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Button as WidgetButton;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Store\Model\ScopeInterface;

class Button extends Field
{
/** @var string */
protected $_template = 'Fintecture_Payment::system/config/button.phtml';

/** @var Http */
protected $request;

public function __construct(
Context $context,
Http $request,
array $data = []
) {
$this->request = $request;
parent::__construct($context, $data);
}

public function render(AbstractElement $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();

return parent::render($element);
}

public function getCustomUrl()
public function getCustomUrl(): string
{
return $this->getUrl('fintecture/settings/ajax');
$scope = $this->getScope();

return $this->getUrl('fintecture/settings/connectiontest', [
'isAjax' => true,
'form_key' => $this->getFormKey(),
'scope' => $scope['scope'],
'scopeId' => $scope['scopeId']
]);
}

public function getButtonHtml()
{
/** @var WidgetButton $button */
$button = $this->getLayout()->createBlock(WidgetButton::class);
$button->setData([
'id' => 'test-connection',
'id' => 'connection-test',
'label' => __('Test Connection')
]);

Expand All @@ -41,4 +63,24 @@ protected function _getElementHtml(AbstractElement $element)
{
return $this->_toHtml();
}

protected function getScope(): array
{
if ($this->request->getParam('store')) {
return [
'scope' => ScopeInterface::SCOPE_STORE,
'scopeId' => (int) $this->request->getParam('store')
];
} elseif ($this->request->getParam('website')) {
return [
'scope' => ScopeInterface::SCOPE_WEBSITE,
'scopeId' => (int) $this->request->getParam('website')
];
}

return [
'scope' => ScopeInterface::SCOPE_STORE,
'scopeId' => 0
];
}
}
102 changes: 102 additions & 0 deletions Controller/Adminhtml/Settings/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Fintecture\Payment\Controller\Adminhtml\Settings;

use Fintecture\Payment\Gateway\Client;
use Fintecture\Payment\Helper\Fintecture as FintectureHelper;
use Fintecture\Payment\Logger\Logger as FintectureLogger;
use Fintecture\Payment\Model\Environment;
use Fintecture\Payment\Model\Fintecture;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\LocalizedException;

class ConnectionTest extends Action
{
public const CONFIG_PREFIX = 'payment/fintecture/';
protected $fintectureModel;
protected $jsonResultFactory;
protected $scopeConfig;
protected $environment = Environment::ENVIRONMENT_PRODUCTION;

/** @var FintectureHelper */
protected $fintectureHelper;

/** @var FintectureLogger */
protected $fintectureLogger;

/** @var Validator*/
protected $formKeyValidator;

public function __construct(
Context $context,
Fintecture $fintectureModel,
JsonFactory $jsonResultFactory,
ScopeConfigInterface $scopeConfig,
FintectureHelper $fintectureHelper,
FintectureLogger $fintectureLogger,
Validator $formKeyValidator
) {
parent::__construct($context);
$this->fintectureModel = $fintectureModel;
$this->jsonResultFactory = $jsonResultFactory;
$this->scopeConfig = $scopeConfig;
$this->fintectureHelper = $fintectureHelper;
$this->fintectureLogger = $fintectureLogger;
$this->formKeyValidator = $formKeyValidator;
}

public function execute()
{
/** @var Http $request */
$request = $this->getRequest();
if (!$request->isPost() || !$request->isAjax() || !$this->formKeyValidator->validate($request)) {
throw new LocalizedException(__('Invalid request'));
}

$scope = $request->getParam('scope');
$scopeId = (int) $request->getParam('scopeId');
$fintectureConnectParameters = $request->getParams();
$environment = $fintectureConnectParameters['fintectureEnv'] ?? Environment::ENVIRONMENT_PRODUCTION;

$fintectureAppId = $fintectureConnectParameters['fintectureAppId'] ?? '';
$fintectureAppSecret = $fintectureConnectParameters['fintectureAppSecret'] ?? '';
$fintecturePrivateKey = $fintectureConnectParameters['fintecturePrivateKey'] ?? '';

// Handle already saved app secret
if ($fintectureAppSecret === '******') {
$fintectureAppSecret = $this->fintectureModel->getAppSecret($environment, $scope, $scopeId);
}

// Handle already saved private key
if ($fintecturePrivateKey === '') {
$fintecturePrivateKey = $this->fintectureModel->getAppPrivateKey($environment, $scope, $scopeId);
if (!$fintecturePrivateKey) {
throw new LocalizedException(__('No private key file found'));
}
}

$clientGateway = new Client(
$this->fintectureHelper,
$this->fintectureLogger,
[
'fintectureApiUrl' => $this->fintectureModel->getFintectureApiUrl(),
'fintecturePrivateKey' => $fintecturePrivateKey,
'fintectureAppId' => $fintectureAppId,
'fintectureAppSecret' => $fintectureAppSecret,
]
);

$response = $clientGateway->testConnection();

$resultJson = $this->jsonResultFactory->create();

return $resultJson->setData($response);
}
}
1 change: 1 addition & 0 deletions Model/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(Data $paymenthelper)

public function getConfig()
{
/** @phpstan-ignore-next-line : we will refactor the plugin without AbstractMethod */
return $this->method->isAvailable() ? [
'payment' => [
'fintecture' => [
Expand Down
31 changes: 20 additions & 11 deletions Model/Fintecture.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/** @phpstan-ignore-next-line : we will refactor the plugin without AbstractMethod */
class Fintecture extends AbstractMethod
{
public const CODE = 'fintecture';
public const CONFIG_PREFIX = 'payment/fintecture/';
public const MODULE_VERSION = '1.4.0';
public const MODULE_VERSION = '1.5.0';

private const PAYMENT_COMMUNICATION = 'FINTECTURE-';
private const REFUND_COMMUNICATION = 'REFUND FINTECTURE-';
Expand Down Expand Up @@ -151,6 +152,7 @@ public function __construct(
$this->invoiceRepository = $invoiceRepository;
$this->creditmemoRepository = $creditmemoRepository;

/** @phpstan-ignore-next-line : we will refactor the plugin without AbstractMethod */
parent::__construct(
$context,
$registry,
Expand Down Expand Up @@ -205,7 +207,7 @@ public function handleSuccessTransaction($order, $status, $sessionId, $statuses,

$this->orderSender->send($order);

if ($order->canInvoice()) {
if ($order->canInvoice() && $this->getInvoicingActive()) {
// Re-enable email sending
$order->setCanSendNewEmailFlag(true);
$this->orderRepository->save($order);
Expand Down Expand Up @@ -456,26 +458,28 @@ public function getFintectureApiUrl(): string
? 'https://api-sandbox.fintecture.com/' : 'https://api.fintecture.com/';
}

public function getAppPrivateKey(): ?string
{
$privateKey = $this->_scopeConfig->getValue(self::CONFIG_PREFIX . 'custom_file_upload_' . $this->environment, ScopeInterface::SCOPE_STORE);
return $privateKey ? $this->encryptor->decrypt($privateKey) : null;
}

public function getShopName(): ?string
{
return $this->_scopeConfig->getValue('general/store_information/name', ScopeInterface::SCOPE_STORE);
}

public function getAppId(?string $environment = null): ?string
public function getAppId(string $environment = null): ?string
{
$environment = $environment ?: $this->environment;
return $this->_scopeConfig->getValue(static::CONFIG_PREFIX . 'fintecture_app_id_' . $environment, ScopeInterface::SCOPE_STORE);
}

public function getAppSecret(): ?string
public function getAppSecret(string $environment = null, string $scope = ScopeInterface::SCOPE_STORE, int $scopeId = null): ?string
{
return $this->_scopeConfig->getValue(static::CONFIG_PREFIX . 'fintecture_app_secret_' . $this->environment, ScopeInterface::SCOPE_STORE);
$environment = $environment ?: $this->environment;
return $this->_scopeConfig->getValue(static::CONFIG_PREFIX . 'fintecture_app_secret_' . $environment, $scope, $scopeId);
}

public function getAppPrivateKey(string $environment = null, string $scope = ScopeInterface::SCOPE_STORE, int $scopeId = null): ?string
{
$environment = $environment ?: $this->environment;
$privateKey = $this->_scopeConfig->getValue(self::CONFIG_PREFIX . 'custom_file_upload_' . $environment, $scope, $scopeId);
return $privateKey ? $this->encryptor->decrypt($privateKey) : null;
}

public function isRewriteModeActive(): bool
Expand Down Expand Up @@ -508,6 +512,11 @@ public function getExpirationAfter(): ?int
return (int) $this->_scopeConfig->getValue('payment/fintecture/expiration_after', ScopeInterface::SCOPE_STORE);
}

public function getInvoicingActive(): ?bool
{
return $this->_scopeConfig->isSetFlag('payment/fintecture/invoicing_active', ScopeInterface::SCOPE_STORE);
}

public function getPaymentGatewayRedirectUrl(): string
{
$this->validateConfigValue();
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
"email": "[email protected]"
},
"type": "magento2-module",
"version": "1.4.0",
"version": "1.5.0",
"license": [
"GPL-3.0"
],
"require": {
"php": ">=7.3",
"ext-json": "*",
"ext-openssl": "*",
"magento/framework": ">=100.0",
"magento/module-developer": ">=100.0"
"magento/framework": ">=100",
"magento/module-developer": ">=100"
},
"require-dev": {
"phpstan/phpstan": "^1",
Expand Down
10 changes: 10 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@
<validate>validate-number validate-greater-than-zero</validate>
</field>
</group>

<group id="fintecture_advanced_invoicing" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Invoicing</label>

<field id="invoicing_active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable invoice generation for paid orders</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/fintecture/invoicing_active</config_path>
</field>
</group>
</group>
<!-- End Advanced Options -->

Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<payment_failed_status>canceled</payment_failed_status>
<expiration_active>0</expiration_active>
<expiration_after>10</expiration_after>
<invoicing_active>1</invoicing_active>
<payment_us_fintecture_general_specificcountry>FR</payment_us_fintecture_general_specificcountry>
<payment_us_fintecture_general_allowspecific>1</payment_us_fintecture_general_allowspecific>
</fintecture>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Fintecture_Payment" setup_version="1.4.0">
<module name="Fintecture_Payment" setup_version="1.5.0">
</module>
</config>
4 changes: 3 additions & 1 deletion i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@
"Automatic expiration of pending orders after", "Automatic expiration of pending orders after"
"In minutes. Minimum value: 1", "In minutes. Minimum value: 1"
"Module version:", "Module version:"
"This section is intended for advanced users. Changing the settings may impact the proper functioning of your system.", "This section is intended for advanced users. Changing the settings may impact the proper functioning of your system."
"This section is intended for advanced users. Changing the settings may impact the proper functioning of your system.", "This section is intended for advanced users. Changing the settings may impact the proper functioning of your system."
"Invoicing", "Invoicing"
"Enable invoice generation for paid orders", "Enable invoice generation for paid orders"
4 changes: 3 additions & 1 deletion i18n/fr_FR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@
"Automatic expiration of pending orders after", "Expiration automatique des commandes en attente après"
"In minutes. Minimum value: 1", "En minutes. Valeur minimum 1"
"Module version:", "Version du module :"
"This section is intended for advanced users. Changing the settings may impact the proper functioning of your system.", "Cette section est destinée aux utilisateurs avancés. La modification des paramètres peut avoir un impact sur le bon fonctionnement de votre système."
"This section is intended for advanced users. Changing the settings may impact the proper functioning of your system.", "Cette section est destinée aux utilisateurs avancés. La modification des paramètres peut avoir un impact sur le bon fonctionnement de votre système."
"Invoicing", "Facturation"
"Enable invoice generation for paid orders", "Activer la génération de facture pour les commandes payées"
12 changes: 8 additions & 4 deletions view/adminhtml/templates/system/config/button.phtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

declare(strict_types=1);

use Fintecture\Payment\Block\System\Config\Button;

/** @var Button $block */
echo $block->getButtonHtml();
?>
<div id="connect-message"></div>
<div id="connection-test-result" class="message">
<span class="message-text">
<strong></strong>
</span>
</div>
<script>
const configurationAjaxUrl = '<?= $block->getCustomUrl() ?>?isAjax=true&form_key=<?= $block->getFormKey() ?>';
</script>

const connectionTestUrl = '<?php echo $block->getCustomUrl(); ?>';
</script>
13 changes: 5 additions & 8 deletions view/adminhtml/web/css/config.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
display: none;
}

.fintecture-admin-section #connect-message {
margin-top: 10px;
}

.fintecture-admin-section #connect-message .error {
color: #FF0000;
.fintecture-admin-section #connection-test-result {
display: none;
}

.fintecture-admin-section #connect-message .success {
color: #00FF12;
.fintecture-admin-section #connection-test-result.message-error,
.fintecture-admin-section #connection-test-result.message-success {
margin-top: 20px;
}
Loading

0 comments on commit bdf8e4f

Please sign in to comment.