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);
}
}
}