PayPal支付不再工作 - Laravel Paypal

时间:2016-11-18 18:52:21

标签: php laravel paypal laravel-5.3

我最近将PayPal付款添加到了我的网站(一般使用Laravel)。因此,我使用this plugin

我使用的完整代码是:

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('PayPalController@getDone'));
        $redirectUrls->setCancelUrl(action('PayPalController@getCancel'));

        $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 {" name":" MALFORMED_REQUEST"," message":"传入的JSON请求未映射到API请求",& #34; information_link":" https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST"," debug_id":" 5c8663496f505"} PayPal \ Exception \ PayPalConnectionException:获得Http响应代码访问https://api.sandbox.paypal.com/v1/payments/payment

时为400

有人发现为什么会发生这种情况会有问题吗?我真的不知道......

编辑:在恢复到最新版本后(我确信它有效),我收到另一个错误,说:

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,它只是产品的计数器,我也添加了一些尝试捕获执行(正如你在代码中看到的)

1 个答案:

答案 0 :(得分:0)

嗨亲爱的抱歉细节。我认为发送给paypal的总金额没有正确计算。 当你在细节中设置小计时,你可以确认那里的小计集是用这个公式计算的 小计=总和((item1价格*数量)+ ...(item2价格*数量)) 然后检查使用此公式计算的总金额 总计=小计+运费 只需确保发送到paypal的金额已正确计算