feat: add MoneyInput for writing Charge items money
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use App\Money\MoneyInterface;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
class MoneyInput
|
||||
{
|
||||
/**
|
||||
* An amount of currency.\
|
||||
* Expressed as the minor unit, e.g: cents, pennies, etc.
|
||||
*/
|
||||
#[Assert\NotBlank()]
|
||||
#[Assert\Positive()]
|
||||
public int $amount;
|
||||
|
||||
/**
|
||||
* 3-letter ISO 4217 currency code.
|
||||
*/
|
||||
#[Assert\NotBlank()]
|
||||
#[Assert\Currency()]
|
||||
public string $currency;
|
||||
|
||||
public function __construct(
|
||||
int $amount,
|
||||
string $currency,
|
||||
) {
|
||||
$this->amount = $amount;
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
public static function of(MoneyInterface $moneyInterface): self
|
||||
{
|
||||
$money = new self(
|
||||
$moneyInterface->getAmount(),
|
||||
$moneyInterface->getCurrency(),
|
||||
);
|
||||
|
||||
return $money;
|
||||
}
|
||||
|
||||
public function getAmount(): int
|
||||
{
|
||||
return $this->amount;
|
||||
}
|
||||
|
||||
public function getCurrency(): string
|
||||
{
|
||||
return $this->currency;
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@
|
||||
namespace App\Dto\Gateway;
|
||||
|
||||
use App\ApiResource\Accounting\AccountingApiResource;
|
||||
use App\ApiResource\MoneyOutput;
|
||||
use App\Entity\Gateway\Charge;
|
||||
use App\ApiResource\MoneyInput;
|
||||
use App\Gateway\ChargeType;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
@@ -44,5 +43,5 @@ class ChargeCreationDto
|
||||
* It is money before fees and taxes, not accountable.
|
||||
*/
|
||||
#[Assert\NotBlank()]
|
||||
public MoneyOutput $money;
|
||||
public MoneyInput $money;
|
||||
}
|
||||
|
||||
@@ -26,9 +26,12 @@ class ChargeRefundListener
|
||||
|
||||
public function preUpdate(Charge $charge, PreUpdateEventArgs $args): void
|
||||
{
|
||||
if (!$args->hasChangedField(self::FIELD_STATUS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
!$args->hasChangedField(self::FIELD_STATUS)
|
||||
&& $args->getOldValue(self::FIELD_STATUS) === ChargeStatus::InCharge->value
|
||||
$args->getOldValue(self::FIELD_STATUS) === ChargeStatus::InCharge->value
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ use App\Entity\Gateway\Checkout;
|
||||
use App\Gateway\GatewayLocator;
|
||||
use App\Mapping\AutoMapper;
|
||||
use App\Money\Conversion\ExchangeLocator;
|
||||
use Brick\Money\Context\CustomContext;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
class CheckoutStateProcessor implements ProcessorInterface
|
||||
@@ -55,9 +54,10 @@ class CheckoutStateProcessor implements ProcessorInterface
|
||||
}
|
||||
|
||||
$exchange = $this->exchangeLocator->get($fromCurrency, $toCurrency);
|
||||
$exchanged = $exchange->convert($charge->getMoney(), $toCurrency, new CustomContext(0, 1));
|
||||
|
||||
$charge->setMoney(EmbeddableMoney::of($exchanged));
|
||||
$charge->setMoney(EmbeddableMoney::of(
|
||||
$exchange->convert($charge->getMoney(), $toCurrency)
|
||||
));
|
||||
}
|
||||
|
||||
$checkout = $this->gatewayLocator->get($data->gateway->name)->process($checkout);
|
||||
|
||||
Reference in New Issue
Block a user