2017-05-30 14 views
2

から400応答を引く私はここにある非常に具体的な質問質問:誰もが、私はそれのPayPal APIから400エラーをキャッチについてよりオープン質問したい助けることができなかったようLaravel 5 catching PayPal PHP API 400 errors on localhost:8000Laravel 5ペイパルAPI

を。

私はPayPalへのリクエストを作成しています。リクエストが成功した場合はすべて正常に動作し、素敵なレスポンスオブジェクトが得られます。

たとえば、不正なカードの詳細が入力された場合、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); 

//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のデバッグモードでのエラーです:

enter image description here

私は私ができるので、私は素敵なオブジェクトを取得する必要がありますPayPalのAPIサンドボックスのログで見てみるとそれに応じた行動。

{ 
    "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" 
    } 
} 

もしあなたが私のヒーローになることができれば、どんなララベルの魔法使いも助けてくれるでしょう。

ニック。

+0

これはあなたを助けますか? https://github.com/paypal/PayPal-PHP-SDK/wiki/exception-'PayPal%5CException%5CPayPalConnectionException-with-message-'Got-Http-response-code-400-when-accessing – rchatburn

+0

ありがとうございます返信@rchatburn!私はそのリンクを見て、私は多くの助けになると確信していますが、私の場合には再びキャッチすることができません:(私のアプリケーションは、この呼び出しを行うようにステータスが400の場合にフロップするようです: '$ payment-> create - > _ apiContext); 'PayPalが何らかのエラーを投げた場合、その行を越えるものはありません。 –

答えて

1

右の男、

LaravelのデフォルトException方法はペイパル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とNOT \PayPalConnectionExceptionでキャッチするように見えました。

私の調査では、私はapp/Exceptions/Handler.phpに出くわしました。ここでレンダリングメソッドを拡張して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); 
} 

素晴らしい仕事が、私にとっては、支払いが成功したかどうかだけで、私はテストしてい一般Exception、のための眺望にキャッチする方法を変更するにすっきりと感じた:コードを参照してください。

似たような問題に直面している人に役立ちます。D !!!

ニック。

+0

catch(Exception $ ex)の代わりにcatch(Exception $ ex)を使用してくれました。これは時間の節約になりました。ありがとうございました。 –

+0

@DharaParmarは助けになりました! –