Laravel 5在localhost:8000上捕获PayPal PHP API 400错误
在我的Laravel 5.2安装中使用PayPal API,特别是以下软件包: https://github.com /anouarabdsslm/laravel-paypalpayment
Using the PayPal API with my Laravel 5.2 install, specifically this package: https://github.com/anouarabdsslm/laravel-paypalpayment
该程序包很棒!而且我正在完美地付款!我正在努力捕捉并重定向错误的详细信息,例如银行卡详细信息由用户输入. Laravel应用程序仅引发400错误.
The package works great! and I am taking payments perfectly! I am struggling to catch and redirect when incorrect details e.g. 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);
成功付款后,我会得到一个不错的退货对象,可以参考并采取相应的措施,当出现类似400错误的问题时,它会完全杀死应用程序,并且不会捕获错误并将其重定向回用户.
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.
错误代码消息是:
PayPalConnectionException in PayPalHttpConnection.php
Got Http response code 400 when accessing
https://api.sandbox.paypal.com/v1/payments/payment.
有人在使用PayPal PHP API时遇到过类似的问题吗?
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.
对,
我在此处发布了答案: Laravel 5捕获了来自PayPal的400条响应API ,但是我想确保碰到这个线程的任何人都知道我是如何解决该问题的!
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的默认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!!!
尼克.