BotImPayLogic.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Pay\AccountPayInfo;
  4. use App\dao\Pay\PayController;
  5. use App\Http\helper\CreateOrder;
  6. use App\Http\helper\NumConfig;
  7. use App\Jobs\Order;
  8. use App\Services\BotImPayService;
  9. use App\Services\CreateLog;
  10. use App\Services\OrderServices;
  11. use App\Services\PayConfig;
  12. use App\Util;
  13. use Illuminate\Support\Facades\DB;
  14. class BotImPayLogic extends BaseApiLogic
  15. {
  16. public $result;
  17. /**
  18. * 支付方式到 pay_code 的映射
  19. * 16=BTC, 32=ETH, 1024=USDT, 2048=USDC
  20. */
  21. protected $payCodeMap = [
  22. 16 => '20303', // BTC
  23. 32 => '20304', // ETH
  24. 1024 => '20301', // USDT
  25. 2048 => '20307', // USDC
  26. ];
  27. /**
  28. * 代收下单
  29. */
  30. public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  31. {
  32. $dao = new AccountPayInfo();
  33. [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
  34. // 礼包类型验证
  35. $PayVerify = new PayController();
  36. $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
  37. if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
  38. $this->error = $PayVerify->getError();
  39. return false;
  40. }
  41. if ($pay_amount < 0) {
  42. $this->error = 'Payment error_4';
  43. return false;
  44. }
  45. $service = new BotImPayService('BotImPay');
  46. $config = $service->config;
  47. $order_sn = CreateOrder::order_sn($userId);
  48. // 生成订单信息
  49. $logic = new OrderLogic();
  50. $amount = (int) round($pay_amount * NumConfig::NUM_VALUE);
  51. $logic->orderCreate($order_sn, $amount, 'BotImPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);
  52. // 确定 pay_code
  53. $payCode = $this->payCodeMap[$pay_method] ?? ($config['pay_code'] ?? '20301');
  54. $payCode = '20301'; // 当前只支持usdt
  55. // 构建支付请求参数(BotImPay 多一个 ip 字段)
  56. $coinMap = [
  57. '20301' => 'USDT',
  58. '20307' => 'USDC',
  59. ];
  60. $params = [
  61. 'mer_no' => $service->merNo,
  62. 'order_no' => $order_sn,
  63. 'amount' => (string)$pay_amount,
  64. 'name' => $userName ?: 'user',
  65. 'email' => $userEmail ?: ($userId . '@unknown.com'),
  66. 'phone' => $userPhone ?: '0000000000',
  67. 'currency' => $config['currency'] ?? 'USDT',
  68. 'pay_code' => $payCode,
  69. 'notify_url' => $config['notify_url'] ?? '',
  70. 'ip' => $buyIP,
  71. 'extra' => json_encode([
  72. 'coin' => $coinMap[$payCode] ?? 'USDT',
  73. 'blockchain' => 'Ethereum', // 必须以下值:BNB,Solana,TRON,Base,Avalanche,Ethereum,Polygon,Arbitrum,Optimism,Avalanche
  74. ]),
  75. ];
  76. // RSA签名
  77. $signedParams = $service->sign($params);
  78. // 生成用户请求信息日志
  79. $request_extra = \GuzzleHttp\json_encode($signedParams);
  80. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  81. $url = $service->apiUrl . '/open/api/order/in';
  82. $result = $service->curlPost($url, $signedParams);
  83. $rresult = compact('result', 'signedParams', 'url');
  84. Util::WriteLog('BotImPay', 'BotImPay支付请求' . $url . " | " . $request_extra);
  85. Util::WriteLog('BotImPay', 'BotImPay支付结果' . json_encode($rresult));
  86. try {
  87. $data = \GuzzleHttp\json_decode($result, true);
  88. } catch (\Exception $e) {
  89. Util::WriteLog("BotImPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  90. $this->error = 'Payment processing error';
  91. return false;
  92. }
  93. return $data;
  94. }
  95. /**
  96. * 代收异步回调处理
  97. *
  98. * BotImPay 回调: { mer_no, order_no, pay_type_code, order_amount, order_reality_amount, status, sign }
  99. */
  100. public function notify($post)
  101. {
  102. $order_no = $post['order_no'] ?? '';
  103. $order = DB::connection('write')->table('agent.dbo.order')
  104. ->where('order_sn', $order_no)
  105. ->first();
  106. if (!$order) {
  107. Util::WriteLog('BotImPay', '订单不存在: ' . $order_no);
  108. return 'ok';
  109. }
  110. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  111. return 'ok';
  112. }
  113. $status = $post['status'] ?? '';
  114. if ($status !== 'success') {
  115. Util::WriteLog('BotImPay', "支付未完成订单: {$order_no}, status: {$status}");
  116. return 'ok';
  117. }
  118. // 支付成功
  119. $GiftsID = $order->GiftsID ?: '';
  120. $userID = $order->user_id ?: '';
  121. $AdId = $order->AdId ?: '';
  122. $eventType = $order->eventType ?: '';
  123. $orderAmount = (float)($post['order_reality_amount'] ?? $post['order_amount'] ?? 0);
  124. $payAmt = $orderAmount;
  125. $body = [
  126. 'payment_sn' => $post['sys_no'] ?? '',
  127. 'updated_at' => date('Y-m-d H:i:s'),
  128. 'pay_status' => 1,
  129. 'pay_at' => date('Y-m-d H:i:s'),
  130. 'finished_at'=> date('Y-m-d H:i:s'),
  131. 'amount' => (int) round($payAmt * NumConfig::NUM_VALUE),
  132. ];
  133. // 根据支付方式计算代收手续费: 费率% * 金额 + 固定$
  134. $config = (new PayConfig())->getConfig('BotImPay');
  135. $payRates = $config['pay_rate'] ?? null;
  136. if (is_array($payRates)) {
  137. $payMethod = $order->order_title ?? 1024;
  138. $payRate = $payRates[$payMethod] ?? ($payRates[1024] ?? [3.5, 0]);
  139. $feePercent = $payRate[0] ?? 3.5;
  140. $feeFixed = $payRate[1] ?? 0;
  141. $body['payment_fee'] = intval(($body['amount'] * $feePercent) / 100)
  142. + (int)($feeFixed * NumConfig::NUM_VALUE);
  143. }
  144. try {
  145. $service = new OrderServices();
  146. if (intval($order->amount) != $body['amount']) {
  147. $body['GiftsID'] = 0;
  148. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  149. $Recharge = $payAmt;
  150. $give = 0;
  151. $favorable_price = $Recharge + $give;
  152. $czReason = 1;
  153. $cjReason = 45;
  154. } else {
  155. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo(
  156. $GiftsID, $userID, $payAmt
  157. );
  158. }
  159. [$Score] = $service->addRecord(
  160. $userID, $payAmt, $favorable_price, $order_no, $GiftsID,
  161. $Recharge, $czReason, $give, $cjReason, $AdId, $eventType,
  162. $body['payment_fee'] ?? 0
  163. );
  164. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_no]);
  165. } catch (\Exception $exception) {
  166. Util::WriteLog("BotImPay_error", $exception->getMessage());
  167. }
  168. $order_up = DB::connection('write')->table('agent.dbo.order')
  169. ->where('order_sn', $order_no)
  170. ->update($body);
  171. if (!$order_up) {
  172. Util::WriteLog('BotImPay', '订单更新失败: ' . $order_no);
  173. return 'ok';
  174. }
  175. Util::WriteLog("BotImPay", 'success: ' . $order_no);
  176. return 'ok';
  177. }
  178. }