function __construct($order_info, $controller, $encoding)
 {
     if ($controller->config->get('maksuturva_sandbox')) {
         $this->sandbox = true;
         $secretKey = '11223344556677889900';
         $sellerId = 'testikauppias';
     } else {
         $secretKey = $controller->config->get('maksuturva_secretkey');
         $sellerId = $controller->config->get('maksuturva_sellerid');
     }
     $dueDate = date("d.m.Y");
     $id = self::getMaksuturvaId($order_info['order_id']);
     //Adding each product from order
     $products_rows = array();
     if ($controller->cart) {
         $products = $controller->cart->getProducts();
     } else {
         $products = array();
     }
     $products_total = 0;
     foreach ($products as $product) {
         // get current product tax
         $tax = 0;
         if ($controller->config->get('config_tax') && method_exists($controller->tax, 'getTax')) {
             $tax = $controller->tax->getTax(100, $product["tax_class_id"]);
         }
         $name = strip_tags(htmlspecialchars_decode($product["name"]));
         $model = strip_tags(htmlspecialchars_decode($product["model"]));
         $price = $product["price"];
         if ($controller->currency && $order_info['currency_value']) {
             $price = $controller->currency->format($price, $order_info['currency_code'], $order_info['currency_value'], false);
         }
         $products_total += $price * $product["quantity"] * (1.0 + $tax / 100);
         $row = array('pmt_row_name' => $name, 'pmt_row_desc' => $name . ": " . $model, 'pmt_row_quantity' => $product["quantity"], 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_net' => str_replace('.', ',', sprintf("%.2f", $price)), 'pmt_row_vat' => str_replace('.', ',', sprintf("%.2f", $tax)), 'pmt_row_discountpercentage' => "0,00", 'pmt_row_type' => 1);
         array_push($products_rows, $row);
     }
     $total_data = array();
     $discount = 0;
     $shipping = 0;
     $taxes = $controller->cart->getTaxes();
     $controller->load->model('setting/extension');
     $results = $controller->model_setting_extension->getExtensions('total');
     foreach ($results as $result) {
         if ($controller->config->get($result['code'] . '_status')) {
             if ($result['code'] == 'coupon') {
                 $controller->load->model('total/coupon');
                 $controller->model_total_coupon->getTotal($total_data, $discount, $taxes);
             } else {
                 if ($result['code'] == 'shipping') {
                     $controller->load->model('total/shipping');
                     $controller->model_total_shipping->getTotal($total_data, $shipping, $taxes);
                 }
             }
         }
     }
     $total = $order_info['total'];
     if ($controller->currency && $order_info['currency_value']) {
         $shipping = $controller->currency->format($shipping, $order_info['currency_code'], $order_info['currency_value'], false);
         $total = $controller->currency->format($total, $order_info['currency_code'], $order_info['currency_value'], false);
     }
     // forcing discount to be the diference (maybe some uncovered opencart's feature)
     $discount = round($total, 2) - ($shipping + $products_total);
     // Adding the shipping cost as a row
     if ($controller->cart) {
         $shipping_cost = $shipping;
     } else {
         $shipping_cost = 0;
     }
     if ($discount < 0) {
         $row = array('pmt_row_name' => 'Coupon Discount', 'pmt_row_desc' => 'Coupon Discount', 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_net' => str_replace('.', ',', sprintf("%.2f", $discount)), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "0,00", 'pmt_row_type' => 6);
         array_push($products_rows, $row);
         $products_total += $discount;
     } else {
         $shipping_cost += $discount;
     }
     $row = array('pmt_row_name' => 'Shipping cost', 'pmt_row_desc' => trim($order_info["shipping_method"]) != '' ? $order_info["shipping_method"] : 'No Shipping method', 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_net' => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "0,00", 'pmt_row_type' => 2);
     array_push($products_rows, $row);
     $options = array("pmt_keygeneration" => $controller->config->get('maksuturva_keyversion'), "pmt_id" => $id, "pmt_orderid" => $id, "pmt_reference" => $id, "pmt_sellerid" => $sellerId, "pmt_duedate" => $dueDate, "pmt_okreturn" => $this->getLink('payment/maksuturva/callback', '', 'SSL'), "pmt_errorreturn" => $this->getLink('payment/maksuturva/error', '', 'SSL'), "pmt_cancelreturn" => $this->getLink('payment/maksuturva/cancel', '', 'SSL'), "pmt_delayedpayreturn" => $this->getLink('payment/maksuturva/delay', '', 'SSL'), "pmt_amount" => str_replace('.', ',', sprintf("%.2f", $products_total)), "pmt_buyername" => trim($order_info["payment_firstname"] . " " . $order_info["payment_lastname"]), "pmt_buyeraddress" => $order_info["payment_address_1"] . " " . $order_info["payment_address_2"], "pmt_buyerpostalcode" => $order_info['payment_postcode'], "pmt_buyercity" => $order_info['payment_city'], "pmt_buyercountry" => $order_info['payment_iso_code_2'], "pmt_buyeremail" => $order_info['email'], "pmt_escrow" => $controller->config->get('maksuturva_emaksut') == "0" ? "Y" : "N", "pmt_deliveryname" => trim($order_info["shipping_firstname"] . " " . $order_info["shipping_lastname"]), "pmt_deliveryaddress" => $order_info["shipping_address_1"] . " " . $order_info["shipping_address_2"], "pmt_deliverypostalcode" => $order_info['shipping_postcode'], "pmt_deliverycity" => $order_info['shipping_city'], "pmt_deliverycountry" => $order_info['shipping_iso_code_2'], "pmt_sellercosts" => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), "pmt_rows" => count($products_rows), "pmt_rows_data" => $products_rows);
     #var_dump($options);exit;
     parent::__construct($secretKey, $options, $encoding, $controller->config->get('maksuturva_url'));
 }
 function __construct($plugin, $order, $cart = null)
 {
     global $woocommerce;
     $secretKey = $plugin->maksuturva_secretkey;
     $sellerId = $plugin->maksuturva_sellerid;
     $dueDate = date("d.m.Y");
     $id = $plugin->maksuturva_orderid_prefix . self::getMaksuturvaId($order->id);
     $pid = self::getMaksuturvaId($order->id);
     $products_rows = array();
     $discount_total = 0;
     $product_total = 0;
     $shipping_total = 0;
     foreach ($order->get_items() as $order_item_id => $item) {
         $product = $order->get_product_from_item($item);
         $desc = '';
         //$product->post->post_excerpt; // $product->get_title();
         if (defined('WOOCOMMERCE_VERSION') && version_compare(WOOCOMMERCE_VERSION, '2.4', '>')) {
             $woi = new WC_Order_Item_Meta($item);
         } else {
             $woi = new WC_Order_Item_Meta($order->get_item_meta($order_item_id, ''));
         }
         $gf = $woi->get_formatted();
         if ($gf) {
             foreach ($gf as $attr) {
                 $desc .= implode('=', $attr) . '|';
             }
         }
         $item_price_with_tax = $order->get_item_subtotal($item, true);
         $item_totalprice_with_tax = $order->get_item_total($item, true);
         $discount_pct = ($item_price_with_tax - $item_totalprice_with_tax) / $item_price_with_tax * 100.0;
         //( $line_price_with_tax - $line_totalprice_with_tax); //)) / $line_price_with_tax*100;
         if ($product->product_type == 'variable') {
             $desc .= implode(',', $product->get_variation_attributes()) . ' ';
         }
         $desc .= $product->post->post_excerpt;
         //apply_filters( 'woocommerce_short_description', $product->post->post_excerpt));
         //$product_total = $product_total + $item_totalprice_with_tax;
         //$discount_total = $discount_total - $line_price_with_tax - $item_totalprice_with_tax;
         $encoding = get_bloginfo('charset');
         //if($plugin->debug) {
         //	$plugin->log->add($plugin->id, $encoding.' Item total price='.$item_totalprice_with_tax." subtotal=".$item_price_with_tax." discount-%=".$discount_pct.":".$desc);
         //}
         $sku = '-';
         if ($product->get_sku()) {
             $sku = $product->get_sku();
         }
         $row = array('pmt_row_name' => $item['name'], 'pmt_row_desc' => strip_tags(html_entity_decode($desc)), 'pmt_row_quantity' => $item['qty'], 'pmt_row_articlenr' => $sku, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("%.2f", $order->get_item_subtotal($item, true))), 'pmt_row_vat' => str_replace('.', ',', sprintf("%.2f", $this->calc_tax_rate($product))), 'pmt_row_discountpercentage' => str_replace('.', ',', sprintf("%.2f", $discount_pct)), 'pmt_row_type' => 1);
         array_push($products_rows, $row);
     }
     //Coupons
     if ($cart) {
         foreach ($cart->get_coupons() as $code => $coupon) {
             $coupon_desc = $code;
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax()) {
                 continue;
             }
             $coupon_post = get_post($coupon->id);
             $excerpt = $coupon_post->post_excerpt;
             if ($excerpt) {
                 $coupon_desc .= ' ' . $excerpt;
             }
             $row = array('pmt_row_name' => __('Discount', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($coupon_desc)), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("-%.2f", $cart->get_coupon_discount_amount($code))), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "00,00", 'pmt_row_type' => 6);
             array_push($products_rows, $row);
         }
     } elseif ($order->get_total_discount()) {
         $discount = $order->get_total_discount();
         $coupon_desc = implode(',', $order->get_used_coupons());
         $row = array('pmt_row_name' => __('Discount', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($coupon_desc)), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("-%.2f", $discount)), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "00,00", 'pmt_row_type' => 6);
         array_push($products_rows, $row);
     }
     //Shipping costs
     $shipping_cost = $order->get_total_shipping() + $order->get_shipping_tax();
     //- $order->get_shipping_tax( );
     if ($order->get_total_shipping() > 0) {
         $shipping_tax = 100 * $order->get_shipping_tax() / $order->get_total_shipping();
     } else {
         $shipping_tax = 0;
     }
     $row = array('pmt_row_name' => __('Shipping cost', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($order->get_shipping_method())), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), 'pmt_row_vat' => str_replace('.', ',', sprintf("%.2f", $shipping_tax)), 'pmt_row_discountpercentage' => "0,00", 'pmt_row_type' => 2);
     array_push($products_rows, $row);
     $returnURL = $plugin->notify_url;
     if ($woocommerce->session && $woocommerce->session->id) {
         $sessionid = $woocommerce->session->id;
         $returnURL = add_query_arg('sessionid', $sessionid, $plugin->notify_url);
         //get_return_url( $order ));
         $returnURL = add_query_arg('orderid', $id, $returnURL);
     }
     //$returnURL = add_query_arg('gateway', 'wc_maksurva_emaksut', $returnURL);
     $billing_email = $order->billing_email;
     $billing_phone = $order->billing_phone;
     if (!empty($order->customer_user)) {
         $user = get_user_by('id', $order->customer_user);
         if (empty($order->billing_email)) {
             $billing_email = $user->user_email;
         }
         if (empty($order->billing_email)) {
             $billing_email = $user->user_phone;
         }
     }
     $locale = get_bloginfo('language');
     global $sitepress;
     if ($sitepress) {
         $locale = $sitepress->get_locale(ICL_LANGUAGE_CODE);
     }
     if ($locale != "fi_FI" || $locale != "sv_FI" || $locale != "en_FI") {
         if (substr($locale, 0, 2) == "fi") {
             $locale = "fi_FI";
         } elseif (strpos($locale, "sv") != FALSE) {
             $locale = "sv_FI";
         } else {
             $locale = "en_FI";
         }
     }
     $options = array("pmt_keygeneration" => $plugin->maksuturva_keyversion, "pmt_id" => $id, "pmt_orderid" => $order->id, "pmt_reference" => $pid, "pmt_sellerid" => $sellerId, "pmt_duedate" => $dueDate, "pmt_userlocale" => $locale, "pmt_okreturn" => add_query_arg('pmt_act', 'ok', $returnURL), "pmt_errorreturn" => add_query_arg('pmt_act', 'error', $returnURL), "pmt_cancelreturn" => add_query_arg('pmt_act', 'cancel', $returnURL), "pmt_delayedpayreturn" => add_query_arg('pmt_act', 'delay', $returnURL), "pmt_amount" => str_replace('.', ',', sprintf("%.2f", $order->get_total() - $shipping_cost)), "pmt_deliveryname" => trim($order->shipping_first_name . ' ' . $order->shipping_last_name), "pmt_deliveryaddress" => trim($order->shipping_address_1 . "  " . $order->shipping_address_2), "pmt_deliverypostalcode" => trim($this->clean($order->shipping_postcode, '000')), "pmt_deliverycity" => trim($this->clean($order->shipping_city)), "pmt_deliverycountry" => trim($this->clean($order->shipping_country)), "pmt_buyername" => trim($order->billing_first_name . ' ' . $order->billing_last_name), "pmt_buyeraddress" => trim($this->clean($order->billing_address_1 . "  " . $order->billing_address_2)), "pmt_buyerpostalcode" => trim($this->clean($order->billing_postcode, '000')), "pmt_buyercity" => trim($this->clean($order->billing_city)), "pmt_buyercountry" => trim($order->billing_country), "pmt_buyeremail" => trim($billing_email), "pmt_buyerphone" => trim($billing_phone), "pmt_escrow" => $plugin->maksuturva_emaksut == "no" ? "Y" : "N", "pmt_sellercosts" => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), "pmt_rows" => count($products_rows), "pmt_rows_data" => $products_rows);
     parent::__construct($secretKey, $options, $plugin->maksuturva_encoding, $plugin->maksuturva_url);
 }