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

hjortur17's avatar

What is the best practice in this scenario?

Hi, hopefully can someone assist me with this. I'm wondering is there a way to keep a rental in some kind of memory while I redirect the customer to payment website and when he comes back I confirm the booking? Because right now, I'm creating the rental before I send them to the payment site and when they have paid I updated the booking and set paid to true. And sometime the customer closes the browser without paying leaving me with few rentals unpaid. I would like to delete those if the customer hasn't paid in 10 minutes.

What is the best tool for this? Should I use some kind of CRON job or what?

0 likes
5 replies
LaryAI's avatar
Level 58

One solution to this problem is to use a job queue system like Laravel's built-in Queue system. When a rental is created, you can dispatch a job to the queue that will handle the payment process. This way, the rental will remain in the queue until the payment is completed. If the customer closes the browser without paying, the job will eventually fail and you can handle the failure by deleting the rental.

To implement this solution, you can create a new job that handles the payment process. Here's an example:

<?php

namespace App\Jobs;

use App\Rental;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessRentalPayment implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $rental;

    /**
     * Create a new job instance.
     *
     * @param Rental $rental
     */
    public function __construct(Rental $rental)
    {
        $this->rental = $rental;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Handle payment process here
        // Update rental status to paid
        $this->rental->update(['paid' => true]);
    }

    /**
     * Handle a job failure.
     *
     * @return void
     */
    public function failed()
    {
        // Handle job failure here
        // Delete rental if payment not completed within 10 minutes
        $this->rental->delete();
    }
}

Then, in your controller where you create the rental, you can dispatch the job to the queue like this:

use App\Jobs\ProcessRentalPayment;

public function createRental(Request $request)
{
    // Create rental
    $rental = Rental::create($request->all());

    // Dispatch job to queue
    ProcessRentalPayment::dispatch($rental)->delay(now()->addMinutes(10));
    
    // Redirect to payment website
    return redirect('payment-website');
}

In this example, we're delaying the job by 10 minutes to give the customer time to complete the payment. If the payment is not completed within 10 minutes, the job will fail and the rental will be deleted.

To run the queue, you can use the php artisan queue:work command. You can also set up a CRON job to run the queue automatically.

1 like
Snapey's avatar

be careful with this. If you cancel the booking, it could be awkward if in the meantime they have sent you a support email.

The other problem is that they might have paid, but you did not get the callback and now you have their money but have cancelled the booking.

Unless you need to release the booking so that others can book it, I would cleanup after 24 hours.

1 like
hjortur17's avatar

@Snapey - That's a good point. Would you do like mention above? One other thing, I'm redirecting to a third-party payment site through Javascript. If I choose to use Queues, how would I dispatch it and redirect? First post to my own api before sending them over?

martinbean's avatar

@hjortur17 This is just e-commerce at the end of the day. Treat room booking as “stock”. Reserve the stock (room) for a certain length of time. If a payment has not been made, then release the stock and cancel the order.

1 like
Jsanwo64's avatar

Yes, there are a few ways you could approach this problem. One solution is to use a session management system to store the rental details temporarily while the user is redirected to the payment website. When the user returns, you can check the session to see if the rental is still available and process the payment. If the payment is successful, you can then create the rental and set it to paid.

Another approach would be to use a database to store the rental details and a unique booking ID. When the user is redirected to the payment website, you could pass the booking ID as a parameter in the URL. When the user returns, you can check the payment status using the booking ID, and if the payment is successful, you can update the rental in the database and set it to paid.

To clean up unpaid rentals, you can set up a cron job that runs every 10 minutes (Amount of minutes you want) and checks for rentals that are still unpaid after 10 minutes (Amount of minutes you want). You can then delete those rentals from the database to keep your system clean.

Please or to participate in this conversation.