我试图通过Paypal API销售多个产品,但我得到糟糕的请求错误。
数组中有像
这样的项 $products = array(
array('name' => 'Product 1', 'price' => $200) ,
array('name' => 'Product 2', 'price' => $240)
);
$transactions = [];
$total = 0;
foreach($products as $product) {
$transaction = new Transaction();
$transaction->setAmount(array('currency' => 'USD', 'amount' => $product['price']));
$transaction->setDescription($product['name']);
$transactions[] = $transaction;
$total += $product['price'];
}
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($returnUrl);
$redirectUrls->setCancelUrl($cancelUrl);
$amount = new Amount();
$amount->setCurrency($currency);
$amount->setTotal($total);
$payment = new Payment();
$payment->setRedirectUrls($redirectUrls);
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setTransactions($transactions);
我怎样才能使上面的代码工作?非常感谢您的意见。
更多细节:
我的方法是基于以下示例和Paypal Rest API。
通过引入/设置项目和项目列表对象来解决。我的代码现在看起来像这样:
$item = new Item();
$item->setQuantity(1);
$item->setName($paymentDesc);
$item->setPrice($total);
$item->setCurrency($currency);
$item->setSku('HELLOWORLD');
$item2 = new Item();
$item2->setQuantity(1);
$item2->setName('Additional product');
$item2->setPrice($total);
$item2->setCurrency($currency);
$item2->setSku('HELLOWORLD2');
$item_list = new ItemList();
$item_list->setItems(array($item, $item2));
$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('Payment description test');
$transaction->setItem_list($item_list);