WithdrawalPayoutMonitor.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Services;
  3. use App\Models\WithdrawalAgentRatioConfig;
  4. use App\Notification\TelegramBot;
  5. use App\Util;
  6. use Illuminate\Support\Facades\DB;
  7. class WithdrawalPayoutMonitor
  8. {
  9. public static function handleFailedCallback($agent, $callback, $orderId = '')
  10. {
  11. $message = self::callbackText($callback);
  12. if (!self::isInsufficientBalance($message)) {
  13. return false;
  14. }
  15. $changed = WithdrawalAgentRatioConfig::disableAgent($agent);
  16. if ($changed) {
  17. self::sendInsufficientBalanceNotice($agent, $orderId, $message);
  18. }
  19. return $changed;
  20. }
  21. private static function isInsufficientBalance($message)
  22. {
  23. return preg_match('/insufficient\s*(balance|funds)|balance\s*(is\s*)?(not\s*)?enough|not\s*enough\s*balance|\x{4F59}\x{989D}\x{4E0D}\x{8DB3}/iu', $message) === 1;
  24. }
  25. private static function callbackText($callback)
  26. {
  27. if (is_array($callback) || is_object($callback)) {
  28. return json_encode($callback, JSON_UNESCAPED_UNICODE);
  29. }
  30. return strval($callback);
  31. }
  32. private static function sendInsufficientBalanceNotice($agent, $orderId, $message)
  33. {
  34. $agentName = DB::table('agent.dbo.admin_configs')
  35. ->where('type', 'cash')
  36. ->where('config_value', strval($agent))
  37. ->value('name');
  38. $content = "Insufficient balance\n"
  39. . "Payout channel: " . ($agentName ?: $agent) . "({$agent})\n"
  40. . "Order ID: " . ($orderId ?: '-') . "\n"
  41. . "Auto payout ratio for this channel has been set to 0\n"
  42. . "Callback: " . mb_substr($message, 0, 800);
  43. try {
  44. TelegramBot::getDefault()->sendProgramNotify('Insufficient balance', $content);
  45. } catch (\Exception $exception) {
  46. Util::WriteLog('WithdrawalPayoutMonitor', 'Telegram notify failed: ' . $exception->getMessage());
  47. }
  48. }
  49. }