PayPal:MALFORMED_REQUEST,没有其他活动
Posted
技术标签:
【中文标题】PayPal:MALFORMED_REQUEST,没有其他活动【英文标题】:PayPal: MALFORMED_REQUEST with no additional activity 【发布时间】:2017-07-11 19:53:36 【问题描述】:以下php:
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 - 传入的 JSON 请求未映射到 API 请求
“意图”:“销售”, “付款人”: "payment_method": "贝宝", “付款人信息”: "first_name": "沙盒", "last_name": "买家", “帐单地址”: "line1": "账单地址第 1 行", “第 2 行”:“”, “城市”:“城市”, “状态”:“ST”, “邮编/ Zip”, “国家代码”:“美国” , “电子邮件”:“沙盒买家电子邮件” , “重定向网址”: "return_url": "返回 URL", "cancel_url": "取消 URL" , “交易”:[ “数量”: “货币:美元”, “总计”:“156.00”, “细节”: “航运”:“23.00”, “税”:“0.00”, “小计”:“133.00” , “项目列表”: “项目”: [ "name": "产品名称", "sku": "产品 SKU", “数量”:1, “价格”:“133.00”, “税”:“0.00”, “货币:美元” ], “送货地址”: "recipient_name": "沙盒买家", "line1": "送货地址第 1 行", “第 2 行”:“”, “城市”:“航运城市”, “状态”:“ST”, “邮编/ Zip”, “国家代码”:“美国” , “发票号码”:25, "notify_url": "通知 URL" ], “收款人”: "email": "沙盒商家电子邮件", "merchant_id": "沙盒商户 ID"
我得到了 MALFORMED_REQUEST 和数据转储,没有额外的活动。用户应该被带到 PayPal(在本例中为沙盒)进行支付处理(然后返回调用站点。我使用的是 PayPal PHP SDK (REST)。我该从哪里开始?
【问题讨论】:
【参考方案1】:我可以看到一些 JSON 问题会导致潜在错误。一个是导致 MALFORMED_REQUEST 错误的原因,另一个是您修复后可能遇到的。
首先,收款人对象实际上将位于交易对象的下方,因此该节点应如下所示:
"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"
]
接下来,在您的收款人对象中,您指定电子邮件和商家 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
最后,invoice_number 需要一个字符串 (https://developer.paypal.com/docs/api/payments/#definition-transaction:v1)。 API 端点可能会自动转换它,但最好让它符合要求。
一旦添加了实际值,我在上面发布的交易对象应该可以满足您的需求。
【讨论】:
有趣的是,SDK Source Docs 表示 Payee 对象可以分配给 Transaction 或 Payment,但它们没有显示何时使用。 这解决了错误信息问题。仍然没有将用户带到 PayPal。 我会看一下 Github 文档,看看如何解决这个问题。当您尝试重定向时发生了什么?是否产生错误?以上是关于PayPal:MALFORMED_REQUEST,没有其他活动的主要内容,如果未能解决你的问题,请参考以下文章
PHP PayPal 错误 - MALFORMED_REQUEST - 传入的 JSON 请求未映射到 API 请求
curl中的paypal MALFORMED REQUEST错误