【问题标题】:PayPal Payment not working any more - Laravel PaypalPayPal 付款不再有效 - Laravel Paypal
【发布时间】:2017-04-02 17:19:53
【问题描述】:

我最近在我的网站上添加了 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 请求"​​,"information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"5c8663496f505"}PayPal\Exception \PayPalConnectionException:访问https://api.sandbox.paypal.com/v1/payments/payment时得到Http响应码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,这只是一个产品计数器,我还添加了一些尝试捕捉周围执行(如您在代码中所见)

【问题讨论】:

  • 对照 API 文档检查您发送的 JSON 格式,您应该能够看到导致问题的部分。
  • @Dontfeedthecode 看起来一切正常,我没有发现任何问题,这就是问题所在......我提供了完整的代码,但我没有看到任何可能破坏功能的东西
  • @Dontfeedthecode 好吧,变得更有趣了,我将编辑我的问题以获取新信息
  • 没人知道吗?

标签: php laravel paypal laravel-5.3


【解决方案1】:

你好,亲爱的,很抱歉细节。我认为发送到贝宝的总金额计算不正确。 当您在详细信息中设置小计时,您能否确认那里的小计集是使用此公式计算的
小计 = sum((item1 价格 * 数量) + ... (item2 价格 * 数量)) 然后检查设置为金额的总金额是使用此公式计算的 总计 = 小计 + 运费 只需确保正确计算发送到贝宝的金额

【讨论】:

    猜你喜欢
    • 2016-11-23
    • 2021-10-15
    • 2013-06-05
    • 2014-05-16
    • 2017-02-03
    • 2015-12-14
    • 1970-01-01
    • 2011-04-21
    • 2013-05-26
    相关资源
    最近更新 更多