mirror of
https://git.yoomoney.ru/scm/sdk/yookassa-sdk-php.git
synced 2026-08-29 11:39:22 +00:00
cbcdbc8ae1
Отказ от поддержки PHP < 8 Изменена структура файлов Переработана логика работы с моделями Добавлено использование валидатора данных в моделях Массивы объектов заменены на объекты-коллекции
140 lines
4.4 KiB
PHP
140 lines
4.4 KiB
PHP
<?php
|
||
|
||
/**
|
||
* The MIT License.
|
||
*
|
||
* Copyright (c) 2023 "YooMoney", NBСO LLC
|
||
*
|
||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
* of this software and associated documentation files (the "Software"), to deal
|
||
* in the Software without restriction, including without limitation the rights
|
||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
* copies of the Software, and to permit persons to whom the Software is
|
||
* furnished to do so, subject to the following conditions:
|
||
*
|
||
* The above copyright notice and this permission notice shall be included in
|
||
* all copies or substantial portions of the Software.
|
||
*
|
||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
* THE SOFTWARE.
|
||
*/
|
||
|
||
namespace YooKassa\Model\Receipt;
|
||
|
||
use YooKassa\Common\AbstractObject;
|
||
use YooKassa\Validator\Constraints as Assert;
|
||
use YooKassa\Validator\Exceptions\EmptyPropertyValueException;
|
||
use YooKassa\Validator\Exceptions\InvalidPropertyValueTypeException;
|
||
use YooKassa\Model\AmountInterface;
|
||
use YooKassa\Model\MonetaryAmount;
|
||
|
||
/**
|
||
* Class Settlement.
|
||
*
|
||
* @category Class
|
||
* @package YooKassa\Model
|
||
* @author cms@yoomoney.ru
|
||
* @link https://yookassa.ru/developers/api
|
||
*
|
||
* @property string $type Вид оплаты в чеке
|
||
* @property AmountInterface $amount Размер оплаты
|
||
*/
|
||
class Settlement extends AbstractObject implements SettlementInterface
|
||
{
|
||
/**
|
||
* @var string|null Вид оплаты в чеке (cashless | prepayment | postpayment | consideration)
|
||
*/
|
||
#[Assert\NotBlank]
|
||
#[Assert\Type('string')]
|
||
#[Assert\Choice(callback: [SettlementType::class, 'getValidValues'])]
|
||
private ?string $_type = null;
|
||
|
||
/**
|
||
* @var AmountInterface|null Размер оплаты
|
||
*/
|
||
private ?AmountInterface $_amount = null;
|
||
|
||
/**
|
||
* Возвращает вид оплаты в чеке (cashless | prepayment | postpayment | consideration).
|
||
*
|
||
* @return string|null Вид оплаты в чеке
|
||
*/
|
||
public function getType(): ?string
|
||
{
|
||
return $this->_type;
|
||
}
|
||
|
||
/**
|
||
* Устанавливает вид оплаты в чеке.
|
||
*
|
||
* @param string|null $type Тип расчета.
|
||
*
|
||
* @return self
|
||
*/
|
||
public function setType(?string $type = null): self
|
||
{
|
||
$this->_type = $this->validatePropertyValue('_type', $type);
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* Возвращает размер оплаты.
|
||
*
|
||
* @return AmountInterface Размер оплаты
|
||
*/
|
||
public function getAmount(): ?AmountInterface
|
||
{
|
||
return $this->_amount;
|
||
}
|
||
|
||
/**
|
||
* Устанавливает сумму платежа.
|
||
*
|
||
* @param AmountInterface|array|null $value Сумма платежа
|
||
*/
|
||
public function setAmount(mixed $value): void
|
||
{
|
||
if (null === $value || '' === $value) {
|
||
throw new EmptyPropertyValueException(
|
||
'Empty value for "amount" parameter in Settlement',
|
||
0,
|
||
'settlement.amount'
|
||
);
|
||
}
|
||
if (is_array($value)) {
|
||
$this->_amount = $this->factoryAmount($value);
|
||
} elseif ($value instanceof AmountInterface) {
|
||
$this->_amount = $value;
|
||
} else {
|
||
throw new InvalidPropertyValueTypeException(
|
||
'Invalid value type for "amount" parameter in Settlement',
|
||
0,
|
||
'settlement.amount',
|
||
$value
|
||
);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Фабричный метод создания суммы.
|
||
*
|
||
* @param array $options Сумма в виде ассоциативного массива
|
||
*
|
||
* @return AmountInterface Созданный инстанс суммы
|
||
*/
|
||
private function factoryAmount(array $options): AmountInterface
|
||
{
|
||
$amount = new MonetaryAmount(null, $options['currency']);
|
||
if ($options['value'] > 0) {
|
||
$amount->setValue($options['value']);
|
||
}
|
||
|
||
return $amount;
|
||
}
|
||
}
|