TransferThirdPartyService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. $order = TransferLogic::createTransferOutForThirdParty(
  107. transferAppId: $transferApp->id,
  108. userId: $farmUserId,
  109. amount: $thirdPartyAmount,
  110. outUserId: $thirdPartyUserId,
  111. remark: $remark,
  112. callbackData: array_merge($callbackData, [
  113. 'third_party_app_id' => $thirdPartyAppId,
  114. 'third_party_user_id' => $thirdPartyUserId,
  115. 'operation_type' => 'withdraw'
  116. ])
  117. );
  118. return TransferOrderDto::fromModel($order);
  119. } catch (\Exception $e) {
  120. \Illuminate\Support\Facades\Log::error('ThirdParty withdraw order creation failed', [
  121. 'third_party_app_id' => $thirdPartyAppId,
  122. 'farm_user_id' => $farmUserId,
  123. 'third_party_user_id' => $thirdPartyUserId,
  124. 'amount' => $thirdPartyAmount,
  125. 'error' => $e->getMessage()
  126. ]);
  127. return $e->getMessage();
  128. }
  129. }
  130. /**
  131. * 根据三方应用ID获取Transfer应用配置
  132. *
  133. * @param int $thirdPartyAppId 三方应用ID
  134. * @return TransferApp|null
  135. */
  136. public static function getTransferAppByThirdPartyId(int $thirdPartyAppId): ?TransferApp
  137. {
  138. return TransferApp::where('out_id3', $thirdPartyAppId)
  139. ->where('is_enabled', true)
  140. ->first();
  141. }
  142. /**
  143. * 检查三方应用是否支持充值
  144. *
  145. * @param int $thirdPartyAppId 三方应用ID
  146. * @return bool
  147. */
  148. public static function supportsRecharge(int $thirdPartyAppId): bool
  149. {
  150. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  151. return $transferApp && $transferApp->supportsTransferIn();
  152. }
  153. /**
  154. * 检查三方应用是否支持提现
  155. *
  156. * @param int $thirdPartyAppId 三方应用ID
  157. * @return bool
  158. */
  159. public static function supportsWithdraw(int $thirdPartyAppId): bool
  160. {
  161. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  162. return $transferApp && $transferApp->supportsTransferOut();
  163. }
  164. /**
  165. * 获取三方应用的手续费配置
  166. *
  167. * @param int $thirdPartyAppId 三方应用ID
  168. * @return array
  169. */
  170. public static function getFeeConfig(int $thirdPartyAppId): array
  171. {
  172. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  173. if (!$transferApp) {
  174. return [
  175. 'error' => '未找到对应的划转应用配置',
  176. 'recharge' => ['rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false],
  177. 'withdraw' => ['rate' => 0, 'min' => 0, 'max' => 0, 'enabled' => false],
  178. ];
  179. }
  180. return [
  181. 'recharge' => [
  182. 'rate' => $transferApp->fee_in_rate,
  183. 'min' => $transferApp->fee_in_min,
  184. 'max' => $transferApp->fee_in_max,
  185. 'enabled' => $transferApp->isFeeEnabled('in'),
  186. ],
  187. 'withdraw' => [
  188. 'rate' => $transferApp->fee_out_rate,
  189. 'min' => $transferApp->fee_out_min,
  190. 'max' => $transferApp->fee_out_max,
  191. 'enabled' => $transferApp->isFeeEnabled('out'),
  192. ],
  193. 'exchange_rate' => $transferApp->exchange_rate,
  194. 'currency_id' => $transferApp->currency_id,
  195. 'fund_id' => $transferApp->fund_id,
  196. ];
  197. }
  198. /**
  199. * 计算充值手续费
  200. *
  201. * @param int $thirdPartyAppId 三方应用ID
  202. * @param string $amount 充值金额
  203. * @return TransferFeeDto
  204. */
  205. public static function calculateRechargeFee(int $thirdPartyAppId, string $amount): TransferFeeDto
  206. {
  207. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  208. if (!$transferApp) {
  209. return TransferFeeDto::error(
  210. originalAmount: $amount,
  211. errorMessage: '未找到对应的划转应用配置'
  212. );
  213. }
  214. // 将三方金额转换为农场内部金额
  215. $internalAmount = bcdiv($amount, (string) $transferApp->exchange_rate, 10);
  216. // 获取手续费计算结果
  217. $feeResult = $transferApp->calculateInFee($internalAmount);
  218. // 将结果转换为DTO
  219. return TransferFeeDto::fromLegacyArray($feeResult, $amount);
  220. }
  221. /**
  222. * 计算提现手续费
  223. *
  224. * @param int $thirdPartyAppId 三方应用ID
  225. * @param string $amount 提现金额
  226. * @return TransferFeeDto
  227. */
  228. public static function calculateWithdrawFee(int $thirdPartyAppId, string $amount): TransferFeeDto
  229. {
  230. $transferApp = self::getTransferAppByThirdPartyId($thirdPartyAppId);
  231. if (!$transferApp) {
  232. return TransferFeeDto::error(
  233. originalAmount: $amount,
  234. errorMessage: '未找到对应的划转应用配置'
  235. );
  236. }
  237. // 将三方金额转换为农场内部金额
  238. $internalAmount = bcmul($amount, (string) $transferApp->exchange_rate, 10);
  239. // 获取手续费计算结果
  240. $feeResult = $transferApp->calculateOutFee($internalAmount);
  241. // 将结果转换为DTO
  242. return TransferFeeDto::fromLegacyArray($feeResult, $amount);
  243. }
  244. }