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