|
|
@@ -13,9 +13,10 @@ use App\Module\Transfer\Models\TransferApp;
|
|
|
*/
|
|
|
class TransferThirdPartyService
|
|
|
{
|
|
|
+
|
|
|
/**
|
|
|
* 创建充值单(转入订单)
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $thirdPartyAppId 三方应用ID
|
|
|
* @param int $farmUserId 农场用户ID
|
|
|
* @param string $thirdPartyUserId 三方用户ID
|
|
|
@@ -26,13 +27,14 @@ class TransferThirdPartyService
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public static function createRechargeOrder(
|
|
|
- int $thirdPartyAppId,
|
|
|
- int $farmUserId,
|
|
|
- string $thirdPartyUserId,
|
|
|
- string $thirdPartyAmount,
|
|
|
+ int $thirdPartyAppId,
|
|
|
+ int $farmUserId,
|
|
|
+ string $thirdPartyUserId,
|
|
|
+ string $thirdPartyAmount,
|
|
|
?string $remark = null,
|
|
|
- array $callbackData = []
|
|
|
- ): TransferOrderDto|string {
|
|
|
+ array $callbackData = []
|
|
|
+ ): TransferOrderDto|string
|
|
|
+ {
|
|
|
try {
|
|
|
// 根据三方应用ID查找对应的Transfer应用配置
|
|
|
$transferApp = TransferApp::where('out_id3', $thirdPartyAppId)
|
|
|
@@ -54,26 +56,26 @@ class TransferThirdPartyService
|
|
|
// 调用Transfer模块的转入逻辑
|
|
|
$order = TransferLogic::createTransferIn(
|
|
|
transferAppId: $transferApp->id,
|
|
|
- userId: $farmUserId,
|
|
|
- businessId: $businessId,
|
|
|
- amount: $thirdPartyAmount,
|
|
|
- outUserId: $thirdPartyUserId,
|
|
|
- remark: $remark,
|
|
|
- callbackData: array_merge($callbackData, [
|
|
|
- 'third_party_app_id' => $thirdPartyAppId,
|
|
|
- 'third_party_user_id' => $thirdPartyUserId,
|
|
|
- 'operation_type' => 'recharge'
|
|
|
- ])
|
|
|
+ userId: $farmUserId,
|
|
|
+ businessId: $businessId,
|
|
|
+ amount: $thirdPartyAmount,
|
|
|
+ outUserId: $thirdPartyUserId,
|
|
|
+ remark: $remark,
|
|
|
+ callbackData: array_merge($callbackData, [
|
|
|
+ 'third_party_app_id' => $thirdPartyAppId,
|
|
|
+ 'third_party_user_id' => $thirdPartyUserId,
|
|
|
+ 'operation_type' => 'recharge'
|
|
|
+ ])
|
|
|
);
|
|
|
|
|
|
return TransferOrderDto::fromModel($order);
|
|
|
} catch (\Exception $e) {
|
|
|
\Illuminate\Support\Facades\Log::error('ThirdParty recharge order creation failed', [
|
|
|
- 'third_party_app_id' => $thirdPartyAppId,
|
|
|
- 'farm_user_id' => $farmUserId,
|
|
|
+ 'third_party_app_id' => $thirdPartyAppId,
|
|
|
+ 'farm_user_id' => $farmUserId,
|
|
|
'third_party_user_id' => $thirdPartyUserId,
|
|
|
- 'amount' => $thirdPartyAmount,
|
|
|
- 'error' => $e->getMessage()
|
|
|
+ 'amount' => $thirdPartyAmount,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
]);
|
|
|
|
|
|
return $e->getMessage();
|
|
|
@@ -93,13 +95,14 @@ class TransferThirdPartyService
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public static function createWithdrawOrder(
|
|
|
- int $thirdPartyAppId,
|
|
|
- int $farmUserId,
|
|
|
- string $thirdPartyUserId,
|
|
|
- string $thirdPartyAmount,
|
|
|
+ int $thirdPartyAppId,
|
|
|
+ int $farmUserId,
|
|
|
+ string $thirdPartyUserId,
|
|
|
+ string $thirdPartyAmount,
|
|
|
?string $remark = null,
|
|
|
- array $callbackData = []
|
|
|
- ): TransferOrderDto|string {
|
|
|
+ array $callbackData = []
|
|
|
+ ): TransferOrderDto|string
|
|
|
+ {
|
|
|
try {
|
|
|
// 根据三方应用ID查找对应的Transfer应用配置
|
|
|
$transferApp = TransferApp::where('out_id3', $thirdPartyAppId)
|
|
|
@@ -119,25 +122,25 @@ class TransferThirdPartyService
|
|
|
// 注意:这里传递的是外部金额,createTransferOutForThirdParty内部会进行金额转换
|
|
|
$order = TransferLogic::createTransferOutForThirdParty(
|
|
|
transferAppId: $transferApp->id,
|
|
|
- userId: $farmUserId,
|
|
|
- amount: $thirdPartyAmount, // 传递外部金额
|
|
|
- outUserId: $thirdPartyUserId,
|
|
|
- remark: $remark,
|
|
|
- callbackData: array_merge($callbackData, [
|
|
|
- 'third_party_app_id' => $thirdPartyAppId,
|
|
|
- 'third_party_user_id' => $thirdPartyUserId,
|
|
|
- 'operation_type' => 'withdraw'
|
|
|
- ])
|
|
|
+ userId: $farmUserId,
|
|
|
+ amount: $thirdPartyAmount, // 传递外部金额
|
|
|
+ outUserId: $thirdPartyUserId,
|
|
|
+ remark: $remark,
|
|
|
+ callbackData: array_merge($callbackData, [
|
|
|
+ 'third_party_app_id' => $thirdPartyAppId,
|
|
|
+ 'third_party_user_id' => $thirdPartyUserId,
|
|
|
+ 'operation_type' => 'withdraw'
|
|
|
+ ])
|
|
|
);
|
|
|
|
|
|
return TransferOrderDto::fromModel($order);
|
|
|
} catch (\Exception $e) {
|
|
|
\Illuminate\Support\Facades\Log::error('ThirdParty withdraw order creation failed', [
|
|
|
- 'third_party_app_id' => $thirdPartyAppId,
|
|
|
- 'farm_user_id' => $farmUserId,
|
|
|
+ 'third_party_app_id' => $thirdPartyAppId,
|
|
|
+ 'farm_user_id' => $farmUserId,
|
|
|
'third_party_user_id' => $thirdPartyUserId,
|
|
|
- 'third_party_amount' => $thirdPartyAmount,
|
|
|
- 'error' => $e->getMessage()
|
|
|
+ 'third_party_amount' => $thirdPartyAmount,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
]);
|
|
|
|
|
|
return $e->getMessage();
|
|
|
@@ -146,7 +149,7 @@ class TransferThirdPartyService
|
|
|
|
|
|
/**
|
|
|
* 根据三方应用ID获取Transfer应用配置
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $thirdPartyAppId 三方应用ID
|
|
|
* @return TransferApp|null
|
|
|
*/
|
|
|
@@ -159,64 +162,64 @@ class TransferThirdPartyService
|
|
|
|
|
|
/**
|
|
|
* 检查三方应用是否支持充值
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $thirdPartyAppId 三方应用ID
|
|
|
* @return bool
|
|
|
*/
|
|
|
public static function supportsRecharge(int $thirdPartyAppId): bool
|
|
|
{
|
|
|
$transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
|
|
|
-
|
|
|
+
|
|
|
return $transferApp && $transferApp->supportsTransferIn();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查三方应用是否支持提现
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $thirdPartyAppId 三方应用ID
|
|
|
* @return bool
|
|
|
*/
|
|
|
public static function supportsWithdraw(int $thirdPartyAppId): bool
|
|
|
{
|
|
|
$transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
|
|
|
-
|
|
|
+
|
|
|
return $transferApp && $transferApp->supportsTransferOut();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取三方应用的手续费配置
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $thirdPartyAppId 三方应用ID
|
|
|
* @return array
|
|
|
*/
|
|
|
public static function getFeeConfig(int $thirdPartyAppId): array
|
|
|
{
|
|
|
$transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
|
|
|
-
|
|
|
+
|
|
|
if (!$transferApp) {
|
|
|
return [
|
|
|
- 'error' => '未找到对应的划转应用配置',
|
|
|
- 'recharge' => ['rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false],
|
|
|
- 'withdraw' => ['rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false],
|
|
|
+ 'error' => '未找到对应的划转应用配置',
|
|
|
+ 'recharge' => [ 'rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false ],
|
|
|
+ 'withdraw' => [ 'rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false ],
|
|
|
];
|
|
|
}
|
|
|
|
|
|
return [
|
|
|
- 'recharge' => [
|
|
|
- 'rate' => $transferApp->fee_in_rate,
|
|
|
- 'min' => $transferApp->fee_in_min,
|
|
|
- 'max' => $transferApp->fee_in_max,
|
|
|
+ 'recharge' => [
|
|
|
+ 'rate' => $transferApp->fee_in_rate,
|
|
|
+ 'min' => $transferApp->fee_in_min,
|
|
|
+ 'max' => $transferApp->fee_in_max,
|
|
|
'enabled' => $transferApp->isFeeEnabled('in'),
|
|
|
],
|
|
|
- 'withdraw' => [
|
|
|
- 'rate' => $transferApp->fee_out_rate,
|
|
|
- 'min' => $transferApp->fee_out_min,
|
|
|
- 'max' => $transferApp->fee_out_max,
|
|
|
+ 'withdraw' => [
|
|
|
+ 'rate' => $transferApp->fee_out_rate,
|
|
|
+ 'min' => $transferApp->fee_out_min,
|
|
|
+ 'max' => $transferApp->fee_out_max,
|
|
|
'enabled' => $transferApp->isFeeEnabled('out'),
|
|
|
],
|
|
|
'exchange_rate' => $transferApp->exchange_rate,
|
|
|
- 'currency_id' => $transferApp->currency_id,
|
|
|
- 'fund_id' => $transferApp->fund_id,
|
|
|
+ 'currency_id' => $transferApp->currency_id,
|
|
|
+ 'fund_id' => $transferApp->fund_id,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -226,6 +229,7 @@ class TransferThirdPartyService
|
|
|
* @param int $thirdPartyAppId 三方应用ID
|
|
|
* @param string $amount 充值金额
|
|
|
* @return TransferFeeDto
|
|
|
+ * @deprecated 暂时无用,有bug
|
|
|
*/
|
|
|
public static function calculateRechargeFee(int $thirdPartyAppId, string $amount): TransferFeeDto
|
|
|
{
|
|
|
@@ -234,31 +238,20 @@ class TransferThirdPartyService
|
|
|
if (!$transferApp) {
|
|
|
return TransferFeeDto::error(
|
|
|
originalAmount: $amount,
|
|
|
- errorMessage: '未找到对应的划转应用配置'
|
|
|
+ errorMessage: '未找到对应的划转应用配置'
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// 将三方金额转换为农场内部金额(充值:外部金额转内部金额)
|
|
|
- $internalAmount = bcmul($amount, (string) $transferApp->exchange_rate, 10);
|
|
|
+ $internalAmount = bcmul($amount, (string)$transferApp->exchange_rate, 10);
|
|
|
|
|
|
// 获取手续费计算结果
|
|
|
$feeResult = $transferApp->calculateInFee($internalAmount);
|
|
|
|
|
|
// 将结果转换为DTO
|
|
|
- return TransferFeeDto::fromLegacyArray($feeResult, $amount);
|
|
|
+ return TransferFeeDto::f($feeResult, $amount);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 计算提现手续费
|
|
|
- *
|
|
|
- * @param int $thirdPartyAppId 三方应用ID
|
|
|
- * @param string $amount 提现金额
|
|
|
- * @return TransferFeeDto
|
|
|
- */
|
|
|
- public static function calculateWithdrawFee(int $thirdPartyAppId, string $amount): TransferFeeDto
|
|
|
- {
|
|
|
- return self::calculateWithdrawFeeWithContext($thirdPartyAppId, $amount, []);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 计算提现手续费(带上下文信息)
|
|
|
@@ -275,17 +268,32 @@ class TransferThirdPartyService
|
|
|
if (!$transferApp) {
|
|
|
return TransferFeeDto::error(
|
|
|
originalAmount: $amount,
|
|
|
- errorMessage: '未找到对应的划转应用配置'
|
|
|
+ errorMessage: '未找到对应的划转应用配置'
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// 将三方金额转换为农场内部金额(提现:外部金额转内部金额)
|
|
|
- $internalAmount = bcmul($amount, (string) $transferApp->exchange_rate, 10);
|
|
|
+ $internalAmount = bcmul($amount, (string)$transferApp->exchange_rate, 10);
|
|
|
|
|
|
// 获取手续费计算结果,传递上下文信息以便URS推广模块计算正确的手续费率
|
|
|
$feeResult = $transferApp->calculateOutFee($internalAmount, $context);
|
|
|
+ // ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 用户总支付金额]
|
|
|
+ // public readonly float $feeRate,
|
|
|
+ // public readonly string $feeAmount,
|
|
|
+ // public readonly string $actualAmount,
|
|
|
+ // public readonly string $originalAmount,
|
|
|
+ // public readonly bool $hasError = false,
|
|
|
+ // public readonly ?string $errorMessage = null,
|
|
|
|
|
|
// 将结果转换为DTO
|
|
|
- return TransferFeeDto::fromLegacyArray($feeResult, $amount);
|
|
|
+ return TransferFeeDto::success(
|
|
|
+ originalAmount: $amount,
|
|
|
+ feeRate: $feeResult['fee_rate'],
|
|
|
+ feeAmount: $feeResult['fee_amount'],
|
|
|
+ actualAmount: $internalAmount,
|
|
|
+ totleAmount: $feeResult['actual_amount'],
|
|
|
+ additionalInfo: []
|
|
|
+ );
|
|
|
}
|
|
|
+
|
|
|
}
|