PaymentStatusExtension.php 3.56 KB
Newer Older
1 2 3 4
<?php

namespace App\EventListener;

5 6 7 8 9
use App\Utils\OperationUtils;
use App\Utils\PaymentUtils;
use App\Utils\TAVCotisationUtils;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
10 11 12 13 14 15
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;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17 18 19 20 21 22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\SerializerInterface;

class PaymentStatusExtension implements ExtensionInterface
{
    private $em;
23 24 25 26 27
    private $eventDispatcher;
    private $serializer;
    private $userManager;
    private $operationUtils;
    private $tavCotisationsUtils;
28
    private $container;
29
    private $paymentUtils;
30 31 32 33 34 35

    /**
     * PaymentStatusExtension constructor.
     *
     * @param EntityManagerInterface $em
     */
36
    public function __construct(
37 38 39
        EntityManagerInterface $em,
        EventDispatcherInterface $eventDispatcher,
        SerializerInterface $serializer,
40 41
        UserManagerInterface $userManager,
        OperationUtils $operationUtils,
42
        TAVCotisationUtils $tavCotisationsUtils,
43 44
        ContainerInterface $container,
        PaymentUtils $paymentUtils
45
    ) {
46 47 48 49
        $this->em = $em;
        $this->eventDispatcher = $eventDispatcher;
        $this->serializer = $serializer;
        $this->userManager = $userManager;
50 51
        $this->operationUtils = $operationUtils;
        $this->tavCotisationsUtils = $tavCotisationsUtils;
52
        $this->container = $container;
53
        $this->paymentUtils = $paymentUtils;
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
    }

    /**
     * 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));
83

84
        $current_payment_status = $payment->getStatus();
85 86 87 88 89 90 91 92 93 94 95
        $new_status = $status->getValue();

        if (
            GetHumanStatus::STATUS_CAPTURED !== $current_payment_status
            && GetHumanStatus::STATUS_AUTHORIZED != $current_payment_status
        ) {
            if (
                GetHumanStatus::STATUS_CAPTURED == $new_status
                || GetHumanStatus::STATUS_AUTHORIZED == $new_status
            ) {
                $this->paymentUtils->handlePayzenNotificationCore($payment);
96
                // Invalidate (delete) notify token after payment is captured
97
                $this->em->remove($token);
98
                $this->em->flush();
99 100 101
            }
        }

102
        //Update status
103
        // Update payment status with status received in payzen response
104 105 106
        $payment->setStatus($new_status);

        //Flush
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        $this->em->persist($payment);
        $this->em->flush();
    }

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

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