mirror of
http://124.126.16.154:8888/singularity/hdk-pay.git
synced 2026-01-15 05:55:08 +08:00
feat(invoice): 添加发票金额和货币支持
- 在 CreateInvoiceCmd 中新增 price 和 currencySymbol 字段 - 在 Invoice 聚合根中集成 Money 对象并添加相关方法 - 更新 InvoiceRepo 以处理价格数据的序列化与反序列化 - 在测试中增加对新字段的验证
This commit is contained in:
@@ -11,7 +11,10 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Singularity\HDK\Pay\Application\Command;
|
namespace Singularity\HDK\Pay\Application\Command;
|
||||||
|
|
||||||
final readonly class CreateInvoiceCmd {
|
use Money\Money;
|
||||||
|
|
||||||
|
final readonly class CreateInvoiceCmd
|
||||||
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $caseId,
|
public string $caseId,
|
||||||
public bool $setFreqInvAddr,
|
public bool $setFreqInvAddr,
|
||||||
@@ -22,5 +25,7 @@ final readonly class CreateInvoiceCmd {
|
|||||||
public string $state,
|
public string $state,
|
||||||
public string $country,
|
public string $country,
|
||||||
public string $zipCode,
|
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;
|
namespace Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice;
|
||||||
|
|
||||||
|
use Money\Money;
|
||||||
use Singularity\HDK\Pay\Domain\AggregateRoot;
|
use Singularity\HDK\Pay\Domain\AggregateRoot;
|
||||||
|
|
||||||
final class Invoice extends AggregateRoot
|
final class Invoice extends AggregateRoot
|
||||||
@@ -22,8 +23,25 @@ final class Invoice extends AggregateRoot
|
|||||||
private readonly Address $address,
|
private readonly Address $address,
|
||||||
private readonly bool $setFreqInvAddr,
|
private readonly bool $setFreqInvAddr,
|
||||||
private readonly string $receiver,
|
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
|
public function getInvoiceNo(): string
|
||||||
{
|
{
|
||||||
return $this->invoiceNo;
|
return $this->invoiceNo;
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ namespace Singularity\HDK\Pay\Infrastructure\Repository;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Hyperf\Codec\Json;
|
use Hyperf\Codec\Json;
|
||||||
|
use Money\Currency;
|
||||||
|
use Money\Money;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Singularity\HDK\Core\Exceptions\ValidateException;
|
use Singularity\HDK\Core\Exceptions\ValidateException;
|
||||||
use Singularity\HDK\Pay\Application\Command\CreateInvoiceCmd;
|
use Singularity\HDK\Pay\Application\Command\CreateInvoiceCmd;
|
||||||
@@ -30,6 +32,7 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface
|
|||||||
*/
|
*/
|
||||||
public function create(CreateInvoiceCmd $cmd): Invoice
|
public function create(CreateInvoiceCmd $cmd): Invoice
|
||||||
{
|
{
|
||||||
|
$money = $cmd->price;
|
||||||
$response = $this->requestService->requestPost(
|
$response = $this->requestService->requestPost(
|
||||||
url: "/rpc/v2/account/logs/points/$cmd->caseId/invoices",
|
url: "/rpc/v2/account/logs/points/$cmd->caseId/invoices",
|
||||||
data: [
|
data: [
|
||||||
@@ -41,12 +44,20 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface
|
|||||||
'state' => $cmd->state,
|
'state' => $cmd->state,
|
||||||
'country' => $cmd->country,
|
'country' => $cmd->country,
|
||||||
'zip' => $cmd->zipCode,
|
'zip' => $cmd->zipCode,
|
||||||
|
'price' => [
|
||||||
|
'amount' => (float)bcmul($money->getAmount(), '100', 2),
|
||||||
|
'currency' => [
|
||||||
|
'code' => $money->getCurrency()->getCode(),
|
||||||
|
'symbol' => $cmd->currencySymbol,
|
||||||
|
],
|
||||||
|
],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
$content = $response->getBody()->getContents();
|
$content = $response->getBody()->getContents();
|
||||||
$result = Json::decode($content);
|
$result = Json::decode($content);
|
||||||
|
|
||||||
|
$price = $result['price'];
|
||||||
return new Invoice(
|
return new Invoice(
|
||||||
invoiceNo: $result['invoice_no'],
|
invoiceNo: $result['invoice_no'],
|
||||||
uid: $result['uid'],
|
uid: $result['uid'],
|
||||||
@@ -61,6 +72,11 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface
|
|||||||
),
|
),
|
||||||
setFreqInvAddr: $result['set_freq_addr'],
|
setFreqInvAddr: $result['set_freq_addr'],
|
||||||
receiver: $result['receiver'],
|
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
|
* Created on 2025/8/29
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use Money\Money;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Singularity\HDK\Pay\Application\Command\CreateInvoiceCmd;
|
use Singularity\HDK\Pay\Application\Command\CreateInvoiceCmd;
|
||||||
use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\Invoice;
|
use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\Invoice;
|
||||||
@@ -29,6 +30,8 @@ it('should can create invoice', function () {
|
|||||||
state: "Illinois",
|
state: "Illinois",
|
||||||
country: "United States",
|
country: "United States",
|
||||||
zipCode: "67890-1234",
|
zipCode: "67890-1234",
|
||||||
|
price: Money::EUR('19900'),
|
||||||
|
currencySymbol: '€'
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user