<?php
namespace App\Controller;
use App\Entity\Billing;
use App\Entity\Client\BillingInfos;
use App\Entity\PaytweakReturn;
use App\Entity\Reglement;
use App\Repository\BillingRepository;
use App\Service\PaytweakService;
use App\Traits\LoggerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class PaytweakController extends AbstractController
{
use LoggerTrait;
/**
* test card : 5017678000000003
* @Route("/paytweak/payment", name="paytweak")
*/
public function index(
Request $request,
EntityManagerInterface $entityManager,
BillingRepository $billingRepository,
LoggerInterface $logger)
{
$content = $request->getContent();
$logger->info($content);
// if (empty($content)) {
// return new JsonResponse('', 400);
// }
// if (!$this->isJsonRequest($request)) {
// return new JsonResponse('', 400);
// }
// if (!$this->transformJsonBody($request)) {
// return new JsonResponse('Unable to parse request.', 400);
// }
$data = json_decode($content, true);
$paytweakreturn = new PaytweakReturn();
$paytweakreturn->setResult(json_encode($data));
$entityManager->persist($paytweakreturn);
$entityManager->flush();
if(!$request->isMethod(Request::METHOD_POST) || !isset($data['status'])){
return new JsonResponse('', 400);
}
$notice = isset($data['notice']) ? $data['notice'] : null ;
$orderId = isset($data['order_id']) ? $data['order_id'] : null;
$billingObj = $billingRepository->findOneBy(['code' => $orderId]);
if($notice === 'MANDATE_SIGNED'){
$RUM = isset($data['RUM']) ? $data['RUM'] : null;
$iban = isset($data['iban']) ? $data['iban'] : null;
$bic = isset($data['bic']) ? $data['bic'] : null;
$dateSigned = isset($data['date_signed']) ? $data['date_signed'] : null;
$client = $billingObj->getClient();
if($client && $client->getBillingInfos()){
$datetime = \DateTime::createFromFormat('d/m/Y\|H:i:s',$dateSigned );
$client->getBillingInfos()->setRum($RUM);
$client->getBillingInfos()->setIban($iban);
$client->getBillingInfos()->setBic($bic);
$client->getBillingInfos()->setPayMode(BillingInfos::PAY_MODE_SEPA);
$client->setHasSignedEasyCollect(true);
if($datetime){
$client->getBillingInfos()->setDateSignatureMandat($datetime);
}
$entityManager->persist($client);
}
}
if($notice === 'SEPA_PAYMENT'){
$status = isset($data['status']) ? $data['status'] : null ;
$amount = isset($data['amount']) ? $data['amount'] : null;
$card = isset($data['cardno']) ? $data['cardno'] : null;
$date = isset($data['date_payment']) ? $data['date_payment']: null;
$brand = isset($data['brand_network']) ? $data['brand_network'] : null;
$linkId = isset($data['link_id']) ? $data['link_id'] : null;
if($status === 5) {
$reglement = new Reglement();
$reglement->setMode(Reglement::MODE_SEPA);
if($billingObj->getCompany()){
$reglement->setCompany($billingObj->getCompany());
}
if($amount){
$reglement->setTtc($amount);
}
$datetime= \DateTime::createFromFormat('d/m/Y\|H:i:s',$date );
//$datetime= new \DateTime();
if($datetime){
$reglement->setDate($datetime);
}
$reglement->setReference("Paytweak - $brand - $card - $orderId");
$entityManager->persist($reglement);
if($billingObj){
$reglement->setClient($billingObj->getClient());
$reglement->addBilling($billingObj);
$entityManager->persist($reglement);
$oldSolde = $billingObj->getSolde();
$newSolde = $oldSolde - $amount;
$billingObj->setSolde($newSolde);
if($newSolde <= 0) {
$billingObj->setStatus(Billing::STATUS_REGLEE);
}
$billingObj->setPaidPaytweak(true);
if($linkId){
$billingObj->setLinkPaytweak($linkId);
}
$entityManager->persist($billingObj);
}
$entityManager->persist($reglement);
}
}
if($notice === 'TRANSFER_PAYMENT'){
$status = isset($data['status']) ? $data['status'] : null ;
$amount = isset($data['amount']) ? $data['amount'] : null;
$card = isset($data['cardno']) ? $data['cardno'] : null;
$date = isset($data['date_payment']) ? $data['date_payment']: null;
$brand = isset($data['brand_network']) ? $data['brand_network'] : null;
$linkId = isset($data['link_id']) ? $data['link_id'] : null;
if($billingObj->getStatus() === Billing::STATUS_REGLEE){
return new JsonResponse('OK', 200);
}
if($status == PaytweakService::PAYMENT_STATUS_AUTHORIZED){
$reglement = new Reglement();
$reglement->setMode(Reglement::MODE_VIREMENT);
if($billingObj->getCompany()){
$reglement->setCompany($billingObj->getCompany());
}
if($amount){
$reglement->setTtc($amount);
}
$datetime= \DateTime::createFromFormat('d/m/Y\|H:i:s',$date );
if($datetime){
$reglement->setDate($datetime);
}
$reglement->setReference("Paytweak Virement - $brand - $card - $orderId");
$entityManager->persist($reglement);
if($billingObj){
$reglement->setClient($billingObj->getClient());
$reglement->addBilling($billingObj);
$entityManager->persist($reglement);
$oldSolde = $billingObj->getSolde();
$newSolde = $oldSolde - $amount;
$billingObj->setSolde($newSolde);
if($newSolde <= 0) {
$billingObj->setStatus(Billing::STATUS_REGLEE);
}
$billingObj->setPaidPaytweak(true);
if($linkId){
$billingObj->setLinkPaytweak($linkId);
}
$entityManager->persist($billingObj);
}
$entityManager->persist($reglement);
}
}
if($notice === 'PAYMENT'){
$status = isset($data['status']) ? $data['status'] : null ;
$amount = isset($data['amount']) ? $data['amount'] : null;
$card = isset($data['cardno']) ? $data['cardno'] : null;
$date = isset($data['date_payment']) ? $data['date_payment']: null;
$brand = isset($data['brand_network']) ? $data['brand_network'] : null;
$linkId = isset($data['link_id']) ? $data['link_id'] : null;
if($billingObj->getStatus() === Billing::STATUS_REGLEE){
return new JsonResponse('OK', 200);
}
if($status == PaytweakService::PAYMENT_STATUS_AUTHORIZED){
$reglement = new Reglement();
$reglement->setMode(Reglement::MODE_CB);
if($billingObj->getCompany()){
$reglement->setCompany($billingObj->getCompany());
}
if($amount){
$reglement->setTtc($amount);
}
//$datetime= new \DateTime();
$datetime= \DateTime::createFromFormat('d/m/Y\|H:i:s',$date );
if($datetime){
$reglement->setDate($datetime);
}
$reglement->setReference("Paytweak - $brand - $card - $orderId");
$entityManager->persist($reglement);
if($billingObj){
$reglement->setClient($billingObj->getClient());
$reglement->addBilling($billingObj);
$entityManager->persist($reglement);
$oldSolde = $billingObj->getSolde();
$newSolde = $oldSolde - $amount;
$billingObj->setSolde($newSolde);
if($newSolde <= 0) {
$billingObj->setStatus(Billing::STATUS_REGLEE);
}
$billingObj->setPaidPaytweak(true);
if($linkId){
$billingObj->setLinkPaytweak($linkId);
}
$entityManager->persist($billingObj);
}
$entityManager->persist($reglement);
}
}
$entityManager->flush();
return new JsonResponse('OK', 200);
}
private function isJsonRequest(Request $request) {
return 'json' === $request->getContentType();
}
private function transformJsonBody(Request $request) {
$data = json_decode($request->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}
if ($data === null) {
return true;
}
$request->request->replace($data);
return true;
}
}