【发布时间】:2020-01-19 17:56:30
【问题描述】:
如何将我已经存储在集合中的项目发送到贝宝结帐,我已经集成了整个过程,但目前它仅适用于贝宝提供的测试项目,我无法整合我自己的项目集合。
我的购物车物品
我有这样的物品:
$items = Session::get('items'); //Json
$decoded_items= json_decode($items,true); //decoded to array
如果我return $items我拥有整个收藏:
{
"Items":[
{
"id":1,
"inventoryID":1,
"title":"Product 1",
"quantity":1,
"unit_price":20,
"image":"img.png"
},
{
"id":2,
"inventoryID":1,
"title":"Product2",
"quantity":1,
"unit_price":25,
"image":"img.png"
}
],
"items_count":2,
"products_count":2
}
但是如果我return $decoded_items['Items']; 我刚刚得到了数组:
[
{
"id":1,
"inventoryID":1,
"title":"Product 1",
"quantity":1,
"unit_price":20,
"image":"img.png"
},
{
"id":2,
"inventoryID":1,
"title":"Product2",
"quantity":1,
"unit_price":25,
"image":"img.png"
}
]
贝宝示例
paypal 示例运行良好,并且是:
$item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setSku("123123") // Similar to `item_number` in Classic API
->setPrice(7.5);
$item2 = new Item();
$item2->setName('Granola bars')
->setCurrency('USD')
->setQuantity(5)
->setSku("321321") // Similar to `item_number` in Classic API
->setPrice(2);
$itemList = new ItemList();
$itemList->setItems(array($item1,$item2));
我试过了
考虑到这个例子以及我尝试获取物品的方式:
$items = Session::get('items');
$decoded_items= json_decode($items,true);
foreach($decoded_items['Items'] as $element) {
$name=$element['title'];
$quantity=$element['quantity'];
$sku= $element['id'];
$price=$element['unit_price'];
$item = new Item();
$item-> setName($name)
->setCurrency('USD')
->setQuantity($quantity)
->setSku($sku) // Similar to `item_number` in Classic API
->setPrice($price);
$results[] = $item;
}
$itemList = new ItemList();
$itemList->setItems($results);
但是发送我的物品时,paypal 支付窗口永远不会打开。
提前谢谢你:)
【问题讨论】:
-
上面的 JSON 转储是复制粘贴,还是您手动输入的?我问是因为在这两种情况下,
"Product2 "之后都缺少逗号。 -
谢谢,修一下,结构是抄的,名字是手写的
-
json_decode对我上面的代码正确的 JSON 工作正常。乍一看,我唯一看到的另一件事是,当->setSku()在示例中似乎接受字符串时,您将整数传递给它。您可以尝试使用(string) $sku进行投射。除此之外,您还需要提供更多代码。 -
您认为我还需要提供哪些关于我的代码的信息?
-
你提到了语法错误。这实际上是停止运行任何东西还是只是一个 IDE 警告?您能否在定义 PayPal 数据后成功转储
dd($results),或者在您到达那里之前代码被暂停?您的日志中有任何错误吗?如果这里没有问题,那么听起来问题可能出在您向 PayPal 提供数据的方式上,而您在当前帖子中没有提供这些数据——即更多代码。