-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored payment confirmation commands
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
Showing
4 changed files
with
92 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
100 changes: 0 additions & 100 deletions
100
src/Models/MailDownloader/SlspNotificationMailDownloader.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters