TransferThirdPartyService.php 10 KB

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