Laravel PHP Rest API 400错误

时间:2016-03-31 19:31:09

标签: php laravel paypal paypal-rest-sdk

每次遇到400错误,我都会看到以下屏幕400 Error

我试图在不成功的事务视图中很好地显示错误。但是,我永远无法通过此屏幕来查看我是否正确解析$ ex。

我的代码如下:

try {
        $payment->create($apiContext);

        $customer = Customer::where('fname', $request->bill_fname)
                        ->where('lname', $request->bill_lname)->first();
        if (!$customer) {
           $customer = Customer::create(['fname' => $request->bill_fname, 'lname' => $request->bill_lname, 'email' => $request->email, 'phone' => $request->phonenum]);
        }

        if(empty($request->cc_token)) {
        $savedcard = CreditCardTable::where('externalCardId', ($request->card_type . substr($request->card_number, -5)))->first();
        if(!$savedcard) {
        $card->create($apiContext);
        $creditcard = CreditCardTable::create(['customer_id' => $customer->id, 'card_id' => $card->id, 'externalCardId' => ($request->card_type . substr($request->card_number, -5))]);
            }
        }

        //dd($payment);
        //store all info in respective tables
        $terminal = Terminal::create(['customer_id' => $customer->id, 'description' => $request->description, 'invoice_id' => $payment->transactions[0]->invoice_number, 'total' => ((($request->amount + $request->shipping_amount) * ($request->tax_rate / 100)) + ($request->amount + $request->shipping_amount)), 'create_time' => $payment->create_time, 'paypal_id' => $payment->id, 'paypal_parent_payment' => $payment->transactions[0]->related_resources[0]->sale->id]);
        $billaddress = BillAddress::where('addrline1', $request->addrline1)
                        ->where('addrline2', $request->addrline2)
                        ->where('zipcode', $request->bill_zip)->first();
        if(!$billaddress) {
            $billaddress = BillAddress::create(['customer_id' => $customer->id, 'addrline1' => $request->addrline1, 'addrline2' => $request->addrline2, 'city' => $request->bill_city, 'state' => $request->bill_state, 'zipcode' => $request->bill_zip]);
        }
        if($request->isSame == 'No') {
        $shipaddress = ShipAddress::where('addrline1', $request->ship_addr_line1)
                        ->where('addrline2', $request->ship_addr_line2)
                        ->where('zipcode', $request->ship_zip)->first();
        if(!$shipaddress) {
            $shipaddress = ShipAddress::create(['customer_id' => $customer->id, 'addrline1' => $request->ship_addr_line1, 'addrline2' => $request->ship_addr_line2, 'city' => $request->ship_bill_city, 'state' => $request->ship_state, 'zipcode' => $request->ship_zip]);
            }
        }
        else {
            //ship address is bill address
            $shipaddress = BillAddress::where('addrline1', $request->addrline1)
                        ->where('addrline2', $request->addrline2)
                        ->where('zipcode', $request->bill_zip)->first();
            if(!$shipaddress) {
            $shipaddress = BillAddress::create(['customer_id' => $customer->id, 'addrline1' => $request->addrline1, 'addrline2' => $request->addrline2, 'city' => $request->bill_city, 'state' => $request->bill_state, 'zipcode' => $request->bill_zip]);
            }
        }
           // dd($payment);
            return view('pages.terminal.success')->with('payment', $payment)->with('fname', $request->bill_fname)->with('lname', $request->bill_lname);
            }
    catch (PayPal\Exception\PayPalConnectionException $ex) {
                return view('errors.unsuccessful')->with('ex', $ex);
                }
    catch (Exception $ex) {
        return view('errors.unsuccessful')->with('ex', $ex);
    }

Errors.Unsuccesful

    @extends('app')

@section('content')
    <div class="alert alert-danger">
      <h4>Oh No!</h4>
      <p>Transaction was not processed processed</p>
    </div>

    <div class="panel panel-danger">
  <div class="panel-heading">
    <h3 class="panel-title">Error</h3>
  </div>
  <div class="panel-body">
    {{ echo $ex->getCode(); // Prints the Error Code
    echo $ex->getData(); // Prints the detailed error message  }}
  </div>
</div>

@endsection

@include('errors.list')

如何很好地显示这些错误,以便告知用户他们做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试使用前面的反斜杠\

从全局命名空间中解析自定义异常
...
catch (\PayPal\Exception\PayPalConnectionException $ex) {
...