I am implementing payments for my website using the API of an external service (ie. the service of the payment provider).
Let's say the user clicks 'BUY', and then we go to my controller which says something along the lines of:
public function buyFunction() {
$result = $this->ExternalService->pay();
if ($result->success == true) {
return 'We are happy';
}
}
I have also created the aforementioned externalService
which has the pay()
method:
class ExternalService {
public function pay() {
response = //Do stuff with Guzzle to call the API to make the payment
return response;
}
}
Now, sometimes things go wrong. Let's say the API returns an error - which means that it throws a GuzzleException - how do I handle that?
Ideally, if there is an error, I would like to log it and redirect the user to a page and tell him that something went wrong.
What I've tried
I have tried using a try/catch statement within the pay() function and using
abort(500)
but this doesn't allow me to redirect to the page I want to.I have tried using a try/catch statement within the pay() function and using
return redirect('/mypage')
but this just returns a Redirect object to the controller, which then fails when it tries to callresult->success
I have tried using number 2 but also adding a try/catch block to the controller method, but nothing changed.
In the end, I have found two solutions. In both, I use a try/catch block inside the pay() method. Then I either return 0;
and check in the controller if (result == 0)
or I use abort( redirect('/mypage') );
inside the try/catch block of the pay() method.
What is the right way to handle this? How to use the try/catch blocks?