首页 > 解决方案 > Laravel PayPal 错误:购物车项目总金额与订单金额不匹配

问题描述

我正在为我的购物车使用srmklive/laravel-paypal包裹。laravel我想将运费添加到小计中。但是当我尝试setExpressCheckout使用结帐数据执行贝宝时,我收到了paypal api名为“购物车商品总额与订单金额不匹配”的错误。

这是我的PaypalController

    public function getExpressCheckout($id,$data,$total){
            $orderData = json_decode($data,true);
            $checkoutData = [
                'items' => $orderData,
                'return_url' => route('paypal.success', $id),
                'cancel_url' => route('paypal.cancel'),
                'invoice_id' => uniqid(),
                'invoice_description' => " Order Description ",    
            ];

            $sub_total = 0;
            foreach ($orderData as $item) {
                $sub_total += $item['price'] * $item['qty'];
            }
            $checkoutData['shipping'] = 5; //example
            $checkoutData['sub_total'] = $sub_total;
            $checkoutData['total'] = $sub_total+ $checkoutData['shipping'];
            //$checkoutData['shipping_discount'] = round((10 / 100) * $sub_total, 2);
            

            $provider = new ExpressCheckout();

            $response = $provider->setExpressCheckout($checkoutData);
            dd($response, $orderData);
        // return redirect($response['paypal_link']);

            return response()->json(['message' => 'PayPal Link', 'link' => $response['paypal_link']], 200);

        }

OrderItems 数组

array:3 [
        0 => array:3 [
            "name" => "sssss"
            "price" => 1250
            "qty" => "1"
        ]
        1 => array:3 [
            "name" => "Upuli Bhagyadf  cgfd d"
            "price" => 1062.5
            "qty" => "1"
        ]
        2 => array:3 [
            "name" => "sssss  dfd"
            "price" => 1100
            "qty" => "1"
        ]
    ]

我得到以下错误

array:10 [
            "TIMESTAMP" => "2020-10-05T09:30:49Z"
            "CORRELATIONID" => "1c41cbb7e7447"
            "ACK" => "Failure"
            "VERSION" => "123"
            "BUILD" => "54950761"
            "L_ERRORCODE0" => "10413"
            "L_SHORTMESSAGE0" => "Transaction refused because of an invalid argument. See additional error messages for details."
            "L_LONGMESSAGE0" => "The totals of the cart item amounts do not match order amounts."
            "L_SEVERITYCODE0" => "Error"
            "paypal_link" => null
        ]

$checkoutData['shipping'] = 0;not exist没有错误,它工作正常。那么我如何添加额外的运费到sub_total

标签: phplaravelpaypalpaypal-sandbox

解决方案


推荐阅读