Commit 3ae1d424 by Damien Moulard

save transaction & send mail for insufficient funds

parent 2fa7b56d
...@@ -174,8 +174,8 @@ class UserAdherentController extends FluxController ...@@ -174,8 +174,8 @@ class UserAdherentController extends FluxController
$plainCode = $data->getPaymentCode(); $plainCode = $data->getPaymentCode();
if (is_numeric($plainCode) && strlen($plainCode) >= 4 && strlen($plainCode) <= 8) { if (is_numeric($plainCode) && strlen($plainCode) >= 4 && strlen($plainCode) <= 8) {
$encoded = $encoder->encodePassword($this->getUser(), $plainCode); $encoded = crypt($plainCode, $this->getUser()->getSalt());
printf($encoded);
$adherent->setPaymentCode($encoded); $adherent->setPaymentCode($encoded);
$this->em->flush(); $this->em->flush();
......
...@@ -9,6 +9,7 @@ use App\Entity\GlobalParameter; ...@@ -9,6 +9,7 @@ use App\Entity\GlobalParameter;
use App\Entity\Payment; use App\Entity\Payment;
use App\Entity\Prestataire; use App\Entity\Prestataire;
use App\Entity\User; use App\Entity\User;
use App\Entity\TransactionAdherentPrestataire;
use App\Enum\MoyenEnum; use App\Enum\MoyenEnum;
use App\Form\Type\CotiserFormType; use App\Form\Type\CotiserFormType;
use App\Form\Type\DonAdherentFormType; use App\Form\Type\DonAdherentFormType;
...@@ -24,10 +25,12 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; ...@@ -24,10 +25,12 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Twig\Environment;
class UserController extends AbstractController class UserController extends AbstractController
{ {
...@@ -36,14 +39,28 @@ class UserController extends AbstractController ...@@ -36,14 +39,28 @@ class UserController extends AbstractController
private $security; private $security;
private $operationUtils; private $operationUtils;
private $cotisationUtils; private $cotisationUtils;
private $session;
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, Security $security, OperationUtils $operationUtils, CotisationUtils $cotisationUtils) private $mailer;
{ private $templating;
public function __construct(
EntityManagerInterface $em,
TranslatorInterface $translator,
Security $security,
OperationUtils $operationUtils,
CotisationUtils $cotisationUtils,
SessionInterface $session,
\Swift_Mailer $mailer,
Environment $templating
) {
$this->em = $em; $this->em = $em;
$this->translator = $translator; $this->translator = $translator;
$this->security = $security; $this->security = $security;
$this->operationUtils = $operationUtils; $this->operationUtils = $operationUtils;
$this->cotisationUtils = $cotisationUtils; $this->cotisationUtils = $cotisationUtils;
$this->session = $session;
$this->mailer = $mailer;
$this->templating = $templating;
} }
/** /**
...@@ -264,14 +281,9 @@ class UserController extends AbstractController ...@@ -264,14 +281,9 @@ class UserController extends AbstractController
} }
// Check validation code // Check validation code
$encoded_input = $encoder->encodePassword($adherent->getUser(), $input_code); // TODO as we use password salt, must change payment code if password changes
printf('<p>encoded_input</p>'); $encoded_input = crypt($input_code, $adherent->getUser()->getSalt());
printf($encoded_input); if (!hash_equals($adherent_code, $encoded_input)) {
printf('<p>adherent_code</p>');
printf($adherent_code);
if ($encoded_input != $adherent_code) {
// if bad validation code
$this->addFlash( $this->addFlash(
'error', 'error',
$this->translator->trans('Code incorrect') $this->translator->trans('Code incorrect')
...@@ -280,13 +292,60 @@ class UserController extends AbstractController ...@@ -280,13 +292,60 @@ class UserController extends AbstractController
goto end; goto end;
} }
// TODO: Check has enough // Check adherent has enough funds
$balance = $adherent->getEmlcAccount()->getBalance();
$transaction_amount = floatval($data["montant"]);
if ($balance < $transaction_amount) {
// Send mail for insufficient funds
$subject = 'Votre tentative de paiement en Monnaie Locale Solidaire : solde insuffisant !';
$mail = (new \Swift_Message($subject))
->setFrom($this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::MLC_NOTIF_EMAIL))
->setTo($adherent->getUser()->getEmail())
->setBody(
$this->templating->render(
'@kohinos/email/tav/insufficient_funds.html.twig',
[
'subject' => $subject,
'montant' => $transaction_amount,
]
),
'text/html'
);
$this->mailer->send($mail);
$this->addFlash(
'error',
$this->translator->trans('Solde de l\'habitant insuffisant')
);
return $this->redirectToRoute('index');
}
// Save transaction
$flux = new TransactionAdherentPrestataire();
$flux->setExpediteur($adherent);
$presta = $this->session->get('_prestagere');
$presta = $this->em->getRepository(Prestataire::class)->findOneById($presta->getId());
$flux->setDestinataire($presta);
$flux->setOperateur($this->security->getUser());
$flux->setMontant($transaction_amount);
$flux->setMoyen(MoyenEnum::MOYEN_EMLC);
$now = (new \Datetime('now'))->format('d/m/Y H:i:s');
$flux->setReference('Achat en Monnaie Solidaire du ' . $now);
$this->em->persist($flux);
$this->operationUtils->executeOperations($flux);
$this->em->flush();
// TODO : ifok save transfer (transaction?)
$this->addFlash( $this->addFlash(
'success', 'success',
$this->translator->trans('Bravo !') $this->translator->trans('Transaction réussie !')
); );
return $this->redirectToRoute('index');
} else { } else {
$this->addFlash( $this->addFlash(
'error', 'error',
......
{% extends '@kohinos/email/email_layout.html.twig' %}
{% set title %}{% spaceless %}
{{ subject }}
{% endspaceless %}
{% endset %}
{% block content %}
<h2 style="font-size: 18px; color: #111111; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-weight: bold; line-height: 1.2em; margin: 40px 0 10px;">
{{ 'Solde insuffisant'|trans }}
</h2><br/>
<p>
Une transaction d'un montant de {{ montant|number_format(2) }} vous a été refusée pour cause de solde insuffisant.
</p>
{% endblock %}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment