PaymentRepository.php 2.89 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7
<?php

namespace App\Repository;

use App\Entity\Payment;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
8
use Payum\Core\Request\GetHumanStatus;
9
use Symfony\Component\HttpClient\HttpClient;
Julien Jorry committed
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

/**
 * @method Siege|null find($id, $lockMode = null, $lockVersion = null)
 * @method Siege|null findOneBy(array $criteria, array $orderBy = null)
 * @method Siege[]    findAll()
 * @method Siege[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PaymentRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Payment::class);
    }

    /**
     * @param int $id User id
     *
     * @return Payment|null Returns user's last payment
     */
    public function getUserLastPayment($id)
    {
        $results = $this->findBy(
            ['clientId' => $id],
            ['id' => 'DESC'],
            1,
            0
        );

        if (empty($results)) {
            return null;
        } else {
            return $results[0];
        }
    }
44 45 46

    /*
     * Everytime a user clicks on "Payer en CB" button on the kohinos side,
Yvon Kerdoncuff committed
47
     * a Payment entry is created.
48 49 50 51
     * We want to prevent a user from starting several payments, which can
     * lead to duplicates.
     * We mark as unvalid Payment that are unvalid when we check them.
     */
52
    public function findValidStartingPayment($clientEmail)
53
    {
Yvon Kerdoncuff committed
54
        //Remark : we don't know how to redirect the user to the existing payment page,
55
        //we simply return the datetime when the payment will be expired.
56 57 58 59 60 61
        $candidates = $this->findBy([
            'clientEmail' => $clientEmail,
            'startingPaymentAnalysisStatus' => null,
        ]);

        foreach ($candidates as $p) {
62 63 64 65
            if (!in_array($p->getStatus(),[null,GetHumanStatus::STATUS_NEW])) {
                $p->setStartingPaymentAnalysisStatus('NOT CONCERNED');
                continue;
            }
66
            if (!$p->getCreatedAt()) {
67 68
                $p->setStartingPaymentAnalysisStatus('TIMEOUT');
            } else {
69 70 71 72 73 74 75
                $createdAt = clone $p->getCreatedAt(); //don't modify original object
                $timeout = $createdAt->add(\DateInterval::createFromDateString("10 minutes"));
                if ($timeout < new \DateTime()) {
                    $p->setStartingPaymentAnalysisStatus('TIMEOUT');
                } else {
                    return $timeout;
                }
76 77
            }
        }
78 79 80 81
        //Note : some fields updates are done in this method to exclude non-candidate payment from future research.
        //We may not want to flush here so flushing will probably not occur when an ongoing starting payment is found,
        //but it will occur only when the payment process succeeds, which is fine.

82
        //no valid payment found
83
        return null;
84
    }
Julien Jorry committed
85
}