BotImPayCashierLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Estatisticas\RechargeWithDraw;
  4. use App\Http\helper\NumConfig;
  5. use App\Inter\CashierInterFace;
  6. use App\Models\PrivateMail;
  7. use App\Models\RecordUserDataStatistics;
  8. use App\Services\BotImPayService;
  9. use App\Services\PayConfig;
  10. use App\Services\StoredProcedure;
  11. use App\Util;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Redis;
  15. class BotImPayCashierLogic implements CashierInterFace
  16. {
  17. const AGENT = 107; // BotImPay代付渠道值
  18. protected $agent = 107;
  19. /**
  20. * PixType 到币种简称映射
  21. * 3=BTC, 4=ETH, 5=USDT, 6=USDC
  22. */
  23. protected $currencyMap = [
  24. 3 => 'BTC',
  25. 4 => 'ETH',
  26. 5 => 'USDT',
  27. 6 => 'USDC',
  28. ];
  29. /**
  30. * 提交代付申请
  31. */
  32. public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
  33. {
  34. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $RecordID)->first();
  35. if (!$query) return 'fail';
  36. $payConfigService = new PayConfig();
  37. $config = $payConfigService->getConfig('BotImPayOut');
  38. // 确定币种:根据 PixType 映射
  39. $currency = $this->currencyMap[$PixType] ?? 'USDT';
  40. // 构建代付请求参数(BotImPay: bank_code 固定 trc20)
  41. $params = [
  42. 'mer_no' => $config['mer_no'] ?? '',
  43. 'order_no' => $OrderId,
  44. 'amount' => number_format($amount / NumConfig::NUM_VALUE, 2, '.', ''),
  45. 'currency' => $currency,
  46. 'bank_code' => 'trc20',
  47. 'name' => $accountName ?: 'user',
  48. 'account' => $PixNum ?: $email,
  49. 'phone' => $phone ?: '0000000000',
  50. 'email' => $email ?: '',
  51. 'notify_url' => $config['notify_url'] ?? '',
  52. ];
  53. $service = new BotImPayService('BotImPayOut');
  54. $signedParams = $service->sign($params);
  55. $url = ($config['apiUrl'] ?? 'https://api.botimpay.top') . '/open/api/order/out';
  56. Log::info('BotImPay 提现参数:', $signedParams);
  57. try {
  58. $result = $service->curlPost($url, $signedParams);
  59. } catch (\Exception $exception) {
  60. Log::info('BotImPay 提现请求异常:', [$exception->getMessage()]);
  61. Util::WriteLog('BotImPay_error', 'BotImPay 提现请求异常:' . $exception->getMessage());
  62. return 'fail';
  63. }
  64. Log::info('BotImPay 提现结果:', [$result ?? "no result"]);
  65. try {
  66. $data = \GuzzleHttp\json_decode($result, true);
  67. } catch (\Exception $e) {
  68. Util::WriteLog("BotImPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  69. return 'fail';
  70. }
  71. // BotImPay 代付成功响应: code=200, data.sys_no 是系统流水号
  72. if (isset($data['code']) && $data['code'] == 200) {
  73. return $data;
  74. }
  75. // 同步失败处理
  76. if ($query->State == 5) {
  77. $WithDraw = $query->WithDraw + $query->ServiceFee;
  78. $bonus = '30000,' . $WithDraw;
  79. PrivateMail::failMail($query->UserID, $OrderId, $WithDraw, 'Liquidation failure', $bonus);
  80. $withdraw_data = [
  81. 'State' => 6,
  82. 'agent' => 1070,
  83. 'finishDate' => now(),
  84. 'remark' => json_encode($data),
  85. ];
  86. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  87. ->where('OrderId', $query->OrderId)
  88. ->update($withdraw_data);
  89. $RecordData = ['after_state' => 6, 'update_at' => now()];
  90. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
  91. ->where('type', 1)->where('RecordID', $RecordID)
  92. ->update($RecordData);
  93. }
  94. return 'fail';
  95. }
  96. /**
  97. * 代付异步回调处理
  98. *
  99. * BotImPay 代付回调格式(POST JSON):
  100. * {
  101. * "mer_no": 3000, "order_no": "xxx",
  102. * "order_amount": "10.00", "order_reality_amount": "10.00",
  103. * "currency": "USDT", "result": "success", "sys_no": "212073",
  104. * "utr": "xxx", "sign": "xxx"
  105. * }
  106. */
  107. public function notify($post)
  108. {
  109. if (!is_array($post)) $post = \GuzzleHttp\json_decode($post, true);
  110. Util::WriteLog('BotImPay', 'BotImPay 提现回调:' . json_encode($post));
  111. try {
  112. $OrderId = $post['order_no'] ?? '';
  113. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  114. ->where('OrderId', $OrderId)->first();
  115. if (!$query) {
  116. Util::WriteLog('BotImPay', '提现订单不存在: ' . $OrderId);
  117. return 'SUCCESS';
  118. }
  119. if ($query->State != 5 && $query->State != 7) {
  120. Util::WriteLog('BotImPay', $OrderId . '_订单状态已完成');
  121. return 'SUCCESS';
  122. }
  123. $agentID = DB::connection('write')->table('agent.dbo.admin_configs')
  124. ->where('config_value', self::AGENT)
  125. ->where('type', 'cash')
  126. ->select('id')->first()->id ?? '';
  127. $now = now();
  128. $notify_data = [
  129. 'state' => 1,
  130. 'finish_at' => $now,
  131. 'casOrdNo' => $post['sys_no'] ?? '',
  132. 'extra' => \GuzzleHttp\json_encode($post),
  133. 'created_at' => $now,
  134. 'updated_at' => $now,
  135. 'order_sn' => $OrderId,
  136. 'amount' => $query->WithDraw,
  137. ];
  138. $result = $post['result'] ?? '';
  139. $orderStatus = 0;
  140. if ($result === 'success') {
  141. $orderStatus = 1;
  142. } elseif ($result === 'fail') {
  143. $orderStatus = 2;
  144. }
  145. if (!$orderStatus) {
  146. Util::WriteLog('BotImPay', 'BotImPay 提现处理中:' . $OrderId);
  147. return 'SUCCESS';
  148. }
  149. Util::WriteLog('BotImPay', 'BotImPay 提现结果:' . $OrderId . '_' . $orderStatus);
  150. $UserID = $query->UserID;
  151. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  152. $withdraw_data = [];
  153. switch ($orderStatus) {
  154. case 1: // 提现成功
  155. Util::WriteLog('BotImPay', 'BotImPay提现成功');
  156. $withdraw_data = [
  157. 'State' => 2,
  158. 'agent' => $agentID,
  159. 'finishDate' => $now,
  160. ];
  161. // 计算代付手续费
  162. $payConfigService = new PayConfig();
  163. $outConfig = $payConfigService->getConfig('BotImPayOut');
  164. $payRates = $outConfig['pay_rate'] ?? null;
  165. if (is_array($payRates)) {
  166. $pixType = $query->PixType ?? 5;
  167. $payRate = $payRates[$pixType] ?? ($payRates[5] ?? [1, 1]);
  168. $feePercent = $payRate[0] ?? 1;
  169. $feeFixed = $payRate[1] ?? 1;
  170. $withdraw_data['withdraw_fee'] = intval(($query->WithDraw * $feePercent) / 100)
  171. + (int)($feeFixed * NumConfig::NUM_VALUE);
  172. }
  173. // 增加提现记录
  174. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')
  175. ->where('UserID', $UserID)->first();
  176. if ($first) {
  177. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')
  178. ->where('UserID', $UserID)->increment('TakeMoney', $TakeMoney);
  179. } else {
  180. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')
  181. ->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  182. try { PrivateMail::praiseSendMail($UserID); } catch (\Exception $e) {}
  183. }
  184. // 免审状态更新
  185. $log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')
  186. ->where('order_sn', $OrderId)->first();
  187. if ($log) {
  188. DB::connection('write')->table('agent.dbo.withdrawal_position_log')
  189. ->where('order_sn', $OrderId)
  190. ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  191. }
  192. try { StoredProcedure::addPlatformData($UserID, 4, $TakeMoney); } catch (\Exception $e) {
  193. Util::WriteLog('StoredProcedure', $e);
  194. }
  195. $ServiceFee = $query->ServiceFee;
  196. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  197. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney, $withdraw_data['withdraw_fee'] ?? 0, $ServiceFee);
  198. Redis::connection()->incr('draw_' . date('Ymd') . $UserID);
  199. PrivateMail::successMail($UserID, $OrderId, $TakeMoney);
  200. break;
  201. case 2: // 提现失败
  202. $msg = 'Encomenda rejeitada pelo banco';
  203. $bonus = '30000,' . $TakeMoney;
  204. PrivateMail::failMail($query->UserID, $OrderId, $TakeMoney, $msg, $bonus);
  205. Util::WriteLog('BotImPayEmail', [$query->UserID, $OrderId, $TakeMoney, $msg, $bonus]);
  206. $withdraw_data = ['State' => 6, 'agent' => $agentID, 'remark' => $post['result_mes'] ?? ''];
  207. $notify_data['state'] = 2;
  208. break;
  209. }
  210. $RecordData = [
  211. 'before_state' => $query->State,
  212. 'after_state' => $withdraw_data['State'] ?? 0,
  213. 'RecordID' => $query->RecordID,
  214. 'update_at' => date('Y-m-d H:i:s'),
  215. ];
  216. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
  217. ->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
  218. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  219. ->where('OrderId', $query->OrderId)->update($withdraw_data);
  220. if (isset($withdraw_data['State']) && $withdraw_data['State'] == 2) {
  221. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  222. }
  223. return 'SUCCESS';
  224. } catch (\Exception $exception) {
  225. Util::WriteLog('BotImPay', 'BotImPay异步业务逻辑处理失败:' . $exception->getMessage());
  226. return 'SUCCESS';
  227. }
  228. }
  229. }