【问题标题】:PayPal SDK (PHP) items creating dynamically from database从数据库动态创建的 PayPal SDK (PHP) 项目
【发布时间】:2017-08-05 21:45:10
【问题描述】:

我正在使用 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') 

【问题讨论】:

    标签: php laravel paypal


    【解决方案1】:
    $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);
    

    【讨论】:

      【解决方案2】:
      $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);
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-28
      • 2014-03-03
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多