Q:

PHP Transaction confirm with javascript

I want to confirm transaction from other third-party, e.g I pay goods of $50 to juma Joe after click pay button and pay page is still loading and tell me to wait almost 5 seconds to check if the transaction is processing succeed, and if the transaction succeeds, it redirects to a success page.
So i need javascript and html confirmation code call from php to check if transaction is success then to show redirect seconds.
Thank you
Bellow is code for for validate transaction
$result = array();
$url = 'https://api.xxxx.co/transaction/verify/reference';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer xxxxxxx']
);
$request = curl_exec($ch);
if(curl_error($ch)){
echo 'error:' . curl_error($ch);
}
curl_close($ch);

if ($request) {
 $result = json_decode($request, true);
}

if (array_key_exists('data', $result) && array_key_exists('status',             $result['data']) && ($result['data']['status'] === 'success')) {
echo "Transaction was successful";
//Compete transaction
}else{
// Not complete
echo "Transaction not complete";
}

A:

So. You can using ajax to request and response in you code.
Example:
PHP Code: validate.php
//....above you code here
// my code config response
$res = [
         'mess' => null,
         'status' => 404
       ];

//...you code validation result
if (array_key_exists('data', $result) && array_key_exists('status', $result['data']) && ($result['data']['status'] === 'success')) {
    //Compete transaction
    $res['mess'] = "Transaction was successful";
    $res['status'] = 200;
}else{
    // Not complete
    $res['mess'] = "Transaction not complete";
    $res['status'] = 404;
}
// my code response mess
echo json_encode($res);

So, below my front-end code, using jQuery Ajax to send POST request.
Document you can check here
$(document).ready(function() {  
  function validate(){
     $.ajax({
        url: 'validate.php',
        type: 'POST',
        dataType: 'json',
        success: function(res) {
            if (res.status === 200) {
                console.log("you code here for success");
            } else {
                console.log("you code here for fail");
            }
        },
        error: function(err) {
            console.log("Request is error with: " + err);
        }
     });
  }
});

HTML for submit button. Call to javascript
<button id="submit" type="submit" onclick="validate();">Send Request</button>

