<?php
// Set default timezone
date_default_timezone_set('Asia/Kolkata');

// Database configuration file include karein
require_once('../database/dbconfig.php');

// Database connection establish karein
try {
$pdo = new PDO("mysql:host=$db_host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}

// Make API call to get_upi_id.php
$api_url = "https://smartupi.in.net/merchantapi/bajaj/get_upi_id.php"; // Adjust the URL as needed
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$response_data = json_decode($response, true);

// Current timestamp
$current_time = date('Y-m-d H:i:s'); // Current timestamp in 'Y-m-d H:i:s' format

// Check if vpa is present in the API response
if (isset($response_data['vpa'])) {
// Update only status and last_sync for all merchants
$update_sql = "UPDATE add_merchant SET status = 'Active', last_sync = :last_sync";
$update_stmt = $pdo->prepare($update_sql);
$update_stmt->bindParam(":last_sync", $current_time);
$update_stmt->execute();

echo "Cron job executed Successfully updated to Active, Last Sync updated to $current_time for all merchants.\n";
} else {
// Update only status and last_sync for all merchants
$update_sql = "UPDATE add_merchant SET status = 'Inactive', last_sync = :last_sync";
$update_stmt = $pdo->prepare($update_sql);
$update_stmt->bindParam(":last_sync", $current_time);
$update_stmt->execute();

echo "Token Expired  updated to Inactive, Last Sync updated to $current_time for all merchants.\n";
}
?>