Skip to content

Commit

Permalink
Refactored payment confirmation commands
Browse files Browse the repository at this point in the history
remp/crm#2878
- Removed redundant mail downloader classes (`CsobMailDownloader`,
`SkCsobMailDownloader`, `TatraBankaMailDownloader`,
`TatraBankaStatementMailDownloader`, `SlspMailDownloader`, `VubMailDownloader`)
and moved logic directly to commands
- Abstracted direct dependency on `Tomaj\ImapMailDownloader\Downloader`
to `ImapMailDownloader`
- Replaced direct dependency on `Tomaj\ImapMailDownloader\Downloader`
with `MailDownloaderInterface` in confirmation commands
- Added option to replace default mail downloader `ImapMailDownloader`
(downloader must implement: `MailDownloaderInterface`)
  • Loading branch information
zoldia committed Sep 4, 2023
1 parent 13a1aef commit 0d00670
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 179 deletions.
110 changes: 91 additions & 19 deletions src/Commands/SlspMailConfirmationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

namespace Crm\SlspSporopayModule\Commands;

use Crm\ApplicationModule\Config\ApplicationConfig;
use Crm\PaymentsModule\MailConfirmation\MailProcessor;
use Crm\SlspSporopayModule\MailConfirmation\SlspMailDownloader;
use Crm\SlspSporopayModule\MailConfirmation\SlspNotificationMailDownloader;
use Crm\PaymentsModule\Models\MailDownloader\EmailInterface;
use Crm\PaymentsModule\Models\MailDownloader\MailDownloaderInterface;
use Crm\SlspSporopayModule\MailParser\SlspMailParser;
use Crm\SlspSporopayModule\MailParser\SlspNotificationMailParser;
use Nette\Utils\FileSystem;
use Nette\Utils\Random;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Tomaj\ImapMailDownloader\MailCriteria;
use Tracy\Debugger;
use Tracy\ILogger;

class SlspMailConfirmationCommand extends Command
{
private $notificationMailDownloader;

private $mailDownloader;

private $mailProcessor;
private OutputInterface $output;

public function __construct(
SlspNotificationMailDownloader $notificationMailDownloader,
SlspMailDownloader $mailDownloader,
MailProcessor $mailProcessor
private string $confirmationTmpDir,
private MailDownloaderInterface $mailDownloader,
private MailProcessor $mailProcessor,
private ApplicationConfig $applicationConfig,
) {
parent::__construct();
$this->notificationMailDownloader = $notificationMailDownloader;
$this->mailDownloader = $mailDownloader;
$this->mailProcessor = $mailProcessor;
}

protected function configure()
Expand All @@ -36,19 +38,89 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->mailDownloader->download(function ($mailContent) use ($output) {
return $this->markMailProcessed($mailContent, $output);
$this->output = $output;

$connectionOptions = [
'imapHost' => $this->applicationConfig->get('slsp_confirmation_host'),
'imapPort' => $this->applicationConfig->get('slsp_confirmation_port'),
'username' => $this->applicationConfig->get('slsp_confirmation_username'),
'password' => $this->applicationConfig->get('slsp_confirmation_password'),
'processedFolder' => $this->applicationConfig->get('slsp_confirmation_processed_folder'),
];

$criteria = new MailCriteria();
$criteria->setFrom('[email protected]');
$criteria->setSubject('Informacia o realizacii platby');
$criteria->setUnseen(true);
$connectionOptions['criteria'] = $criteria;

$this->mailDownloader->download($connectionOptions, function (EmailInterface $email) {
$slspMailParser = new SlspMailParser();

$mailContent = $slspMailParser->parse(quoted_printable_decode($email->getBody()));
if ($mailContent !== null) {
$this->mailProcessor->processMail($mailContent, $this->output);
}
});

$this->notificationMailDownloader->download(function ($mailContent) use ($output) {
return $this->markMailProcessed($mailContent, $output);
$connectionOptions = [
'imapHost' => $this->applicationConfig->get('slsp_notification_confirmation_host'),
'imapPort' => $this->applicationConfig->get('slsp_notification_confirmation_port'),
'username' => $this->applicationConfig->get('slsp_notification_confirmation_username'),
'password' => $this->applicationConfig->get('slsp_notification_confirmation_password'),
'processedFolder' => $this->applicationConfig->get('slsp_notification_confirmation_processed_folder'),
];

$criteria = new MailCriteria();
$criteria->setFrom('[email protected]');
$criteria->setSubject('Notifikacia');
$criteria->setUnseen(true);

$options = array_merge($connectionOptions, ['criteria' => $criteria]);
$this->mailDownloader->download($options, function (EmailInterface $email) {
$parser = new SlspNotificationMailParser();

$body = $this->getAttachmentBody($email);
$mailContent = $parser->parse(quoted_printable_decode($body));
if ($mailContent !== null && $body) {
$this->mailProcessor->processMail($mailContent, $this->output);
}
});

return Command::SUCCESS;
}

private function markMailProcessed($mailContent, $output)
private function validateAttachment(EmailInterface $email): bool
{
return !$this->mailProcessor->processMail($mailContent, $output);
$attachments = $email->getAttachments();
if (empty($attachments)) {
Debugger::log(
'missing slsp notification mail attachment for payment sent on: ' . $email->getDate(),
ILogger::WARNING
);
return false;
}
return true;
}

private function getAttachmentBody(EmailInterface $email): bool|string
{
if (!$this->validateAttachment($email)) {
return false;
}

$filePath = $this->confirmationTmpDir . DIRECTORY_SEPARATOR . 'payments-mail-parser/' . Random::generate() . '.zip';

FileSystem::write($filePath, array_values($email->getAttachments())[0]['attachment']);

$zip = new \ZipArchive();
$zip->open($filePath);
$contents = $zip->getFromIndex(0);

$contents = iconv("ISO-8859-1", "UTF-8", $contents);

FileSystem::delete($filePath);

return $contents;
}
}
57 changes: 0 additions & 57 deletions src/Models/MailDownloader/SlspMailDownloader.php

This file was deleted.

100 changes: 0 additions & 100 deletions src/Models/MailDownloader/SlspNotificationMailDownloader.php

This file was deleted.

4 changes: 1 addition & 3 deletions src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ services:
- Crm\SlspSporopayModule\Seeders\ConfigsSeeder
- Crm\SlspSporopayModule\Seeders\PaymentGatewaysSeeder
- Crm\SlspSporopayModule\Gateways\SlspSporopay
- Crm\SlspSporopayModule\MailConfirmation\SlspMailDownloader
- Crm\SlspSporopayModule\MailConfirmation\SlspNotificationMailDownloader(%tempDir%)
- Crm\SlspSporopayModule\Commands\SlspMailConfirmationCommand
- Crm\SlspSporopayModule\Commands\SlspMailConfirmationCommand(%tempDir%)

gatewayFactory:
setup:
Expand Down

0 comments on commit 0d00670

Please sign in to comment.