public static function calculate($order, $send_items = false)
 {
     $PaymentOrderItems = array();
     $ctr = $giftwrapamount = $total_items = $total_discount = $total_tax = $shipping = 0;
     $ITEMAMT = 0;
     if ($order) {
         $order_total = $order->get_total();
         $items = $order->get_items();
         /*
          * Set shipping and tax values.
          */
         if (get_option('woocommerce_prices_include_tax') == 'yes') {
             $shipping = $order->get_total_shipping() + $order->get_shipping_tax();
             $tax = 0;
         } else {
             $shipping = $order->get_total_shipping();
             $tax = $order->get_total_tax();
         }
         if ('yes' === get_option('woocommerce_calc_taxes') && 'yes' === get_option('woocommerce_prices_include_tax')) {
             $tax = $order->get_total_tax();
         }
     } else {
         //if empty order we get data from cart
         $order_total = WC()->cart->total;
         $items = WC()->cart->get_cart();
         /**
          * Get shipping and tax.
          */
         if (get_option('woocommerce_prices_include_tax') == 'yes') {
             $shipping = WC()->cart->shipping_total + WC()->cart->shipping_tax_total;
             $tax = 0;
         } else {
             $shipping = WC()->cart->shipping_total;
             $tax = WC()->cart->get_taxes_total();
         }
         if ('yes' === get_option('woocommerce_calc_taxes') && 'yes' === get_option('woocommerce_prices_include_tax')) {
             $tax = WC()->cart->get_taxes_total();
         }
     }
     if ($send_items) {
         foreach ($items as $item) {
             /*
              * Get product data from WooCommerce
              */
             if ($order) {
                 $_product = $order->get_product_from_item($item);
                 $qty = absint($item['qty']);
                 $item_meta = new WC_Order_Item_Meta($item, $_product);
                 $meta = $item_meta->display(true, true);
             } else {
                 $_product = $item['data'];
                 $qty = absint($item['quantity']);
                 $meta = WC()->cart->get_item_data($item, true);
             }
             $sku = $_product->get_sku();
             $item['name'] = html_entity_decode($_product->get_title(), ENT_NOQUOTES, 'UTF-8');
             if ($_product->product_type == 'variation') {
                 if (empty($sku)) {
                     $sku = $_product->parent->get_sku();
                 }
                 if (!empty($meta)) {
                     $item['name'] .= " - " . str_replace(", \n", " - ", $meta);
                 }
             }
             $Item = array('name' => $item['name'], 'desc' => '', 'amt' => self::round($item['line_subtotal'] / $qty), 'number' => $sku, 'qty' => $qty);
             array_push($PaymentOrderItems, $Item);
             $ITEMAMT += self::round($item['line_subtotal'] / $qty) * $qty;
         }
         /**
          * Add custom Woo cart fees as line items
          */
         foreach (WC()->cart->get_fees() as $fee) {
             $Item = array('name' => $fee->name, 'desc' => '', 'amt' => self::number_format($fee->amount, 2, '.', ''), 'number' => $fee->id, 'qty' => 1);
             /**
              * The gift wrap amount actually has its own parameter in
              * DECP, so we don't want to include it as one of the line
              * items.
              */
             if ($Item['number'] != 'gift-wrap') {
                 array_push($PaymentOrderItems, $Item);
                 $ITEMAMT += self::round($fee->amount);
             } else {
                 $giftwrapamount = self::round($fee->amount);
             }
             $ctr++;
         }
         //caculate discount
         if ($order) {
             if (!AngellEYE_Gateway_Paypal::is_wc_version_greater_2_3()) {
                 if ($order->get_cart_discount() > 0) {
                     foreach (WC()->cart->get_coupons('cart') as $code => $coupon) {
                         $Item = array('name' => 'Cart Discount', 'number' => $code, 'qty' => '1', 'amt' => '-' . self::number_format(WC()->cart->coupon_discount_amounts[$code]));
                         array_push($PaymentOrderItems, $Item);
                     }
                     $total_discount -= $order->get_cart_discount();
                 }
                 if ($order->get_order_discount() > 0) {
                     foreach (WC()->cart->get_coupons('order') as $code => $coupon) {
                         $Item = array('name' => 'Order Discount', 'number' => $code, 'qty' => '1', 'amt' => '-' . self::number_format(WC()->cart->coupon_discount_amounts[$code]));
                         array_push($PaymentOrderItems, $Item);
                     }
                     $total_discount -= $order->get_order_discount();
                 }
             } else {
                 if ($order->get_total_discount() > 0) {
                     $Item = array('name' => 'Total Discount', 'qty' => 1, 'amt' => -self::number_format($order->get_total_discount()), 'number' => implode(", ", $order->get_used_coupons()));
                     array_push($PaymentOrderItems, $Item);
                     $total_discount -= $order->get_total_discount();
                 }
             }
         } else {
             if (WC()->cart->get_cart_discount_total() > 0) {
                 foreach (WC()->cart->get_coupons('cart') as $code => $coupon) {
                     $Item = array('name' => 'Cart Discount', 'qty' => '1', 'number' => $code, 'amt' => '-' . self::number_format(WC()->cart->coupon_discount_amounts[$code]));
                     array_push($PaymentOrderItems, $Item);
                     $total_discount -= self::number_format(WC()->cart->coupon_discount_amounts[$code]);
                 }
             }
             if (!AngellEYE_Gateway_Paypal::is_wc_version_greater_2_3()) {
                 if (WC()->cart->get_order_discount_total() > 0) {
                     foreach (WC()->cart->get_coupons('order') as $code => $coupon) {
                         $Item = array('name' => 'Order Discount', 'qty' => '1', 'number' => $code, 'amt' => '-' . self::number_format(WC()->cart->coupon_discount_amounts[$code]));
                         array_push($PaymentOrderItems, $Item);
                         $total_discount -= self::number_format(WC()->cart->coupon_discount_amounts[$code]);
                     }
                 }
             }
         }
     }
     if ($tax > 0) {
         $tax = self::number_format($tax);
     }
     if ($shipping > 0) {
         $shipping = self::number_format($shipping);
     }
     if ($total_discount) {
         $total_discount = self::round($total_discount);
     }
     if (empty($ITEMAMT)) {
         $cart_fees = WC()->cart->get_fees();
         if (isset($cart_fees[0]->id) && $cart_fees[0]->id == 'gift-wrap') {
             $giftwrapamount = isset($cart_fees[0]->amount) ? $cart_fees[0]->amount : 0;
         } else {
             $giftwrapamount = 0;
         }
         $Payment['itemamt'] = $order_total - $tax - $shipping - $giftwrapamount;
     } else {
         $Payment['itemamt'] = self::number_format($ITEMAMT + $total_discount);
     }
     /*
      * Set tax
      */
     if ($tax > 0) {
         $Payment['taxamt'] = self::number_format($tax);
         // Required if you specify itemized L_TAXAMT fields.  Sum of all tax items in this order.
     } else {
         $Payment['taxamt'] = 0;
     }
     /*
      * Set shipping
      */
     if ($shipping > 0) {
         $Payment['shippingamt'] = self::number_format($shipping);
         // Total shipping costs for this order.  If you specify SHIPPINGAMT you mut also specify a value for ITEMAMT.
     } else {
         $Payment['shippingamt'] = 0;
     }
     $Payment['order_items'] = $PaymentOrderItems;
     // Rounding amendment
     if (trim(self::number_format($order_total)) !== trim(self::number_format($Payment['itemamt'] + $giftwrapamount + $tax + $shipping))) {
         $diffrence_amount = AngellEYE_Gateway_Paypal::get_diffrent($order_total, $Payment['itemamt'] + $tax + $shipping);
         if ($shipping > 0) {
             $Payment['shippingamt'] = self::number_format($shipping + $diffrence_amount);
         } elseif ($tax > 0) {
             $Payment['taxamt'] = self::number_format($tax + $diffrence_amount);
         } else {
             //make change to itemamt
             $Payment['itemamt'] = self::number_format($Payment['itemamt'] + $diffrence_amount);
             //also make change to the first item
             if ($send_items) {
                 $Payment['order_items'][0]['amt'] = self::number_format($Payment['order_items'][0]['amt'] + $diffrence_amount / $Payment['order_items'][0]['qty']);
             }
         }
     }
     return $Payment;
 }