Tuesday, May 23, 2017

Laravel 5 catching PayPal PHP API 400 errors on localhost:8000

Using the PayPal API with my Laravel 5.2 install, specifically this package: https://github.com/anouarabdsslm/laravel-paypalpayment

The package works great! and I am taking payments perfectly! I am struggling to catch and redirect when incorrect details e.g. bank card details are entered by a user. The Laravel application just throws a 400 error.

What I am wanting to do is catch the errors and redirect back and notify the user.

The code below is where I make a request:

try {
    // ### Create Payment
    // Create a payment by posting to the APIService
    // using a valid ApiContext
    // The return object contains the status;

    $payment->create($this->_apiContext);

} catch (\PPConnectionException $ex) {
    return Redirect::back()->withErrors([$ex->getMessage() . PHP_EOL]);
}

dd($payment);

When a successful payment is made I get a nice return object that I can reference and action accordingly, when there is an issue like a 400 error it kills the application completely and DOES NOT catch and redirect the errors back to the user.

The error code messages are:

PayPalConnectionException in PayPalHttpConnection.php
Got Http response code 400 when accessing 
https://api.sandbox.paypal.com/v1/payments/payment.

Has anyone faced similar issues with the PayPal PHP API?

I know when the application isn't in dev mode I can have error pages specifically to catch certain error codes. But I really want to catch errors and redirect back to the form with notifications for the user.

Thanks in advance to any wizard who can help.



via Nick Howarth

Advertisement