From 244dd1220a2c19197ef08f41a82bcb273d0900e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Thu, 27 Nov 2025 11:51:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(invoice):=20=E6=B7=BB=E5=8A=A0=E5=8F=91?= =?UTF-8?q?=E7=A5=A8=E9=87=91=E9=A2=9D=E5=92=8C=E8=B4=A7=E5=B8=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CreateInvoiceCmd 中新增 price 和 currencySymbol 字段 - 在 Invoice 聚合根中集成 Money 对象并添加相关方法 - 更新 InvoiceRepo 以处理价格数据的序列化与反序列化 - 在测试中增加对新字段的验证 --- src/Application/Command/CreateInvoiceCmd.php | 25 +++++++++++-------- .../Invoice/Aggregate/Invoice/Invoice.php | 18 +++++++++++++ src/Infrastructure/Repository/InvoiceRepo.php | 16 ++++++++++++ tests/Feature/Invoice/CreateInvoiceTest.php | 3 +++ 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/Application/Command/CreateInvoiceCmd.php b/src/Application/Command/CreateInvoiceCmd.php index 82ed3f9..4d6abf9 100644 --- a/src/Application/Command/CreateInvoiceCmd.php +++ b/src/Application/Command/CreateInvoiceCmd.php @@ -11,16 +11,21 @@ declare(strict_types=1); namespace Singularity\HDK\Pay\Application\Command; -final readonly class CreateInvoiceCmd { +use Money\Money; + +final readonly class CreateInvoiceCmd +{ public function __construct( - public string $caseId, - public bool $setFreqInvAddr, - public string $receiver, - public string $patientName, - public string $address, - public string $city, - public string $state, - public string $country, - public string $zipCode, + public string $caseId, + public bool $setFreqInvAddr, + public string $receiver, + public string $patientName, + public string $address, + public string $city, + public string $state, + public string $country, + public string $zipCode, + public Money $price, + public string $currencySymbol, ) {} } \ No newline at end of file diff --git a/src/Domain/Invoice/Aggregate/Invoice/Invoice.php b/src/Domain/Invoice/Aggregate/Invoice/Invoice.php index d095923..b689500 100644 --- a/src/Domain/Invoice/Aggregate/Invoice/Invoice.php +++ b/src/Domain/Invoice/Aggregate/Invoice/Invoice.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice; +use Money\Money; use Singularity\HDK\Pay\Domain\AggregateRoot; final class Invoice extends AggregateRoot @@ -22,8 +23,25 @@ final class Invoice extends AggregateRoot private readonly Address $address, private readonly bool $setFreqInvAddr, private readonly string $receiver, + private readonly Money $price, + private readonly string $currencySymbol, ) {} + public function getAmount(): float + { + return (float)bcdiv($this->price->getAmount(), '100', 2); + } + + public function getCurrencyCode(): string + { + return $this->price->getCurrency()->getCode(); + } + + public function getCurrencySymbol(): string + { + return $this->currencySymbol; + } + public function getInvoiceNo(): string { return $this->invoiceNo; diff --git a/src/Infrastructure/Repository/InvoiceRepo.php b/src/Infrastructure/Repository/InvoiceRepo.php index ee84361..3543080 100644 --- a/src/Infrastructure/Repository/InvoiceRepo.php +++ b/src/Infrastructure/Repository/InvoiceRepo.php @@ -13,6 +13,8 @@ namespace Singularity\HDK\Pay\Infrastructure\Repository; use Carbon\Carbon; use Hyperf\Codec\Json; +use Money\Currency; +use Money\Money; use Psr\Http\Message\ResponseInterface; use Singularity\HDK\Core\Exceptions\ValidateException; use Singularity\HDK\Pay\Application\Command\CreateInvoiceCmd; @@ -30,6 +32,7 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface */ public function create(CreateInvoiceCmd $cmd): Invoice { + $money = $cmd->price; $response = $this->requestService->requestPost( url: "/rpc/v2/account/logs/points/$cmd->caseId/invoices", data: [ @@ -41,12 +44,20 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface 'state' => $cmd->state, 'country' => $cmd->country, 'zip' => $cmd->zipCode, + 'price' => [ + 'amount' => (float)bcmul($money->getAmount(), '100', 2), + 'currency' => [ + 'code' => $money->getCurrency()->getCode(), + 'symbol' => $cmd->currencySymbol, + ], + ], ], ); $content = $response->getBody()->getContents(); $result = Json::decode($content); + $price = $result['price']; return new Invoice( invoiceNo: $result['invoice_no'], uid: $result['uid'], @@ -61,6 +72,11 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface ), setFreqInvAddr: $result['set_freq_addr'], receiver: $result['receiver'], + price: new Money( + bcmul((string)$price['amount'], '100', 2), + new Currency($price['code']), + ), + currencySymbol: $price['symbol'], ); } diff --git a/tests/Feature/Invoice/CreateInvoiceTest.php b/tests/Feature/Invoice/CreateInvoiceTest.php index 2c48ca0..c100cc4 100644 --- a/tests/Feature/Invoice/CreateInvoiceTest.php +++ b/tests/Feature/Invoice/CreateInvoiceTest.php @@ -8,6 +8,7 @@ * Created on 2025/8/29 */ +use Money\Money; use Psr\Http\Message\ResponseInterface; use Singularity\HDK\Pay\Application\Command\CreateInvoiceCmd; use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\Invoice; @@ -29,6 +30,8 @@ it('should can create invoice', function () { state: "Illinois", country: "United States", zipCode: "67890-1234", + price: Money::EUR('19900'), + currencySymbol: '€' ), );