Hi Devs,
I am trying to redirect to a route after an ajax call but the am not getting a redirect
my controller
public function pollTransaction(Request $request)
{
$this->validate($request, [
'transaction' => 'exists:orders,id'
]);
// Find a transaction matching the given transaction id
$transaction = Order::findOrFail($request->input('transaction'));
try {
// Try to poll the transaction
// $status = $this->paynow->pollTransaction($transaction->poll_url);
$status = $this->paynow->pollTransaction($transaction->token);
if ($status->paid()) {
// Update the transaction of the transaction
$transaction->is_paid = true;
// Persist the new status
$transaction->save();
session()->flash('success', "Thank you $transaction->billing_fullname, your payment has been received successfully!");
if (auth()->check()) {
return redirect(route('my-orders.index')); /// am trying to redirect here
} else{
return redirect(route('thank.you'));
}
}
} catch (Exception $e) {
// Log out the error
logger()->error($e->getMessage() . "\t\t" . $e->getTraceAsString());
return response()->json([
'status' => 'error',
'message' => 'An error occurred while polling transaction'
]);
}
}
AJAX
(function () {
let poll = function() {
$.ajax({
"_token": "{{ csrf_token() }}",
url: '{!! route('pollTransaction') !!}',
method: "GET",
data : {
transaction : {!! $newTransaction['transaction']['id'] !!},
},
contentType: false,
cache: false,
proccesData: false,
dataType: "json",
success:function(data) {
console.log(data)
}
})
}
setInterval(function() {
poll()
}, 10000);
})();
without ajax the redirect works perfectly