mirror of
https://github.com/getnamingo/registry.git
synced 2025-07-23 19:10:30 +02:00
Added registry admin balance notification script
This commit is contained in:
parent
62b9b30930
commit
7c52826fee
2 changed files with 143 additions and 15 deletions
|
@ -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'];
|
||||
|
|
137
tests/balance-notify.php
Normal file
137
tests/balance-notify.php
Normal file
|
@ -0,0 +1,137 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Low Balance Notification Script
|
||||
*
|
||||
* This script checks all registrars for three conditions:
|
||||
* - Low balance (accountBalance < creditThreshold)
|
||||
* - Zero balance (accountBalance == 0)
|
||||
* - Over limit (accountBalance + creditLimit < 0)
|
||||
*
|
||||
* If any of these conditions are met, the registry admin is notified.
|
||||
*/
|
||||
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '1');
|
||||
|
||||
$c = require_once '/opt/registry/automation/config.php';
|
||||
|
||||
// Ensure the script is run from the command line.
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
die("This script must be run from the command line.");
|
||||
}
|
||||
|
||||
date_default_timezone_set('UTC');
|
||||
|
||||
$dsn = "mysql:host={$c['db_host']};dbname={$c['db_database']};charset=utf8mb4";
|
||||
|
||||
try {
|
||||
$pdo = new PDO($dsn, $c['db_username'], $c['db_password'], [
|
||||
PDO::ATTR_ERRMODE => 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue