bundles/CheckoutBundle/Repository/CreditCardRepository.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CreditCardRepository.php File.
  5. * This file is part of the Payment.net Project.
  6. *
  7. * PHP version 5
  8. *
  9. * @category Application
  10. * @package Bdm\CheckoutBundle\Repository
  11. * @author Pavel Baraulya <pbaraulya@bdmultimedia.fr>
  12. * @link http://www.payment.net/
  13. *
  14. * FEATURES :
  15. * ==========
  16. *
  17. * TODO-LIST :
  18. * ===========
  19. *
  20. * HISTORY :
  21. * =========
  22. * 20150710 - Pavel Baraulya
  23. * 20150916 - Alexandr Bakurin - add iterateCardsExpiringBeforePayment() method
  24. * 20151009 - Helen Tochko - update Token
  25. */
  26. namespace Bdm\CheckoutBundle\Repository;
  27. use Bdm\CheckoutBundle\Entity\CreditCard;
  28. use Bdm\CheckoutBundle\Entity\Subscription;
  29. use Bdm\CheckoutBundle\Entity\SubscriptionPlan;
  30. use Bdm\CheckoutBundle\Entity\Token;
  31. use Bdm\CheckoutBundle\Exception\CreditCardException;
  32. use Doctrine\ORM\EntityManager;
  33. use Doctrine\ORM\Query;
  34. use Doctrine\Persistence\ObjectRepository;
  35. use Symfony\Component\Config\Definition\Exception\Exception;
  36. /**
  37. * Class CreditCardRepository
  38. */
  39. class CreditCardRepository
  40. {
  41. protected ObjectRepository $oRepository;
  42. /**
  43. * Constructor
  44. *
  45. * @param EntityManager $oEm entity manager
  46. * @param TokenRepository $oTokenRepository token repository
  47. */
  48. public function __construct(protected EntityManager $oEm, protected TokenRepository $oTokenRepository)
  49. {
  50. $this->oRepository = $oEm->getRepository(CreditCard::class);
  51. }
  52. /**
  53. * Find by token
  54. *
  55. * @param string $sToken token
  56. * @param bool $bCheckToken check token
  57. *
  58. * @return CreditCard
  59. * @throws InvalidTokenException
  60. */
  61. public function findOneByToken($sToken, $bCheckToken = true)
  62. {
  63. if ((bool) $bCheckToken) {
  64. $oToken = $this->oTokenRepository->findValidToken($sToken);
  65. } else {
  66. $oToken = $this->oTokenRepository->findByHash($sToken);
  67. }
  68. $oEntity = $this->oRepository->find($oToken->getEntityId());
  69. if ($oEntity instanceof CreditCard) {
  70. return $oEntity;
  71. }
  72. throw new InvalidTokenException();
  73. }
  74. /**
  75. * Save credit card
  76. *
  77. * @param CreditCard $oCreditCard credit card
  78. *
  79. * @return CreditCard
  80. * @throws Exception
  81. */
  82. public function save(CreditCard $oCreditCard)
  83. {
  84. $this->oEm->persist($oCreditCard);
  85. $this->oEm->flush();
  86. return $oCreditCard;
  87. }
  88. /**
  89. * Add new credit card
  90. *
  91. * @deprecated: use self::save(CreditCard $oCreditCard) instead of this method
  92. *
  93. * @param CreditCard $oCreditCard credit card
  94. *
  95. * @return CreditCard
  96. * @throws Exception
  97. */
  98. public function add(CreditCard $oCreditCard)
  99. {
  100. return $this->save($oCreditCard);
  101. }
  102. /**
  103. * Update token
  104. *
  105. * @param CreditCard $oCreditCard credit card
  106. *
  107. * @return CreditCard
  108. */
  109. public function updateToken(CreditCard $oCreditCard)
  110. {
  111. if ($oCreditCard->getToken()) {
  112. $oCreditCard->getToken()->setUsed();
  113. }
  114. $oToken = new Token();
  115. $oToken->setEntity($oCreditCard);
  116. $oCreditCard->setToken($oToken);
  117. $this->oTokenRepository->add($oToken);
  118. return $oCreditCard;
  119. }
  120. /**
  121. * Find which is gonna be expired before next payment
  122. *
  123. * @return \Generator<CreditCard>
  124. */
  125. public function iterateCardsExpiringBeforePayment()
  126. {
  127. $oIterator = $this->oRepository->createQueryBuilder('cc')
  128. ->innerJoin(Subscription::class, 's', 'WITH', 's.oPaymentMethod = cc.iId')
  129. ->innerJoin(SubscriptionPlan::class, 'sp', 'WITH', 'sp.iId = s.oSubscriptionPlan')
  130. ->where('cc.bActive = :isCcActive')
  131. ->andWhere('s.bActive = :isSubActive')
  132. ->andWhere('s.sState = :subState')
  133. ->andWhere('s.oUpcomingPaymentDate > cc.oExpDate')
  134. ->andWhere('s.oUpcomingPaymentDate < :beforeDate')
  135. ->andWhere('sp.bRecurring = :isRecurring')
  136. ->andWhere('sp.bActive = :isSpActive')
  137. ->andWhere('sp.sState = :spStatus')
  138. ->setParameter('isCcActive', true)
  139. ->setParameter('beforeDate', (new \DateTime())->add(new \DateInterval('P1M')))
  140. ->setParameter('isSubActive', true)
  141. ->setParameter('isSpActive', true)
  142. ->setParameter('subState', Subscription::STATE_ACTIVE)
  143. ->setParameter('spStatus', SubscriptionPlan::STATUS_ACTIVE)
  144. ->setParameter('isRecurring', true)
  145. ->groupBy('cc.iId')
  146. ->getQuery()
  147. ->iterate();
  148. foreach ($oIterator as $oRow) {
  149. $oCard = $oRow[0];
  150. if ($oCard->getUpcomingPayment()->getState() === Subscription::STATE_ACTIVE) {
  151. yield $oCard;
  152. }
  153. }
  154. }
  155. }