|
@@ -0,0 +1,60 @@
|
|
|
|
|
+<?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);
|
|
|
|
|
+ if ($changed) {
|
|
|
|
|
+ self::sendInsufficientBalanceNotice($agent, $orderId, $message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $changed;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static function isInsufficientBalance($message)
|
|
|
|
|
+ {
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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)
|
|
|
|
|
+ {
|
|
|
|
|
+ $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 for this channel has been set to 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());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|