src/Entity/Payment/Payment.php line 39

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Entity\Payment;
  12. use App\Entity\Customer\Customer;
  13. use App\Entity\IdentifiableTrait;
  14. use App\Entity\Order\Order;
  15. use App\Entity\Promotion\PromotionCode;
  16. use App\PaymentStates;
  17. use App\Repository\Payment\PaymentRepository;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Gedmo\Timestampable\Traits\TimestampableEntity;
  20. use Payum\Core\Model\CreditCardInterface;
  21. use Payum\Core\Model\PaymentInterface;
  22. use Sylius\Component\Resource\Annotation\SyliusCrudRoutes;
  23. use Sylius\Component\Resource\Model\ResourceInterface;
  24. #[ORM\Entity(repositoryClassPaymentRepository::class)]
  25. #[ORM\Table(name'app_payment')]
  26. #[SyliusCrudRoutes(alias'app.payment'path'/admin/payments'section'backend'templates'backend/payment'grid'app_backend_payment'only: ['index''show'], vars: [
  27.     'all' => [
  28.         'subheader' => 'app.ui.manage_your_payments',
  29.     ],
  30.     'index' => [
  31.         'icon' => 'payment',
  32.     ],
  33. ])]
  34. class Payment implements ResourceInterfacePaymentInterface
  35. {
  36.     use IdentifiableTrait;
  37.     use TimestampableEntity;
  38.     #[ORM\Column(type'string'length100)]
  39.     private string $number;
  40.     #[ORM\ManyToOne(targetEntityCustomer::class)]
  41.     #[ORM\JoinColumn(name"customer_id"referencedColumnName"id"onDelete"CASCADE")]
  42.     private ?Customer $customer null;
  43.     #[ORM\Column(type'string'length20)]
  44.     private string $state;
  45.     #[ORM\Column(type'integer')]
  46.     private int $credits 0;
  47.     #[ORM\Column(type'string')]
  48.     private string $description;
  49.     #[ORM\Column(type'integer')]
  50.     private int $amount 0;
  51.     #[ORM\Column(type'string'length10)]
  52.     private string $currencyCode;
  53.     #[ORM\Column(type'json')]
  54.     private array $details;
  55.     #[ORM\ManyToOne(targetEntityPromotionCode::class, inversedBy'payments')]
  56.     #[ORM\JoinColumn(nullabletrue)]
  57.     private ?PromotionCode $promotionCode;
  58.     /** Order paid by this payment when paymentType is order_direct or order_complement. */
  59.     #[ORM\ManyToOne(targetEntityOrder::class)]
  60.     #[ORM\JoinColumn(nullabletrueonDelete'SET NULL')]
  61.     private ?Order $order null;
  62.     /** credit_pack | order_direct | order_complement */
  63.     #[ORM\Column(type'string'length30nullabletrueoptions: ['default' => 'credit_pack'])]
  64.     private ?string $paymentType 'credit_pack';
  65.     /** Credits to debit from customer when paymentType is order_complement (after Stripe confirms). */
  66.     #[ORM\Column(type'integer'options: ['default' => 0])]
  67.     private int $creditsToDebit 0;
  68.     public function __construct()
  69.     {
  70.         $this->details = [];
  71.         $this->state PaymentStates::STATE_NEW;
  72.     }
  73.     /**
  74.      * Get the value of customer
  75.      *
  76.      * @return ?Customer
  77.      */
  78.     public function getCustomer(): ?Customer
  79.     {
  80.         return $this->customer;
  81.     }
  82.     /**
  83.      * Set the value of customer
  84.      *
  85.      * @return self
  86.      */
  87.     public function setCustomer(?Customer $customer): self
  88.     {
  89.         $this->customer $customer;
  90.         return $this;
  91.     }
  92.     /**
  93.      * Get the value of state
  94.      *
  95.      * @return string
  96.      */
  97.     public function getState(): string
  98.     {
  99.         return $this->state;
  100.     }
  101.     /**
  102.      * Set the value of state
  103.      *
  104.      * @return self
  105.      */
  106.     public function setState(string $state): self
  107.     {
  108.         $this->state $state;
  109.         return $this;
  110.     }
  111.     /**
  112.      * Get the value of credits
  113.      *
  114.      * @return ?int
  115.      */
  116.     public function getCredits(): ?int
  117.     {
  118.         return $this->credits;
  119.     }
  120.     /**
  121.      * Set the value of credits
  122.      *
  123.      * @return self
  124.      */
  125.     public function setCredits(?int $credits): self
  126.     {
  127.         $this->credits $credits;
  128.         return $this;
  129.     }
  130.     
  131.     public function __toString(): string
  132.     {
  133.         return (string) ($this->getNumber() ?? $this->getId());
  134.     }
  135.     /**
  136.      * Get the value of description
  137.      *
  138.      * @return string
  139.      */
  140.     public function getDescription()
  141.     {
  142.         return $this->description;
  143.     }
  144.     /**
  145.      * Set the value of description
  146.      *
  147.      * @param string $description
  148.      *
  149.      * @return self
  150.      */
  151.     public function setDescription(string $description): self
  152.     {
  153.         $this->description $description;
  154.         return $this;
  155.     }
  156.     /**
  157.      * Get the value of amount
  158.      *
  159.      * @return int
  160.      */
  161.     public function getAmount()
  162.     {
  163.         return $this->amount;
  164.     }
  165.     /**
  166.      * Set the value of amount
  167.      *
  168.      * @param int $amount
  169.      *
  170.      * @return self
  171.      */
  172.     public function setAmount(int $amount): self
  173.     {
  174.         $this->amount $amount;
  175.         return $this;
  176.     }
  177.     /**
  178.      * Get the value of currencyCode
  179.      *
  180.      * @return string
  181.      */
  182.     public function getCurrencyCode()
  183.     {
  184.         return $this->currencyCode;
  185.     }
  186.     /**
  187.      * Set the value of currencyCode
  188.      *
  189.      * @param string $currencyCode
  190.      *
  191.      * @return self
  192.      */
  193.     public function setCurrencyCode(string $currencyCode): self
  194.     {
  195.         $this->currencyCode $currencyCode;
  196.         return $this;
  197.     }
  198.     /**
  199.      * Get the value of number
  200.      *
  201.      * @return array
  202.      */
  203.     public function getDetails(): array
  204.     {
  205.         return $this->details;
  206.     }
  207.     /**
  208.      * Set the value of details
  209.      *
  210.      *  @param object $details
  211.      *
  212.      * @return self
  213.      */
  214.     public function setDetails($details): self
  215.     {
  216.         if ($details instanceof \Traversable) {
  217.             $details iterator_to_array($details);
  218.         }
  219.         $this->details $details;
  220.         return $this;
  221.     }
  222.     /**
  223.      * Get the value of number
  224.      *
  225.      * @return string
  226.      */
  227.     public function getNumber()
  228.     {
  229.         return $this->number;
  230.     }
  231.     /**
  232.      * Set the value of number
  233.      *
  234.      * @param string $number
  235.      *
  236.      * @return self
  237.      */
  238.     public function setNumber(string $number): self
  239.     {
  240.         $this->number $number;
  241.         return $this;
  242.     }
  243.     /**
  244.      * Get the value of promotionCode
  245.      *
  246.      * @return ?PromotionCode
  247.      */
  248.     public function getPromotionCode(): ?PromotionCode
  249.     {
  250.         return $this->promotionCode;
  251.     }
  252.     /**
  253.      * Set the value of promotionCode
  254.      *
  255.      * @param ?PromotionCode $promotionCode
  256.      *
  257.      * @return self
  258.      */
  259.     public function setPromotionCode(?PromotionCode $promotionCode): self
  260.     {
  261.         $this->promotionCode $promotionCode;
  262.         return $this;
  263.     }
  264.     public function getClientEmail(): string
  265.     {
  266.         return $this->getCustomer()->getEmail() ?? '';
  267.     }
  268.     public function getClientId(): string
  269.     {
  270.         return (string) $this->getCustomer()->getId() ?? '';
  271.     }
  272.     public function getTotalAmount(): int
  273.     {
  274.         return $this->getAmount();
  275.     }
  276.     public function getCreditCard(): CreditCardInterface|null
  277.     {
  278.         return null;
  279.     }
  280.     public function getPaymentIntent(): ?string
  281.     {
  282.         if (isset($this->getDetails()['object']) && $this->getDetails()['object'] === 'payment_intent') {
  283.             return ((string) $this->getDetails()['id']) ?? null;
  284.         }
  285.         return null;
  286.     }
  287.     public function getClientSecret(): ?string
  288.     {
  289.         if (isset($this->getDetails()['client_secret'])) {
  290.             return ((string) $this->getDetails()['client_secret']) ?? null;
  291.         }
  292.         return null;
  293.     }
  294.     public function getOrder(): ?Order
  295.     {
  296.         return $this->order;
  297.     }
  298.     public function setOrder(?Order $order): self
  299.     {
  300.         $this->order $order;
  301.         return $this;
  302.     }
  303.     public function getPaymentType(): ?string
  304.     {
  305.         return $this->paymentType;
  306.     }
  307.     public function setPaymentType(?string $paymentType): self
  308.     {
  309.         $this->paymentType $paymentType;
  310.         return $this;
  311.     }
  312.     public function getCreditsToDebit(): int
  313.     {
  314.         return $this->creditsToDebit;
  315.     }
  316.     public function setCreditsToDebit(int $creditsToDebit): self
  317.     {
  318.         $this->creditsToDebit $creditsToDebit;
  319.         return $this;
  320.     }
  321.     public function isOrderPayment(): bool
  322.     {
  323.         return $this->paymentType === 'order_direct' || $this->paymentType === 'order_complement';
  324.     }
  325. }