src/Domain/Payment/Model/Payment.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Domain\Payment\Model;
  4. use App\Domain\Payment\Event\PaymentCancelledEvent;
  5. use App\Domain\Payment\Event\PaymentCompletedEvent;
  6. use App\Domain\Payment\Event\PaymentCreatedEvent;
  7. use App\Domain\Payment\Event\PaymentPendingEvent;
  8. use App\Domain\Payment\Event\PaymentRejectedEvent;
  9. use App\Domain\PaymentNotification\Model\PaymentNotification;
  10. use App\Domain\Shared\Exception\BusinessLogicViolationException;
  11. use App\Domain\Shared\Model\Aggregate;
  12. use App\Domain\Shared\Model\TimestampTrait;
  13. use App\Domain\Site\Model\Site;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Payum\Core\Model\CreditCardInterface;
  17. use Payum\Core\Model\PaymentInterface;
  18. use Symfony\Component\Uid\Uuid;
  19. use App\Domain\PaymentGateway\Model\UserPaymentInfo;
  20. class Payment extends Aggregate implements PaymentInterface
  21. {
  22.     use TimestampTrait;
  23.     private PaymentStatus $status;
  24.     private bool $resolvedBySite;
  25.     /**
  26.      * @var Collection<array-key, PaymentNotification>
  27.      */
  28.     private Collection $notifications;
  29.     public function __construct(
  30.         private Uuid $id,
  31.         private readonly string $externalId,
  32.         private readonly Site $site,
  33.         private ?UserPaymentInfo $userPaymentInfo,
  34.         private readonly string $description,
  35.         private readonly int $amount,
  36.         private readonly string $currency,
  37.         private readonly string $successUrl,
  38.         private readonly string $failureUrl,
  39.         private string $pendingUrl,
  40.         private string $mappingId,
  41.         private readonly string $notificationUrl,
  42.         private readonly string $customerId,
  43.         private string $pispStatus,
  44.         private string $bankStatus,
  45.         private array $details = [],
  46.         private ?string $paymentType = null,
  47.     ) {
  48.         $this->pendingUrl = $pendingUrl;
  49.         $this->updatedAt = null;
  50.         $this->createdAt = new \DateTimeImmutable();
  51.         $this->notifications = new ArrayCollection();
  52.         $this->status = PaymentStatus::initiated();
  53.         $this->pispStatus = 'initiated';
  54.         $this->bankStatus = 'initiated';
  55.         $this->resolvedBySite = false;
  56.         $this->raise(new PaymentCreatedEvent($this));
  57.     }
  58.     public function getNumber(): string
  59.     {
  60.         return (string) $this->id;
  61.     }
  62.     public function getDescription(): string
  63.     {
  64.         return $this->description;
  65.     }
  66.     public function getClientEmail()
  67.     {
  68.         // TODO: Implement getClientEmail() method.
  69.     }
  70.     public function getClientId(): string
  71.     {
  72.         return $this->customerId;
  73.     }
  74.     public function getTotalAmount(): int
  75.     {
  76.         return $this->amount;
  77.     }
  78.     public function getCurrencyCode(): string
  79.     {
  80.         return $this->currency;
  81.     }
  82.     public function getCreditCard(): ?CreditCardInterface
  83.     {
  84.         return null;
  85.     }
  86.     public function getUserPaymentInfo(): ?UserPaymentInfo
  87.     {
  88.         return $this->userPaymentInfo;
  89.     }
  90.     public function setDetails($details)
  91.     {
  92.         if ($details instanceof \Traversable) {
  93.             $details = iterator_to_array($details);
  94.         }
  95.         $this->details = $details;
  96.     }
  97.     public function getDetails(): array
  98.     {
  99.         return $this->details;
  100.     }
  101.     public function getId(): Uuid
  102.     {
  103.         return $this->id;
  104.     }
  105.     public function getSite(): Site
  106.     {
  107.         return $this->site;
  108.     }
  109.     public function getAmount(): int
  110.     {
  111.         return $this->amount;
  112.     }
  113.     public function getFloatAmount(): float
  114.     {
  115.         return $this->getAmount() / 100;
  116.     }
  117.     public function getCurrency(): string
  118.     {
  119.         return $this->currency;
  120.     }
  121.     public function getSuccessUrl(): string
  122.     {
  123.         return $this->successUrl;
  124.     }
  125.     public function getFailureUrl(): string
  126.     {
  127.         return $this->failureUrl;
  128.     }
  129.     public function setPendingUrl($pendingUrl)
  130.     {
  131.         if ($pendingUrl instanceof \Traversable) {
  132.             $pendingUrl = $pendingUrl;
  133.         }
  134.         $this->pendingUrl = $pendingUrl;
  135.     }
  136.     public function getPendingUrl(): string
  137.     {
  138.         return $this->pendingUrl;
  139.     }
  140.     public function setMappingId($mappingId)
  141.     {
  142.         $this->mappingId = $mappingId;
  143.     }
  144.     public function getMappingId(): string
  145.     {
  146.         return $this->mappingId;
  147.     }
  148.     public function getNotificationUrl(): string
  149.     {
  150.         return $this->notificationUrl;
  151.     }
  152.     public function getStatus(): PaymentStatus
  153.     {
  154.         return $this->status;
  155.     }
  156.     public function setStatus(PaymentStatus $status): void
  157.     {
  158.         $this->status = $status;
  159.     }
  160.     public function cancel(): void
  161.     {
  162.         $this->status = PaymentStatus::cancelled();
  163.         $this->setUpdatedAt(new \DateTimeImmutable());
  164.         $this->raise(new PaymentCancelledEvent($this));
  165.     }
  166.     /**
  167.      * @throws BusinessLogicViolationException
  168.      */
  169.     public function complete(): void
  170.     {
  171.         if ($this->status->isCompleted()) {
  172.             return;
  173.         }
  174.         if ($this->status->isFailed()) {
  175.             throw new BusinessLogicViolationException('Failed payment can\'t be completed');
  176.         }
  177.         $this->status = PaymentStatus::completed();
  178.         $this->setUpdatedAt(new \DateTimeImmutable());
  179.         $this->raise(new PaymentCompletedEvent($this));
  180.     }
  181.     /**
  182.      * @throws BusinessLogicViolationException
  183.      */
  184.     public function reject(): void
  185.     {
  186.         if ($this->status->isRejected()) {
  187.             return;
  188.         }
  189.         if ($this->status->isCompleted()) {
  190.             throw new BusinessLogicViolationException('Completed payment can\'t be rejected');
  191.         }
  192.         $this->status = PaymentStatus::rejected();
  193.         $this->setUpdatedAt(new \DateTimeImmutable());
  194.         $this->raise(new PaymentRejectedEvent($this));
  195.     }
  196.     /**
  197.      * @throws BusinessLogicViolationException
  198.      */
  199.     public function pending(): void
  200.     {
  201.         if ($this->status->isPending()) {
  202.             return;
  203.         }
  204.         // if ($this->status->isCompleted() || $this->status->isFailed() || $this->status->isRejected()) {
  205.         //     throw new BusinessLogicViolationException('Completed, failed or rejected payment can\'t be pending');
  206.         // }
  207.         $this->status = PaymentStatus::pending();
  208.         $this->setUpdatedAt(new \DateTimeImmutable());
  209.         $this->raise(new PaymentPendingEvent($this));
  210.     }
  211.     public function setResolvedBySite(bool $resolvedBySite): void
  212.     {
  213.         $this->resolvedBySite = $resolvedBySite;
  214.     }
  215.     public function getExternalId(): string
  216.     {
  217.         return $this->externalId;
  218.     }
  219.     public function getPispStatus(): string
  220.     {
  221.         return $this->pispStatus;
  222.     }
  223.     public function setPispStatus($status): void
  224.     {
  225.         $this->pispStatus = $status;
  226.     }
  227.     public function getBankStatus(): string
  228.     {
  229.         return $this->bankStatus;
  230.     }
  231.     public function setBankStatus($status): void
  232.     {
  233.         $this->bankStatus = $status;
  234.     }
  235.     public function setUserPaymentInfo(UserPaymentInfo $userPaymentInfo): void
  236.     {
  237.         $this->userPaymentInfo = $userPaymentInfo;
  238.     }
  239.     public function getPaymentType(): ?string
  240.     {
  241.         return $this->paymentType;
  242.     }
  243.     public function setPaymentType(?string $paymentType): void
  244.     {
  245.         $this->paymentType = $paymentType;
  246.     }
  247. }