Skip to content

Commit

Permalink
Merge pull request #4 from Marvin-Magmodules/release/1.0.6
Browse files Browse the repository at this point in the history
Release/1.0.6
  • Loading branch information
lighe authored Sep 13, 2023
2 parents 981b8c7 + 2391738 commit 7d1eb77
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 107 deletions.
16 changes: 5 additions & 11 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ jobs:
php-74:
runs-on: ubuntu-latest
steps:
- uses: StephaneBour/actions-php-lint@7.4
with:
dir: './'
- uses: prestashop/github-action-php-lint/7.4@v2.1

php-80:
php-81:
runs-on: ubuntu-latest
steps:
- uses: StephaneBour/actions-php-lint@8.0
with:
dir: './'
- uses: prestashop/github-action-php-lint/8.1@v2.1

php-81:
php-82:
runs-on: ubuntu-latest
steps:
- uses: StephaneBour/actions-php-lint@8.1
with:
dir: './'
- uses: prestashop/github-action-php-lint/8.2@v2.1
3 changes: 3 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jobs:
- name: Upload our code into the docker container
run: docker cp $(pwd) magento-project-community-edition:/data/extensions/

- name: Set minimum-stability for composer (temp)
run: docker exec magento-project-community-edition composer config minimum-stability dev

- name: Install the extensions in Magento
run: docker exec magento-project-community-edition composer require truelayer/magento2:@dev --no-plugins --with-all-dependencies

Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/setup-di-compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
MAGENTO_VERSION: 2.4.4
- PHP_VERSION: php81-fpm
MAGENTO_VERSION: 2.4.5
- PHP_VERSION: php82-fpm
MAGENTO_VERSION: 2.4.6
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -23,8 +25,14 @@ jobs:
- name: Create branch for Composer and remove version from composer.json
run: git checkout -b continuous-integration-test-branch && sed -i '/version/d' ./composer.json

- name: Set minimum-stability for composer (temp)
run: docker exec magento-project-community-edition composer config minimum-stability dev

- name: Upload the code into the docker container
run: docker cp $(pwd) magento-project-community-edition:/data/extensions/ && docker exec magento-project-community-edition composer require truelayer/magento2:@dev --no-plugins --with-all-dependencies
run: docker cp $(pwd) magento-project-community-edition:/data/extensions/

- name: Install the extensions in Magento
run: docker exec magento-project-community-edition composer require truelayer/magento2:@dev --no-plugins --with-all-dependencies

- name: Activate the extension and run setup:upgrade and setup:di:compile
run: docker exec magento-project-community-edition ./retry "php bin/magento module:enable TrueLayer_Connect && php bin/magento setup:upgrade && php bin/magento setup:di:compile"
122 changes: 90 additions & 32 deletions Controller/Adminhtml/Credentials/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Io\File;
use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository;
use TrueLayer\Connect\Service\Api\GetClient;
use TrueLayer\Interfaces\Client\ClientInterface;

/**
* Credential check controller to validate API data
*/
class Check extends Action implements HttpPostActionInterface
{

private const PEM_UPLOAD_FILE = '/truelayer/temp/private_key.pem';
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @var GetClient
*/
Expand All @@ -32,6 +42,10 @@ class Check extends Action implements HttpPostActionInterface
* @var ConfigRepository
*/
private $configProvider;
/**
* @var File
*/
private $file;

/**
* Check constructor.
Expand All @@ -40,16 +54,22 @@ class Check extends Action implements HttpPostActionInterface
* @param JsonFactory $resultJsonFactory
* @param GetClient $getClient
* @param ConfigRepository $configProvider
* @param File $file
* @param DirectoryList $directoryList
*/
public function __construct(
Action\Context $context,
JsonFactory $resultJsonFactory,
GetClient $getClient,
ConfigRepository $configProvider
ConfigRepository $configProvider,
File $file,
DirectoryList $directoryList
) {
$this->getClient = $getClient;
$this->resultJson = $resultJsonFactory->create();
$this->configProvider = $configProvider;
$this->file = $file;
$this->directoryList = $directoryList;
parent::__construct($context);
}

Expand All @@ -58,38 +78,10 @@ public function __construct(
*/
public function execute(): Json
{
$config = $this->getCredentials();
if (!$config['credentials']['client_id'] || !$config['credentials']['client_secret']) {
return $this->resultJson->setData(
[
'success' => true,
'msg' => __('Set credentials first')
]
);
}

try {
$result = $this->getClient->execute(
(int)$config['store_id'],
['credentials' => $config['credentials']]
);
} catch (\Exception $exception) {
return $this->resultJson->setData(
['success' => false, 'msg' => $exception->getMessage()]
);
}
if (!$result) {
return $this->resultJson->setData(
['success' => false, 'msg' => 'Credentials are not correct']
);
}
try {
$result->getMerchantAccounts();
$this->testCredentials()->getMerchantAccounts();
return $this->resultJson->setData(
[
'success' => true,
'msg' => __('Credentials correct!')->render()
]
['success' => true, 'msg' => __('Credentials correct!')->render()]
);
} catch (\Exception $exception) {
return $this->resultJson->setData(
Expand All @@ -98,8 +90,39 @@ public function execute(): Json
}
}

/**
* @throws LocalizedException
* @throws FileSystemException
*/
private function testCredentials(): ?ClientInterface
{
$config = $this->getCredentials();

if (!$config['credentials']['client_id']) {
throw new LocalizedException(__('No Client ID set!'));
}

if (!$config['credentials']['client_secret']) {
throw new LocalizedException(__('No Client Secret set!'));
}

$result = $this->getClient->execute(
(int)$config['store_id'],
['credentials' => $config['credentials']]
);

$this->cleanSavedTemporaryPrivateKey();

if (!$result) {
throw new LocalizedException(__('Credentials are not correct.'));
}

return $result;
}

/**
* @return array
* @throws FileSystemException
*/
private function getCredentials(): array
{
Expand All @@ -125,9 +148,44 @@ private function getCredentials(): array
'credentials' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'private_key' => $configCredentials['private_key'],
'private_key' => $this->getPrivateKeyPath($configCredentials),
'key_id' => $keyId
]
];
}

/**
* @param array $configCredentials
* @return string
* @throws FileSystemException
*/
private function getPrivateKeyPath(array $configCredentials): string
{
if ($privateKey = $this->getRequest()->getParam('private_key')) {
$path = $this->directoryList->getPath('var') . self::PEM_UPLOAD_FILE;
$fileInfo = $this->file->getPathInfo($path);

if (!$this->file->fileExists($fileInfo['dirname'])) {
$this->file->mkdir($fileInfo['dirname']);
}

$this->file->write($path, $privateKey);

return $path;
}

return $configCredentials['private_key'];
}

/**
* @return void
* @throws FileSystemException
*/
private function cleanSavedTemporaryPrivateKey(): void
{
$path = $this->directoryList->getPath('var') . self::PEM_UPLOAD_FILE;
if ($this->file->fileExists($path)) {
$this->file->rm($path);
}
}
}
2 changes: 1 addition & 1 deletion Controller/Checkout/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function execute(): Redirect
$result = $this->processReturn->execute((string)$transactionId);
if ($result['success']) {
$resultRedirect->setPath('checkout/onepage/success');
} elseif ($result['status'] == 'settled' || $result['status'] == 'executed') {
} elseif (in_array($result['status'], ['settled', 'executed', 'authorized'])) {
$resultRedirect->setPath('truelayer/checkout/pending', ['payment_id' => $transactionId]);
} else {
$this->messageManager->addErrorMessage('Something went wrong');
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "truelayer/magento2",
"description": "TrueLayer extension for Magento 2",
"type": "magento2-module",
"version": "1.0.5",
"version": "1.0.6",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<default>
<payment>
<truelayer>
<version>1.0.5</version>
<version>1.0.6</version>
<model>TrueLayerFacade</model>
<title>TrueLayer</title>
<description>Pay using TrueLayer</description>
Expand Down
27 changes: 26 additions & 1 deletion view/adminhtml/templates/system/config/button/credentials.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ use TrueLayer\Connect\Block\Adminhtml\System\Config\Button\Credentials;
'jquery',
'prototype'
], function (jQuery) {
let private_key_sandbox = '',
private_key_production = '',
truelayer_mode = 'sandbox';

document.querySelector('#truelayer_general').addEventListener('change', (e) => {
// Check mode
if (e.target.getAttribute('name').includes('[mode]')) {
truelayer_mode = e.target.value;
}

if (e.target.getAttribute('type') === 'file') {
const FR = new FileReader();

FR.onload = () => {
truelayer_mode === 'sandbox'
? private_key_sandbox = FR.result
: private_key_production = FR.result;
}

FR.readAsText(e.target.files[0], "UTF-8");
}
});

var resultSpan = jQuery('#result_api');
jQuery('#truelayer-button_credentials').click(function () {
var params = {
Expand All @@ -32,8 +55,10 @@ use TrueLayer\Connect\Block\Adminhtml\System\Config\Button\Credentials;
"sandbox_key_id":
jQuery("input[name='groups[general][fields][sandbox_key_id][value]']").val(),
"mode":
jQuery("select[name='groups[general][fields][mode][value]']").val()
jQuery("select[name='groups[general][fields][mode][value]']").val(),
"private_key": truelayer_mode === 'sandbox' ? private_key_sandbox : private_key_production,
};

new Ajax.Request('<?= $block->escapeUrl($block->getApiCheckUrl()) ?>', {
parameters: params,
loaderArea: false,
Expand Down
34 changes: 21 additions & 13 deletions view/frontend/templates/checkout/pending.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@
$viewModel = $block->getData('view_model');
?>
<div class="checkout-pending">
<p>
<h2>
<?= $block->escapeHtml(__('Your payment has been accepted by your bank.')) ?>
<br />
<?= $block->escapeHtml(__(
'Your payment has been accepted by TrueLayer. We are redirecting you to the order confirmation page ' .
'but it might take an extra second.'
'We are redirecting you to the order confirmation page but it might take an extra second.'
)) ?>
</p>
<div class="truelayer-loading" id="truelayer-loading">
<div class="redirect-block">
<h1><?= $block->escapeHtml(__('Refreshing...')); ?></h1>
<div class="loader"></div>
</h2>
<div class="truelayer-loading" data-loader>
<div class="loading-container">
<div class="loading"></div>
<div class="progress"></div>
</div>
</div>
<div class="actions-toolbar" id="truelayer-refresh-button" style="display:none">
<div class="primary">
<a class="action primary refresh" href="<?= $block->escapeUrl($viewModel->getRefreshUrl()) ?>">
<span><?= $block->escapeHtml(__('Refresh')) ?></span>
</a>

<div class="truelayer-result">
<div class="message success" data-success style="display: none;">
<div>
<?= $block->escapeHtml(__('Success! You will now be redirected to another page.')) ?>
</div>
</div>
<div class="message info" data-error style="display: none;">
<div>
<?= $block->escapeHtml(__('Your payment is being processed by your bank.')) ?>
<?= $block->escapeHtml(__('Please wait for an order confirmation email.')) ?>
</div>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit 7d1eb77

Please sign in to comment.