src/Service/Payment/HiPay/EventSubscriber/HiPayCallbackSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Service\Payment\HiPay\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use App\Service\PrintLMDVInterface;
  5. use App\Service\Payment\PaymentEventInterface;
  6. use App\Service\Payment\HiPay\HiPayManager;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Component\Mime\Address;
  11. use App\Service\Payment\HiPay\Event\HiPayCallbackEvent;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class HiPayCallbackSubscriber implements EventSubscriberInterface {
  14.   /**
  15.    * @var \Symfony\Component\Mailer\MailerInterface
  16.    */
  17.   protected $mailer;
  18.   /**
  19.    * @var string
  20.    */
  21.   protected $fromEmail;
  22.   /**
  23.    * @var \Symfony\Contracts\Translation\TranslatorInterface
  24.    */
  25.   protected $translator;
  26.   /**
  27.    * @var \App\Service\PrintLMDVInterface
  28.    */
  29.   protected $print;
  30.   public function __construct(MailerInterface $mailerstring $fromEmailTranslatorInterface $translatorPrintLMDVInterface $print) {
  31.     $this->mailer $mailer;
  32.     $this->fromEmail $fromEmail;
  33.     $this->translator $translator;
  34.     $this->print $print;
  35.   }
  36.   public static function getSubscribedEvents()
  37.   {
  38.       return [
  39.           HiPayCallbackEvent::class => 'onHostedPaymentCallback'
  40.       ];
  41.   }
  42.   public function onHostedPaymentCallback(PaymentEventInterface $event) {
  43.     //XXX: It has been decided to not send mails using Symfony, so we stop here.
  44.     return;
  45.     switch($event->getType()) {
  46.       case HiPayManager::HIPAY_CALLBACK_ACCEPT:
  47.         $this->sendEmail($event'payment/emails/success.html.twig');
  48.         break;
  49.       default:
  50.         $this->sendEmail($event'payment/emails/error.html.twig');
  51.         break;
  52.     }
  53.   }
  54.   protected function sendEmail(PaymentEventInterface $event$template) {
  55.     $voyage $event->getVoyage();
  56.     $to $event->getTransaction()['email'];
  57.     $subject $this->translator->trans('payment.email.subject');
  58.     $transaction $event->getTransaction();
  59.     $transaction['amount'] = number_format($transaction['amount'], 2','' ');
  60.     $transaction['amount'] = str_replace(',00'''$transaction['amount']);
  61.     $transaction['date'] = date('d/m/Y H:i:s'$transaction['date']);
  62.     $email = (new TemplatedEmail())
  63.       ->from(Address::create($this->fromEmail))
  64.       ->to(new Address($to))
  65.       ->subject($subject)
  66.       // path of the Twig template to render
  67.       ->htmlTemplate($template)
  68.       // pass variables (name => value) to the template
  69.       ->context([
  70.           'nom' => $voyage->getEtape2()->getNom(),
  71.           'prenom' => $voyage->getEtape2()->getPrenom(),
  72.           'transaction'=> $transaction,
  73.       ]);
  74.     $pdfContent '';
  75.     try {
  76.       $pdfContent $this->print->print($voyage);
  77.     }
  78.     catch(\Exception $e) {
  79.     }
  80.     if($pdfContent) {
  81.       $email->attach($pdfContent'contrat_de_vente.pdf''application/pdf');
  82.     }
  83.     try {
  84.         $this->mailer->send($email);
  85.     }
  86.     catch (\Exception $e) {
  87.       throw $e;
  88.     }
  89.   }
  90. }