物品清单上的PayPal Api补丁不起作用

时间:2017-04-26 10:16:33

标签: api paypal

我想在我的交易中添加一个项目。



$json = '
    [
      {
        "name": "Voucher",
        "description":"Voucher",
        "price":"50.00",
        "currency":"EUR",
        "quantity":"1"
      }
    ]';

    $patchAddItem = new \PayPal\Api\Patch();
    $patchAddItem->setOp('add')
        ->setPath('/transactions/0/item_list/items')
        ->setValue(json_decode($json));

$patchReplace = new \PayPal\Api\Patch();
$patchReplace->setOp('replace')
->setPath('/transactions/0/amount')
->setValue(json_decode('{
                "total": "159.00",
                "currency": "EUR",
            }'));

$patchRequest = new \PayPal\Api\PatchRequest();
$patchRequest->setPatches(array($patchAddItem, $patchReplace));

try {
    $this->payment->update($patchRequest, $this->apiContext);
} catch (PayPal\Exception\PayPalConnectionExceptio $ex) {
    echo '<pre>';print_r(json_decode($ex->getData()));exit;
}
&#13;
&#13;
&#13;

但我得到以下错误

Eception: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment/PAY... in PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php on line 154
PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php on line 73: PayPal\Core\PayPalHttpConnection->execute("[{"op":"add","path":"/transactions/0/item_list/ite"... )
PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php on line 102: PayPal\Transport\PayPalRestCall->execute(array[1],"/v1/payments/payment/PAY-1S151200BX2478240LEAG3CI","PATCH","[{"op":"add","path":"/transactions/0/item_list/ite"... ,null)
PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php on line 615: PayPal\Common\PayPalResourceModel::executeCall("/v1/payments/payment/PAY-1S151200BX2478240LEAG3CI","PATCH","[{"op":"add","path":"/transactions/0/item_list/ite"... ,null,object,null)

此刻我没有执行付款对象。我是否还要从金额中编辑总的归属?好吧,我也试过这个问题,同样的问题......

1 个答案:

答案 0 :(得分:1)

即使您只向PayPal发送一个项目,您仍然需要将它们设置为setItemList()的项目列表。

如果您的付款数组中有json_decode,那么该数组应该可见:

[item_list] => Array
    (
       [items] => Array
           (
               [0] => Array
                   (
                       [name] => Ground Coffee 40 oz
                       [sku] => 123123
                       [price] => 52.80
                       [currency] => USD
                       [quantity] => 1
                   )

           )

我还没有为某个项目运行补丁。我尝试发送与您的代码类似的'add',并尝试使用items数组中的下一个数字将路径更改为'/transactions/0/item_list/items/1'。但无法增加工作量。

我可以修改item_list的唯一方法是执行item_list的完整'replace',因此在正在运行的购物车中必须包含所有正在购买的商品,而不仅仅是新商品。

为此,我更喜欢使用PayPal sdk中的函数来构建json数组。他们如何创建和执行付款的示例相当不错,并使用SDK功能。 http://paypal.github.io/PayPal-PHP-SDK/sample/

但是,更新付款的示例会直接构建json数组。

以下是使用Paypay PHP SDK类功能修改item_list的测试功能。我对小计和总计进行了硬编码,以匹配购物车中的值以及新项目的增加值。 item_list也使用PP的示例数据进行硬编码。否则,项目的数组将基于用户的购物车项目构建。类型设置为'replace'

所以,是的。小计和总计需要更新以匹配,否则PP调用将失败。

function updatePayPalPayment ($type, $createdPayment, $total, $subtotal, $shipping, $currency) {
$subtotal = '54.80';
$total = '71.73';

$details = new Details();
$details->setShipping($shipping)
    ->setSubtotal($subtotal);

$amount = new Amount();
$amount->setCurrency($currency)
    ->setTotal($total)
    ->setDetails($details);

$item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setSku("123123") // Similar to `item_number` in Classic API
    ->setPrice(52.80);

$item2 = new Item();
$item2->setName('Granola bars')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setSku("321321") // Similar to `item_number` in Classic API
    ->setPrice(2.0);

$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));


$patchItem = new Patch();
$patchItem->setOp($type)
    ->setPath('/transactions/0/item_list')
    ->setValue($itemList);

$patchAmount = new Patch();
$patchAmount->setOp($type)
    ->setPath('/transactions/0/amount')
    ->setValue($amount);


$patchRequest = new PatchRequest();
$patchRequest->setPatches(array($patchAmount, $patchItem));

$update = $createdPayment->update($patchRequest, getApiContext());

return $update;

}

我还发现将apiContext设置为记录到DEBUG并输出到开发中的文件以获得更好的错误消息非常有用。

'log.LogEnabled' => true,
'log.FileName' => '_PayPal.log',
'log.LogLevel' => 'DEBUG',

希望有所帮助。