src/Controller/PaymentController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\FormationPrice;
  4. use App\Entity\Payment;
  5. use App\Entity\Preinscription;
  6. use App\Entity\Tarif;
  7. use App\Entity\Transaction;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Payum\Core\Bridge\Psr\Log\LogExecutedActionsExtension;
  10. use Payum\Core\Payum;
  11. use Payum\Core\Request\GetHumanStatus;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class PaymentController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/payment/request/{id}", name="mmpp_front_payment_request")
  20.      * @Route("/payment/request/{type}/{id}", name="mmpp_front_payment_pr_request")
  21.      */
  22.     public function requestAction(Preinscription $preinscriptionPayum $payum, ?string $type nullEntityManagerInterface $em)
  23.     {
  24.         $gatewayName 'axepta';
  25.         $storage $payum->getStorage(Payment::class);
  26.         $number uniqid();
  27.         /** @var Payment $payment */
  28.         $payment $storage->create();
  29.         $payment->setNumber($number);
  30.         $payment->setCurrencyCode('EUR');
  31.         if ($preinscription->getFormulaire()->getId() == 161 ||
  32.             $preinscription->getFormulaire()->getId() == 162 ||
  33.             $preinscription->getFormulaire()->getId() == 200 ||
  34.             $preinscription->getFormulaire()->getId() == 201 ) {
  35.             $payment->setTotalAmount(9500);
  36.         } else {
  37.             $payment->setTotalAmount(19000);
  38.         }
  39.         if(null !== $type && $preinscription->hasPreRentree()){
  40.             $tarification $em->getRepository(FormationPrice::class)->findOneBy(['formulaire' => $preinscription->getFormulaire(), 'periode' => $preinscription->getPeriode()]);
  41.             $payment->setOrderPaymentSpe($type);
  42.             $payment->setTotalAmount((intval($tarification->getStagePreRentreePrimant())*100));
  43.             $payment->setOrderPaymentSpe('PR');
  44.         }
  45.         $payment->setDescription('Preinscription #'.$preinscription->getId());
  46.         $payment->setClientId($preinscription->getId());
  47.         $payment->setClientEmail($preinscription->getEmail());
  48.         $storage->update($payment);
  49.         $captureToken $payum->getTokenFactory()->createCaptureToken(
  50.             $gatewayName,
  51.             $payment,
  52.             'mmpp_front_payment_done' // the route to redirect after capture
  53.         );
  54.         return $this->redirect($captureToken->getTargetUrl());
  55.     }
  56.     /**
  57.      * @Route("/payment/done", name="mmpp_front_payment_done")
  58.      */
  59.     public function doneAction(
  60.         EntityManagerInterface $em,
  61.         Request $request,
  62.         LoggerInterface $logger,
  63.         Payum $payum
  64.     ) {
  65.         $token $payum->getHttpRequestVerifier()->verify($request);
  66.         $gateway $payum->getGateway($token->getGatewayName());
  67.         $gateway->addExtension(new LogExecutedActionsExtension($logger));
  68.         // You can invalidate the token, so that the URL cannot be requested any more:
  69.         // $this->get('payum')->getHttpRequestVerifier()->invalidate($token);
  70.         // Once you have the token, you can get the payment entity from the storage directly.
  71.         // $identity = $token->getDetails();
  72.         // $payment = $this->get('payum')->getStorage($identity->getClass())->find($identity);
  73.         // Or Payum can fetch the entity for you while executing a request (preferred).
  74.         $gateway->execute($status = new GetHumanStatus($token));
  75.         /** @var Payment $payment */
  76.         $payment $status->getFirstModel();
  77.         $logger->error('payment data', [
  78.             'payment' => $payment,
  79.             'json' => \json_encode($payment),
  80.             'status' => $status,
  81.             'token' => $token
  82.         ]);
  83.         $details $payment->getDetails();
  84.         // deja arrivĂ©
  85.         $transaction $em->getRepository(Transaction::class)
  86.             ->findOneBy(['authorisationId' => $details['PayID']]);
  87.         if (null === $transaction) {
  88.             $transaction = new Transaction();
  89.             $transaction->setAmount($payment->getTotalAmount());
  90.             $transaction->setAuthorisationId($details['PayID']);
  91.             $transaction->setCurrencyCode($payment->getCurrencyCode());
  92.             $transaction->setCustomerEmail($payment->getClientEmail());
  93.             $transaction->setCustomerIpAddress($_SERVER['REMOTE_ADDR']);
  94.             $transaction->setParameters(\json_encode($details));
  95.             $transaction->setResponseCode($details['Code']);
  96.             $transaction->setTransactionDatetime(new \DateTime());
  97.             $transaction->setTransactionReference($details['TransID']);
  98.             $transaction->setPreinscription(
  99.                 $em->getRepository(Preinscription::class)->find($payment->getClientId())
  100.             );
  101.             $em->persist($transaction);
  102.             $em->flush();
  103.         }
  104.         return $this->render('payment/response.html.twig', ['details' => $details]);
  105.     }
  106.     /**
  107.      * @Route("/payment/response", name="mmpp_front_payment_response")
  108.      */
  109.     /*public function responseAction(Mercanet $mercanet)
  110.     {
  111.         /** @var Mercanet $mercanet *
  112.         $mercanet->setResponse($_POST);
  113.         return $this->render('payment/response.html.twig', array(
  114.             'mercanet' => $mercanet
  115.         ));
  116.     }*/
  117.     /**
  118.      * @Route("/payment/auto-response", name="mmpp_front_payment_autoresponse")
  119.      *
  120.      * @throws \Exception
  121.      */
  122.     /*public function autoResponseAction(
  123.         EntityManagerInterface $em,
  124.         Mercanet $mercanet
  125.     ) {
  126.         $mercanet->setResponse($_POST);
  127.         $parameters = $mercanet->toArray();
  128.         $transaction = new Transaction();
  129.         $transaction->setAmount($parameters['amount']);
  130.         $transaction->setAuthorisationId($parameters['authorisationId']);
  131.         $transaction->setCurrencyCode($parameters['currencyCode']);
  132.         $transaction->setCustomerEmail($parameters['customerEmail']);
  133.         $transaction->setCustomerIpAddress($parameters['customerIpAddress']);
  134.         $transaction->setParameters(\json_encode($parameters));
  135.         $transaction->setResponseCode($parameters['responseCode']);
  136.         $transaction->setTransactionDatetime(new \DateTime($parameters['transactionDateTime']));
  137.         $transaction->setTransactionReference($parameters['transactionReference']);
  138.         $transaction->setPreinscription(
  139.             $em->getRepository(Preinscription::class)->find($parameters['orderId'])
  140.         );
  141.         $em->persist($transaction);
  142.         $em->flush();
  143.         die();
  144.     }*/
  145.     /**
  146.      * @Route("/payment/success", name="mmpp_front_payment_success")
  147.      */
  148.     /*public function successAction()
  149.     {
  150.         return $this->render('payment/response.html.twig', array(
  151.             'path' => 'contact',
  152.             'mercanet' => ['valid' => true]
  153.         ));
  154.     }*/
  155. }