Готов класс оплаты Payment для будущей поддержки оплат в документе

This commit is contained in:
2021-12-03 23:53:42 +08:00
parent 65ec639014
commit 7d0c526663
7 changed files with 301 additions and 99 deletions

119
src/Entities/Payment.php Normal file
View File

@@ -0,0 +1,119 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types = 1);
namespace AtolOnline\Entities;
use AtolOnline\Constants\Constraints;
use AtolOnline\Enums\PaymentTypes;
use AtolOnline\Exceptions\InvalidEnumValueException;
use AtolOnline\Exceptions\NegativePaymentSumException;
use AtolOnline\Exceptions\TooHighPaymentSumException;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
/**
* Класс, описывающий оплату
*
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 30
*/
class Payment extends Entity
{
/**
* @var int Тип оплаты
*/
protected int $type;
/**
* @var float Сумма оплаты (1031, 1081, 1215, 1216, 1217)
*/
protected float $sum;
/**
* Конструктор
*
* @param int $type Тип оплаты
* @param float $sum Сумма оплаты
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
*/
public function __construct(int $type, float $sum)
{
$this->setType($type)->setSum($sum);
}
/**
* Возвращает установленный тип оплаты
*
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* Устанавливает тип оплаты
*
* @param int $type
* @return $this
* @throws InvalidEnumValueException
*/
public function setType(int $type): self
{
PaymentTypes::isValid($type) && $this->type = $type;
return $this;
}
/**
* Возвращает установленную сумму оплаты
*
* @return float
*/
public function getSum(): float
{
return $this->sum;
}
/**
* Устанавливает сумму оплаты
*
* @param float $sum
* @return $this
* @throws TooHighPaymentSumException
* @throws NegativePaymentSumException
*/
public function setSum(float $sum): self
{
$sum = round($sum, 2);
if ($sum > Constraints::MAX_COUNT_PAYMENT_SUM) {
throw new TooHighPaymentSumException($sum);
}
if ($sum < 0) {
throw new NegativePaymentSumException($sum);
}
$this->sum = $sum;
return $this;
}
/**
* @inheritDoc
*/
#[Pure]
#[ArrayShape(['type' => 'int', 'sum' => 'float'])]
public function jsonSerialize(): array
{
return [
'type' => $this->getType(),
'sum' => $this->getSum(),
];
}
}

View File

@@ -1,99 +0,0 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types = 1);
namespace AtolOnline\Entities;
use AtolOnline\Enums\PaymentTypes;
/**
* Класс, описывающий оплату. Тег ФФД - 1031, 1081, 1215, 1216, 1217.
*
* @package AtolOnline\Entities
*/
class todoPayment extends Entity
{
/**
* @var int Тип оплаты
*/
protected int $type;
/**
* @var float Сумма оплаты
*/
protected float $sum;
/**
* todoPayment constructor.
*
* @param int $payment_type Тип оплаты
* @param float $sum Сумма оплаты
*/
public function __construct(int $payment_type = PaymentTypes::ELECTRON, float $sum = 0.0)
{
$this->setType($payment_type);
$this->setSum($sum);
}
/**
* Возвращает тип оплаты. Тег ФФД - 1031, 1081, 1215, 1216, 1217.
*
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* Устанавливает тип оплаты. Тег ФФД - 1031, 1081, 1215, 1216, 1217.
*
* @param int $type
* @return $this
*/
public function setType(int $type): todoPayment
{
$this->type = $type;
return $this;
}
/**
* Возвращает сумму оплаты
*
* @return float
*/
public function getSum(): float
{
return $this->sum;
}
/**
* Устанавливает сумму оплаты
*
* @param float $sum
* @return $this
*/
public function setSum(float $sum): todoPayment
{
$this->sum = $sum;
return $this;
}
/**
* @inheritDoc
*/
public function jsonSerialize()
{
return [
'type' => $this->getType(),
'sum' => $this->getSum(),
];
}
}