I'm trying to make a script to process a payment after a while. The idea was to keep the date of the moment in which the request for payment was sent and the date on which that payment should be processed. When that date is met, the system should automatically show me a message indicating that the payment was processed. I was thinking about doing this with AJAX, but the way I do it is not working for me, because after that time the page does not show any message. Maybe I'm doing everything wrong. I show you the code that I have:
this is the code of the principal.php file:
<!DOCTYPE html>
<html>
<head>
<title>PICK PAYMENT</title>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#show').hide();
$("#send").click( function() {
var cont1 = $("#cont").val();
var empre = $("#company").val();
var cant = $("#cant").val();
$.ajax({
data: {cont: cont1, empre: empre, pay: cant},
url: 'ajaxdate.php',
dataType: 'html',
type: 'post',
beforeSend: function () {
$('#show').show();
},
success: function (response) {
console.log(response);
$('#show').hide();
$("#result").html(response);
$("#result").css("background-color", "green");
//window.location.href = "comparar.php";
$(location).attr("href", "displayResults.php");
}
});
});
});
</script>
</head>
<body>
<h2>PAYMENTS SYSTEM!!!</h2>
<form method="post" action="" id="pagoform">
<input type="text" name="company" placeholder="company name" id="company" />
<input type="hidden" name="cont" id="cont" value="1" />
<input type="number" name="cant" placeholder="quantity" id="cant" />
<input type="submit" name="send" value="send" id="send" />
</form>
<div id="result"></div>
<br>
<br>
<span id="show">Please wait...</span>
</body>
</html>
This is the code of the ajaxdate.php file:
$cont = $_POST['cont'];
$empre = $_POST['empre'];
$pay = $_POST['pay'];
$response = '';
$Datetime = 'America/Caracas';
date_default_timezone_set($Datetime);
$datePay = date('Y-m-d H:i:s');
$dateProcessed = date_create($datePay);
date_add($dateProcessed, date_interval_create_from_date_string('10 minutes'));
$process = date_format($dateProcessed, 'Y-m-d H:i:s');
$nextPayment = date_create($datePay);
date_add($nextPayment, date_interval_create_from_date_string('7 days'));
$nextP = date_format($nextPayment, 'Y-m-d H:i:s');
$mysqli = new mysqli("localhost", "root", "", "DateDB");
if ($mysqli->connect_errno) {
echo "Falló la conexión con MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
else {
$sql = "INSERT INTO DateTable (company, dateP, datePro, nextPay, payment) VALUES ('$empre', '$datePay', '$process', '$nextP', $pay)";
$reg = $mysqli->query($sql);
if (!$reg == null) {
$response= "PAYMENT RECEIVED!";
}
else {
$response = "PAYMENT WAN'T RECEIVED";
}
}
echo $response;
Here is the code of the file displayResults.php - Here I thought about making a query through ajax, get the current time and make a comparison that tells me if the time has come to process the payment. But I want the system to do it automatically, without having to press a button or something, just when the time is right, or at least make the comparison every time a user accesses the system (it would not be the solution that I'm exactly looking but could serve me), and show me a "processed payment" message,
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>RESULTS</title>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#mesage").hide();
$('#show').hide();
var idempre = $("#empreid").val();
$.ajax({
url: 'compare.php',
data: {id: idempre},
dataType: 'html',
type: 'post',
beforeSend: function () {
$('#show').show();
},
success: function (response) {
console.log(response);
$('#show').hide();
if (response == 1) {
$("#mesage").show();
} else {
$("#wait").show();
}
}
});
});
</script>
</head>
<body>
<h2>PAYMENTS!!!</h2>
<br>
<input type="hidden" name="idempre" id="empreid" value="<?php echo $_SESSION['idcompany']; ?>" />
<div id="mesage">YOUR PAYMENT WAS SUCCESSFULLY PROCESSED!</div>
<br>
<span id="show">Please wait...</span>
<br>
<div id="wait">YOUR PAYMENT IS IN TAIL TO PROCESS.</div>
</body>
</html>
File code compare.php
$id = $_POST['id'];
$Datetime = 'America/Caracas';
date_default_timezone_set($Datetime);
$date1 = date('Y-m-d H:i:s');
$mysqli = new mysqli("localhost", "root", "", "DateDB");
if ($mysqli->connect_errno) {
echo "Fail conexión with MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
$sql = "SELECT datePro FROM DateTable WHERE id=$id";
$reg = $mysqli->query($sql);
$reg->data_seek(0);
$row = $reg->fetch_assoc();
$hour = $row['datePro'];
$strStart = $date1;
$strEnd = $hour;
$result = null;
if ($strStart == $strEnd) {
$result = 1;
} else {
$result = 0;
}
echo $result;
What I try to do is basically collect the payment in the first file, then process it and save it in the second file. Then, redirect to the results.php file, there you are supposed to consult the database and compare if the time and date to process the payment has arrived and, if so, show on the screen that the payment has been processed .
I'm doing this in test mode to learn how to do these kinds of things with the dates, since later I plan to do the same, but instead of minutes, the payment processing date will be after several days (it's say, for example: a payment was sent one day Thursday, and processed 7 later).
The problem is presented from the third file (displayResults.php), the first two files work well. But from the third only shows me the message please wait ... and when it comes time to process the payment does not do anything else. Maybe there is a better way to do this than with good results. The idea is to be as automatic as possible ... Any suggestions? I had in mind to make a kind of clock or refresh the page every so often with javascript, but it has not worked for me, besides that it would also be a problem when I want to use days and not hours-minutes.