PaymentUtils.php 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
<?php

namespace App\Utils;

use App\Entity\AchatMonnaieAdherent;
use App\Entity\AchatMonnaiePrestataire;
use App\Entity\Adherent;
use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire;
use App\Entity\Don;
use App\Entity\Geoloc;
use App\Entity\Groupe;
use App\Entity\Payment;
use App\Entity\Prestataire;
use App\Entity\Siege;
use App\Entity\User;
use App\Entity\Usergroup;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Payum\Core\Request\GetHumanStatus;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;

class PaymentUtils
{
    private $em;
    private $serializer;
    private $userManager;
    private $operationUtils;
    private $tavCotisationsUtils;
    private $container;

    /**
     * PaymentUtils constructor.
     */
    public function __construct(
        EntityManagerInterface $em,
        SerializerInterface $serializer,
        UserManagerInterface $userManager,
        OperationUtils $operationUtils,
        TAVCotisationUtils $tavCotisationsUtils,
        ContainerInterface $container
    ) {
        $this->em = $em;
        $this->serializer = $serializer;
        $this->userManager = $userManager;
        $this->operationUtils = $operationUtils;
        $this->tavCotisationsUtils = $tavCotisationsUtils;
        $this->container = $container;
    }

    /**
     * @param $payment
     * @return void
     * @throws \Exception
     */
    public function handlePayzenNotificationCore($payment): void
    {
        $current_payment_status = $payment->getStatus();

        // Payment can be captured if it hasn't been captured before
        // Allow STATUS_CAPTURED and STATUS_AUTHORIZED for recurrent payment only.
        $flux_array = json_decode($payment->getFluxData(), true);

        $type = $payment->getDescription();
        if (Payment::TYPE_ACHAT_MONNAIE_ADHERENT == $type) {
            $flux = $this->serializer->deserialize(
                $payment->getFluxData(),
                AchatMonnaieAdherent::class,
                'json',
                ['disable_type_enforcement' => true]
            );

            $exp = $this->em->getRepository(Siege::class)->find($flux_array['expediteur']);
            $flux->setExpediteur($exp);

            $dest = $this->em->getRepository(Adherent::class)->find($flux_array['destinataire']);
            $flux->setDestinataire($dest);

            $op = $this->em->getRepository(User::class)->find($flux_array['operateur']);
            $flux->setOperateur($op);
            $flux->setReconverti(true);

            if (null != $flux->getDon()) {
                $flux->getDon()->setType(Don::TYPE_DON_ADHERENT);
                $flux->getDon()->setOperateur($op);
                $flux->getDon()->setExpediteur($dest);
                $flux->getDon()->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]));
            }
        } else if (Payment::TYPE_ACHAT_MONNAIE_PRESTA == $type) {
            $flux = $this->serializer->deserialize(
                $payment->getFluxData(),
                AchatMonnaiePrestataire::class,
                'json',
                ['disable_type_enforcement' => true]
            );

            $exp = $this->em->getRepository(Siege::class)->find($flux_array['expediteur']);
            $flux->setExpediteur($exp);

            $dest = $this->em->getRepository(Prestataire::class)->find($flux_array['destinataire']);
            $flux->setDestinataire($dest);

            $op = $this->em->getRepository(User::class)->find($flux_array['operateur']);
            $flux->setOperateur($op);
            $flux->setReconverti(true);

            if (null != $flux->getDon()) {
                $flux->getDon()->setType(Don::TYPE_DON_PRESTATAIRE);
                $flux->getDon()->setOperateur($op);
                $flux->getDon()->setExpediteur($dest);
                $flux->getDon()->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]));
            }
        } else if (Payment::TYPE_COTISATION_ADHERENT == $type) {
            $flux = $this->serializer->deserialize(
                $payment->getFluxData(),
                CotisationAdherent::class,
                'json',
                ['disable_type_enforcement' => true]
            );

            $exp = $this->em->getRepository(Adherent::class)->find($flux_array['expediteur']);
            $flux->setExpediteur($exp);

            $dest = $this->em->getRepository(Prestataire::class)->find($flux_array['destinataire']);
            $flux->setDestinataire($dest);

            $op = $this->em->getRepository(User::class)->find($flux_array['operateur']);
            $flux->setOperateur($op);

            $flux->setRecu(true);

            if (null != $flux->getDon()) {
                $flux->getDon()->setType(Don::TYPE_DON_ADHERENT);
                $flux->getDon()->setOperateur($op);
                $flux->getDon()->setExpediteur($exp);
                $flux->getDon()->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]));
            }
        } else if (Payment::TYPE_COTISATION_PRESTA == $type) {
            $flux = $this->serializer->deserialize(
                $payment->getFluxData(),
                CotisationPrestataire::class,
                'json',
                ['disable_type_enforcement' => true]
            );

            $exp = $this->em->getRepository(Prestataire::class)->find($flux_array['expediteur']);
            $flux->setExpediteur($exp);

            $dest = $this->em->getRepository(Prestataire::class)->find($flux_array['destinataire']);
            $flux->setDestinataire($dest);

            $op = $this->em->getRepository(User::class)->find($flux_array['operateur']);
            $flux->setOperateur($op);

            $flux->setRecu(true);

            if (null != $flux->getDon()) {
                $flux->getDon()->setType(Don::TYPE_DON_PRESTATAIRE);
                $flux->getDon()->setOperateur($op);
                $flux->getDon()->setExpediteur($exp);
                $flux->getDon()->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]));
            }
        } else if (Payment::TYPE_ADHESION == $type) {
            $new_adherent_data = json_decode($payment->getExtraData());

            $adherent = new Adherent();
            $user = $this->userManager->createUser();
            $usergroup = $this->em->getRepository(Usergroup::class)->findOneByName('Adherent');
            $group = $this->em->getRepository(Groupe::class)->findOneBy(array('id' => $new_adherent_data->groupe->id));

            $user->setEmail($new_adherent_data->user->email);
            $user->setUsername($new_adherent_data->user->username);
            $user->setFirstname($new_adherent_data->user->firstname);
            $user->setLastname($new_adherent_data->user->lastname);
            $user->setPlainPassword($new_adherent_data->user->plainPassword);
            $user->setEnabled(true);
            $user->addPossiblegroup($usergroup);
            $user->addGroup($usergroup);
            $user->addRole('ROLE_ADHERENT');
            $user->setAdherent($adherent);
            $adherent->setEcompte('0');
            $adherent->setUser($user);
            $adherent->setGroupe($group);

            if ($adherent->getGeoloc() == null) {
                $geoloc = new Geoloc();
                $geoloc->setAdresse($new_adherent_data->geoloc->adresse);
                $geoloc->setCpostal($new_adherent_data->geoloc->cpostal);
                $geoloc->setVille($new_adherent_data->geoloc->ville);
                $adherent->setGeoloc($geoloc);
            }

            $this->em->persist($adherent);
            $this->em->flush();

            // Create first cotisation
            $flux = $this->serializer->deserialize(
                $payment->getFluxData(),
                CotisationAdherent::class,
                'json',
                ['disable_type_enforcement' => true]
            );

            $flux->setOperateur($user);
            $flux->setExpediteur($adherent);
            $flux->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(array('mlc' => true)));
            $flux->setRole('Adherent');
            $flux->setRecu(true);

            // Update payment with new user id, remove user data
            $payment->setClientId($user->getId());
            $payment->setExtraData('');
            $this->em->persist($payment);
        } else if (Payment::TYPE_PAIEMENT_COTISATION_TAV == $type || Payment::TYPE_PAIEMENT_RECURRENT_COTISATION_TAV) {
            $flux = $this->serializer->deserialize(
                $payment->getFluxData(),
                AchatMonnaieAdherent::class,
                'json',
                ['disable_type_enforcement' => true]
            );

            $exp = $this->em->getRepository(Siege::class)->find($flux_array['expediteur']);
            $flux->setExpediteur($exp);

            $dest = $this->em->getRepository(Adherent::class)->find($flux_array['destinataire']);
            $flux->setDestinataire($dest);

            $op = $this->em->getRepository(User::class)->find($flux_array['operateur']);
            $flux->setOperateur($op);
            $flux->setReconverti(true);

            //For recurrent payment: save don only for first payment, otherwise remove don
            //Use current payment status to decide (if first payment -> add don, if not -> remove don)
            if (null != $flux->getDon() && $current_payment_status === GetHumanStatus::STATUS_NEW) {
                $flux->getDon()->setType(Don::TYPE_DON_ADHERENT);
                $flux->getDon()->setOperateur($op);
                $flux->getDon()->setExpediteur($dest);
                $flux->getDon()->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]));
            } else {
                $flux->setDon(null);
            }
        } else {
            // Bad request
        }

        $this->em->persist($flux);
        $this->operationUtils->executeOperations($flux);

        if (Payment::TYPE_PAIEMENT_COTISATION_TAV == $type || Payment::TYPE_PAIEMENT_RECURRENT_COTISATION_TAV) {
            // Create new flux for cotisation, depending on process
            if ($this->container->getParameter('household_based_allowance')) {
                $this->tavCotisationsUtils->applyHouseholdAllowance($flux);
            } else {
                $this->tavCotisationsUtils->applyTauxCotisation($flux);
            }
        }
    }
}