【问题标题】:Laravel 5 catching PayPal PHP API 400 errors on localhost:8000Laravel 5 在 localhost:8000 上捕获 PayPal PHP API 400 错误
【发布时间】:2017-10-21 21:32:22
【问题描述】:

在我的 Laravel 5.2 安装中使用 PayPal API,特别是这个包:https://github.com/anouarabdsslm/laravel-paypalpayment

这个包很好用!我正在完美地付款!当细节不正确时,我正在努力捕捉和重定向,例如银行卡详细信息由用户输入。 Laravel 应用程序只是抛出 400 错误。

我想要做的是捕获错误并重定向回来并通知用户。

下面的代码是我提出请求的地方:

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

当支付成功时,我会得到一个不错的返回对象,我可以引用它并相应地采取行动,当出现 400 错误之类的问题时,它会完全终止应用程序,并且不会捕获并将错误重定向回用户。

错误代码信息是:

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

有人在使用 PayPal PHP API 时遇到过类似问题吗?

我知道当应用程序不处于开发模式时,我可以有专门的错误页面来捕获某些错误代码。但我真的很想捕捉错误并重定向回带有用户通知的表单。

提前感谢任何可以提供帮助的向导。

【问题讨论】:

    标签: php laravel laravel-5 paypal paypal-sandbox


    【解决方案1】:

    对的人,

    我在这里发布了答案:Laravel 5 catching 400 response from PayPal API,但我想确保任何点击此帖子的人都知道我是如何解决问题的!

    Laravel 的默认 Exception 方法似乎干扰了 PayPal API PayPalConnectionException。所以我修改了代码以仅捕获一般的Exception 错误,因为它包含所有必需的错误对象。 Exception 之前的 \ 很关键!因为它需要正确的命名空间(无论如何,在我的情况下,您的应用程序可能会有所不同)。

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

    @rchatburn 发布的这个link 非常有用,一旦我正确命名了所有内容,该应用程序似乎总是能抓住\Exception 而不是\PayPalConnectionException

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

    //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 感觉更简洁,我正在测试付款是否成功。

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

    尼克。

    【讨论】:

      【解决方案2】:

      如果您想要 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);
            }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2017-10-30
        • 2022-01-21
        • 2016-07-20
        • 1970-01-01
        • 1970-01-01
        • 2013-11-12
        • 2016-03-31
        • 2015-03-10
        • 2015-08-06
        相关资源
        最近更新 更多