get_items(); $linedata = array(); /** * @var $orderline WC_Order_item */ foreach ($orderlines as $orderline_key => $orderline) { if ($orderline['qty'] == 0) { // WooCommerce may return 0 for some items during a partial refund if ($orderline['line_total'] != 0 || $orderline['line_tax'] != 0) { return new WP_Error('error', __('Quantity cannot be 0 for item ' . $orderline['name'], 'valitor')); } continue; // Ignore this order line } $_product = wc_get_product($orderline['product_id']); $sku = $_product->get_sku(); $variation_id = $orderline['variation_id']; if ($variation_id) { $result = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_sku' AND post_id = '" . $variation_id . "'"); if ($result[0]->meta_value) { $sku = $result[0]->meta_value; } } $withTax = $_product->get_price_including_tax(); $withoutTax = $_product->get_price_excluding_tax(); $taxAmount = $withTax - $withoutTax; $taxPercent = ($taxAmount / $withoutTax) * 100; $qty = $orderline['qty']; $totalTax = $orderline['line_tax']; // Skip the products that are not defined in the list - if there is such a list if (!empty($products)) { if (in_array($sku, $products['skuList'])) { $totalTax = (float)number_format(($totalTax / $qty) * $products['skuQty'][$sku], 2, '.', ''); $qty = $products['skuQty'][$sku]; } else continue; // Ignore this order line if is not part of the product list } if ($returnRefundOrderLines) { $linedata[$orderline_key] = array( 'qty' => $qty, 'refund_total' => wc_format_decimal($orderline['line_total']), 'refund_tax' => wc_format_decimal($totalTax) ); } else { $linedata[] = array( 'description' => $orderline['name'], 'itemId' => $sku, 'quantity' => $qty, 'unitPrice' => (float)number_format($orderline['line_total'] / $qty, 2, '.', ''), 'taxAmount' => (float)number_format($totalTax, 2, '.', ''), //'taxPercent' => (float)round(number_format($taxPercent, 2, '.', '')), //'discount' => (float) ($line_discount_percent > 0) ? $line_discount_percent : '', ); } } // Add shipping prices $order_shipping_methods = $order->get_shipping_methods(); $shipping_id = 'NaN'; foreach ($order_shipping_methods as $orderShippingKey => $orderShippingMethods) { $shipping_id = $orderShippingMethods['method_id']; } // In a refund it's possible to have order_shipping == 0 and order_shipping_tax != 0 at the same time if ($order->get_shipping_total() <> 0 || $order->get_shipping_tax() <> 0) { if (!empty($products)) { if (!in_array($shipping_id, $products['skuList'])) { return $linedata; } } $totalShipping = $order->get_shipping_total(); $totalShippingTax = $order->get_shipping_tax(); if ($returnRefundOrderLines) { $linedata[$orderShippingKey] = array( 'qty' => 1, 'refund_total' => wc_format_decimal($totalShipping), 'refund_tax' => wc_format_decimal($totalShippingTax) ); } else { $linedata[] = array( 'description' => $order->get_shipping_method(), 'itemId' => $shipping_id, 'quantity' => 1, 'unitPrice' => (float)number_format($totalShipping, 2, '.', ''), 'taxAmount' => (float)number_format($totalShippingTax, 2, '.', ''), 'goodsType' => 'shipment' ); } } return $linedata; } function transactionInfo( $transaction_info = array()){ $valitorPluginData = get_plugin_data(__DIR__ . '/valitor.php'); $pluginVersion = $valitorPluginData['Version']; $pluginName = $valitorPluginData['Name']; $woocommercePluginData = get_plugin_data(__DIR__ . '/../woocommerce/woocommerce.php'); $woocommerceVersion = $woocommercePluginData['Version']; $ecomName = get_bloginfo('name'); $otherinfo = 'storeName-'.$ecomName.','.'woocomVersion-'.$woocommerceVersion; $transaction_info = array( 'ecomPlatform' => 'Wordpress', 'ecomVersion' => get_bloginfo('version'), 'valitorPluginName' => $pluginName, 'valitorPluginVersion' => $pluginVersion, 'otherInfo' => $otherinfo, ); return $transaction_info; } function setShippingAddress($customerInfo){ $shippingCountry = $customerInfo['shipping_country']; $shippingCity = $customerInfo['shipping_city']; $shippingAddress = $customerInfo['shipping_address']; $shippingPostal = $customerInfo['shipping_postal']; //Use billing address in case one of the shipping parameters is missing if(empty($shippingCountry) || empty($shippingCity) || empty($shippingAddress) || empty($shippingPostal) ) { if (empty($customerInfo['billing_country'])) { //throw error since the payment cannot be made without shipping country return new WP_Error('error', __('Shipping country is required', 'valitor')); } $customerInfo['shipping_country'] = $customerInfo['billing_country']; $customerInfo['shipping_address'] = $customerInfo['billing_address']; $customerInfo['shipping_city'] = $customerInfo['billing_city']; $customerInfo['shipping_region'] = $customerInfo['billing_region']; $customerInfo['shipping_postal'] = $customerInfo['billing_postal']; } return $customerInfo; }