且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5从PayPal API捕获了400条响应

更新时间:2022-05-29 06:03:29

对,

Laravel的默认Exception方法似乎正在干扰PayPal API PayPalConnectionException.因此,我修改了代码以仅捕获常规Exception错误,因为它包含所有必需的错误对象. Exception之前的\至关重要!因为它需要正确的名称空间(就我而言,您的应用程序可能有所不同).

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());
}

链接非常有用,一旦我正确地命名了所有名称空间,应用程序似乎总是会遇到\Exception而不是\PayPalConnectionException的问题.

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.

在调查中,我遇到了app/Exceptions/Handler.php.在这里,您可以扩展render方法以获取PayPalConnectionException,并针对该特定异常唯一地处理错误.查看代码:

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);
} 

这两种方法都很好,但是对我来说,将catch方法更改为监视一般Exception的想法让我感到很整洁,我在这里测试付款是否成功.

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.

希望这可以帮助面临类似问题的任何人:D !!!

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

尼克.