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

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