TransferFeeDto.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Module\Transfer\Dtos;
  3. use UCore\Dto\BaseDto;
  4. /**
  5. * 划转手续费计算结果数据传输对象
  6. *
  7. * 用于表示手续费计算的结果,包含手续费率、手续费金额、实际到账金额等信息
  8. */
  9. class TransferFeeDto extends BaseDto
  10. {
  11. /**
  12. * 构造函数
  13. *
  14. * @param float $feeRate 手续费率(小数形式,如 0.01 表示 1%)
  15. * @param string $feeAmount 手续费金额(字符串格式,保持精度)
  16. * @param string $actualAmount 实际到账金额(字符串格式,保持精度),本金
  17. * @param string $totleAmount 总需金额(字符串格式,保持精度)
  18. * @param string $originalAmount 原始金额(字符串格式,保持精度),外部金额
  19. * @param bool $hasError 是否有错误
  20. * @param string|null $errorMessage 错误信息
  21. * @param array $additionalInfo 额外信息
  22. */
  23. public function __construct(
  24. public readonly float $feeRate,
  25. public readonly string $feeAmount,
  26. public readonly string $actualAmount,
  27. public readonly string $totleAmount,
  28. public readonly string $originalAmount,
  29. public readonly bool $hasError = false,
  30. public readonly ?string $errorMessage = null,
  31. public readonly array $additionalInfo = []
  32. )
  33. {
  34. }
  35. /**
  36. * 创建成功的手续费计算结果
  37. *
  38. * @param string $originalAmount 原始金额
  39. * @param float $feeRate 手续费率
  40. * @param string $feeAmount 手续费金额
  41. * @param string $actualAmount 实际到账金额
  42. * @param string $totleAmount 总支付金额
  43. * @param array $additionalInfo 额外信息
  44. * @return self
  45. */
  46. public static function success(
  47. string $originalAmount,
  48. float $feeRate,
  49. string $feeAmount,
  50. string $actualAmount,
  51. string $totleAmount,
  52. array $additionalInfo = []
  53. ): self
  54. {
  55. return new self(
  56. feeRate: $feeRate,
  57. feeAmount: $feeAmount,
  58. actualAmount: $actualAmount,
  59. originalAmount: $originalAmount,
  60. totleAmount: $totleAmount,
  61. hasError: false,
  62. errorMessage: null,
  63. additionalInfo: $additionalInfo
  64. );
  65. }
  66. /**
  67. * 创建错误的手续费计算结果
  68. *
  69. * @param string $originalAmount 原始金额
  70. * @param string $errorMessage 错误信息
  71. * @param array $additionalInfo 额外信息
  72. * @return self
  73. */
  74. public static function error(
  75. string $originalAmount,
  76. string $errorMessage,
  77. array $additionalInfo = []
  78. ): self
  79. {
  80. return new self(
  81. feeRate: 0.0000,
  82. feeAmount: '0.0000',
  83. actualAmount: $originalAmount,
  84. originalAmount: $originalAmount,
  85. totleAmount: 0,
  86. hasError: true,
  87. errorMessage: $errorMessage,
  88. additionalInfo: $additionalInfo
  89. );
  90. }
  91. /**
  92. * 转换为数组
  93. *
  94. * @return array
  95. */
  96. public function toArray(): array
  97. {
  98. $result = [
  99. 'fee_rate' => $this->feeRate,
  100. 'fee_amount' => $this->feeAmount,
  101. 'actual_amount' => $this->actualAmount,
  102. 'original_amount' => $this->originalAmount,
  103. 'has_error' => $this->hasError,
  104. ];
  105. $result['error'] = $this->errorMessage;
  106. $result['additionalInfo'] = $this->additionalInfo;
  107. return $result;
  108. }
  109. /**
  110. * 判断是否计算成功
  111. *
  112. * @return bool
  113. */
  114. public function isSuccess(): bool
  115. {
  116. return !$this->hasError;
  117. }
  118. }