| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace App\Module\Transfer\Services;
- use App\Module\Transfer\Dtos\TransferOrderDto;
- use App\Module\Transfer\Dtos\TransferFeeDto;
- use App\Module\Transfer\Logics\TransferLogic;
- use App\Module\Transfer\Models\TransferApp;
- /**
- * Transfer模块为ThirdParty模块提供的服务接口
- * 专门为第三方应用提供充值和提现功能
- */
- class TransferThirdPartyService
- {
- /**
- * 创建充值单(转入订单)
- *
- * @param int $thirdPartyAppId 三方应用ID
- * @param int $farmUserId 农场用户ID
- * @param string $thirdPartyUserId 三方用户ID
- * @param string $thirdPartyAmount 三方金额
- * @param string|null $remark 备注
- * @param array $callbackData 回调数据
- * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
- * @throws \Exception
- */
- public static function createRechargeOrder(
- int $thirdPartyAppId,
- int $farmUserId,
- string $thirdPartyUserId,
- string $thirdPartyAmount,
- ?string $remark = null,
- array $callbackData = []
- ): TransferOrderDto|string
- {
- try {
- // 根据三方应用ID查找对应的Transfer应用配置
- $transferApp = TransferApp::where('out_id3', $thirdPartyAppId)
- ->where('is_enabled', true)
- ->first();
- if (!$transferApp) {
- return '未找到对应的划转应用配置或应用已禁用';
- }
- // 检查应用是否支持转入
- if (!$transferApp->supportsTransferIn()) {
- return '该应用不支持充值功能或充值功能已被禁用';
- }
- // 生成业务订单ID(使用时间戳+随机字符串)
- $businessId = 'TP_IN_' . date('YmdHis') . '_' . substr(md5(uniqid()), 0, 8);
- // 调用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'
- ])
- );
- 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_user_id' => $thirdPartyUserId,
- 'amount' => $thirdPartyAmount,
- 'error' => $e->getMessage()
- ]);
- return $e->getMessage();
- }
- }
- /**
- * 创建提现单(转出订单)
- *
- * @param int $thirdPartyAppId 三方应用ID
- * @param int $farmUserId 农场用户ID
- * @param string $thirdPartyUserId 三方用户ID
- * @param string $thirdPartyAmount 三方金额(外部金额)
- * @param string|null $remark 备注
- * @param array $callbackData 回调数据
- * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
- * @throws \Exception
- */
- public static function createWithdrawOrder(
- int $thirdPartyAppId,
- int $farmUserId,
- string $thirdPartyUserId,
- string $thirdPartyAmount,
- ?string $remark = null,
- array $callbackData = []
- ): TransferOrderDto|string
- {
- try {
- // 根据三方应用ID查找对应的Transfer应用配置
- $transferApp = TransferApp::where('out_id3', $thirdPartyAppId)
- ->where('is_enabled', true)
- ->first();
- if (!$transferApp) {
- return '未找到对应的划转应用配置或应用已禁用';
- }
- // 检查应用是否支持转出
- if (!$transferApp->supportsTransferOut()) {
- return '该应用不支持提现功能或提现功能已被禁用';
- }
- // 调用Transfer模块的第三方转出逻辑(跳过密码验证)
- // 注意:这里传递的是外部金额,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'
- ])
- );
- 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_user_id' => $thirdPartyUserId,
- 'third_party_amount' => $thirdPartyAmount,
- 'error' => $e->getMessage()
- ]);
- return $e->getMessage();
- }
- }
- /**
- * 根据三方应用ID获取Transfer应用配置
- *
- * @param int $thirdPartyAppId 三方应用ID
- * @return TransferApp|null
- */
- public static function getTransferAppByThirdPartyId(int $thirdPartyAppId): ?TransferApp
- {
- return TransferApp::where('out_id3', $thirdPartyAppId)
- ->where('is_enabled', true)
- ->first();
- }
- /**
- * 检查三方应用是否支持充值
- *
- * @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 ],
- ];
- }
- return [
- '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,
- 'enabled' => $transferApp->isFeeEnabled('out'),
- ],
- 'exchange_rate' => $transferApp->exchange_rate,
- 'currency_id' => $transferApp->currency_id,
- 'fund_id' => $transferApp->fund_id,
- ];
- }
- /**
- * 计算充值手续费
- *
- * @param int $thirdPartyAppId 三方应用ID
- * @param string $amount 充值金额
- * @return TransferFeeDto
- * @deprecated 暂时无用,有bug
- */
- public static function calculateRechargeFee(int $thirdPartyAppId, string $amount): TransferFeeDto
- {
- $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
- if (!$transferApp) {
- return TransferFeeDto::error(
- originalAmount: $amount,
- errorMessage: '未找到对应的划转应用配置'
- );
- }
- // 将三方金额转换为农场内部金额(充值:外部金额转内部金额)
- $internalAmount = bcmul($amount, (string)$transferApp->exchange_rate, 10);
- // 获取手续费计算结果
- $feeResult = $transferApp->calculateInFee($internalAmount);
- // 将结果转换为DTO
- return TransferFeeDto::f($feeResult, $amount);
- }
- /**
- * 计算提现手续费(带上下文信息)
- *
- * @param int $thirdPartyAppId 三方应用ID
- * @param string $amount 提现金额
- * @param array $context 上下文信息(包含用户ID等,用于URS推广模块计算手续费率)
- * @return TransferFeeDto
- */
- public static function calculateWithdrawFeeWithContext(int $thirdPartyAppId, string $amount, array $context = []): TransferFeeDto
- {
- $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
- if (!$transferApp) {
- return TransferFeeDto::error(
- originalAmount: $amount,
- errorMessage: '未找到对应的划转应用配置'
- );
- }
- // 将三方金额转换为农场内部金额(提现:外部金额转内部金额)
- $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::success(
- originalAmount: $amount,
- feeRate: $feeResult['fee_rate'],
- feeAmount: $feeResult['fee_amount'],
- actualAmount: $internalAmount,
- totleAmount: $feeResult['actual_amount'],
- additionalInfo: []
- );
- }
- }
|