Ngozistephen's avatar

Queuing Verification Process

pls help me look at this code, i want to queue this verify process that when a user makes a transaction, that it returns a responses immedately and the verifing process like getting of order and sending mail to the user is done in the background while the responses is sent to the user immedately. Pls how can i achieve this. the laravel app is already in production and it uses database as it connection

<?php
// require_once __DIR__ . '/vendor/autoload.php';

namespace App\Http\Controllers;
use App\Models\Reservations;
use App\Models\Ticket;
use App\Models\Event;
use App\Models\Article;
use App\Models\Category;
use App\Models\GeneralSettings;
use App\Jobs\VerifyOrderJob;
use App\Models\Merch;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Mail\ReservationMail as ReservationMail;

use App\Mail\StudentMail as StudentMail;
use App\Mail\GeneralMail as GeneralMail;
use App\Mail\BroadcastMail as BroadcastMail;
use App\Models\Student;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

use Cloudinary\Configuration\Configuration;
use Cloudinary\Api\Upload\UploadApi;

class ReservationsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    
    // START ORDER
    
  
    public function verify_order(Request $request){
        $validator = Validator::make($request->all(), [
            'reference' => 'required|string',
        ]);

        if (!$validator) {
            return response(['errors' => $validator->errors()->all()], 422);
        }  

        $response = gashsgghsdhgasdgasdghsdhgshgshsgh
        
        if($response['status'] == "error") return response(['errors' => 'Transaction is not valid...'.$response['message']], 500);
        
        if($response['data'] == null || $response['data']['status'] != 'successful') return response(['errors' => 'Transaction is not valid...'], 500);
        
        $data = $response['data'];
        
        // if($data['meta']==null) return response(['errors' => 'Transaction is not valid...'], 500);
        
        // get the order_id from the consumer_mac prop
        $order_id = $data['meta']['consumer_mac'];
        
        // make sure the order exists
        $order =  Order::where('id', $order_id)->first();
        
        // make sure the prices match
        $price_match = $order->total_amount == $data['amount'];
        
        if($price_match && $data['status'] == 'successful'){
            $order->status = 'fulfilled';
            $order->save();
            
            // notify the buyer of the order
            $user = Reservations::find($order->user_id);
            $mailData = [
                'name' => $user->name,
                'subject' => "Order Received",
                'image'=>'',
                'msg' => "Hi ".$user->name.", we've received your order and will get back to you shortly",
            ];
             Mail::to($user->email)->send(new BroadcastMail($mailData));
         
            
            // notify the platform owner of the order
            $admin_email = "#";
            $mailData2 = [
                'name' => "#",
                'image'=>'',
                'subject' => "New Order Placed",
                'msg' => "Hi admin, the customer, ".$user->name.", has placed a new order at Unboxed website. Their order ID is ".$order->id.". Please do well to attend to this order as soon as possible!",
            ];
             Mail::to($admin_email)->send(new BroadcastMail($mailData2));

            return response()->json(['success' => true]); 
        }else{
            return response(['errors' => 'Transaction is not valid'], 500);
        }
    }

}

0 likes
1 reply
aleahy's avatar

I assume the code you have after the response is what you want to queue?

You would need to have a system to keep track of your queued jobs, which could be a redis database or simply a table in your db.

You also would need to have some queue workers running in the background which would be checking for your queued jobs so they could run them.

Finally you need to create a class (called a job in laravel) which is the code that would be executed when the job is picked up by the workers. It will need to implement the ShouldQueue interface so it is sent to the queue and not executed immediately before the response is returned.

The job can then be sent to the queue by calling MyJob::dispatch($data); This would go before you return your response.

This then creates an entry on the queue database that there is a job to execute. When one of your workers is not busy, it will check the database to see if there is a job, pick it up and execute the code. Eg: sending an email.

How you set these up is all here: https://laravel.com/docs/10.x/queues

I recommend watching some Laracasts videos to help you get going: https://laracasts.com/series/queue-it-up https://laracasts.com/series/laravel-queue-mastery

Please or to participate in this conversation.