PaymentStatusExtension.php 13 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
<?php

namespace App\EventListener;

use Payum\Core\Extension\Context;
use Payum\Core\Extension\ExtensionInterface;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Request\Generic;
use Payum\Core\Request\GetHumanStatus;
use Payum\Core\Request\GetStatusInterface;
use Payum\Core\Bridge\Symfony\Event\ExecuteEvent;

use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\SerializerInterface;
use App\Events\MLCEvents;
use App\Events\FluxEvent;
use App\Entity\Flux;
use App\Entity\Payment;
use App\Entity\PaymentToken;
use App\Entity\Siege;
use App\Entity\User;
use App\Entity\Adherent;
use App\Entity\Prestataire;
use App\Entity\Geoloc;
use App\Entity\Groupe;
use App\Entity\Usergroup;
use App\Entity\AchatMonnaieAdherent;
use App\Entity\AchatMonnaiePrestataire;
use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire;
use App\Entity\Don;
34 35
use App\Utils\OperationUtils;
use App\Utils\TAVCotisationUtils;
36 37 38 39

class PaymentStatusExtension implements ExtensionInterface
{
    private $em;
40 41 42 43 44
    private $eventDispatcher;
    private $serializer;
    private $userManager;
    private $operationUtils;
    private $tavCotisationsUtils;
45 46 47 48 49 50

    /**
     * PaymentStatusExtension constructor.
     *
     * @param EntityManagerInterface $em
     */
51 52 53 54 55 56 57 58
    public function __construct(
        EntityManagerInterface $em, 
        EventDispatcherInterface $eventDispatcher, 
        SerializerInterface $serializer, 
        UserManagerInterface $userManager,
        OperationUtils $operationUtils,
        TAVCotisationUtils $tavCotisationsUtils
    ) {
59 60 61 62
        $this->em = $em;
        $this->eventDispatcher = $eventDispatcher;
        $this->serializer = $serializer;
        $this->userManager = $userManager;
63 64
        $this->operationUtils = $operationUtils;
        $this->tavCotisationsUtils = $tavCotisationsUtils;
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
    }

    /**
     * Fired after a payum 'execute' to detect payment status changes after a notification
     * {@inheritDoc}
     */
    public function onPostExecute(Context $context)
    {
        $request = $context->getRequest();
        if (false == $request instanceof Generic) {
            return;
        }
        if ($request instanceof GetStatusInterface) {
            return;
        }

        $payment = $request->getFirstModel();
        if (false == $payment instanceof PaymentInterface) {
            return;
        }

        try {
            $token = $request->getToken();
        } catch (\Exception $e) {
            return;
        }

        // Get current & new status
        $context->getGateway()->execute($status = new GetHumanStatus($payment));
        $current_payment_status = $payment->getStatus();

        // Payment can be captured if it hasn't been captured before 
        if ($current_payment_status !== GetHumanStatus::STATUS_CAPTURED 
            && $current_payment_status != GetHumanStatus::STATUS_AUTHORIZED) 
        {
            // If payment succesful, persist serialized 'Flux' stored in payment
            if ($status->getValue() == GetHumanStatus::STATUS_CAPTURED 
                || $status->getValue() == GetHumanStatus::STATUS_AUTHORIZED) 
            {
                $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);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
                } else if (Payment::TYPE_PAIEMENT_COTISATION_TAV == $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]));
                    }
280 281 282 283 284
                } else {
                    // Bad request
                }

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

287 288 289 290
                if (Payment::TYPE_PAIEMENT_COTISATION_TAV == $type) {
                    // Apply cotisation rate, create new flux
                    $this->tavCotisationsUtils->applyTauxCotisation($flux);
                }
291 292 293

                // Invalidate (delete) notify token after payment is captured
                $this->em->remove($token);
294 295

                $this->em->flush();
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
            }
        }

        // Update payment status with status received in payzen response
        $payment->setStatus($status->getValue());
        $this->em->persist($payment);
        $this->em->flush();
    }

    /**
     * {@inheritDoc}
     */
    public function onPreExecute(Context $context)
    {
    }

    /**
     * {@inheritDoc}
     */
    public function onExecute(Context $context)
    {
    }
}