2016-11-18 77 views
-1

我最近在我的网站上添加了PayPal付款(一般使用Laravel)。因此我使用的是this pluginPayPal付款无法正常工作 - Laravel Paypal

我使用的完整源代码:

public function getCheckout(Request $request) 
    { 
     $userInfo = \App\User::where('id', '=', \Auth::user()->id) 
      ->first(); 

     if ($userInfo->street == "") { 
      $request->session()->flash('alert-info', 'Vor der ersten Bestellung müssen Sie erst Ihre Daten vervollständigen'); 
      return redirect('/editData'); 
     } 

     $cart = Cart::where('user_id', \Auth::user()->id)->first(); 
     $items = $cart->cartItems; 
     $total = 0; 
     foreach ($items as $item) { 
      $total += $item->product->price; 
     } 

     $itemList = PayPal::itemList(); 

     foreach ($items as $item) { 
      $product = Product::where('id', '=', $item->product->id)->first(); 
      $itemName = $product->name; 
      $itemPrice = $product->price; 
      $payPalItem = PayPal::item(); 
      $payPalItem->setName($itemName) 
       ->setDescription($itemName) 
       ->setCurrency('EUR') 
       ->setQuantity(1) 
       ->setPrice($itemPrice); 
      $itemList->addItem($payPalItem); 
     } 

     $payer = PayPal::Payer(); 
     $payer->setPaymentMethod('paypal'); 

     $details = PayPal::details(); 
     $details->setShipping("2.50") 
      ->setSubtotal($total); 

     $amount = PayPal:: Amount(); 
     $amount->setCurrency('EUR'); 
     $amount->setTotal(($total + 2.5)); 
     $amount->setDetails($details); 

     $transaction = PayPal::Transaction(); 
     $transaction->setAmount($amount); 
     $transaction->setItemList($itemList); 
     $transaction->setDescription('Ihr Warenkorb'); 

     $redirectUrls = PayPal:: RedirectUrls(); 
     $redirectUrls->setReturnUrl(action('[email protected]')); 
     $redirectUrls->setCancelUrl(action('[email protected]')); 

     $payment = PayPal::Payment(); 
     $payment->setIntent('sale'); 
     $payment->setPayer($payer); 
     $payment->setRedirectUrls($redirectUrls); 
     $payment->setTransactions(array($transaction)); 

     try { 
      $response = $payment->create($this->_apiContext); 
     } catch (PayPalConnectionException $ex) { 
      echo $ex->getCode(); // Prints the Error Code 
      echo $ex->getData(); // Prints the detailed error message 
      die($ex); 
     } 

     $redirectUrl = $response->links[1]->href; 

     return Redirect::to($redirectUrl); 
    } 

    public function getDone(Request $request) 
    { 
     $cart = Cart::where('user_id', \Auth::user()->id)->first(); 
     $items = $cart->cartItems; 
     $total = 0; 
     foreach ($items as $item) { 
      $total += $item->product->price; 
     } 

     $order = new Order(); 
     $order->total_paid = $total + 2.5; 
     $order->user_id = \Auth::user()->id; 
     $order->save(); 

     foreach ($items as $item) { 
      $orderItem = new OrderItem(); 
      $orderItem->order_id = $order->id; 
      $orderItem->product_id = $item->product->id; 
      $orderItem->save(); 
      $product = $item->product; 
      if ($product->stockCount != "") { 
       $product->stockCount--; 
      } 
      $product->save(); 
      CartItem::destroy($item->id); 
     } 

     $id = $request->get('paymentId'); 
     $token = $request->get('token'); 
     $payer_id = $request->get('PayerID'); 

     $payment = PayPal::getById($id, $this->_apiContext); 

     $paymentExecution = PayPal::PaymentExecution(); 

     $paymentExecution->setPayerId($payer_id); 

     try { 
      $payment->create($this->_apiContext); 
     } catch (PayPalConnectionException $ex) { 
      echo $ex->getCode(); // Prints the Error Code 
      echo $ex->getData(); // Prints the detailed error message 
      die($ex); 
     } 
     try { 

      $payment->execute($paymentExecution, $this->_apiContext); 
     } catch (PayPalConnectionException $ex) { 
      echo $ex->getCode(); 
      echo $ex->getData(); 
      die($ex); 
     } 

     // Clear the shopping cart, write to database, send notifications, etc. 

     // Thank the user for the purchase 

     $userInfo = \App\User::where('id', '=', \Auth::user()->id) 
      ->first(); 

     $userMail = $userInfo->email; 

     $orderItems = OrderItem::where('order_id', '=', $order->id)->get(); 

     Mail::to($userMail)->send(new orderFinished($order)); 

     $sellerArray = []; 

     foreach ($orderItems as $orderItem) { 
      $product = $orderItem->product; 
      $seller = User::where('id', '=', $product->user_id)->first(); 
      $buyer = User::where('id', '=', $order->user_id)->first(); 
      if (!in_array($seller, $sellerArray)) { 
       Mail::to($seller->email)->send(new newOrderMail($order, $seller, $buyer)); 
       array_push($sellerArray, $seller); 
      } 
     } 

     return view('checkout.done'); 
    } 

这工作之前,我敢肯定我没有改变什么,但它突然不工作了...... 我收到这个错误:

400 { “名称”: “MALFORMED_REQUEST”, “消息”: “传入JSON请求不映射到API请求”, “information_link”: “https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST”, “debug_id”: “5c8663496f505”} PayPal \ Exception \ PayPalConnectionException:访问时得到Http响应代码400 https://api.sandbox.paypal.com/v1/payments/payment

有没有人看到一个问题,为什么会发生这种情况?我真的不知道....

编辑:恢复到最新版本(这里我确信它的工作),我得到另一个错误,他说:

PayPalConnectionException in PayPalHttpConnection.php line 174: 
Got Http response code 500 when accessing https://api.sandbox.paypal.com/v1/payments/payment/PAY-1DS60820MW1392725LAYAB6A/execute. 
in PayPalHttpConnection.php line 174 
at PayPalHttpConnection->execute('{"payer_id":"J922HVAQ2RHAW"}') in PayPalRestCall.php line 74 
at PayPalRestCall->execute(array('PayPal\Handler\RestHandler'), '/v1/payments/payment/PAY-1DS60820MW1392725LAYAB6A/execute', 'POST', '{"payer_id":"J922HVAQ2RHAW"}', array()) in PayPalResourceModel.php line 102 
at PayPalResourceModel::executeCall('/v1/payments/payment/PAY-1DS60820MW1392725LAYAB6A/execute', 'POST', '{"payer_id":"J922HVAQ2RHAW"}', null, object(ApiContext), object(PayPalRestCall)) in Payment.php line 650 
at Payment->execute(object(PaymentExecution), object(ApiContext)) in PayPalController.php line 142 
at PayPalController->getDone(object(Request)) 
at call_user_func_array(array(object(PayPalController), 'getDone'), array(object(Request))) in Controller.php line 55 
at Controller->callAction('getDone', array(object(Request))) in ControllerDispatcher.php line 44 
at ControllerDispatcher->dispatch(object(Route), object(PayPalController), 'getDone') in Route.php line 189 

不过:这并未每次都会发生,有些命令有效,有些不会,或者用其他语言说:有时它有效,有时不会......我想知道的不仅仅是,为什么会出现错误,还有为什么行为会改变,因为正如我所说的,我所做的唯一的事情就是将文件恢复到最新版本(使用GIT)。从本地版本到最新版本的唯一变化是一些格式化的东西((之前的空格等等)以及stockCount,这只是一个产品的计数器,我还在执行过程中添加了一些尝试(例如你可以在代码中看到)

+0

根据API文档检查您发送的JSON格式,您应该能够看到什么部分是造成这个问题。 –

+0

@Dontfeedthecode看起来都很好,我看不出有什么问题,这就是问题所在......我提供的完整代码,但我没有看到任何可能破坏功能 – nameless

+0

@Dontfeedthecode好,会变得更加有趣,我将编辑我的问题以获取新信息 – nameless

回答

0

嗨,亲爱的抱歉的细节。我认为发送到PayPal的总金额计算不正确。 当您在小细节内设置小计时,您可以确认使用此公式计算的小计集合
小计=总计((item1价格*数量)+ ...(item2价格*数量)) 然后检查总集使用此公式计算金额 total =小计+运费 只需确保发送到PayPal的金额得到了正确计算