我正在尝试通过PatchRequest将第二个项目添加到创建的付款中,但似乎无法弄清楚。
这是我的代码:
$patch = new \PayPal\Api\Patch();
$patch->setOp('add');
$patch->setPath('/transactions/0/item_list/items/1');
$json = '
{
"description":"Testartikel",
"price":"50.00",
"currency":"EUR",
"quantity":1
}';
$data = json_decode($json);
$patch->setValue($data);
$patches[] = $patch;
$patchRequest = new \PayPal\Api\PatchRequest();
$patchRequest->setPatches($patches);
$payment->update($patchRequest, $apiContext);
因此,请求如下所示:
[{"op":"add","path":"/transactions/0/item_list/items/1","value":{"description":"Testartikel","price":"50.00","currency":"EUR","quantity":1}}]
我收到此错误:
{"name":"MALFORMED_REQUEST","message":"MALFORMED_REQUEST","information_link":"https://developer.paypal.com/docs/api/#MALFORMED_REQUEST","debug_id":"682441321797"}
我猜路径
/transactions/0/item_list/items/1
可能是错误的,但我不知道什么是正确的。我已经尝试过/transactions/0/item_list
和/transactions/0/item_list/items
具有相同的结果。任何帮助将不胜感激。
最佳答案
我有同样的错误信息。
问题是JSON周围缺少方括号。
正确的路径是“ / transactions / 0 / item_list / items”。
$json = '
[
{
"description":"Testartikel",
"price":"50.00",
"currency":"EUR",
"quantity":1
}
]';
$patchAdd = new \PayPal\Api\Patch();
$patchAdd->setOp('add')
->setPath('/transactions/0/item_list/items')
->setValue(json_decode($json));
$patchRequest = new \PayPal\Api\PatchRequest();
$patchRequest->setPatches(array($patchAdd);
这对我有用。
关于php - Paypal 付款REST API更新付款以添加商品,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39472140/