- <?php
- namespace App\Controller;
- use App\Entity\FormationPrice;
- use App\Entity\Payment;
- use App\Entity\Preinscription;
- use App\Entity\Tarif;
- use App\Entity\Transaction;
- use Doctrine\ORM\EntityManagerInterface;
- use Payum\Core\Payum;
- use Payum\Core\Request\GetHumanStatus;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\Annotation\Route;
- class PaymentController extends AbstractController
- {
-     /**
-      * @Route("/payment/request/{id}", name="mmpp_front_payment_request")
-      * @Route("/payment/request/{type}/{id}", name="mmpp_front_payment_pr_request")
-      */
-     public function requestAction(Preinscription $preinscription, Payum $payum, ?string $type = null, EntityManagerInterface $em)
-     {
-         $gatewayName = 'axepta';
-         $storage = $payum->getStorage(Payment::class);
-         $number = uniqid();
-         /** @var Payment $payment */
-         $payment = $storage->create();
-         $payment->setNumber($number);
-         $payment->setCurrencyCode('EUR');
-         if ($preinscription->getFormulaire()->getId() == 161 || $preinscription->getFormulaire()->getId() == 162) {
-             $payment->setTotalAmount(7000);
-         } else {
-             $payment->setTotalAmount(14000);
-         }
-         if(null !== $type && $preinscription->hasPreRentree()){
-             $tarification = $em->getRepository(FormationPrice::class)->findOneBy(['formulaire' => $preinscription->getFormulaire(), 'periode' => $preinscription->getPeriode()]);
-             $payment->setOrderPaymentSpe($type);
-             $payment->setTotalAmount((intval($tarification->getStagePreRentreePrimant())*100));
-             $payment->setOrderPaymentSpe('PR');
-         }
-         $payment->setDescription('Preinscription #'.$preinscription->getId());
-         $payment->setClientId($preinscription->getId());
-         $payment->setClientEmail($preinscription->getEmail());
-         $storage->update($payment);
-         $captureToken = $payum->getTokenFactory()->createCaptureToken(
-             $gatewayName,
-             $payment,
-             'mmpp_front_payment_done' // the route to redirect after capture
-         );
-         return $this->redirect($captureToken->getTargetUrl());
-     }
-     /**
-      * @Route("/payment/done/{payum_token}", name="mmpp_front_payment_done")
-      */
-     public function doneAction(
-         EntityManagerInterface $em,
-         Request $request,
-         Payum $payum
-     ) {
-         $token = $payum->getHttpRequestVerifier()->verify($request);
-         $gateway = $payum->getGateway($token->getGatewayName());
-         // You can invalidate the token, so that the URL cannot be requested any more:
-         // $this->get('payum')->getHttpRequestVerifier()->invalidate($token);
-         // Once you have the token, you can get the payment entity from the storage directly.
-         // $identity = $token->getDetails();
-         // $payment = $this->get('payum')->getStorage($identity->getClass())->find($identity);
-         // Or Payum can fetch the entity for you while executing a request (preferred).
-         $gateway->execute($status = new GetHumanStatus($token));
-         /** @var Payment $payment */
-         $payment = $status->getFirstModel();
-         $details = $payment->getDetails();
-         // deja arrivĂ©
-         $transaction = $em->getRepository(Transaction::class)
-             ->findOneBy(['authorisationId' => $details['PayID']]);
-         if (null === $transaction) {
-             $transaction = new Transaction();
-             $transaction->setAmount($payment->getTotalAmount());
-             $transaction->setAuthorisationId($details['PayID']);
-             $transaction->setCurrencyCode($payment->getCurrencyCode());
-             $transaction->setCustomerEmail($payment->getClientEmail());
-             $transaction->setCustomerIpAddress($_SERVER['REMOTE_ADDR']);
-             $transaction->setParameters(\json_encode($details));
-             $transaction->setResponseCode($details['Code']);
-             $transaction->setTransactionDatetime(new \DateTime());
-             $transaction->setTransactionReference($details['TransID']);
-             $transaction->setPreinscription(
-                 $em->getRepository(Preinscription::class)->find($payment->getClientId())
-             );
-             $em->persist($transaction);
-             $em->flush();
-         }
-         return $this->render('payment/response.html.twig', ['details' => $details]);
-     }
-     /**
-      * @Route("/payment/response", name="mmpp_front_payment_response")
-      */
-     /*public function responseAction(Mercanet $mercanet)
-     {
-         /** @var Mercanet $mercanet *
-         $mercanet->setResponse($_POST);
-         return $this->render('payment/response.html.twig', array(
-             'mercanet' => $mercanet
-         ));
-     }*/
-     /**
-      * @Route("/payment/auto-response", name="mmpp_front_payment_autoresponse")
-      *
-      * @throws \Exception
-      */
-     /*public function autoResponseAction(
-         EntityManagerInterface $em,
-         Mercanet $mercanet
-     ) {
-         $mercanet->setResponse($_POST);
-         $parameters = $mercanet->toArray();
-         $transaction = new Transaction();
-         $transaction->setAmount($parameters['amount']);
-         $transaction->setAuthorisationId($parameters['authorisationId']);
-         $transaction->setCurrencyCode($parameters['currencyCode']);
-         $transaction->setCustomerEmail($parameters['customerEmail']);
-         $transaction->setCustomerIpAddress($parameters['customerIpAddress']);
-         $transaction->setParameters(\json_encode($parameters));
-         $transaction->setResponseCode($parameters['responseCode']);
-         $transaction->setTransactionDatetime(new \DateTime($parameters['transactionDateTime']));
-         $transaction->setTransactionReference($parameters['transactionReference']);
-         $transaction->setPreinscription(
-             $em->getRepository(Preinscription::class)->find($parameters['orderId'])
-         );
-         $em->persist($transaction);
-         $em->flush();
-         die();
-     }*/
-     /**
-      * @Route("/payment/success", name="mmpp_front_payment_success")
-      */
-     /*public function successAction()
-     {
-         return $this->render('payment/response.html.twig', array(
-             'path' => 'contact',
-             'mercanet' => ['valid' => true]
-         ));
-     }*/
- }
-