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

时间:2014-06-12 12:25:14

标签: php rest paypal paypal-sandbox payment-processing

我正在尝试通过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);

如何使上述代码有效?非常感谢您的意见。

更多详情:

我的方法基于以下examplePaypal Rest API

1 个答案:

答案 0 :(得分:0)

通过引入/设置项目和项目列表对象来解决。我的代码现在看起来像这样:

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