src/Controller/PaytweakController.php line 198

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Billing;
  4. use App\Entity\Client\BillingInfos;
  5. use App\Entity\PaytweakReturn;
  6. use App\Entity\Reglement;
  7. use App\Repository\BillingRepository;
  8. use App\Service\PaytweakService;
  9. use App\Traits\LoggerTrait;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class PaytweakController extends AbstractController
  17. {
  18.     use LoggerTrait;
  19.     /**
  20.      * test card : 5017678000000003
  21.      * @Route("/paytweak/payment", name="paytweak")
  22.      */
  23.     public function index(
  24.         Request $request,
  25.         EntityManagerInterface $entityManager,
  26.         BillingRepository $billingRepository,
  27.         LoggerInterface $logger)
  28.     {
  29.         $content $request->getContent();
  30.         $logger->info($content);
  31. //        if (empty($content)) {
  32. //            return new JsonResponse('', 400);
  33. //        }
  34. //        if (!$this->isJsonRequest($request)) {
  35. //            return new JsonResponse('', 400);
  36. //        }
  37. //        if (!$this->transformJsonBody($request)) {
  38. //            return new JsonResponse('Unable to parse request.', 400);
  39. //        }
  40.         $data json_decode($contenttrue);
  41.         $paytweakreturn = new PaytweakReturn();
  42.         $paytweakreturn->setResult(json_encode($data));
  43.         $entityManager->persist($paytweakreturn);
  44.         $entityManager->flush();
  45.         if(!$request->isMethod(Request::METHOD_POST) || !isset($data['status'])){
  46.             return new JsonResponse(''400);
  47.         }
  48.         $notice = isset($data['notice']) ? $data['notice'] : null ;
  49.         $orderId = isset($data['order_id']) ? $data['order_id'] : null;
  50.         $billingObj $billingRepository->findOneBy(['code' => $orderId]);
  51.         if($notice === 'MANDATE_SIGNED'){
  52.             $RUM = isset($data['RUM']) ? $data['RUM'] : null;
  53.             $iban = isset($data['iban']) ? $data['iban'] : null;
  54.             $bic = isset($data['bic']) ? $data['bic'] : null;
  55.             $dateSigned = isset($data['date_signed']) ? $data['date_signed'] : null;
  56.             $client $billingObj->getClient();
  57.             if($client &&     $client->getBillingInfos()){
  58.                 $datetime \DateTime::createFromFormat('d/m/Y\|H:i:s',$dateSigned );
  59.                 $client->getBillingInfos()->setRum($RUM);
  60.                 $client->getBillingInfos()->setIban($iban);
  61.                 $client->getBillingInfos()->setBic($bic);
  62.                 $client->getBillingInfos()->setPayMode(BillingInfos::PAY_MODE_SEPA);
  63.                 $client->setHasSignedEasyCollect(true);
  64.                 if($datetime){
  65.                     $client->getBillingInfos()->setDateSignatureMandat($datetime);
  66.                 }
  67.                 $entityManager->persist($client);
  68.             }
  69.         }
  70.         if($notice === 'SEPA_PAYMENT'){
  71.             $status = isset($data['status']) ? $data['status'] : null ;
  72.             $amount = isset($data['amount']) ? $data['amount'] : null;
  73.             $card = isset($data['cardno']) ? $data['cardno'] : null;
  74.             $date = isset($data['date_payment']) ?  $data['date_payment']: null;
  75.             $brand = isset($data['brand_network']) ? $data['brand_network'] : null;
  76.             $linkId = isset($data['link_id']) ? $data['link_id'] : null;
  77.             if($status === 5) {
  78.                 $reglement = new Reglement();
  79.                 $reglement->setMode(Reglement::MODE_SEPA);
  80.                 if($billingObj->getCompany()){
  81.                     $reglement->setCompany($billingObj->getCompany());
  82.                 }
  83.                 if($amount){
  84.                     $reglement->setTtc($amount);
  85.                 }
  86.                 $datetime\DateTime::createFromFormat('d/m/Y\|H:i:s',$date );
  87.                 //$datetime= new \DateTime();
  88.                 if($datetime){
  89.                     $reglement->setDate($datetime);
  90.                 }
  91.                 $reglement->setReference("Paytweak - $brand - $card - $orderId");
  92.                 $entityManager->persist($reglement);
  93.                 if($billingObj){
  94.                     $reglement->setClient($billingObj->getClient());
  95.                     $reglement->addBilling($billingObj);
  96.                     $entityManager->persist($reglement);
  97.                     $oldSolde $billingObj->getSolde();
  98.                     $newSolde $oldSolde $amount;
  99.                     $billingObj->setSolde($newSolde);
  100.                     if($newSolde <= 0) {
  101.                         $billingObj->setStatus(Billing::STATUS_REGLEE);
  102.                     }
  103.                     $billingObj->setPaidPaytweak(true);
  104.                     if($linkId){
  105.                         $billingObj->setLinkPaytweak($linkId);
  106.                     }
  107.                     $entityManager->persist($billingObj);
  108.                 }
  109.                 $entityManager->persist($reglement);
  110.             }
  111.         }
  112.         if($notice === 'TRANSFER_PAYMENT'){
  113.             $status = isset($data['status']) ? $data['status'] : null ;
  114.             $amount = isset($data['amount']) ? $data['amount'] : null;
  115.             $card = isset($data['cardno']) ? $data['cardno'] : null;
  116.             $date = isset($data['date_payment']) ?  $data['date_payment']: null;
  117.             $brand = isset($data['brand_network']) ? $data['brand_network'] : null;
  118.             $linkId = isset($data['link_id']) ? $data['link_id'] : null;
  119.             if($billingObj->getStatus() === Billing::STATUS_REGLEE){
  120.                 return new JsonResponse('OK'200);
  121.             }
  122.             if($status == PaytweakService::PAYMENT_STATUS_AUTHORIZED){
  123.                 $reglement = new Reglement();
  124.                 $reglement->setMode(Reglement::MODE_VIREMENT);
  125.                 if($billingObj->getCompany()){
  126.                     $reglement->setCompany($billingObj->getCompany());
  127.                 }
  128.                 if($amount){
  129.                     $reglement->setTtc($amount);
  130.                 }
  131.                 $datetime\DateTime::createFromFormat('d/m/Y\|H:i:s',$date );
  132.                 if($datetime){
  133.                     $reglement->setDate($datetime);
  134.                 }
  135.                 $reglement->setReference("Paytweak Virement - $brand - $card - $orderId");
  136.                 $entityManager->persist($reglement);
  137.                 if($billingObj){
  138.                     $reglement->setClient($billingObj->getClient());
  139.                     $reglement->addBilling($billingObj);
  140.                     $entityManager->persist($reglement);
  141.                     $oldSolde $billingObj->getSolde();
  142.                     $newSolde $oldSolde $amount;
  143.                     $billingObj->setSolde($newSolde);
  144.                     if($newSolde <= 0) {
  145.                         $billingObj->setStatus(Billing::STATUS_REGLEE);
  146.                     }
  147.                     $billingObj->setPaidPaytweak(true);
  148.                     if($linkId){
  149.                         $billingObj->setLinkPaytweak($linkId);
  150.                     }
  151.                     $entityManager->persist($billingObj);
  152.                 }
  153.                 $entityManager->persist($reglement);
  154.             }
  155.         }
  156.         if($notice === 'PAYMENT'){
  157.             $status = isset($data['status']) ? $data['status'] : null ;
  158.             $amount = isset($data['amount']) ? $data['amount'] : null;
  159.             $card = isset($data['cardno']) ? $data['cardno'] : null;
  160.             $date = isset($data['date_payment']) ?  $data['date_payment']: null;
  161.             $brand = isset($data['brand_network']) ? $data['brand_network'] : null;
  162.             $linkId = isset($data['link_id']) ? $data['link_id'] : null;
  163.             if($billingObj->getStatus() === Billing::STATUS_REGLEE){
  164.                 return new JsonResponse('OK'200);
  165.             }
  166.             if($status == PaytweakService::PAYMENT_STATUS_AUTHORIZED){
  167.                 $reglement = new Reglement();
  168.                 $reglement->setMode(Reglement::MODE_CB);
  169.                 if($billingObj->getCompany()){
  170.                     $reglement->setCompany($billingObj->getCompany());
  171.                 }
  172.                 if($amount){
  173.                     $reglement->setTtc($amount);
  174.                 }
  175.                 //$datetime= new \DateTime();
  176.                 $datetime\DateTime::createFromFormat('d/m/Y\|H:i:s',$date );
  177.                 if($datetime){
  178.                     $reglement->setDate($datetime);
  179.                 }
  180.                 $reglement->setReference("Paytweak - $brand - $card - $orderId");
  181.                 $entityManager->persist($reglement);
  182.                 if($billingObj){
  183.                     $reglement->setClient($billingObj->getClient());
  184.                     $reglement->addBilling($billingObj);
  185.                     $entityManager->persist($reglement);
  186.                     $oldSolde $billingObj->getSolde();
  187.                     $newSolde $oldSolde $amount;
  188.                     $billingObj->setSolde($newSolde);
  189.                     if($newSolde <= 0) {
  190.                         $billingObj->setStatus(Billing::STATUS_REGLEE);
  191.                     }
  192.                     $billingObj->setPaidPaytweak(true);
  193.                     if($linkId){
  194.                         $billingObj->setLinkPaytweak($linkId);
  195.                     }
  196.                     $entityManager->persist($billingObj);
  197.                 }
  198.                 $entityManager->persist($reglement);
  199.             }
  200.         }
  201.         $entityManager->flush();
  202.         return new JsonResponse('OK'200);
  203.     }
  204.     private function isJsonRequest(Request $request) {
  205.         return 'json' === $request->getContentType();
  206.     }
  207.     private function transformJsonBody(Request $request) {
  208.         $data json_decode($request->getContent(), true);
  209.         if (json_last_error() !== JSON_ERROR_NONE) {
  210.             return false;
  211.         }
  212.         if ($data === null) {
  213.             return true;
  214.         }
  215.         $request->request->replace($data);
  216.         return true;
  217.     }
  218. }