TransferThirdPartyService.php 9.3 KB

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