From 7c52826feecc410f328fe22bc363974b54be5a81 Mon Sep 17 00:00:00 2001 From: Pinga <121483313+getpinga@users.noreply.github.com> Date: Fri, 7 Feb 2025 09:20:54 +0200 Subject: [PATCH] Added registry admin balance notification script --- automation/registrar.php | 21 ++---- tests/balance-notify.php | 137 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 tests/balance-notify.php diff --git a/automation/registrar.php b/automation/registrar.php index ccb2146..81a18a8 100644 --- a/automation/registrar.php +++ b/automation/registrar.php @@ -43,17 +43,8 @@ if ($row) { $registryName = 'Example Registry LLC'; } -$stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name"); -$stmt->execute(['name' => 'currency']); -$row = $stmt->fetch(); -if ($row) { - $currency = $row['value']; -} else { - $currency = 'USD'; -} - // Define the query -$sql = "SELECT id, clid, name, accountBalance, creditThreshold, creditLimit, email FROM registrar"; +$sql = "SELECT id, clid, name, accountBalance, creditThreshold, creditLimit, email, currency FROM registrar"; try { $stmt = $pdo->query($sql); @@ -61,13 +52,13 @@ try { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { if ($row['accountBalance'] < $row['creditThreshold']) { // Case 1: accountBalance is less than creditThreshold - sendEmail($row, 'low_balance', $log, $supportEmail, $supportPhoneNumber, $registryName, $currency); + sendEmail($row, 'low_balance', $log, $supportEmail, $supportPhoneNumber, $registryName); } elseif ($row['accountBalance'] == 0) { // Case 2: accountBalance is 0 - sendEmail($row, 'zero_balance', $log, $supportEmail, $supportPhoneNumber, $registryName, $currency); + sendEmail($row, 'zero_balance', $log, $supportEmail, $supportPhoneNumber, $registryName); } elseif (($row['accountBalance'] + $row['creditLimit']) < 0) { // Case 3: accountBalance + creditLimit is less than 0 - sendEmail($row, 'over_limit', $log, $supportEmail, $supportPhoneNumber, $registryName, $currency); + sendEmail($row, 'over_limit', $log, $supportEmail, $supportPhoneNumber, $registryName); } } @@ -79,13 +70,13 @@ try { } // Function to send email -function sendEmail($data, $case, $log, $supportEmail, $supportPhoneNumber, $registryName, $currency) { +function sendEmail($data, $case, $log, $supportEmail, $supportPhoneNumber, $registryName) { $message = "Dear ".$data['name'].",\n\n"; switch ($case) { case 'low_balance': $subject = "Low balance alert for registrar: " . $data['clid']; - $message .= "We are writing to inform you that your account with us currently has a low balance. As of now, your account balance is $currency {$data['accountBalance']}, which is below the minimum credit threshold of $currency {$data['creditThreshold']}.\n\n"; + $message .= "We are writing to inform you that your account with us currently has a low balance. As of now, your account balance is {$data['currency']} {$data['accountBalance']}, which is below the minimum credit threshold of {$data['currency']} {$data['creditThreshold']}.\n\n"; break; case 'zero_balance': $subject = "Zero balance alert for registrar: " . $data['clid']; diff --git a/tests/balance-notify.php b/tests/balance-notify.php new file mode 100644 index 0000000..fa3fb53 --- /dev/null +++ b/tests/balance-notify.php @@ -0,0 +1,137 @@ +#!/usr/bin/env php + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]); + + // Get registry admin email + $stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name LIMIT 1"); + $stmt->execute(['name' => 'email']); + $adminEmail = $stmt->fetchColumn(); + + // Fetch registrar balances + $sql = "SELECT id, clid, name, accountBalance, creditThreshold, creditLimit, email, currency FROM registrar"; + $stmt2 = $pdo->query($sql); + + $notifications = []; + + while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) { + $message = null; + $balance = $row['accountBalance']; + $threshold = $row['creditThreshold']; + $creditLimit = $row['creditLimit']; + $totalCredit = $balance + $creditLimit; + + if ($balance < $threshold) { + $message = sprintf( + "Registrar %s (ID: %s) has a low balance.\nCurrent Balance: %.2f %s\nCredit Threshold: %.2f %s\n", + $row['name'], $row['clid'], $balance, $row['currency'], $threshold, $row['currency'] + ); + } elseif ($balance == 0) { + $message = sprintf( + "Registrar %s (ID: %s) has a zero balance.\nCurrent Balance: 0.00 %s\n", + $row['name'], $row['clid'], $row['currency'] + ); + } elseif ($totalCredit < 0) { + $message = sprintf( + "Registrar %s (ID: %s) has exceeded the credit limit.\nCurrent Balance: %.2f %s\nCredit Limit: %.2f %s\nTotal Credit: %.2f %s\n", + $row['name'], $row['clid'], $balance, $row['currency'], $creditLimit, $row['currency'], $totalCredit, $row['currency'] + ); + } + + if ($message) { + $notifications[] = $message; + } + } + + // If no notifications, exit + if (empty($notifications)) { + echo "No registrars have low balances.\n"; + exit(0); + } + + // Build the email body + $emailBody = "Low Balance Notification\n"; + $emailBody .= "The following registrars have low balance issues:\n\n"; + $emailBody .= implode("\n", $notifications); + + // Prepare email data + $emailData = [ + 'type' => 'sendmail', + 'toEmail' => $adminEmail, + 'subject' => 'Low Balance Notification - Registrars Requiring Attention', + 'body' => $emailBody, + ]; + + $jsonPayload = json_encode($emailData); + if ($jsonPayload === false) { + throw new Exception("JSON encoding error: " . json_last_error_msg()); + } + + // Send the Email via cURL + $mailServiceUrl = 'http://127.0.0.1:8250'; + $curlOptions = [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $jsonPayload, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + 'Content-Length: ' . strlen($jsonPayload), + ], + ]; + + $ch = curl_init($mailServiceUrl); + if ($ch === false) { + throw new Exception("Failed to initialize cURL."); + } + + curl_setopt_array($ch, $curlOptions); + $response = curl_exec($ch); + + if ($response === false) { + throw new Exception("cURL error (" . curl_errno($ch) . "): " . curl_error($ch)); + } + + $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($httpStatus >= 400) { + throw new Exception("HTTP error code {$httpStatus} received. Response: {$response}"); + } + + echo "Low balance notification sent successfully. Response: " . $response . PHP_EOL; + +} catch (Exception $ex) { + error_log("Error in low balance notification script: " . $ex->getMessage()); + echo "An error occurred: " . $ex->getMessage() . PHP_EOL; + exit(1); +}