Laravel 5从PayPal API捕获了400条响应 [英] Laravel 5 catching 400 response from PayPal API

查看:86
本文介绍了Laravel 5从PayPal API捕获了400条响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里提出了一个非常具体的问题: Laravel 5在localhost:8000上捕获PayPal PHP API 400错误

I asked a very specific question located here: Laravel 5 catching PayPal PHP API 400 errors on localhost:8000

由于没有人可以帮忙,我想提出一个更开放的问题,即从PayPal API捕获400错误.

As nobody could help I want to make it a more open question about catching a 400 error from the PayPal API.

我正在向PayPal发出请求,当成功发出请求后,一切都会正常进行,并且我得到一个漂亮的响应对象,可以与之配合并采取相应的行动.

I am making requests to PayPal, when a successful request is made everything works perfectly and I get a lovely response object, which I can work with and action accordingly.

例如,当输入错误的卡详细信息时,Laravel抛出400错误,但未能捕获该错误,因此我无法采取相应措施.

When for instance incorrect card details are entered Laravel throws a 400 error and fails to catch the error for me to action accordingly.

摘要:

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

//will not catch here :( throws Laravel 400 error! want to redirect with message!
} catch (\PPConnectionException $ex) {
    return Redirect::back()->withErrors([$ex->getMessage() . PHP_EOL]);
}

//if we get an approved payment ! This fires perfectly when succesful!!! woo!!
if($payment->state == 'approved') {
    //if success we hit here fine!!!
} else {
    //won't get here, dies before catch.
}

这是Laravel调试模式下的错误:

Here is the error in Laravel debug mode:

当我查看PayPal API沙箱日志时,我应该得到一个不错的对象,以便可以采取相应措施.

When I look in the PayPal API sandbox logs I should be getting a nice object so I can action accordingly.

{
    "status": 400,
    "duration_time": 60,
    "body": {
        "message": "Invalid request. See details.",
        "information_link": "https://developer.paypal.com/webapps/developer/docs/api/#VALIDATION_ERROR",
        "details": [
            {
                "field": "payer.funding_instruments[0].credit_card.number",
                "issue": "Value is invalid."
            }
        ],
        "name": "VALIDATION_ERROR",
        "debug_id": "XXXXXXXXXXXXXX"
    },
    "additional_properties": {},
    "header": {
        "Date": "Thu, 25 May 2017 14:44:43 GMT",
        "paypal-debug-id": "2f88f18d519c3",
        "APPLICATION_ID": "APP-XXXXXXXXXXXX",
        "Content-Language": "*",
        "CALLER_ACCT_NUM": "XXXXXXXXXXXXX"
    }
}

如果有任何Laravel向导可以帮助您,将成为我的英雄.

If any Laravel wizard can help you would be my hero.

尼克.

推荐答案

对,

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!!!

尼克.

这篇关于Laravel 5从PayPal API捕获了400条响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆