简体   繁体   English

Laravel 5在localhost:8000上捕获PayPal PHP API 400错误

[英]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 在我的Laravel 5.2安装中使用PayPal API,特别是以下软件包: 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. Laravel应用程序仅引发400错误。

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. 成功付款后,我会得到一个不错的退货对象,可以参考并采取相应的措施,当出现类似400错误的问题时,它会完全杀死应用程序,并且不会捕获错误并将其重定向回用户。

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? 有没有人遇到过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! 我在这里发布了答案: Laravel 5从PayPal API捕获了400条响应,但我想确保任何人点击该线程都知道我如何解决该问题!

It would appear that Laravel's default Exception method was interfering with the PayPal API PayPalConnectionException . 看来Laravel的默认Exception方法正在干扰PayPal API PayPalConnectionException So I modified the code to catch general Exception errors only as it contained all required error objects. 因此,我修改了代码以仅捕获常规的Exception错误,因为它包含所有必需的错误对象。 The \\ before Exception was critical! Exception之前的\\非常重要! 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. @rchatburn发布的此链接非常有用,一旦正确设置了所有命名空间,应用程序似乎总会\\PayPalConnectionException \\Exception而不是\\PayPalConnectionException

In my investigations I came across app/Exceptions/Handler.php . 在调查中,我遇到了app/Exceptions/Handler.php Here you can extend the render method to grab a PayPalConnectionException and handle the errors uniquely to that specific exception . 在这里,您可以扩展render方法以获取PayPalConnectionException并针对该特定异常唯一地处理错误。 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. 两种方法都很好,但是对我来说,将catch方法更改为监视一般Exception感到很整洁,在该Exception中我正在测试付款是否成功。

Hope this helps anyone facing similar issues :D!!! 希望这可以帮助任何面临类似问题的人:D !!!

Nick. 缺口。

If you want The JSON details of your API call you can ADD the following code. 如果需要API调用的JSON详细信息,则可以添加以下代码。

  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 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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