简体   繁体   中英

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 eg 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.

Right guys,

I posted the answer here: Laravel 5 catching 400 response from PayPal API but I want to make sure anyone hitting this thread knows how I solved the issue!

It would appear that Laravel's default Exception method was interfering with the PayPal API PayPalConnectionException . So I modified the code to catch general Exception errors only as it contained all required error objects. The \\ before Exception was critical! as it needs the correct namespace (in my case anyway, your application may be different).

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 (\Exception $ex) {
    return Redirect::back()->withErrors([$ex->getData()])->withInput(Input::all());
}

This link that @rchatburn posted was highly useful, the application always seemed to catch at the point \\Exception and NOT \\PayPalConnectionException once I had everything namespaced correctly.

In my investigations I came across app/Exceptions/Handler.php . Here you can extend the render method to grab a PayPalConnectionException and handle the errors uniquely to that specific exception . See code:

//Be sure to include the exception you want at the top of the file
use PayPal\Exception\PayPalConnectionException;//pull in paypal error exception to work with

public function render($request, Exception $e)
{
    //check the specific exception
    if ($e instanceof PayPalConnectionException) {
        //return with errors and with at the form data
        return Redirect::back()->withErrors($e->getData())->withInput(Input::all());
    }

    return parent::render($request, $e);
} 

Either work great, but for me it felt neater to just change the catch method to a lookout for a general Exception , where I am testing if a payment was successful.

Hope this helps anyone facing similar issues :D!!!

Nick.

If you want The JSON details of your API call you can ADD the following code.

  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 (\Exception $ex) {
          return dd($ex->getData());
          exit(1);
      }

Hope it helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM