<?php
namespace App\Controller;
use App\Entity\App\AppMenu;
use App\Entity\App\AppProfile;
use App\Entity\App\AppUser;
use App\Entity\Rcd\RcdDonation;
use App\Entity\Rcd\RcdInvoice;
use App\Entity\Rcd\RcdLogs;
use App\Entity\Rcd\RcdLogsDonations;
use App\Entity\Rcd\RcdOrganization;
use App\Entity\Rcd\RcdProject;
use App\Entity\Rcd\RcdQuota;
use App\Entity\Rcd\RcdUserBkToken;
use App\Form\Rcd\PersonLegal\RcdUserLegalType;
use App\Form\Rcd\PersonNatural\RcdUserNaturalType;
use App\Form\Rcd\RcdUserAnonymousType;
use App\Services\App\AppTools;
use App\Services\App\FileUploader;
use App\Services\Transbank\ServiceOneClickMall;
use App\Services\Transbank\ServiceWebpayPlus;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class IndexController extends AbstractController
{
public $appTools;
public $serviceWebpayPlus;
public $serviceOneclickMall;
private $responseCode = [
0 => 'Transacción aprobada',
-1 => 'Rechazo de transacción - Reintente (Posible error en el ingreso de datos de la transacción)',
-2 => 'Rechazo de transacción (Se produjo fallo al procesar la transacción. Este mensaje de rechazo está relacionado a parámetros de la tarjeta y/o su cuenta asociada)',
-3 => 'Error en transacción (Interno Transbank)',
-4 => 'Rechazo emisor (Rechazada por parte del emisor)',
-5 => 'Rechazo - Posible Fraude (Transacción con riesgo de posible fraude)',
-96 => 'tbk_user no existente',
-97 => 'Límites Oneclick, máximo monto diario de pago excedido.',
-98 => 'Límites Oneclick, máximo monto de pago excedido',
-99 => 'Límites Oneclick, máxima cantidad de pagos diarios excedido.',
];
public function __construct(AppTools $appTools, ServiceWebpayPlus $serviceWebpayPlus, ServiceOneClickMall $serviceOneClickMall)
{
$this->appTools = $appTools;
$this->serviceWebpayPlus = $serviceWebpayPlus;
$this->serviceOneclickMall = $serviceOneClickMall;
}
private function newTransactionWebpayPlus($idUser, RcdOrganization $rcdOrganization, $amount, $project, $idRcDonation, $newUser)
{
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$url = $protocol.'://'.$this->appTools->requestStack()->getHttpHost().$this->generateUrl('index_responseWebPayPlus',[
'token' => $rcdOrganization->getToken(),
'project' => $project,
'rcDonation' => $idRcDonation,
'user' => $idUser,
'newUser' => $newUser? 'yes': 'nop',
]);
if($rcdOrganization->getWebPayPlusType()){
$response = $this->serviceWebpayPlus->newTransactionMall($amount, $url, $rcdOrganization, $idRcDonation);
}else{
$response = $this->serviceWebpayPlus->newTransaction($amount, $url, $rcdOrganization, $idRcDonation);
}
return [
'url' => $response['url'],
'token' => $response['token']
];
}
private function newInscriptionOneClickMall(AppUser $appUser, RcdOrganization $rcdOrganization, $amount, $project, $idRcDonation, $newUser)
{
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$json = json_encode([
'token' => $rcdOrganization->getToken(),
'project' => $project,
'rcDonation' => $idRcDonation,
'user' => $appUser->getId(),
'newUser' => $newUser? 'yes': 'nop',
'mount' => $amount,
]);
$url = $protocol.'://'.$this->appTools->requestStack()->getHttpHost().$this->generateUrl('index_responseOneClickMall',[
'token' => base64_encode($json),
#'project' => $project,
#'user' => $appUser->getId(),
#'mount' => ,
]);
$response = $this->serviceOneclickMall->newInscription($appUser->getUsername(), $appUser->getEmail(), $url, $rcdOrganization);
return [
'url' => $response['url'],
'token' => $response['token']
];
}
private function registerFirstDonation($appUser, $amount, $token, $idProject)
{
$em = $this->getDoctrine()->getManager();
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token' => $token]);
$rcdProject = null;
if ($idProject) {
$rcdProject = $em->getRepository(RcdProject::class)->find($idProject);
}
$observation = null;
if($appUser->getRcdPerson()){
$observation = $appUser->getRcdPerson()->getObservation();
}
$rcdInvoice = new RcdInvoice();
$rcdInvoice
->setAmount($amount)
->setAppUser($appUser)
->setRcdOrganization($rcdOrganization)
->setRcdProject($rcdProject)
->setStatusTransaction('PE')
->setCreationDate(new \DateTime())
;
$em->persist($rcdInvoice);
$rcdDonate = new RcdDonation();
$rcdDonate
->setRcdInvoice($rcdInvoice)
->setAppUser($appUser)
->setObservation($observation)
->setRcdProject($rcdProject)
->setRcdOrganization($rcdOrganization)
->setCreationDate(new \DateTime())
->setAmount($amount)
->setCode(null)
->setToken(null)
->setAuthorizationCode(null)
->setResponseCode(null)
->setPaymentTypeCode(null)
->setVci(null)
->setStatusTransaction('PENDING')
;
if($appUser->getRcdPerson()){
if($appUser->getRcdPerson()->getNumberQuota() == 1 ){
$rcdDonate->setFlagRecurringDonation(true);
} else {
$rcdDonate->setFlagRecurringDonation(false);
}
} else {
$rcdDonate->setFlagRecurringDonation(false);
}
$em->persist($rcdDonate);
$em->flush();
return $rcdDonate->getId();
}
/**
* @Route("/personNatural", name="index_personNatural", methods={"POST","GET"})
*/
public function personNatural(Request $request, UserPasswordEncoderInterface $encoder, FileUploader $fileUploader, \Swift_Mailer $mailer): Response
{
$em = $this->getDoctrine()->getManager();
$token = $request->get('rcdOrganization');
$type = $request->get('type');
$idProject = $request->get('project');
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
$formSubmit = $request->request->get('form');
if($this->getUser()){
$appUser = $em->getRepository(AppUser::class)->find($this->getUser()->getId());
}elseif(isset($formSubmit['email'])){
$appUser = $em->getRepository(AppUser::class)->loadUserByUsername($formSubmit['email']);
if(!$appUser){
$appUser = new AppUser();
}
}else{
$appUser = new AppUser();
}
$form = $this->createForm(RcdUserNaturalType::class, $appUser, [
'action' => $this->generateUrl('index_personNatural', [
'type' => $type,
'project' => $idProject,
'rcdOrganization' => $token,
]),
'attr' => [
'id' => 'form',
'method' => 'POST',
'autocomplete' => 'off',
]
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newUser = true;
if($appUser->getId()){
$newUser = false;
} else {
$username = explode("@", $appUser->getEmail());
$appUser_userName = $em->getRepository(AppUser::class)->loadUserByUsername($username[0]);
if($appUser_userName){
$username = $username[0].uniqid();
} else {
$username = $username[0];
}
$appUser->setSalt(md5(time()));
$password = substr($appUser->getSalt(), 0, 5);
$encoded = $encoder->encodePassword($appUser, $password);
$appUser->setPassword($encoded);
$appProfile = $em->getRepository(AppProfile::class)->findOneBy([
'code' => 'DON'
]);
$appUser
->setUsername($username)
->setCreationDate(new \DateTime())
->setFlagDelete(false)
->setFlagStatus(true)
->setFlagAccess(true)
->setAppProfile($appProfile)
;
}
$rcdPerson = $appUser->getRcdPerson();
if($rcdPerson->getBusinessName()){
if($type == 'dnr'){
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(true);
}
} else {
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(false);
}
}
$rcdPerson
->setFlagTypePerson(true)
;
}else{
if($type == 'dnr'){
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(false);
}
} else {
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(false);
}
}
$rcdPerson
->setFlagTypePerson(false)
;
}
if($rcdPerson->getFlagPay()){
$rcdPerson->setFlagPay(true);
} else {
$rcdPerson->setFlagPay(false);
}
$em->persist($appUser);
$em->flush();
if($newUser){
$this->appTools->sendMail($appUser, $password);
}
$data = $request->get('form');
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
#####
## Guardar donacion
#####
if($type == 'dnu'){
$idUser = $appUser->getId();
$idRcDonation = $this->registerFirstDonation($appUser, $data['amount'], $token, $idProject);
$newTransaction = $this->newTransactionWebpayPlus($idUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/payWebPayPlus.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
} elseif($type == 'dnp') {
if($rcdPerson->getNumberQuota() == 1){
$idUser = $appUser->getId();
$idRcDonation = $this->registerFirstDonation($appUser, $data['amount'], $token, $idProject);
$newTransaction = $this->newTransactionWebpayPlus($idUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/payWebPayPlus.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
} else {
$amount = round($data['amount'] / $rcdPerson->getNumberQuota(),0);
$idRcDonation = $this->registerFirstDonation($appUser, $amount, $token, $idProject);
$newTransaction = $this->newInscriptionOneClickMall($appUser, $rcdOrganization, $amount, $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
}
} else {
$idRcDonation = $this->registerFirstDonation($appUser, $data['amount'], $token, $idProject);
$newTransaction = $this->newInscriptionOneClickMall($appUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
}
}
return $this->render('index/_formNatural.html.twig', [
'form' => $form->createView(),
'idProject' => $idProject,
'rcdOrganization' => $rcdOrganization
]);
}
/**
* @Route("/personLegal", name="index_personLegal", methods={"POST","GET"})
*/
public function personLegal(Request $request, UserPasswordEncoderInterface $encoder, FileUploader $fileUploader, \Swift_Mailer $mailer): Response
{
$em = $this->getDoctrine()->getManager();
$token = $request->get('rcdOrganization');
$type = $request->get('type');
$idProject = $request->get('project');
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
$formSubmit = $request->request->get('form');
if($this->getUser()){
$appUser = $em->getRepository(AppUser::class)->find($this->getUser()->getId());
}elseif(isset($formSubmit['email'])){
$appUser = $em->getRepository(AppUser::class)->loadUserByUsername($formSubmit['email']);
if(!$appUser){
$appUser = new AppUser();
}
}else{
$appUser = new AppUser();
}
$form = $this->createForm(RcdUserLegalType::class, $appUser, [
'action' => $this->generateUrl('index_personLegal', [
'rcdOrganization' => $token,
'type' => $type,
'project' => $idProject
]),
'attr' => [
'id' => 'form',
'method' => 'POST',
'autocomplete' => 'off',
]
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newUser = true;
if($appUser->getId()){
$newUser = false;
} else {
$username = explode("@", $appUser->getEmail());
$appUser_userName = $em->getRepository(AppUser::class)->loadUserByUsername($username[0]);
if($appUser_userName){
$username = $username[0].uniqid();
} else {
$username = $username[0];
}
$appUser->setSalt(md5(time()));
$password = substr($appUser->getSalt(), 0, 5);
$encoded = $encoder->encodePassword($appUser, $password);
$appUser->setPassword($encoded);
$appProfile = $em->getRepository(AppProfile::class)->findOneBy([
'code' => 'DON'
]);
$appUser
->setUsername($username)
->setCreationDate(new \DateTime())
->setFlagDelete(false)
->setFlagStatus(true)
->setFlagAccess(true)
->setAppProfile($appProfile)
;
}
$rcdPerson = $appUser->getRcdPerson();
if($rcdPerson->getBusinessName()){
if($type == 'dnr'){
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(true);
}
} else {
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(false);
}
}
$rcdPerson
->setFlagTypePerson(true)
;
}else{
if($type == 'dnr'){
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(false);
}
} else {
if($rcdPerson->getFlagRecurringDonation()){
$rcdPerson->setFlagRecurringDonation(true);
} else {
$rcdPerson->setFlagRecurringDonation(false);
}
}
$rcdPerson
->setFlagTypePerson(false)
;
}
if($rcdPerson->getFlagPay()){
$rcdPerson->setFlagPay(true);
} else {
$rcdPerson->setFlagPay(false);
}
$em->persist($appUser);
$em->flush();
if($newUser){
$this->appTools->sendMail($appUser, $password);
}
$data = $request->get('form');
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
#####
## Guardar donacion
#####
if($type == 'dnu'){
$idUser = $appUser->getId();
$idRcDonation = $this->registerFirstDonation($appUser, $data['amount'], $token, $idProject);
$newTransaction = $this->newTransactionWebpayPlus($idUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/payWebPayPlus.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
} elseif($type == 'dnp') {
if($rcdPerson->getNumberQuota() == 1){
$idUser = $appUser->getId();
$idRcDonation = $this->registerFirstDonation($appUser, $data['amount'], $token, $idProject);
$newTransaction = $this->newTransactionWebpayPlus($idUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/payWebPayPlus.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
}else{
$amount = round($data['amount'] / $rcdPerson->getNumberQuota(),0);
$idRcDonation = $this->registerFirstDonation($appUser, $amount, $token, $idProject);
$newTransaction = $this->newInscriptionOneClickMall($appUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
}
} else {
$idRcDonation = $this->registerFirstDonation($appUser, $data['amount'], $token, $idProject);
$newTransaction = $this->newInscriptionOneClickMall($appUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
}
}
return $this->render('index/_formLegal.html.twig', [
'form' => $form->createView(),
'idProject' => $idProject,
'rcdOrganization' => $rcdOrganization
]);
}
/**
* @Route("/personAnonymous", name="index_personAnonymous", methods={"POST","GET"})
*/
public function personAnonymous(Request $request, UserPasswordEncoderInterface $encoder, FileUploader $fileUploader, \Swift_Mailer $mailer): Response
{
$em = $this->getDoctrine()->getManager();
$token = $request->get('rcdOrganization');
$type = $request->get('type');
$idProject = $request->get('project');
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
$formSubmit = $request->request->get('form');
if($this->getUser()){
$appUser = $em->getRepository(AppUser::class)->find($this->getUser()->getId());
$email = $appUser->getEmail();
}elseif(isset($formSubmit['email'])){
$appUser = $em->getRepository(AppUser::class)->loadUserByUsername($formSubmit['email']);
$email = $appUser->getEmail();
if(!$appUser){
$appUser = new AppUser();
$email = 'anonimo@cloudbase.cl';
}
}else{
$appUser = new AppUser();
$email = 'anonimo@cloudbase.cl';
}
$form = $this->createForm(RcdUserAnonymousType::class, $appUser, [
'action' => $this->generateUrl('index_personAnonymous', [
'rcdOrganization' => $token,
'type' => $type,
'project' => $idProject
]),
'attr' => [
'id' => 'form',
'method' => 'POST',
'autocomplete' => 'off',
]
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newUser = true;
if($appUser->getId()){
$newUser = false;
} else {
$username = explode("@", $appUser->getEmail());
$appUser_userName = $em->getRepository(AppUser::class)->loadUserByUsername($username[0]);
if($appUser_userName){
$username = $username[0].uniqid();
} else {
$username = $username[0];
}
$appUser->setSalt(md5(time()));
$password = substr($appUser->getSalt(), 0, 5);
$encoded = $encoder->encodePassword($appUser, $password);
$appUser->setPassword($encoded);
$appProfile = $em->getRepository(AppProfile::class)->findOneBy([
'code' => 'DON'
]);
$appUser
->setName('Anónimo')
->setSurname(null)
->setUsername($username)
->setCreationDate(new \DateTime())
->setFlagDelete(false)
->setFlagStatus(true)
->setFlagAccess(true)
->setAppProfile($appProfile)
;
}
$em->persist($appUser);
$em->flush();
if($newUser){
$this->appTools->sendMail($appUser, $password);
}
if($type != 'dnr'){
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
$data = $request->get('form');
$idUser = $appUser->getId();
#####
## Guardar donacion
#####
$idRcDonation = $this->registerFirstDonation($appUser, $data['amount'], $token, $idProject);
$newTransaction = $this->newTransactionWebpayPlus($idUser, $rcdOrganization, $data['amount'], $idProject,$idRcDonation, $newUser);
return $this->render('index/TransBank/payWebPayPlus.html.twig', [
'url' => $newTransaction['url'],
'token' => $newTransaction['token'],
'rcdOrganization' => $rcdOrganization
]);
}
}
return $this->render('index/_formAnonymous.html.twig', [
'form' => $form->createView(),
'rcdOrganization' => $rcdOrganization,
'email' => $email
]);
}
/**
* @Route("/", name="index")
*/
public function index(Request $request): Response
{
$token = $request->get('token');
if($token){
$em = $this->getDoctrine()->getManager();
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
if($rcdOrganization){
$appUser = new AppUser();
return $this->render('index/index.html.twig', [
'rcdOrganization' => $rcdOrganization
]);
} else {
dump('la organizacion a la que esta intentando acceder no existe');
exit();
}
} else {
dump('es necesario usar el token para poder acceder');
exit();
}
}
private function debug($idOrganization, $var_dump, $exit = false)
{
if($idOrganization == 4) {
dump($var_dump);
if ($exit) {
exit();
}
}
}
/**
* @Route("/privacy", name="index_privacy")
*/
public function privacy(Request $request): Response
{
$token = $request->get('token');
if($token){
$em = $this->getDoctrine()->getManager();
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
if($rcdOrganization){
$appUser = new AppUser();
return $this->render('index/indexPrivacy.html.twig', [
'rcdOrganization' => $rcdOrganization
]);
} else {
dump('la organizacion a la que esta intentando acceder no existe');
exit();
}
} else {
dump('es necesario usar el token para poder acceder');
exit();
}
}
/**
* @Route("/responseWebPayPlus", name="index_responseWebPayPlus")
*/
public function responseWebPayPlus(Request $request, \Swift_Mailer $mailer): Response
{
$token = $request->get('token');
$idProject = $request->get('project');
$idRcDonation = $request->get('rcDonation');
$idUser = $request->get('user');
$newUser = $request->get('newUser');
$token_ws = $request->get('token_ws');
$em = $this->getDoctrine()->getManager();
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token' => $token]);
if($token_ws) {
$appUser = $em->getRepository(AppUser::class)->find($idUser);
$rcdProject = null;
if ($idProject) {
$rcdProject = $em->getRepository(RcdProject::class)->find($idProject);
}
if($rcdOrganization->getWebPayPlusType()) {
$response = $this->serviceWebpayPlus->responseTransactionMall($request->get('token_ws'), $rcdOrganization);
$amount = $response['details'][0]['amount'];
$buyOrder = $response['buyOrder'];
$authorizationCode = $response['details'][0]['authorizationCode'];
$responseCode = $response['details'][0]['responseCode'];
$paymentTypeCode = $response['details'][0]['paymentTypeCode'];
$vci = $response['vci'];
$status = $response['details'][0]['status'];
$responseCodeText = $response['details'][0]['responseCodeText'];
} else {
$response = $this->serviceWebpayPlus->responseTransaction($request->get('token_ws'), $rcdOrganization);
$amount = $response['amount'];
$buyOrder = $response['buyOrder'];
$authorizationCode = $response['authorizationCode'];
$responseCode = $response['responseCode'];
$paymentTypeCode = $response['paymentTypeCode'];
$vci = $response['vci'];
$status = $response['status'];
$responseCodeText = $response['responseCodeText'];
}
$rcdLogs = new RcdLogsDonations();
$rcdLogs
->setCreationDate(new \DateTime())
->setAppUser($appUser)
->setDescription(json_encode($response))
;
$em->persist($rcdLogs);
$em->flush();
$rcdDonate = $em->getRepository(RcdDonation::class)->find($idRcDonation);
$rcdDonate
->setCode($buyOrder)
->setToken($token_ws)
->setAuthorizationCode($authorizationCode)
->setResponseCode($responseCode)
->setPaymentTypeCode($paymentTypeCode)
->setVci($vci)
->setStatusTransaction($status)
->setFlagRecurringDonation(false)
;
$rcdInvoice = $rcdDonate->getRcdInvoice();
if($responseCode != 0) {
$rcdInvoice
->setStatusTransaction('ER')
;
} else {
$rcdInvoice
->setStatusTransaction('OK')
;
}
$rcdInvoice->setFlagFlagType(false);
$em->persist($rcdDonate);
$em->persist($rcdInvoice);
$em->flush();
if($newUser == 'yes'){
if($responseCode == 0){
$this->appTools->sendMailDonationNoRecurrence($appUser, $rcdDonate->getAmount());
} else {
$this->appTools->sendMailDonationError($appUser);
}
} else {
if($responseCode != 0){
$this->appTools->sendMailDonationError($appUser);
}
}
return $this->redirectToRoute('index_responseHtml',[
'codeResponse' => $responseCode,
'buyOrder' => $buyOrder,
'amount' => $amount,
'rcdDonateDate' => $rcdDonate->getCreationDate()->format('d-m-Y'),
'idOrganization' => $rcdOrganization->getId(),
'idRcdProject' => $idProject
]);
} else {
return $this->redirectToRoute('index',['token'=>$token]);
}
}
/**
* @Route("/responseOneClickMall", name="index_responseOneClickMall")
*/
public function responseOneClickMall(Request $request, \Swift_Mailer $mailer): Response
{
$token = $request->get('token');
$decryp = json_decode(base64_decode($token), true);
$token = $decryp['token'];
$idUser = $decryp['user'];
$newUser = $decryp['newUser'];
$idProject = $decryp['project'];
$idRcDonation = $decryp['rcDonation'];
$mount = $decryp['mount'];
$token_tbk = $request->get('TBK_TOKEN');
$em = $this->getDoctrine()->getManager();
$rcdOrganization = $em->getRepository(RcdOrganization::class)->findOneBy(['token' => $token]);
$response = $this->serviceOneclickMall->confirmInscription($token_tbk,$rcdOrganization);
if($response['tbkUser']) {
$appUser = $em->getRepository(AppUser::class)->find($idUser);
$numberQuota = $appUser->getRcdPerson()->getNumberQuota();
$rcdProject = null;
if ($idProject) {
$rcdProject = $em->getRepository(RcdProject::class)->find($idProject);
}
$rcdQuota = false;
if($numberQuota > 1){
$numberQuota = $numberQuota-1;
$date = new \DateTime();
$dateNewDonation = $date->add(new \DateInterval("P{$numberQuota}M"));
$rcdQuota = new RcdQuota();
$rcdQuota
->setNumberQuota($appUser->getRcdPerson()->getNumberQuota())
->setAppUser($appUser)
->setAmount($mount)
->setTbkUser($response['tbkUser'])
->setDate(new \DateTime())
->setDateFin($dateNewDonation)
->setRcdOrganization($rcdOrganization)
->setRcdProject($rcdProject)
->setFlagFinish(false)
;
$em->persist($rcdQuota);
$buyOrder = str_pad($idRcDonation.date('YmdHis'), 26, 0, STR_PAD_LEFT);
$response = $this->serviceOneclickMall->newTransaction($appUser->getUsername(), $rcdQuota->getTbkUser(), $buyOrder, [
[
"commerce_code" => $rcdOrganization->getOneClickMallCommerceCodeSell(),
"buy_order" => $buyOrder,
"amount" => $mount,
"installments_number" => 0
],
],$rcdOrganization);
$rcdLogs = new RcdLogsDonations();
$rcdLogs
->setCreationDate(new \DateTime())
->setAppUser($appUser)
->setDescription(json_encode($response))
;
$em->persist($rcdLogs);
$em->flush();
$rcdDonate = $em->getRepository(RcdDonation::class)->find($idRcDonation);
$rcdDonate
->setCode($response['details'][0]['buyOrder'])
->setToken($response['details'][0]['commerceCode'])
->setAuthorizationCode($response['details'][0]['authorizationCode'])
->setResponseCode($response['details'][0]['responseCode'])
->setPaymentTypeCode($response['details'][0]['paymentTypeCode'])
->setStatusTransaction($response['details'][0]['status'])
;
if ($appUser->getRcdPerson()->getNumberQuota() == 1) {
$rcdDonate->setFlagRecurringDonation(true);
} else {
$rcdDonate->setFlagRecurringDonation(false);
}
$rcdInvoice = $rcdDonate->getRcdInvoice();
if($response['details'][0]['responseCode'] != 0) {
$rcdInvoice
->setStatusTransaction('ER')
;
} else {
$rcdInvoice
->setStatusTransaction('OK')
;
}
$em->persist($rcdDonate);
if($rcdDonate->getResponseCode() == 0) {
$rcdInvoice->setRcdQuota($rcdQuota);
}
$em->persist($rcdInvoice);
$em->flush();
if($rcdDonate->getResponseCode() != 0) {
if($rcdQuota){
$em->remove($rcdQuota);
$em->flush();
} else {
$rcdPerson = $appUser->getRcdPerson();
$rcdPerson
->setFlagRecurringDonation(false)
->setDateRecurringDonation(null)
->setAmountRecurringDonation(0)
;
$em->persist($rcdPerson);
$em->flush();
}
}
if($newUser == 'yes'){
if($rcdDonate->getResponseCode() == 0){
$this->appTools->sendMailDonationRecurrence($appUser, $rcdDonate->getAmount(), $appUser->getRcdPerson()->getDateRecurringDonation()->format('d'));
} else {
$this->appTools->sendMailDonationError($appUser);
}
} else {
if($rcdDonate->getResponseCode() != 0){
$this->appTools->sendMailDonationError($appUser);
}
}
return $this->redirectToRoute('index_responseHtml',[
'codeResponse' => $response['details'][0]['responseCode'],
'buyOrder' => $response['details'][0]['buyOrder'],
'amount' => $response['details'][0]['amount'],
'rcdDonateDate' => $rcdDonate->getCreationDate()->format('d-m-Y'),
'idOrganization' => $rcdOrganization->getId(),
'idRcdProject' => $idProject
]);
} else {
$dayDonations = $rcdOrganization->getPayDay();
$date = new \DateTime(date('Y-m-').$dayDonations);
if($date->format('Y-m-d') < date('Y-m-d')){
$date = $date->add(new \DateInterval("P1M"));
}
$appUser
->setRcdOrganization($rcdOrganization)
;
$rcdPerson = $appUser->getRcdPerson();
$rcdPerson
->setTbkUser($response['tbkUser'])
->setTypeCard($response['cardType'])
->setNumberCard($response['cardNumber'])
->setFlagRecurringDonation(true)
->setDateRecurringDonation($date)
->setAmountRecurringDonation($mount);
$em->persist($appUser);
$em->persist($rcdPerson);
$rcdUserBkToken = new RcdUserBkToken();
$rcdUserBkToken
->setFlagFlagDelete(false)
->setAppUser($appUser)
->setRcdOrganization($rcdOrganization)
->setAmountRecurringDonation($mount)
->setDateRecurringDonation($date)
->setTbkUser($response['tbkUser'])
->setTypeCard($response['cardType'])
->setNumberCard($response['cardNumber'])
;
$em->persist($rcdUserBkToken);
$rcdDonate = $em->getRepository(RcdDonation::class)->find($idRcDonation);
$rcdInvoice = $rcdDonate->getRcdInvoice();
$em->remove($rcdDonate);
$em->remove($rcdInvoice);
$em->flush();
return $this->redirectToRoute('index_responseHtml2',[
'amount' => $mount,
'rcdDonateDate' => $date->format('d-m-Y'),
'idOrganization' => $rcdOrganization->getId()
]);
}
} else {
return $this->redirectToRoute('index',['token'=>$token]);
}
}
/**
* @Route("/responseHtml/{idOrganization}/{codeResponse}/{buyOrder}/{amount}/{rcdDonateDate}/{idRcdProject}", name="index_responseHtml")
*/
public function responseHtml($idOrganization, $codeResponse, $buyOrder, $amount, $rcdDonateDate, $idRcdProject): Response
{
$em = $this->getDoctrine()->getManager();
$rcdOrganization = $em->getRepository(RcdOrganization::class)->find($idOrganization);
$rcdProject = $em->getRepository(RcdProject::class)->find($idRcdProject);
if(isset($this->responseCode[$codeResponse])){
$responseCode = $this->responseCode[$codeResponse];
} else {
$responseCode = 'error interno';
}
return $this->render('index/TransBank/responseHtmlWeb.html.twig', [
'responseCodeText' => $responseCode,
'codeResponse' => $codeResponse,
'buyOrder' => $buyOrder,
'amount' => $amount,
'date' => $rcdDonateDate,
'rcdOrganization' => $rcdOrganization,
'rcdProject' => $rcdProject,
]);
}
/**
* @Route("/responseHtml2/{idOrganization}/{amount}/{rcdDonateDate}", name="index_responseHtml2")
*/
public function responseHtml2($idOrganization, $amount, $rcdDonateDate): Response
{
$em = $this->getDoctrine()->getManager();
$rcdOrganization = $em->getRepository(RcdOrganization::class)->find($idOrganization);
return $this->render('index/TransBank/responseHtmlWeb2.html.twig', [
'amount' => $amount,
'date' => $rcdDonateDate,
'rcdOrganization' => $rcdOrganization
]);
}
/**
* @Route("/test", name="index_test")
*/
public function test(Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$appUser = $em->getRepository(AppUser::class)->find(1);
$rcdInvoice = $em->getRepository(RcdInvoice::class)->find(105);
$this->appTools->sendMailDonationsError($rcdInvoice, $appUser);
exit();
}
}