PayPal API 格式错误的请求

Posted

技术标签:

【中文标题】PayPal API 格式错误的请求【英文标题】:PayPal API Malformed Request 【发布时间】:2015-07-11 15:48:07 【问题描述】:

我已编辑我的代码以正确计算,但是当我尝试创建付款时收到格式错误的请求错误。如果有人可以提供一些关于我可能出错的地方的见解,我将不胜感激。因为我无法弄清楚为什么该请求被认为是无效的。

我的代码:

public function store()
    
        //require('vendor/autoload.php');
        $client_ID = "edited_out"; 
        $client_Secret = "edited_out";
        $apiContext = new \PayPal\Rest\ApiContext(
          new \PayPal\Auth\OAuthTokenCredential(
            $client_ID,
            $client_Secret
          )
        );
        // ### Payer
        // A resource representing a Payer that funds a payment
        // For paypal account payments, set payment method
        // to 'paypal'.
        $payer = new Payer();
        $payer->setPaymentMethod("paypal");

        // ### Itemized information
        // (Optional) Lets you specify item wise
        // information
        $total_shipping=0; 
        foreach(Cart::content() as $row)
        
            foreach(Inventory::where('id', '=', $row->id)->get() as $product)
                   

                foreach(Inventory::where( 'id', '=', $row->id)->get() as $product) 
                 
                    $total_shipping += $product->shipping; 
                    //echo $total_shipping;  echo $total_shipping;
                
            
        

        $i = 0;
        $myshipping = 0;
        foreach(Cart::content() as $row)
        
            foreach(Inventory::where('id', '=', $row->id)->get() as $product)
               
                $myshipping += $product->shipping;

                $items[$i] = new Item();
                $items[$i]->setQuantity($row->qty)
                    ->setName($row->name)
                    ->setPrice($row->price)
                    ->setCurrency('USD'); 

                $details[$i] = new Details();
                $details[$i]->setSubtotal($row->price * $row->qty)
                    ->setTax(($row->price * $row->qty) * 0.07)
                    ->setShipping($product->shipping);

                $i++;
            
        

        $itemList = new ItemList();
        $itemList->setItems($items);

        // ### Additional payment details
        // Use this optional field to set additional
        // payment information such as tax, shipping
        // charges etc.

        // ### Amount
        // Lets you specify a payment amount.
        // You can also specify additional details
        // such as shipping, tax.

        $amount = new Amount();
        $amount->setCurrency("USD")
            ->setDetails($details)
            ->setTotal(Cart::total() + (Cart::total() * 0.07) + $total_shipping);

        // ### Transaction
        // A transaction defines the contract of a
        // payment - what is the payment for and who
        // is fulfilling it. 
        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($itemList)
            ->setDescription("Payment description")
            ->setInvoiceNumber(uniqid());

        // ### Redirect urls
        // Set the urls that the buyer must be redirected to after 
        // payment approval/ cancellation.
        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl("edited_out")
            ->setCancelUrl("edited_out");

        // ### Payment
        // A Payment Resource; create one using
        // the above types and intent set to 'sale'
        $payment = new Payment();
        $payment->setIntent("sale")
            ->setPayer($payer)
            ->setRedirectUrls($redirectUrls)
            ->setTransactions(array($transaction));


        // For Sample Purposes Only.
        $request = clone $payment;

        // ### Create Payment
        // Create a payment by calling the 'create' method
        // passing it a valid apiContext.
        // (See bootstrap.php for more on `ApiContext`)
        // The return object contains the state and the
        // url to which the buyer must be redirected to
        // for payment approval
        try 
            $payment->create($apiContext);
         catch (Exception $ex) 
            //ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
            //var_dump($items);
            echo "<pre>" . $payment;
            echo $ex->getData();
            echo $product->shipping;
            echo $amount->getTotal;
            exit(1);
        

        $approvalUrl = $payment->getApprovalLink();
        return Redirect::to($approvalUrl);
    

响应/请求:

    
    "intent": "sale",
    "payer": 
        "payment_method": "paypal"
    ,
    "redirect_urls": 
        "return_url": "http://aidevserver.co/projects/casels/public/orders",
        "cancel_url": "http://aidevserver.co/projects/casels/public/checkout/cancel"
    ,
    "transactions": [
        
            "amount": 
                "currency": "USD",
                "details": [
                    
                        "subtotal": "9.98",
                        "tax": "0.70",
                        "shipping": "7.60"
                    
                ],
                "total": "18.28"
            ,
            "item_list": 
                "items": [
                    
                        "quantity": 2,
                        "name": "This item is a test for shipping calculation",
                        "price": "4.99",
                        "currency": "USD"
                    
                ]
            ,
            "description": "Payment description",
            "invoice_number": "5543c8511631b"
        
    ]
"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"3cb07a91c7227"

【问题讨论】:

【参考方案1】:

根据文档,我认为您的数量详细信息属性不应该是一个数组

【讨论】:

我在循环中需要它,并且在循环中我有一个整数增加,所以它添加了购物车中每个产品的详细信息。如果它不能是数组,我将如何解决这个问题?

以上是关于PayPal API 格式错误的请求的主要内容,如果未能解决你的问题,请参考以下文章

PayPal REST API 格式错误的 JSON 错误

发送 Paypal 支付调用返回 400 格式错误的请求错误

如何使用 c# 为 paypal CreateInvoice api 创建 SOAP XML 请求消息

我想以 JSON 格式调用 PayPal MassPay API

Python 提出请求。 Spotify API 提出请求格式错误的 Json

Paypal:PHP SDK 错误传入 JSON 请求未映射到 API 请求