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