TransferThirdPartyService.php 9.8 KB

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