TransferThirdPartyService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Module\Transfer\Services;
  3. use App\Module\Transfer\Dtos\TransferOrderDto;
  4. use App\Module\Transfer\Dtos\TransferFeeDto;
  5. use App\Module\Transfer\Logics\TransferLogic;
  6. use App\Module\Transfer\Models\TransferApp;
  7. /**
  8. * Transfer模块为ThirdParty模块提供的服务接口
  9. * 专门为第三方应用提供充值和提现功能
  10. */
  11. class TransferThirdPartyService
  12. {
  13. /**
  14. * 创建充值单(转入订单)
  15. *
  16. * @param int $thirdPartyAppId 三方应用ID
  17. * @param int $farmUserId 农场用户ID
  18. * @param string $thirdPartyUserId 三方用户ID
  19. * @param string $thirdPartyAmount 三方金额
  20. * @param string|null $remark 备注
  21. * @param array $callbackData 回调数据
  22. * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
  23. * @throws \Exception
  24. */
  25. public static function createRechargeOrder(
  26. int $thirdPartyAppId,
  27. int $farmUserId,
  28. string $thirdPartyUserId,
  29. string $thirdPartyAmount,
  30. ?string $remark = null,
  31. array $callbackData = []
  32. ): TransferOrderDto|string
  33. {
  34. try {
  35. // 根据三方应用ID查找对应的Transfer应用配置
  36. $transferApp = TransferApp::where('out_id3', $thirdPartyAppId)
  37. ->where('is_enabled', true)
  38. ->first();
  39. if (!$transferApp) {
  40. return '未找到对应的划转应用配置或应用已禁用';
  41. }
  42. // 检查应用是否支持转入
  43. if (!$transferApp->supportsTransferIn()) {
  44. return '该应用不支持充值功能或充值功能已被禁用';
  45. }
  46. // 生成业务订单ID(使用时间戳+随机字符串)
  47. $businessId = 'TP_IN_' . date('YmdHis') . '_' . substr(md5(uniqid()), 0, 8);
  48. // 调用Transfer模块的转入逻辑
  49. $order = TransferLogic::createTransferIn(
  50. transferAppId: $transferApp->id,
  51. userId: $farmUserId,
  52. businessId: $businessId,
  53. amount: $thirdPartyAmount,
  54. outUserId: $thirdPartyUserId,
  55. remark: $remark,
  56. callbackData: array_merge($callbackData, [
  57. 'third_party_app_id' => $thirdPartyAppId,
  58. 'third_party_user_id' => $thirdPartyUserId,
  59. 'operation_type' => 'recharge'
  60. ])
  61. );
  62. return TransferOrderDto::fromModel($order);
  63. } catch (\Exception $e) {
  64. \Illuminate\Support\Facades\Log::error('ThirdParty recharge order creation failed', [
  65. 'third_party_app_id' => $thirdPartyAppId,
  66. 'farm_user_id' => $farmUserId,
  67. 'third_party_user_id' => $thirdPartyUserId,
  68. 'amount' => $thirdPartyAmount,
  69. 'error' => $e->getMessage()
  70. ]);
  71. return $e->getMessage();
  72. }
  73. }
  74. /**
  75. * 创建提现单(转出订单)
  76. *
  77. * @param int $thirdPartyAppId 三方应用ID
  78. * @param int $farmUserId 农场用户ID
  79. * @param string $thirdPartyUserId 三方用户ID
  80. * @param string $thirdPartyAmount 三方金额(外部金额)
  81. * @param string|null $remark 备注
  82. * @param array $callbackData 回调数据
  83. * @return TransferOrderDto|string 成功返回DTO,失败返回错误信息
  84. * @throws \Exception
  85. */
  86. public static function createWithdrawOrder(
  87. int $thirdPartyAppId,
  88. int $farmUserId,
  89. string $thirdPartyUserId,
  90. string $thirdPartyAmount,
  91. ?string $remark = null,
  92. array $callbackData = []
  93. ): TransferOrderDto|string
  94. {
  95. try {
  96. // 根据三方应用ID查找对应的Transfer应用配置
  97. $transferApp = TransferApp::where('out_id3', $thirdPartyAppId)
  98. ->where('is_enabled', true)
  99. ->first();
  100. if (!$transferApp) {
  101. return '未找到对应的划转应用配置或应用已禁用';
  102. }
  103. // 检查应用是否支持转出
  104. if (!$transferApp->supportsTransferOut()) {
  105. return '该应用不支持提现功能或提现功能已被禁用';
  106. }
  107. // 调用Transfer模块的第三方转出逻辑(跳过密码验证)
  108. // 注意:这里传递的是外部金额,createTransferOutForThirdParty内部会进行金额转换
  109. $order = TransferLogic::createTransferOutForThirdParty(
  110. transferAppId: $transferApp->id,
  111. userId: $farmUserId,
  112. amount: $thirdPartyAmount, // 传递外部金额
  113. outUserId: $thirdPartyUserId,
  114. remark: $remark,
  115. callbackData: array_merge($callbackData, [
  116. 'third_party_app_id' => $thirdPartyAppId,
  117. 'third_party_user_id' => $thirdPartyUserId,
  118. 'operation_type' => 'withdraw'
  119. ])
  120. );
  121. return TransferOrderDto::fromModel($order);
  122. } catch (\Exception $e) {
  123. \Illuminate\Support\Facades\Log::error('ThirdParty withdraw order creation failed', [
  124. 'third_party_app_id' => $thirdPartyAppId,
  125. 'farm_user_id' => $farmUserId,
  126. 'third_party_user_id' => $thirdPartyUserId,
  127. 'third_party_amount' => $thirdPartyAmount,
  128. 'error' => $e->getMessage()
  129. ]);
  130. return $e->getMessage();
  131. }
  132. }
  133. /**
  134. * 根据三方应用ID获取Transfer应用配置
  135. *
  136. * @param int $thirdPartyAppId 三方应用ID
  137. * @return TransferApp|null
  138. */
  139. public static function getTransferAppByThirdPartyId(int $thirdPartyAppId): ?TransferApp
  140. {
  141. return TransferApp::where('out_id3', $thirdPartyAppId)
  142. ->where('is_enabled', true)
  143. ->first();
  144. }
  145. /**
  146. * 检查三方应用是否支持充值
  147. *
  148. * @param int $thirdPartyAppId 三方应用ID
  149. * @return bool
  150. */
  151. public static function supportsRecharge(int $thirdPartyAppId): bool
  152. {
  153. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  154. return $transferApp && $transferApp->supportsTransferIn();
  155. }
  156. /**
  157. * 检查三方应用是否支持提现
  158. *
  159. * @param int $thirdPartyAppId 三方应用ID
  160. * @return bool
  161. */
  162. public static function supportsWithdraw(int $thirdPartyAppId): bool
  163. {
  164. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  165. return $transferApp && $transferApp->supportsTransferOut();
  166. }
  167. /**
  168. * 获取三方应用的手续费配置
  169. *
  170. * @param int $thirdPartyAppId 三方应用ID
  171. * @return array
  172. */
  173. public static function getFeeConfig(int $thirdPartyAppId): array
  174. {
  175. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  176. if (!$transferApp) {
  177. return [
  178. 'error' => '未找到对应的划转应用配置',
  179. 'recharge' => [ 'rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false ],
  180. 'withdraw' => [ 'rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false ],
  181. ];
  182. }
  183. return [
  184. 'recharge' => [
  185. 'rate' => $transferApp->fee_in_rate,
  186. 'min' => $transferApp->fee_in_min,
  187. 'max' => $transferApp->fee_in_max,
  188. 'enabled' => $transferApp->isFeeEnabled('in'),
  189. ],
  190. 'withdraw' => [
  191. 'rate' => $transferApp->fee_out_rate,
  192. 'min' => $transferApp->fee_out_min,
  193. 'max' => $transferApp->fee_out_max,
  194. 'enabled' => $transferApp->isFeeEnabled('out'),
  195. ],
  196. 'exchange_rate' => $transferApp->exchange_rate,
  197. 'currency_id' => $transferApp->currency_id,
  198. 'fund_id' => $transferApp->fund_id,
  199. ];
  200. }
  201. /**
  202. * 计算充值手续费
  203. *
  204. * @param int $thirdPartyAppId 三方应用ID
  205. * @param string $amount 充值金额
  206. * @return TransferFeeDto
  207. * @deprecated 暂时无用,有bug
  208. */
  209. public static function calculateRechargeFee(int $thirdPartyAppId, string $amount): TransferFeeDto
  210. {
  211. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  212. if (!$transferApp) {
  213. return TransferFeeDto::error(
  214. originalAmount: $amount,
  215. errorMessage: '未找到对应的划转应用配置'
  216. );
  217. }
  218. // 将三方金额转换为农场内部金额(充值:外部金额转内部金额)
  219. $internalAmount = bcmul($amount, (string)$transferApp->exchange_rate, 10);
  220. // 获取手续费计算结果
  221. $feeResult = $transferApp->calculateInFee($internalAmount);
  222. // 将结果转换为DTO
  223. return TransferFeeDto::f($feeResult, $amount);
  224. }
  225. /**
  226. * 计算提现手续费(带上下文信息)
  227. *
  228. * @param int $thirdPartyAppId 三方应用ID
  229. * @param string $amount 提现金额
  230. * @param array $context 上下文信息(包含用户ID等,用于URS推广模块计算手续费率)
  231. * @return TransferFeeDto
  232. */
  233. public static function calculateWithdrawFeeWithContext(int $thirdPartyAppId, string $amount, array $context = []): TransferFeeDto
  234. {
  235. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  236. if (!$transferApp) {
  237. return TransferFeeDto::error(
  238. originalAmount: $amount,
  239. errorMessage: '未找到对应的划转应用配置'
  240. );
  241. }
  242. // 将三方金额转换为农场内部金额(提现:外部金额转内部金额)
  243. $internalAmount = bcmul($amount, (string)$transferApp->exchange_rate, 10);
  244. // 获取手续费计算结果,传递上下文信息以便URS推广模块计算正确的手续费率
  245. $feeResult = $transferApp->calculateOutFee($internalAmount, $context);
  246. // ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 用户总支付金额]
  247. // public readonly float $feeRate,
  248. // public readonly string $feeAmount,
  249. // public readonly string $actualAmount,
  250. // public readonly string $originalAmount,
  251. // public readonly bool $hasError = false,
  252. // public readonly ?string $errorMessage = null,
  253. // 将结果转换为DTO
  254. return TransferFeeDto::success(
  255. originalAmount: $amount,
  256. feeRate: $feeResult['fee_rate'],
  257. feeAmount: $feeResult['fee_amount'],
  258. actualAmount: $internalAmount,
  259. totleAmount: $feeResult['actual_amount'],
  260. additionalInfo: []
  261. );
  262. }
  263. }