BotImPayLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // 构建支付请求参数(BotImPay 多一个 ip 字段)
  55. $params = [
  56. 'mer_no' => $service->merNo,
  57. 'order_no' => $order_sn,
  58. 'amount' => (string)$pay_amount,
  59. 'name' => $userName ?: 'user',
  60. 'email' => $userEmail ?: ($userId . '@unknown.com'),
  61. 'phone' => $userPhone ?: '0000000000',
  62. 'currency' => $config['currency'] ?? 'USDT',
  63. 'pay_code' => $payCode,
  64. 'notify_url' => $config['notify_url'] ?? '',
  65. 'ip' => $buyIP,
  66. ];
  67. // RSA签名
  68. $signedParams = $service->sign($params);
  69. // 生成用户请求信息日志
  70. $request_extra = \GuzzleHttp\json_encode($signedParams);
  71. CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);
  72. $url = $service->apiUrl . '/open/api/order/in';
  73. $result = $service->curlPost($url, $signedParams);
  74. $rresult = compact('result', 'signedParams', 'url');
  75. Util::WriteLog('BotImPay', 'BotImPay支付请求' . $url . " | " . $request_extra);
  76. Util::WriteLog('BotImPay', 'BotImPay支付结果' . json_encode($rresult));
  77. try {
  78. $data = \GuzzleHttp\json_decode($result, true);
  79. } catch (\Exception $e) {
  80. Util::WriteLog("BotImPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  81. $this->error = 'Payment processing error';
  82. return false;
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 代收异步回调处理
  88. *
  89. * BotImPay 回调: { mer_no, order_no, pay_type_code, order_amount, order_reality_amount, status, sign }
  90. */
  91. public function notify($post)
  92. {
  93. $order_no = $post['order_no'] ?? '';
  94. $order = DB::connection('write')->table('agent.dbo.order')
  95. ->where('order_sn', $order_no)
  96. ->first();
  97. if (!$order) {
  98. Util::WriteLog('BotImPay', '订单不存在: ' . $order_no);
  99. return 'ok';
  100. }
  101. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  102. return 'ok';
  103. }
  104. $status = $post['status'] ?? '';
  105. if ($status !== 'success') {
  106. Util::WriteLog('BotImPay', "支付未完成订单: {$order_no}, status: {$status}");
  107. return 'ok';
  108. }
  109. // 支付成功
  110. $GiftsID = $order->GiftsID ?: '';
  111. $userID = $order->user_id ?: '';
  112. $AdId = $order->AdId ?: '';
  113. $eventType = $order->eventType ?: '';
  114. $orderAmount = (float)($post['order_reality_amount'] ?? $post['order_amount'] ?? 0);
  115. $payAmt = $orderAmount;
  116. $body = [
  117. 'payment_sn' => $post['sys_no'] ?? '',
  118. 'updated_at' => date('Y-m-d H:i:s'),
  119. 'pay_status' => 1,
  120. 'pay_at' => date('Y-m-d H:i:s'),
  121. 'finished_at'=> date('Y-m-d H:i:s'),
  122. 'amount' => (int) round($payAmt * NumConfig::NUM_VALUE),
  123. ];
  124. // 根据支付方式计算代收手续费: 费率% * 金额 + 固定$
  125. $config = (new PayConfig())->getConfig('BotImPay');
  126. $payRates = $config['pay_rate'] ?? null;
  127. if (is_array($payRates)) {
  128. $payMethod = $order->order_title ?? 1024;
  129. $payRate = $payRates[$payMethod] ?? ($payRates[1024] ?? [3.5, 0]);
  130. $feePercent = $payRate[0] ?? 3.5;
  131. $feeFixed = $payRate[1] ?? 0;
  132. $body['payment_fee'] = intval(($body['amount'] * $feePercent) / 100)
  133. + (int)($feeFixed * NumConfig::NUM_VALUE);
  134. }
  135. try {
  136. $service = new OrderServices();
  137. if (intval($order->amount) != $body['amount']) {
  138. $body['GiftsID'] = 0;
  139. $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
  140. $Recharge = $payAmt;
  141. $give = 0;
  142. $favorable_price = $Recharge + $give;
  143. $czReason = 1;
  144. $cjReason = 45;
  145. } else {
  146. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo(
  147. $GiftsID, $userID, $payAmt
  148. );
  149. }
  150. [$Score] = $service->addRecord(
  151. $userID, $payAmt, $favorable_price, $order_no, $GiftsID,
  152. $Recharge, $czReason, $give, $cjReason, $AdId, $eventType,
  153. $body['payment_fee'] ?? 0
  154. );
  155. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_no]);
  156. } catch (\Exception $exception) {
  157. Util::WriteLog("BotImPay_error", $exception->getMessage());
  158. }
  159. $order_up = DB::connection('write')->table('agent.dbo.order')
  160. ->where('order_sn', $order_no)
  161. ->update($body);
  162. if (!$order_up) {
  163. Util::WriteLog('BotImPay', '订单更新失败: ' . $order_no);
  164. return 'ok';
  165. }
  166. Util::WriteLog("BotImPay", 'success: ' . $order_no);
  167. return 'ok';
  168. }
  169. }