PayPal SDK(PHP)项目从数据库动态创建



我正在使用PayPal SDK(PHP)。我正在尝试从数据库动态创建项目。所有计算将通过动态完成。但是我失败了。谁能帮我?这里代码:

$item_1 = new Item();
        $item_1->setName('Item 1') /** item name **/
            ->setCurrency('USD')
            ->setQuantity(1)
            ->setPrice($request->get('amount')); /** unit price **/
        $item_list = new ItemList();
        $item_list->setItems(array($item_1));
        $amount = new Amount();
        $amount->setCurrency('USD')
            ->setTotal($request->get('amount'));
        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($item_list)
            ->setDescription('Your transaction description') 
$order_subtotal = $order->invoice->sub_total;
       $ship_tax = 0;
       $ship_cost = 0;
       $shipping_discount = $order->invoice->discount;
       $total = $order_subtotal + $ship_tax + $ship_cost - $shipping_discount;
        $items = [];
    foreach($order->invoice->products as $product)
    {
        $item = new Item();
        $item->setName($product->name)
            ->setPrice($product->price)
            ->setCurrency('USD')
            ->setQuantity($product->qty);
        $items[] = $item; 
    }
    $itemList = new ItemList();
    $itemList->setItems($items);
    $details = new Details();
    $details->setSubtotal($order_subtotal)
            ->setTax($ship_tax)
            ->setShipping($ship_cost)
            ->setShippingDiscount(- $shipping_discount);

    $amount = new Amount();
    $amount->setCurrency('USD')
        ->setDetails($details)
            ->setTotal($total);
$order_discount = $order->invoice->discount;
$order_subtotal = $order->invoice->sub_total;
$order_grandtotal = $order->invoice->grand_total;
$i = 1;
$order_items = array();
foreach ($order->invoice->products as $product) {
$order_items[$i] = new Item();
$order_items[$i]->setName($product->name)
->setCurrency('USD')
->setQuantity($product->qty)
->setPrice($product->price);
$i++;
}
if ($order->order_discount > 0) {
$order_items[$i] = new Item();
$order_items[$i]->setName('Discount')
->setCurrency('USD')
->setQuantity(1)
->setPrice('-' . $order_discount);
}
$item_list = new ItemList();
$item_list->setItems($order_items);
$amount_details = new Details();
$amount_details->setSubtotal($order_subtotal);
$amount = new Amount();
$amount->setCurrency('USD')
->setDetails($amount_details)
->setTotal($order_grandtotal);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Your transaction description')
->setInvoiceNumber($order->invoice->invoice_no);

最新更新