<?php
//error_reporting(E_ALL);

// Errors ko display karein
//ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);


date_default_timezone_set('Asia/Kolkata');
require_once('dbconfig.php');

$user_ip = $_SERVER['REMOTE_ADDR'];




$merchant_sql = $conn->prepare("SELECT * FROM add_merchant WHERE user = ?");
$merchant_sql->execute([$_SESSION['username']]);
$merchant_data = $merchant_sql->fetch(); // Fetch single row
$token = ($merchant_data && isset($merchant_data['access_token'])) ? $merchant_data['access_token'] : null;
?>

<?php
// API URL
$apiUrl = "https://smartupi.in.net/merchantapi/bharatpe/account.php?token=$token";

// Use cURL with timeout to prevent page hanging
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // 3 second timeout
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); // 2 second connection timeout
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);

// Decode the JSON response
$data = null;
if ($response && $httpCode == 200) {
    $data = json_decode($response, true);
}

// Check if the API call was successful
if ($data && isset($data['status']) && $data['status'] === "success") {
    $current_balance = $data['data']['current_balance'];
    $current_principal = $data['data']['current_principal'];
    $today_collection = $data['data']['today_collection'];
    $today_total_withdrawal = $data['data']['today_total_withdrawal'];
    $hold_balance = $data['data']['hold_balance'];
} else {
    // Handle API error
    $current_balance = $current_principal = $today_collection = $today_total_withdrawal = $hold_balance = "N/A";
}
?>



<?php

// Profile API Call

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['access_token']) && !isset($_POST['merchant_id'])) {
// Handle Business Name Button Click
$access_token = $_POST['access_token'];
$apiUrl = "https://smartupi.in.net/merchantapi/bharatpe/profile.php?access_token=" . $access_token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
//echo $apiUrl;
if (curl_errno($ch)) {
echo json_encode(['error' => 'cURL error: ' . curl_error($ch)]);
} else {
$data = json_decode($response, true);
$merchant_id = isset($data['merchant_id']) ? $data['merchant_id'] : 'Unknown';
$business_name = isset($data['business_name']) ? $data['business_name'] : 'Unknown';
$message = isset($data['message']) ? $data['message'] : 'No message';

if ($data && $data['status_code'] == 200 && $data['status'] == 'Success') {
echo '
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
title: "'.$business_name.' ",
text: "Merchant Id '.$merchant_id.' ",
icon: "success",
showConfirmButton: true,
timer: 2000,
willClose: () => {
window.location.href = "";
}
});
</script>
';
} else {
echo '
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
title: "Opps ",
text: " No Merchant. ",
icon: "error",
showConfirmButton: true,
timer: 2000,
willClose: () => {
window.location.href = "";
}
});
</script>
';    
}
}

curl_close($ch);
exit;

// Download API Call

} elseif (isset($_POST['merchant_id']) && isset($_POST['access_token'])) {
// Handle QR Code Button Click
$merchant_id = $_POST['merchant_id'];
$access_token = $_POST['access_token'];
$apiUrl = "https://smartupi.in.net/merchantapi/bharatpe/download-qr.php?merchant_id=$merchant_id&access_token=$access_token";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
echo json_encode(['error' => 'cURL error: ' . curl_error($ch)]);
} else {
$data = json_decode($response, true);

if ($data && $data['status'] == 'success' && isset($data['qr_url'])) {
$qr_url = $data['qr_url'];
$message = $data['message'];

// Show alert, open QR code URL in a new tab, and refresh the current page
echo '
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
title: "Opening QR Code...",
text: "The QR code will open in a new tab in 2 seconds.",
icon: "info",
timer: 2000,
showConfirmButton: false,
willClose: () => {
window.open("'.$qr_url.'", "_blank"); // Open QR code in a new tab
window.location.href = ""; // Refresh the current page
}
});
</script>
';
} else {
    
echo '
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
title: "Opps ",
text: " No Merchant. ",
icon: "error",
showConfirmButton: true,
timer: 1000,
willClose: () => {
window.location.href = "";
}
});
</script>
';     
}
}

curl_close($ch);
exit;
}
}
?>