| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- namespace App\Services;
- use App\Models\WithdrawalAgentRatioConfig;
- use App\Notification\TelegramBot;
- use App\Util;
- use Illuminate\Support\Facades\DB;
- class WithdrawalPayoutMonitor
- {
- public static function handleFailedCallback($agent, $callback, $orderId = '')
- {
- $message = self::callbackText($callback);
- if (!self::isInsufficientBalance($message)) {
- return false;
- }
- $changed = WithdrawalAgentRatioConfig::disableAgent($agent);
- self::sendInsufficientBalanceNotice($agent, $orderId, $message, $changed);
- return $changed;
- }
- private static function isInsufficientBalance($message)
- {
- 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;
- }
- private static function callbackText($callback)
- {
- if (is_array($callback) || is_object($callback)) {
- return json_encode($callback, JSON_UNESCAPED_UNICODE);
- }
- return strval($callback);
- }
- private static function sendInsufficientBalanceNotice($agent, $orderId, $message, $changed)
- {
- $agentName = DB::table('agent.dbo.admin_configs')
- ->where('type', 'cash')
- ->where('config_value', strval($agent))
- ->value('name');
- $content = "Insufficient balance\n"
- . "Payout channel: " . ($agentName ?: $agent) . "({$agent})\n"
- . "Order ID: " . ($orderId ?: '-') . "\n"
- . "Auto payout ratio status: " . ($changed ? 'set to 0' : 'already 0') . "\n"
- . "Callback: " . mb_substr($message, 0, 800);
- try {
- TelegramBot::getDefault()->sendProgramNotify('Insufficient balance', $content);
- } catch (\Exception $exception) {
- Util::WriteLog('WithdrawalPayoutMonitor', 'Telegram notify failed: ' . $exception->getMessage());
- }
- }
- }
|