fix(InvoiceRepo): 修复InvoiceProduct构造函数缺少prices参数的问题

添加PointPrice数组参数到InvoiceProduct构造函数调用中,确保API响应中的价格数据被正确处理。同时添加必要的类导入声明。
This commit is contained in:
李东云
2025-12-01 14:04:36 +08:00
parent f0550c4272
commit 46da805cb4
3 changed files with 80 additions and 2 deletions

View File

@@ -0,0 +1,66 @@
## 修复计划
### 问题分析
`/Users/weili/Projects/HDK/Pay/src/Infrastructure/Repository/InvoiceRepo.php` 第131-137行创建 `InvoiceProduct` 实例时缺少 `prices` 参数,导致构造函数调用失败。
### 错误原因
1. `InvoiceProduct` 类构造函数需要6个参数`caseId`, `uid`, `name`, `sku`, `description`, `prices`
2. 当前代码只传递了5个参数缺少 `prices` 参数
3. `prices` 参数是 `PointPrice[]` 类型的数组,不能为空
### 修复步骤
1. **添加必要的导入**
-`InvoiceRepo.php` 中添加 `Money``Currency` 类的导入
- 添加 `PointPrice` 类的导入
2. **处理API响应中的prices数据**
- 从API响应中提取 `prices` 数据(路径:`$result['product']['prices']`
- 遍历 `prices` 数据,为每个价格创建 `PointPrice` 实例
- 使用 `array_map` 函数简化处理过程
3. **完善InvoiceProduct实例创建**
- 将处理好的 `prices` 数组传递给 `InvoiceProduct` 构造函数
### 预期结果
- 修复后,`InvoiceRepo::getInvoiceInfo()` 方法能够成功创建 `InvoiceProduct` 实例
- 不再出现构造函数参数缺失的错误
- 代码能够正常运行,返回完整的 `InvoiceInfo` 对象
### 修复代码示例
```php
// 1. 在InvoiceRepo.php顶部添加必要的导入
use Money\Currency;
use Money\Money;
use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\ValueObject\PointPrice;
// 2. 在getInvoiceInfo()方法中完善InvoiceProduct实例创建
product: new InvoiceProduct(
caseId: $result['case_id'],
uid: $result['uid'],
name: $result['product']['name'],
sku: $result['product']['sku'],
description: $result['product']['description'],
prices: array_map(
callback: fn(array $price) => new PointPrice(
price: new Money(
amount: bcmul((string)$price['amount'], '100', 2),
currency: new Currency($price['currency']),
),
currencySymbol: $price['symbol'],
),
array: $result['product']['prices'],
),
),
```
### 检查结果
- 已检查所有使用 `InvoiceProduct` 的地方,只有 `InvoiceRepo.php` 中存在参数缺失问题
- `InvoiceProductRepo.php` 中的实现是正确的,可以作为参考
- 修复后,所有 `InvoiceProduct` 实例创建都会符合构造函数要求
### 测试建议
- 修复后,运行相关测试用例验证修复效果
- 可以使用 `./run-in-docker.sh APP_STATUS=false vendor/bin/pest --coroutine --prepend=tests/bootstrap.php` 运行测试
- 检查是否还有其他类似的构造函数参数问题

View File

@@ -7,6 +7,7 @@
* Powered by PhpStorm
* Created on 2025/8/29
*/
declare(strict_types=1);
namespace Singularity\HDK\Pay\Infrastructure\Repository;
@@ -23,6 +24,7 @@ use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\Invoice;
use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\InvoiceInfo;
use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\InvoiceProduct;
use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\ValueObject\PointBalance;
use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\ValueObject\PointPrice;
use Singularity\HDK\Pay\Domain\Invoice\Repository\InvoiceRepoInterface;
final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface
@@ -134,6 +136,16 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface
name: $result['product']['name'],
sku: $result['product']['sku'],
description: $result['product']['description'],
prices: array_map(
callback: fn(array $price) => new PointPrice(
price: new Money(
amount: bcmul((string)$price['amount'], '100', 2),
currency: new Currency($price['currency']),
),
currencySymbol: $price['symbol'],
),
array: $result['product']['prices'] ?? [],
),
),
balance: new PointBalance(
total: $result['balance']['total'],