WithdrawalPayoutMonitor.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. self::sendInsufficientBalanceNotice($agent, $orderId, $message, $changed);
  17. return $changed;
  18. }
  19. private static function isInsufficientBalance($message)
  20. {
  21. return preg_match('/insufficien\w*|insufficent|not\s*sufficient|balance\s*(is\s*)?(not\s*)?enough|not\s*enough\s*balance|\x{4F59}\x{989D}\x{4E0D}\x{8DB3}/iu', $message) === 1;
  22. }
  23. private static function callbackText($callback)
  24. {
  25. if (is_array($callback) || is_object($callback)) {
  26. return json_encode($callback, JSON_UNESCAPED_UNICODE);
  27. }
  28. return strval($callback);
  29. }
  30. private static function sendInsufficientBalanceNotice($agent, $orderId, $message, $changed)
  31. {
  32. $agentName = DB::table('agent.dbo.admin_configs')
  33. ->where('type', 'cash')
  34. ->where('config_value', strval($agent))
  35. ->value('name');
  36. $content = "Insufficient balance\n"
  37. . "Payout channel: " . ($agentName ?: $agent) . "({$agent})\n"
  38. . "Order ID: " . ($orderId ?: '-') . "\n"
  39. . "Auto payout ratio status: " . ($changed ? 'set to 0' : 'already 0') . "\n"
  40. . "Callback: " . mb_substr($message, 0, 800);
  41. try {
  42. TelegramBot::getDefault()->sendProgramNotify('Insufficient balance', $content);
  43. } catch (\Exception $exception) {
  44. Util::WriteLog('WithdrawalPayoutMonitor', 'Telegram notify failed: ' . $exception->getMessage());
  45. }
  46. }
  47. }