/**
  * Fetches customer from DB or creates it at Fortnox
  *
  * @access public
  * @param $order
  * @return int
  */
 private function get_or_create_customer($order)
 {
     include_once "class-fortnox3-contact-xml.php";
     $databaseInterface = new WCF_Database_Interface();
     $customer = $databaseInterface->get_customer_by_email($order->billing_email);
     //Init API
     $apiInterface = new WCF_API();
     if ($apiInterface->has_error) {
         return $this->ERROR_LOGIN;
     }
     //create Contact XML
     $contactDoc = new WCF_Contact_XML_Document();
     $contactXml = $contactDoc->create($order);
     if (empty($customer) || $customer[0]->customer_number == 0) {
         logthis("CREATING CUSTOMER");
         $customerId = $databaseInterface->create_customer($order->billing_email);
         //send Contact XML
         $contactResponse = $apiInterface->create_customer_request($contactXml);
         if (array_key_exists('Error', $contactResponse)) {
             return $this->handle_error($contactResponse);
         }
         $customerNumber = $contactResponse['CustomerNumber'];
         $databaseInterface->update_customer($customerId, $customerNumber);
     } else {
         logthis("UPDATING CUSTOMER");
         $customerNumber = $customer[0]->customer_number;
         $contactResponse = $apiInterface->update_customer_request($contactXml, $customerNumber);
         if (array_key_exists('Error', $contactResponse)) {
             return $this->handle_error($contactResponse, $link = 'fel-i-kunddatabastabellen');
         }
     }
     return $customerNumber;
 }
function clean_customer_table_callback()
{
    global $wpdb;
    // this is how you get access to the database
    include_once "class-fortnox3-database-interface.php";
    logthis('clean_customer_table_callback');
    check_ajax_referer('fortnox_woocommerce', 'security');
    $databaseInterface = new WCF_Database_Interface();
    $customer_emails = $databaseInterface->clean_customer_table();
    if ($customer_emails) {
        logthis($customer_emails);
        $message = 'Tabell rensad.';
        if (is_array($customer_emails)) {
            $message .= 'För att undvika dubbletter, ta bort dessa kunder i er Fortnox: ';
            foreach ($customer_emails as $email) {
                logthis($email);
                $message .= $email->email . ', ';
            }
            $message = substr($message, 0, strlen($message) - 2);
        }
        echo json_encode(array('success' => true, 'message' => $message));
    } else {
        echo json_encode(array('success' => false, 'message' => 'Tabell rensad.'));
    }
    die;
    // this is required to return a proper result
}