Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Patwan's avatar

Auto Refreshing some PHP code after 1.5 seconds

I have some PHP code whereby I post data to an API and get some response data. When a user pays the response status from the API should change. I am trying to delay the execution of PHP code and wait for 15 seconds before getting the payment status from the API which is not working as expected.. I have no idea how to do that in PHP or rather Laravel Queues since am using Laravel 5.4

~ Kindly assist?

 $data = array(
            'payment_reference' => $checkID,
            'payment_type' => $type
        );

        //Post request to an API and get the status and store in a variable
        $paySt = $this->global_Curl($data, 'api/payment/status')->data;

        sleep(15);

        //Second call to the API after sleep to check if status has changed
       $payStat = $this->global_Curl($data, 'api/payment/status')->data;

        if($payStat->status === '1'){
             return 'true';   
        }
0 likes
11 replies
lostdreamer_nl's avatar

if it is listening (ie: The Payment Gateway will call your script) then you should not do anything. When the payment gateway has a change in it's status, it should be calling your endpoint again.

Otherwise, you'd need to start a background task, which will do some HTTP request to the payment gateway to poll for the status every X seconds (not adviced).

Could you give a bit more background info? (Which payment gateway, is it an endpoint on your end that's being called by the gateway, or is the user being returned to your website and you need to POLL for the status ?)

If you have to GET the status from the Payment Gateway, please also post that code here.

Patwan's avatar

@lostdreamer_nl Apologies for the delay,, this was due to internet connectivity issues.. Please check the updated question...

lostdreamer_nl's avatar

Right now, your code will ask the payment status...... wait 15 seconds ...... ask the payment status again, and then it will check if the last check returned $payStat->status === '1' ( So the status should be a string value 1, not an integer)

I'm still not sure what you are asking... do you want it to repeat it over and over until the status == '1'? Because if so: What would happen if someone does not pay? Your code hangs indefinately.....

Normally, a payment provider will either:

  • send a user back to your page with some ID in the URL, you then use that ID to check the status of that payment once because the customer already either paid or cancelled
  • send a user back to your page, but before that point, their system will notify yours (via a webhook) about the change in status of the payment.

Are you sure you want to be polling for the status ?

Patwan's avatar

@lostdreamer_nl Yah, am waiting for the status to determine my cause of action,

I have read sample from the docs of the API and it has 3 examples with different status to be returned depending on the type of status as shown in the code below. In my case am waiting for about 15 seconds (estimated time required by the user to use his/her phone to pay for the transcation). Then I initiate a second call to the API after 15 seconds to check if the status has changed to integer 1, if true, redirect to another page (which in this case uses AJAX to redirect to another page in the U.I showing the user h/she has successfully paid).

//When not paid
{
    status: 0,
    message: 'Not Paid',
    amount: 20
}

//When paid
{
    status: 1,
    message: 'Paid',
    amount: 20
}

//When cancelled
{
    status: 2,
    message: 'Cancelled',
    amount: 20
}
Patwan's avatar

@lostdreamer_nl As you previously suggested,I would like to initiate the above call to the API after a period of 15 seconds for 3 consecutive times, if the user has not paid by that time, the atatus ahould change to integer 0 whereby I will initiate AJAX call to a U.I telling the user that h/she has not paid..

Patwan's avatar

@anonymouse703 Thanks for help,,, I am working on an existing code base that was build using Laravel version 5.4,, Am not sure if cron was implemented by them,, concerning Js,, would you assist by kindly showing an example about it?

~ Regards

lostdreamer_nl's avatar
Level 53

I'd still advice against it (as a payment provider should be sending you the status on change, you should not be polling it)

But to answer the question, as the script will be hanging for 45 seconds, and most servers are setup to be able to run a php script for 30-60 seconds by default:

        ini_set('max_execution_time ', 70);
        set_time_limit(70);
        $data = array(
            'payment_reference' => $checkID,
            'payment_type' => $type
        );
        for($try=1; $try<=3; $try++) {
            sleep(15);
            $payStat = $this->global_Curl($data, 'api/payment/status')->data;
            if ($payStat->status === '1') {
                return 'true';
            }
        }
        return "false";

Now it will try for max 3x, if it's 1, it will stop trying, after 3x still no status = 1, it will return false.

Patwan's avatar

@lostdreamer_nl Thanks alot,, just one last question,, am a bit confused by === versus == which is best to use since I want to check if the payment status is equal to integer 1

//Should I use
if ($payStat->status === '1') {
                return 'true';
}

//Or this one
if ($payStat->status ==  1 ) {
                return 'true';
}

Please or to participate in this conversation.