如何通过Paypal PHP API处理多个产品的销售

如何通过Paypal PHP API处理多个产品的销售,php,rest,paypal,paypal-sandbox,payment-processing,Php,Rest,Paypal,Paypal Sandbox,Payment Processing,我试图通过销售多个产品,但我收到了错误的请求 我有一个数组中的项目,如 $products = array( array('name' => 'Product 1', 'price' => $200) , array('name' => 'Product 2', 'price' => $240) ); $transactions = []; $total = 0; foreach($products as $product) { $tran

我试图通过销售多个产品,但我收到了错误的请求

我有一个数组中的项目,如

 $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);
我如何才能使上述代码工作?非常感谢您的意见

更多详情:


我的方法基于以下内容。

通过引入/设置项目和项目列表对象来解决。我的代码现在如下所示:

$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);