From 02024acd20430fdc4e6a486ca135edf58f5397bf Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Wed, 8 Apr 2026 16:32:59 +0200 Subject: [PATCH] feat: add MoneyInput for writing Charge items money --- src/ApiResource/MoneyInput.php | 52 ++++++++++++++++++++ src/Dto/Gateway/ChargeCreationDto.php | 5 +- src/EventListener/ChargeRefundListener.php | 7 ++- src/State/Gateway/CheckoutStateProcessor.php | 6 +-- 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/ApiResource/MoneyInput.php diff --git a/src/ApiResource/MoneyInput.php b/src/ApiResource/MoneyInput.php new file mode 100644 index 0000000..5a2407f --- /dev/null +++ b/src/ApiResource/MoneyInput.php @@ -0,0 +1,52 @@ +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; + } +} diff --git a/src/Dto/Gateway/ChargeCreationDto.php b/src/Dto/Gateway/ChargeCreationDto.php index d594e8f..f9849e2 100644 --- a/src/Dto/Gateway/ChargeCreationDto.php +++ b/src/Dto/Gateway/ChargeCreationDto.php @@ -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; } diff --git a/src/EventListener/ChargeRefundListener.php b/src/EventListener/ChargeRefundListener.php index 8bdfa78..c43ba6b 100644 --- a/src/EventListener/ChargeRefundListener.php +++ b/src/EventListener/ChargeRefundListener.php @@ -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; } diff --git a/src/State/Gateway/CheckoutStateProcessor.php b/src/State/Gateway/CheckoutStateProcessor.php index e834bb4..c9466f2 100644 --- a/src/State/Gateway/CheckoutStateProcessor.php +++ b/src/State/Gateway/CheckoutStateProcessor.php @@ -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);