mirror of
http://124.126.16.154:8888/singularity/hdk-pay.git
synced 2026-01-15 02:15:07 +08:00
feat(invoice): 添加发票金额和货币支持
- 在 CreateInvoiceCmd 中新增 price 和 currencySymbol 字段 - 在 Invoice 聚合根中集成 Money 对象并添加相关方法 - 更新 InvoiceRepo 以处理价格数据的序列化与反序列化 - 在测试中增加对新字段的验证
This commit is contained in:
@@ -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,
|
||||
) {}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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'],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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: '€'
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user