/**
  * Process the payment
  * @param  integer $order_id
  * @return array   result & redirect URL
  */
 function process_payment($order_id)
 {
     // WooCommerce global
     global $woocommerce;
     // Require Triveneto class
     require 'PgConsTriv.php';
     // Get Order Object
     $order = new WC_Order($order_id);
     // init PgConsTriv Class for PaymentInit (action Purchase)
     $pg = new PgConsTriv(ICL_LANGUAGE_CODE, $this->settings);
     $pg->setAction('Purchase');
     // Payment Provider response will be sent to our triveneto_response_interface
     // Which is defined in 'woocommerce-triveneto-gateway.php'
     $pg->setResponseURL(get_home_url() . '/?triveneto_response_interface=1');
     // Define the goto URL if something goes wrong
     $pg->setErrorURL($order->get_cancel_order_url());
     // Send the message of PaymentInit
     $amount = $woocommerce->cart->get_cart_total();
     $amount = floatval(preg_replace('#[^\\d.]#', '', $amount));
     // parse numbers
     $tracking_id = $order_id;
     $pg->sendVal_PI($amount, $tracking_id);
     // Get result of request
     $hasError = $pg->hasError_PI();
     $ID_PI = $pg->getID_PI();
     // Check for errors
     if ($hasError) {
         $errorCode = $pg->getError_PI();
         // Cancel process and redirect to other page:
         return array('result' => 'error', 'redirect' => $order->get_cancel_order_url());
     }
     // Mark as on-hold (we're awaiting the cheque)
     $order->update_status('on-hold', __('Awaiting TrivenetoBassilichi payment', 'woocommerce_gateway_tvb'));
     // Reduce stock levels
     $order->reduce_order_stock();
     // Remove cart
     $woocommerce->cart->empty_cart();
     // Get the payment URL for the customer:
     $paymentURL = $pg->getPaymentURL_PI();
     // Send to payment URL
     return array('result' => 'success', 'redirect' => $paymentURL);
 }
 function triveneto_response_interface($template)
 {
     global $wp_query;
     // If the 'triveneto_response_interface' query var isn't appended to the URL,
     // don't do anything and return default
     if (!isset($wp_query->query['triveneto_response_interface'])) {
         return $template;
     }
     // .. otherwise,
     if ($wp_query->query['triveneto_response_interface'] == '1') {
         // Load basics
         require_once 'wp/wp-load.php';
         require_once plugin_dir_path(__FILE__) . '/classes/PgConsTriv.php';
         // Check if we have the $_POST vars
         if (!isset($_POST) || empty($_POST)) {
             // if not ... nothing to see here
             header('Location:' . get_home_url());
         }
         // Log the $_POST vars received
         $postvars = print_r($_POST, true);
         PgConsTriv::triveneto_log('[PostVars] ' . $postvars);
         // Log Errors if any
         if (isset($_POST['Error']) && isset($_POST['ErrorText'])) {
             // Get vars
             $Error = $_POST['Error'];
             $ErrorText = $_POST['ErrorText'];
             // record to log
             PgConsTriv::triveneto_log('Detected error: ' . $Error . ' => ' . $ErrorText);
         }
         // Process the order
         if (isset($_POST['trackid'])) {
             // Get vars
             $trackid = intval($_POST['trackid']);
             // Create the Order object
             $order = new WC_Order($trackid);
             // Mark as 'Processing'
             $order->update_status('processing', __('Received successful TrivenetoBassilichi payment', 'woocommerce_gateway_tvb'));
             // log
             PgConsTriv::triveneto_log('Received successful TrivenetoBassilichi payment');
             // Order successful URL
             $url = $order->get_checkout_order_received_url();
             // Command the redirection to the ThankYou page
             echo "REDIRECT=" . $url;
         }
         exit;
     }
     return $template;
 }