当前位置: 首页 > 知识库问答 >
问题:

PayPal:MALFORMED_REQUEST没有其他活动

林弘壮
2023-03-14
    public function processPayment($data)
{
    // Create Payer object
    $payer = new Payer();
    // Payment method is via PayPal. Take the customer to PayPal for processing.
    $payer->setPaymentMethod("paypal");
    // Create billingAddress as Address object and fill with customer's billing address.
    $billingAddress = new Address();
    $billingAddress->setLine1($data['payment_address_1'])
        ->setLine2($data['payment_address_2'])
        ->setCity($data['payment_city'])
        ->setState(/* TWO-LETTER STATE POSTAL ABBREV */)
        ->setPostalCode($data['payment_postcode'])
        ->setCountryCode(/* COUNTRY CODE */);
    // Create PayerInfo object, populate with customer's billing
    // info (name, billingAddress, phone, email)
    $payerInfo = new PayerInfo();
    $payerInfo->setFirstName($data['payment_firstname'])
        ->setLastName($data['payment_lastname'])
        ->setBillingAddress($billingAddress)
        //->setPhone($data['telephone'])
        ->setEmail($data['email']);
    // Assign payerInfo to payer.
    $payer->setPayerInfo($payerInfo);

    /**
     * List of items sold and their details
     * Add shipping address
     */
    $itemList = new ItemList();
    foreach ($data['products'] as $product)
    {
        if ($product['product_sku']) {
            $item = new Item();
            $item->setName($product['product_name'])
                ->setSku($product['product_sku'])
                ->setQuantity($product['quantity'])
                ->setPrice(number_format($product['price'], 2 , "." , "," ))
                ->setTax(number_format($product['tax'] , 2 , "." , "," ))
                ->setCurrency($data['currency_code']);
            $itemList->addItem($item);
        }
    }
    $shippingAddress = new ShippingAddress();
    $shippingAddress->setRecipientName($data['shipping_firstname'].' '.$data['shipping_lastname'])
        ->setLine1($data['shipping_address_1'])
        ->setLine2($data['shipping_address_2'])
        ->setCity($data['shipping_city'])
        ->setState(/* TWO-LETTER STATE POSTAL ABBREV */)
        ->setPostalCode($data['shipping_postcode'])
        ->setCountryCode(/* COUNTRY CODE */);
    $itemList->setShippingAddress($shippingAddress);

    $details = new Details();
    $details->setShipping(number_format($totals['shipping'] , 2 , "." , "," ))
        ->setTax(number_format($totals['tax'] , 2 , "." , "," ))
        ->setSubtotal(number_format($totals['subTotal'] , 2 , "." , "," ));

    $amount = new Amount();
    $amount->setCurrency($data['currency_code'])
        ->setTotal(number_format($data['total'] , 2 , "." , "," ))
        ->setDetails($details);

    $transaction = new Transaction();
    $transaction->setAmount($amount)
        ->setItemList($itemList)
        ->setInvoiceNumber($data['invoice_number'])
        ->setNotifyUrl(/* NOTIFY URL */);

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl(/* RETURN URL */)
        ->setCancelUrl(/* CANCEL URL */);

    $payment = new Payment();
    $payment->setIntent("sale")
        ->setPayer($payer)
        ->setRedirectUrls($redirectUrls)
        ->addTransaction($transaction)
        ->setPayee($this->payee); // payee created and populated in _constructor

    echo '<h1>Redirecting. . . .</h1>';

    try {
        $payment->create($this->apiContext); // apiContext created and populated in _constructor
    } catch (Exception $ex) {
        echo $this->PayPalError($ex); // Print detailed error messages
    }
    echo "<pre>$payment</pre>";
    return;
}

MALFORMED_REQUEST - Incoming JSON request does not map to API request

{
    "intent": "sale",
    "payer": {
        "payment_method": "paypal",
        "payer_info": {
            "first_name": "Sandbox",
            "last_name": "Buyer",
            "billing_address": {
                "line1": "BILLING ADDRESS LINE 1",
                "line2": "",
                "city": "CITY",
                "state": "ST",
                "postal_code": "ZIP",
                "country_code": "US"
            },
            "email": "SANDBOX BUYER EMAIL"
        }
    },
    "redirect_urls": {
        "return_url": "RETURN URL",
        "cancel_url": "CANCEL URL"
    },
    "transactions": [
        {
            "amount": {
                "currency": "USD",
                "total": "156.00",
                "details": {
                    "shipping": "23.00",
                    "tax": "0.00",
                    "subtotal": "133.00"
                }
            },
            "item_list": {
                "items": [
                    {
                        "name": "PRODUCT NAME",
                        "sku": "PRODUCT SKU",
                        "quantity": 1,
                        "price": "133.00",
                        "tax": "0.00",
                        "currency": "USD"
                    }
                ],
                "shipping_address": {
                    "recipient_name": "Sandbox Buyer",
                    "line1": "SHIPPING ADDRESS LINE 1",
                    "line2": "",
                    "city": "SHIPPING CITY",
                    "state": "ST",
                    "postal_code": "ZIP",
                    "country_code": "US"
                }
            },
            "invoice_number": 25,
            "notify_url": "NOTIFY URL"
        }
    ],
    "payee": {
        "email": "SANDBOX MERCHANT EMAIL",
        "merchant_id": "SANDBOX MERCHANT ID"
    }
}

共有1个答案

陆晓博
2023-03-14

我可以看到JSON的一些问题会导致潜在的错误。其中一个是导致MALFORMED_REQUEST错误的原因,另两个是修复后可能遇到的原因。

首先,payee对象实际上将位于transactions对象的下面,因此该节点看起来应该类似于:

"transactions": [{
    "amount": {
        "currency": "USD",
        "total": "156.00",
        "details": {
            "shipping": "23.00",
            "tax": "0.00",
            "subtotal": "133.00"
        }
    },
    "payee": {
        "email": "SANDBOX MERCHANT EMAIL"
    },
    "item_list": {
        "items": [{
            "name": "PRODUCT NAME",
            "sku": "PRODUCT SKU",
            "quantity": 1,
            "price": "133.00",
            "tax": "0.00",
            "currency": "USD"
        }],
        "shipping_address": {
            "recipient_name": "Sandbox Buyer",
            "line1": "SHIPPING ADDRESS LINE 1",
            "line2": "",
            "city": "SHIPPING CITY",
            "state": "ST",
            "postal_code": "ZIP",
            "country_code": "US"
        }
    },
    "invoice_number": "25",
    "notify_url": "NOTIFY URL"
}]

接下来,在payee对象中,同时指定email和merchant_id。只需要其中一个。有一个小怪癖发生时,这两个添加,可能没有正确匹配,将产生一个错误。具体错误如下所示:

{"response":{"name":"PAYEE_ACCOUNT_INVALID","message":"Payee account is invalid.","information_link":"https://developer.paypal.com/docs/api/#PAYEE_ACCOUNT_INVALID","debug_id":"feefw4543a1a5","httpStatusCode":400},"httpStatusCode":400}
 类似资料:
  • 我有3个活动A、B和C。A导致B,B导致C。我希望能够在A和B之间来回移动,但我希望在C开始后完成A和B。我知道如何在通过意图启动C时关闭B,但在启动C时如何关闭A?

  • 我使用pyenv来管理python安装和虚拟环境。我在Mac(Catalina)上安装了几个python。 < li >系统python(usr/bin中的python 2.7) < Li > usr/bin中的python 3.9(来自python.org/downloads中的默认安装程序) < Li > python 3.9 in/opt/anaconda 3/bin/python 3(来自

  • 问题内容: 有人知道如何从其他活动中关闭一个活动吗?例如:我有3个活动(活动A,B和C),并且可以从活动C中关闭活动A。我的活动结构是活动A->活动B->活动C如何从中关闭活动A活动C? 我曾尝试此代码: 但是该代码只能关闭活动B的活动A,而不能直接关闭活动C的活动A。 有谁知道直接从其他活动关闭活动?谢谢.. 问题答案:

  • 这个想法的利弊是什么? 注: 请不要给我片段和活动的链接。 null null

  • 我知道如何使用发送捆绑包,但我的代码会导致。但我不知道为什么。 主要活动 维护活动2(此处接收) 这里,哪一部分错了被添加到捆绑包中,但每次抛出时。