// Print the credentials we're using
//print_r($creds);
// This is our current realm
$realm = $creds['qb_realm'];
// Load the OAuth information from the database
if ($Context = $IPP->context()) {
    // Set the DBID
    $IPP->dbid($Context, 'something');
    // Set the IPP flavor
    $IPP->flavor($creds['qb_flavor']);
    // Get the base URL if it's QBO
    if ($creds['qb_flavor'] == QuickBooks_IPP_IDS::FLAVOR_ONLINE) {
        $IPP->baseURL($IPP->getBaseURL($Context, $realm));
    }
    //print('Base URL is [' . $IPP->baseURL() . ']' . "\n\n");
    $CustomerService = new QuickBooks_IPP_Service_Customer();
    $Customer = new QuickBooks_IPP_Object_Customer();
    $Customer->setName('Willy Wonka #' . mt_rand(0, 1000));
    $Customer->setGivenName('Willy');
    $Customer->setFamilyName('Wonka');
    $resp = $CustomerService->add($Context, $realm, $Customer);
    print 'We added a new customer named [' . $Customer->getName() . '] and got back an ID value of [' . $resp . ']' . "\n\n";
    /*
    print("\n\n\n\n");
    print('Request [' . $IPP->lastRequest() . ']');
    print("\n\n\n\n");
    print('Response [' . $IPP->lastResponse() . ']');
    print("\n\n\n\n");
    */
} else {
    die('Unable to load a context...?');
<?php

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/views/header.tpl.php';
?>

<pre>

<?php 
$CustomerService = new QuickBooks_IPP_Service_Customer();
// Get the existing customer first (you need the latest SyncToken value)
$customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer WHERE Id = '34' ");
$Customer = $customers[0];
// Change something
$Customer->setDisplayName('Updated ' . date('Y-m-d H-i-s'));
// Update their email address too
$PrimaryEmailAddr = $Customer->getPrimaryEmailAddr();
$PrimaryEmailAddr->setAddress('*****@*****.**');
// What are we doing?
print 'Updating the customer name to: ' . $Customer->getDisplayName() . '<br>';
if ($CustomerService->update($Context, $realm, $Customer->getId(), $Customer)) {
    print '&nbsp; Updated!<br>';
} else {
    print '&nbsp; Error: ' . $CustomerService->lastError($Context);
}
/*
print('<br><br><br><br>');
print("\n\n\n\n\n\n\n\n");
print('Request [' . $IPP->lastRequest() . ']');
print("\n\n\n\n");
print('Response [' . $IPP->lastResponse() . ']');
<?php 
// Set up the IPP instance
$IPP = new QuickBooks_IPP($dsn);
// Get our OAuth credentials from the database
$creds = $IntuitAnywhere->load($the_username, $the_tenant);
// Tell the framework to load some data from the OAuth store
$IPP->authMode(QuickBooks_IPP::AUTHMODE_OAUTH, $the_username, $creds);
// Print the credentials we're using
//print_r($creds);
// This is our current realm
$realm = $creds['qb_realm'];
// Load the OAuth information from the database
if ($Context = $IPP->context()) {
    // Set the IPP version to v3
    $IPP->version(QuickBooks_IPP_IDS::VERSION_3);
    $CustomerService = new QuickBooks_IPP_Service_Customer();
    $customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer ");
    foreach ($customers as $Customer) {
        //print_r($Customer);
        print 'Customer #' . $Customer->getXPath('//Customer/Id') . "\n";
        print '  Phone: ' . $Customer->getXPath('//Customer/PrimaryPhone/FreeFormNumber') . "\n";
        print '  Email: ' . $Customer->getXPath('//Customer/PrimaryEmailAddr/Address') . "\n\n";
    }
    /*
    print("\n\n\n\n");
    print('Request [' . $IPP->lastRequest() . ']');
    print("\n\n\n\n");
    print('Response [' . $IPP->lastResponse() . ']');
    print("\n\n\n\n");
    */
} else {
<?php

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/views/header.tpl.php';
?>

<pre>

<?php 
// Jobs are really just Customers, so we can use the CustomerService and Customer query methods to do this
$CustomerService = new QuickBooks_IPP_Service_Customer();
// Get all jobs that have a parent customer "Derrick Huckleberry"
$jobs = $CustomerService->query($Context, $realm, "SELECT * FROM Customer WHERE FullyQualifiedName LIKE 'Derrick Huckleberry:%' ");
//print_r($customers);
foreach ($jobs as $Job) {
    print 'Job Id=' . $Job->getId() . ' is named: ' . $Job->getFullyQualifiedName() . '<br>';
}
/*
print("\n\n\n\n");
print('Request [' . $CustomerService->lastRequest() . ']');
print("\n\n\n\n");
print('Response [' . $CustomerService->lastResponse() . ']');
print("\n\n\n\n");
*/
?>

</pre>

<?php 
require_once dirname(__FILE__) . '/views/footer.tpl.php';
<?php

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/views/header.tpl.php';
?>

<pre>

<?php 
$CustomerService = new QuickBooks_IPP_Service_Customer();
$customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer WHERE FullyQualifiedName LIKE '%Keith O\\'Mally%' ");
//print_r($customers);
foreach ($customers as $Customer) {
    print 'Customer Id=' . $Customer->getId() . ' is named: ' . $Customer->getFullyQualifiedName() . '<br>';
}
print "\n\n\n\n";
print 'Request [' . $CustomerService->lastRequest() . ']';
print "\n\n\n\n";
print 'Response [' . $CustomerService->lastResponse() . ']';
print "\n\n\n\n";
?>

</pre>

<?php 
require_once dirname(__FILE__) . '/views/footer.tpl.php';
<?php

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/views/header.tpl.php';
?>

<pre>

<?php 
$Payments = new QuickBooks_Payments($oauth_consumer_key, $oauth_consumer_secret, $sandbox);
$CustomerService = new QuickBooks_IPP_Service_Customer();
$customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer MAXRESULTS 5");
foreach ($customers as $Customer) {
    print 'Customer Id=' . $Customer->getId() . ' is named: ' . $Customer->getFullyQualifiedName() . '<br>';
    $result = $Payments->getCards($Context, $Customer->getId());
}
print "\n\n\n\n";
print 'Request [' . $CustomerService->lastRequest() . ']';
print "\n\n\n\n";
print 'Response [' . $CustomerService->lastResponse() . ']';
print "\n\n\n\n";
print 'Error [' . $CustomerService->lastError() . ']';
print "\n\n\n\n";
?>

</pre>

<?php 
require_once dirname(__FILE__) . '/views/footer.tpl.php';
<?php 
// Set up the IPP instance
$IPP = new QuickBooks_IPP($dsn);
// Get our OAuth credentials from the database
$creds = $IntuitAnywhere->load($the_username, $the_tenant);
// Tell the framework to load some data from the OAuth store
$IPP->authMode(QuickBooks_IPP::AUTHMODE_OAUTH, $the_username, $creds);
// Print the credentials we're using
//print_r($creds);
// This is our current realm
$realm = $creds['qb_realm'];
// Load the OAuth information from the database
if ($Context = $IPP->context()) {
    // Set the IPP version to v3
    $IPP->version(QuickBooks_IPP_IDS::VERSION_3);
    $CustomerService = new QuickBooks_IPP_Service_Customer();
    $Customer = new QuickBooks_IPP_Object_Customer();
    $Customer->setTitle('Ms');
    $Customer->setGivenName('Shannon');
    $Customer->setMiddleName('B');
    $Customer->setFamilyName('Palmer');
    $Customer->setDisplayName('Shannon B Palmer ' . mt_rand(0, 1000));
    // Terms (e.g. Net 30, etc.)
    $Customer->setSalesTermRef(4);
    // Phone #
    $PrimaryPhone = new QuickBooks_IPP_Object_PrimaryPhone();
    $PrimaryPhone->setFreeFormNumber('860-532-0089');
    $Customer->setPrimaryPhone($PrimaryPhone);
    // Mobile #
    $Mobile = new QuickBooks_IPP_Object_Mobile();
    $Mobile->setFreeFormNumber('860-532-0089');
// Print the credentials we're using
//print_r($creds);
// This is our current realm
$realm = $creds['qb_realm'];
// Load the OAuth information from the database
if ($Context = $IPP->context()) {
    // Set the DBID
    $IPP->dbid($Context, 'something');
    // Set the IPP flavor
    $IPP->flavor($creds['qb_flavor']);
    // Get the base URL if it's QBO
    if ($creds['qb_flavor'] == QuickBooks_IPP_IDS::FLAVOR_ONLINE) {
        $IPP->baseURL($IPP->getBaseURL($Context, $realm));
    }
    //print('Base URL is [' . $IPP->baseURL() . ']' . "\n\n");
    $CustomerService = new QuickBooks_IPP_Service_Customer();
    $perpage = 10;
    $page = 1;
    do {
        print '<h3>PAGE ' . $page . '</h3>' . "\n\n";
        $list = $CustomerService->findAll($Context, $realm, null, $page, $perpage);
        foreach ($list as $Customer) {
            print '&bull; ' . $Customer->getId() . ' => ' . $Customer->getName() . '<br>' . "\n\n";
        }
        $page++;
    } while (count($list) >= $perpage);
    /*
    print("\n\n\n\n");
    print('Request [' . $IPP->lastRequest() . ']');
    print("\n\n\n\n");
    print('Response [' . $IPP->lastResponse() . ']');
<?php 
// Set up the IPP instance
$IPP = new QuickBooks_IPP($dsn);
// Get our OAuth credentials from the database
$creds = $IntuitAnywhere->load($the_username, $the_tenant);
// Tell the framework to load some data from the OAuth store
$IPP->authMode(QuickBooks_IPP::AUTHMODE_OAUTH, $the_username, $creds);
// Print the credentials we're using
//print_r($creds);
// This is our current realm
$realm = $creds['qb_realm'];
// Load the OAuth information from the database
if ($Context = $IPP->context()) {
    // Set the IPP version to v3
    $IPP->version(QuickBooks_IPP_IDS::VERSION_3);
    $CustomerService = new QuickBooks_IPP_Service_Customer();
    $count = $CustomerService->query($Context, $realm, "SELECT COUNT(*) FROM Customer  ");
    print 'There are a total of ' . $count . ' customers!';
    /*
    print("\n\n\n\n");
    print('Request [' . $CustomerService->lastRequest() . ']');
    print("\n\n\n\n");
    print('Response [' . $CustomerService->lastResponse() . ']');
    print("\n\n\n\n");
    */
} else {
    die('Unable to load a context...?');
}
?>

</pre>
 /**
  * Test to see if a connection actually works (make sure you haven't been disconnected on Intuit's end)
  *
  */
 public function test($app_username, $app_tenant)
 {
     if ($creds = $this->load($app_username, $app_tenant)) {
         $IPP = new QuickBooks_IPP();
         $IPP->authMode(QuickBooks_IPP::AUTHMODE_OAUTH, $app_username, $creds);
         if ($Context = $IPP->context()) {
             // Set the DBID
             $IPP->dbid($Context, 'something');
             // Set the IPP flavor
             $IPP->flavor($creds['qb_flavor']);
             // Get the base URL if it's QBO
             if ($creds['qb_flavor'] == QuickBooks_IPP_IDS::FLAVOR_ONLINE) {
                 $cur_version = $IPP->version();
                 $IPP->version(QuickBooks_IPP_IDS::VERSION_3);
                 // Need v3 for this
                 $CustomerService = new QuickBooks_IPP_Service_Customer();
                 $customers = $CustomerService->query($Context, $creds['qb_realm'], "SELECT * FROM Customer MAXRESULTS 1");
                 $IPP->version($cur_version);
                 // Revert back to whatever they set
                 //$IPP->baseURL($IPP->getBaseURL($Context, $creds['qb_realm']));
             } else {
                 $companies = $IPP->getAvailableCompanies($Context);
             }
             //print('[[' . $IPP->lastRequest() . ']]' . "\n\n");
             //print('[[' . $IPP->lastResponse() . ']]' . "\n\n");
             //print('here we are! [' . $IPP->errorCode() . ']');
             // Check the last error code now...
             if ($IPP->errorCode() == 401 or $IPP->errorCode() == 3200) {
                 return false;
             }
             return true;
         }
     }
     return false;
 }
function update_payment_gateways_quickbooks($GatewayName = 'paytabs')
{
    /* include dirname(__FILE__).'/php-qb/quickbooks-php-master/docs/partner_platform/example_app_ipp_v3/config.php';*/
    include '../php-qb/quickbooks-php-master/docs/partner_platform/example_app_ipp_v3-dev/config.php';
    date_default_timezone_set('Asia/Dubai');
    echo "<br>";
    if ($GatewayName == 'paytabs') {
        $OrderNumber = $_REQUEST['reference_id'] . '-' . rand();
        $TotalAmount = $_REQUEST['amount'];
        $Currency = $_REQUEST['currency'];
        $customer_telephone = $_REQUEST['phone_num'];
        $shipping_city = $_REQUEST['shipping_city'];
        $shipping_region = $_REQUEST['shipping_state'];
        $shipping_street = $_REQUEST['shipping_address'];
        $shipping_country = $_REQUEST['shipping_country'];
        $order_currency_code = $_REQUEST['currency'];
        /*  GETTING CUSTOMER INFO*/
        $customer_fullname = $_REQUEST['customer_name'] . rand();
        $customer_fullname = explode(' ', $customer_fullname);
        $customer_firstname = $customer_fullname[0];
        $customer_lastname = $customer_fullname[1];
        $customer_email = $_REQUEST['email'];
        $order_number = $_REQUEST['reference_id'];
    }
    //END IF PAYTABS
    $order = Mage::getModel('sales/order')->load($order_number, 'increment_id');
    //CHECK THIS CUSTOMER ALREADY IN QB OR NO
    $db->where("DisplayName", "{$customer_firstname} {$customer_lastname}");
    $user = $db->getOne("qb_example_customer");
    echo $db->getLastQuery();
    if ($db->count > 0) {
        $count_customr = $db->count;
        $qb_customer_id = $user['ListID'];
        /////if customer found then check his existing currency from qb if its diffrent from orderd currency
        //IF ORDERD CURRENCY IS DIFFERENT FROM CUTOMER CURRENCY CHECK THIS CUSTOMER IN QB OR NO
        if ($user['Currency'] != $order_currency_code) {
            //CHECK THIS CUSIMER EXIST WITH CURRNCY PREFIX
            $db->where("DisplayName", "{$customer_firstname} {$customer_lastname}-{$order_currency_code}");
            $user = $db->getOne("qb_customers");
            $count_customr = $db->count;
            if ($db->count > 0) {
                $qb_customer_id = $user['qb_customer_id'];
                $data = array('CustomerId' => $order['customer_id'], "Currency" => $order_currency_code);
                $db->where('ListID', $qb_customer_id);
                $db->update('qb_example_customer', $data);
            }
        } else {
            $data = array('CustomerId' => $order['customer_id'], "Currency" => $order_currency_code);
            $db->where('ListID', $qb_customer_id);
            $db->update('qb_example_customer', $data);
        }
    } else {
        //IF NOT FOUND IN OUR TABLE THEN WE HAVE TO DO CHECK SAME PERSON WITH  CURRENCY IN DISPLAY NAME
        $db->where("DisplayName", "{$customer_firstname} {$customer_lastname}-{$order_currency_code}");
        $user = $db->getOne("qb_example_customer");
        $count_customr = $db->count;
        if ($db->count > 0) {
            $qb_customer_id = $user['qb_customer_id'];
            $data = array('CustomerId' => $order['customer_id'], "Currency" => $order_currency_code);
            $db->where('ListID', $qb_customer_id);
            $db->update('qb_example_customer', $data);
        }
    }
    //if not found then insert in qb and in return insert id from qb to local database
    if ($count_customr == 0) {
        ////QICKBOOK ADD CUSTOMER
        $CustomerService = new QuickBooks_IPP_Service_Customer();
        $Customer = new QuickBooks_IPP_Object_Customer();
        $Customer->setGivenName($customer_firstname);
        $Customer->setFamilyName($customer_lastname);
        $Customer->setDisplayName($customer_firstname . ' ' . $customer_lastname . '-' . $order_currency_code);
        // Phone #
        $PrimaryPhone = new QuickBooks_IPP_Object_PrimaryPhone();
        $PrimaryPhone->setFreeFormNumber($customer_telephone);
        $Customer->setPrimaryPhone($PrimaryPhone);
        // Mobile #
        $Mobile = new QuickBooks_IPP_Object_Mobile();
        $Mobile->setFreeFormNumber($customer_telephone);
        $Customer->setMobile($Mobile);
        // Fax #
        $Fax = new QuickBooks_IPP_Object_Fax();
        $Fax->setFreeFormNumber($customer_telephone);
        $Customer->setFax($Fax);
        // Bill address
        $BillAddr = new QuickBooks_IPP_Object_BillAddr();
        $BillAddr->setLine1($shipping_street);
        $BillAddr->setCity($shipping_city);
        $BillAddr->setCountrySubDivisionCode($shipping_country);
        $Customer->setBillAddr($BillAddr);
        $Customer->setCurrencyRef($order_currency_code);
        // Email
        $PrimaryEmailAddr = new QuickBooks_IPP_Object_PrimaryEmailAddr();
        $PrimaryEmailAddr->setAddress($customer_email);
        $Customer->setPrimaryEmailAddr($PrimaryEmailAddr);
        if ($resp = $CustomerService->add($Context, $realm, $Customer)) {
            $resp = str_replace('-', '', $resp);
            $resp = str_replace('{', '', $resp);
            $resp = str_replace('}', '', $resp);
            echo "<br>";
            print 'Our new customer ID is: [' . $resp . '] (name "' . $Customer->getDisplayName() . '")';
            $data = array("CustomerId" => $order['customer_id'], "ListID" => $resp, "FullName" => $customer_firstname . ' ' . $customer_lastname, "FirstName" => $customer_firstname, "LastName" => $customer_lastname, "Email" => $customer_email, "DisplayName" => $customer_firstname . ' ' . $customer_lastname . '-' . $order_currency_code, "Currency" => $order_currency_code);
            $id = $db->insert('qb_example_customer', $data);
            $qb_customer_id = $resp;
        } else {
            print $CustomerService->lastError($Context);
        }
    }
    if ($qb_customer_id != 0) {
        ////CREATING PAMENTS
        $total = $TotalAmount;
        $PaymentService = new QuickBooks_IPP_Service_Payment();
        // Create payment object
        $Payment = new QuickBooks_IPP_Object_Payment();
        $Payment->setPaymentRefNum($OrderNumber);
        $Payment->setTxnDate(date('Y-m-d'));
        $Payment->setTotalAmt($total);
        //6 for paytabs
        $Payment->setPaymentMethodRef('6');
        ///CHART OF ACCOUNT REF///
        if ($GatewayName == 'paytabs') {
            /* 
                ID=59 NAME="PT-Paytabs-SAR"
                ID=57 NAME="PT-Paytabs-AED"
                ID=55 NAME="PT-Paytabs-USD"
            */
            if ($order_currency_code == 'USD') {
                $Payment->setDepositToAccountRef('55');
            }
            if ($order_currency_code == 'AED') {
                $Payment->setDepositToAccountRef('57');
            }
            if ($order_currency_code == 'SAR') {
                $Payment->setDepositToAccountRef('59');
            }
        }
        if ($GatewayName == 'paypal') {
            ///ID 58 NAME "PP-Paypal-USD"
            if ($order_currency_code == 'USD') {
                $Payment->setDepositToAccountRef('58');
            }
        }
        $Payment->setCustomerRef($qb_customer_id);
        // Send payment to QBO
        if ($resp = $PaymentService->add($Context, $realm, $Payment)) {
            print 'Our new Payment ID is: [' . $resp . ']';
        } else {
            print $PaymentService->lastError();
        }
    }
}
<?php 
// Set up the IPP instance
$IPP = new QuickBooks_IPP($dsn);
// Get our OAuth credentials from the database
$creds = $IntuitAnywhere->load($the_username, $the_tenant);
// Tell the framework to load some data from the OAuth store
$IPP->authMode(QuickBooks_IPP::AUTHMODE_OAUTH, $the_username, $creds);
// Print the credentials we're using
//print_r($creds);
// This is our current realm
$realm = $creds['qb_realm'];
// Load the OAuth information from the database
if ($Context = $IPP->context()) {
    // Set the IPP version to v3
    $IPP->version(QuickBooks_IPP_IDS::VERSION_3);
    $CustomerService = new QuickBooks_IPP_Service_Customer();
    $InvoiceService = new QuickBooks_IPP_Service_Invoice();
    $customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer WHERE FamilyName = 'Palmer' ");
    //print_r($customers);
    if (count($customers)) {
        foreach ($customers as $Customer) {
            print 'Customer Id=' . $Customer->getId() . ' is named: ' . $Customer->getFullyQualifiedName() . '<br>';
            $invoices = $InvoiceService->query($Context, $realm, "SELECT * FROM Invoice WHERE CustomerRef = '" . QuickBooks_IPP_IDS::usableIDType($Customer->getId()) . "' ");
            /*
            print("\n\n\n\n");
            print('Request [' . $IPP->lastRequest() . ']');
            print("\n\n\n\n");
            print('Response [' . $IPP->lastResponse() . ']');
            print("\n\n\n\n");
            exit;
            */
header('Content-Type: text/plain');
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/Users/kpalmer/Projects/QuickBooks/');
require_once '../QuickBooks.php';
//
$username = '******';
$password = '******';
$token = 'tex3r7hwifx6cci3zk43ibmnd';
$realmID = 173642438;
//
$IPP = new QuickBooks_IPP();
if ($Context = $IPP->authenticate($username, $password, $token)) {
    $IPP->application($Context, 'be9mh7qd5');
    $user = $IPP->getUserInfo($Context);
    print_r($user);
    //exit;
    $Service = new QuickBooks_IPP_Service_Customer();
    if ($list = $Service->findAll($Context, $realmID)) {
        //print_r($list);
        foreach ($list as $Customer) {
            print 'Name is [' . $Customer->getName() . ']' . "\n";
        }
    }
    //print($Service->lastRequest());
    //print($Service->lastResponse());
} else {
    print 'Auth failed!';
}
exit;
<?php

require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/views/header.tpl.php';
?>

<pre>

<?php 
$CustomerService = new QuickBooks_IPP_Service_Customer();
$customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer MAXRESULTS 25");
foreach ($customers as $Customer) {
    print 'Customer Id=' . $Customer->getId() . ' is named: ' . $Customer->getFullyQualifiedName() . '<br>';
}
/*
print("\n\n\n\n");
print('Request [' . $CustomerService->lastRequest() . ']');
print("\n\n\n\n");
print('Response [' . $CustomerService->lastResponse() . ']');
print("\n\n\n\n");
print('Error [' . $CustomerService->lastError() . ']');
print("\n\n\n\n");
*/
?>

</pre>

<?php 
require_once dirname(__FILE__) . '/views/footer.tpl.php';
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
require_once '../QuickBooks.php';
//
$username = '******';
$password = '******';
$token = 'tex3r7hwifx6cci3zk43ibmnd';
$realmID = 173642438;
//
$IPP = new QuickBooks_IPP();
$Context = $IPP->authenticate($username, $password, $token);
$IPP->application($Context, 'be9mh7qd5');
$IPP->useIDSParser(false);
$CustomerService = new QuickBooks_IPP_Service_Customer();
$CheckService = new QuickBooks_IPP_Service_Check();
$InvoiceService = new QuickBooks_IPP_Service_Invoice();
$EstimateService = new QuickBooks_IPP_Service_Estimate();
// 4791075
// 4792532
// 4792533
$xml = '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<Del RequestId="' . md5(microtime()) . '" xmlns="http://www.intuit.com/sb/cdm/v2">
   <Object xsi:type="Check"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Id idDomain="NG">4800355</Id>
      </Object>
</Del>';
$response = $CheckService->rawQuery($Context, $realmID, $xml);
print $CheckService->lastRequest($Context);
function update_quickbooks($GatewayName = 'paytabs', $OrderNumber = '', $PaymentTotal = 0, $Currency = '', $PaymentOnly = true)
{
    include dirname(__FILE__) . '/php-qb/quickbooks-php-master/docs/partner_platform/example_app_ipp_v3/config.php';
    /* include '../php-qb/quickbooks-php-master/docs/partner_platform/example_app_ipp_v3/config.php';*/
    date_default_timezone_set('Asia/Dubai');
    $order = Mage::getModel('sales/order')->load($OrderNumber, 'increment_id');
    $Invoice = 0;
    $estimate_id = 0;
    $qb_customer_id = 0;
    /*
    echo $OrderNumber='test-'.rand();*/
    echo "<br>";
    /*  GETTING SHIPPING INFO*/
    $shippingAddress = $order->getShippingAddress();
    $country_id = $shippingAddress['country_id'];
    $countryName = Mage::getModel('directory/country')->load($country_id)->getName();
    $customer_telephone = $shippingAddress['telephone'];
    $shipping_city = $shippingAddress['city'];
    $shipping_region = $shippingAddress['region'];
    $shipping_street = $shippingAddress['street'];
    $shipping_country = $countryName;
    $shipping_company = $shippingAddress['company'];
    /*  GETTING CUSTOMER INFO*/
    $customer_firstname = $order['customer_firstname'];
    $customer_lastname = $order['customer_lastname'];
    $customer_email = $order['customer_email'];
    //$order_currency_code = $order['order_currency_code'];
    $billingAddress = $order->getBillingAddress();
    if ($order['customer_firstname'] == '') {
        $customer_firstname = $billingAddress->firstname;
        $customer_lastname = $billingAddress->lastname;
        $customer_email = $billingAddress->email;
    }
    $order_currency_code = $Currency;
    /*GETTING ORDER ITEMS*/
    $items = $order->getAllVisibleItems();
    //CHECK THIS CUSTOMER ALREADY IN QB OR NO
    $db->where("display_name", "{$customer_firstname} {$customer_lastname}");
    $user = $db->getOne("qb_customers");
    if ($db->count > 0) {
        $count_customr = $db->count;
        $qb_customer_id = $user['qb_customer_id'];
        /////if customer found then check his existing currency from qb if its diffrent from orderd currency
        //IF ORDERD CURRENCY IS DIFFERENT FROM CUTOMER CURRENCY CHECK THIS CUSTOMER IN QB OR NO
        if ($user['currency'] != $order_currency_code) {
            //CHECK THIS CUSIMER EXIST WITH CURRNCY PREFIX
            $db->where("display_name", "{$customer_firstname} {$customer_lastname}-{$order_currency_code}");
            $user = $db->getOne("qb_customers");
            $count_customr = $db->count;
            if ($db->count > 0) {
                $qb_customer_id = $user['qb_customer_id'];
                $data = array('customer_id' => $order['customer_id'], "currency" => $order_currency_code);
                $db->where('qb_customer_id', $qb_customer_id);
                $db->update('qb_customers', $data);
            }
        } else {
            $data = array('customer_id' => $order['customer_id'], "currency" => $order_currency_code);
            $db->where('qb_customer_id', $qb_customer_id);
            $db->update('qb_customers', $data);
        }
    } else {
        //IF NOT FOUND IN OUR TABLE THEN WE HAVE TO DO CHECK SAME PERSON WITH  CURRENCY IN DISPLAY NAME
        $db->where("display_name", "{$customer_firstname} {$customer_lastname}-{$order_currency_code}");
        $user = $db->getOne("qb_customers");
        $count_customr = $db->count;
        if ($db->count > 0) {
            $qb_customer_id = $user['qb_customer_id'];
            $data = array('customer_id' => $order['customer_id'], "currency" => $order_currency_code);
            $db->where('qb_customer_id', $qb_customer_id);
            $db->update('qb_customers', $data);
        }
    }
    //if not found then insert in qb and in return insert id from qb to local database
    if ($count_customr == 0) {
        ////QICKBOOK ADD CUSTOMER
        $CustomerService = new QuickBooks_IPP_Service_Customer();
        $Customer = new QuickBooks_IPP_Object_Customer();
        $Customer->setGivenName($customer_firstname);
        $Customer->setFamilyName($customer_lastname);
        $Customer->setDisplayName($customer_firstname . ' ' . $customer_lastname . '-' . $order_currency_code);
        // Phone #
        $PrimaryPhone = new QuickBooks_IPP_Object_PrimaryPhone();
        $PrimaryPhone->setFreeFormNumber($customer_telephone);
        $Customer->setPrimaryPhone($PrimaryPhone);
        // Mobile #
        $Mobile = new QuickBooks_IPP_Object_Mobile();
        $Mobile->setFreeFormNumber($customer_telephone);
        $Customer->setMobile($Mobile);
        // Fax #
        $Fax = new QuickBooks_IPP_Object_Fax();
        $Fax->setFreeFormNumber($customer_telephone);
        $Customer->setFax($Fax);
        // Bill address
        $BillAddr = new QuickBooks_IPP_Object_BillAddr();
        $BillAddr->setLine1($shipping_street);
        $BillAddr->setCity($shipping_city);
        $BillAddr->setCountrySubDivisionCode($shipping_country);
        $Customer->setBillAddr($BillAddr);
        $Customer->setCurrencyRef($order_currency_code);
        // Email
        $PrimaryEmailAddr = new QuickBooks_IPP_Object_PrimaryEmailAddr();
        $PrimaryEmailAddr->setAddress($customer_email);
        $Customer->setPrimaryEmailAddr($PrimaryEmailAddr);
        if ($resp = $CustomerService->add($Context, $realm, $Customer)) {
            $resp = str_replace('-', '', $resp);
            $resp = str_replace('{', '', $resp);
            $resp = str_replace('}', '', $resp);
            echo "<br>";
            print 'Our new customer ID is: [' . $resp . '] (name "' . $Customer->getDisplayName() . '")';
            $data = array("customer_id" => $order['customer_id'], "qb_customer_id" => $resp, "customer_firstname" => $customer_firstname, "customer_lastname" => $customer_lastname, "customer_email" => $customer_email, "display_name" => $customer_firstname . ' ' . $customer_lastname . '-' . $order_currency_code, "currency" => $order_currency_code);
            $id = $db->insert('qb_customers', $data);
            $qb_customer_id = $resp;
        } else {
            print $CustomerService->lastError($Context);
        }
    }
    //if not papal or paytabs else we just make direct payment
    if (!$PaymentOnly) {
        if ($qb_customer_id != 0) {
            //CREATING ESTIMATE///
            $EstimateService = new QuickBooks_IPP_Service_Estimate();
            $Estimate = new QuickBooks_IPP_Object_Estimate();
            $Estimate->setDocNumber($OrderNumber);
            $Estimate->setTxnDate(date('Y-m-d'));
            ///loop for each record for order items
            foreach ($items as $_item) {
                $newskus = $_item->getSku();
                $newdescs = $_item->getName();
                $sku = $_product["sku"];
                $qty = $_item->getQtyOrdered();
                $item_price = round($_item['price']);
                $sub_total = round($_item['row_total']);
                $OrderSkuArray = split('-', $newskus);
                if (count($OrderSkuArray) > 1) {
                    $first_sku = $OrderSkuArray[0];
                    $d = $OrderSkuArray[1];
                    //this is the logic where we check if item buy with accessories
                    foreach ($OrderSkuArray as $OrderSkuArrayKey => $OrderSkuArrayValue) {
                        $skuq = "SELECT *  FROM `catalog_product_entity` WHERE sku='{$OrderSkuArrayValue}'";
                        $squery = mysqli_query($kdbcon, $skuq);
                        $squerycount = mysqli_num_rows($squery);
                        if ($squerycount == 0) {
                            unset($OrderSkuArray[0]);
                        }
                    }
                }
                $d = 0;
                //ADD ITEMS IN ESTIMATE IF ONE ORDER CONTAIN MULTIPLE SKUS FIRST SKU WILL ADD WITH PRICE
                foreach ($OrderSkuArray as $OrderSkuArrayKey => $OrderSkuArrayValue) {
                    //CHECK THIS ITEM ALREADY IN QB OR NO
                    $db->where("Name like '%{$OrderSkuArrayValue}%'");
                    $item_array = $db->getOne("qb_example_item");
                    if ($db->count > 0) {
                        $qb_item_id = $item_array['ListID'];
                        $qb_item_desc = $item_array['SalesDesc'];
                    } else {
                        $ItemService = new QuickBooks_IPP_Service_Item();
                        $Item = new QuickBooks_IPP_Object_Item();
                        $Item->setName($OrderSkuArrayValue);
                        $Item->setType('Inventory');
                        $Item->setSku($OrderSkuArrayValue);
                        $Item->setAssetAccountRef('83');
                        //83 = Inventory Asset
                        $Item->setInvStartDate(date('Y-m-d'));
                        $Item->setQtyOnHand($qty);
                        ///1 is sales account
                        $Item->setIncomeAccountRef('1');
                        if ($resp = $ItemService->add($Context, $realm, $Item)) {
                            $resp = str_replace('-', '', $resp);
                            $resp = str_replace('{', '', $resp);
                            $resp = str_replace('}', '', $resp);
                            $data = array("ListID" => $resp, "Name" => $OrderSkuArrayValue, "FullName" => $OrderSkuArrayValue);
                            $id = $db->insert('qb_example_item', $data);
                            echo "<br>";
                            print 'Our new Item ID is: [' . $resp . ']';
                            $qb_item_id = $resp;
                        } else {
                            print $ItemService->lastError($Context);
                        }
                        $qb_item_desc = '--------------------';
                    }
                    ///IF MULTIPLE SKUS IN ONE ITEM THEN WE WILL ADD PRICE WITH FIRST SKU IN ESTIMATE
                    if ($d != 0) {
                        $item_price = $sub_total = 0;
                    }
                    $qty = round($qty);
                    echo "<br>";
                    echo "sku={$OrderSkuArrayValue}&amount={$item_price}&qty={$qty}&sub_total={$sub_total}&qb_item_desc={$qb_item_desc}<br>";
                    ///add EACH ITE
                    $Line = new QuickBooks_IPP_Object_Line();
                    $Line->setDetailType('SalesItemLineDetail');
                    $Line->setAmount($item_price * $qty);
                    $Line->setDescription($qb_item_desc);
                    $SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
                    $SalesItemLineDetail->setUnitPrice($item_price);
                    $SalesItemLineDetail->setItemRef($qb_item_id);
                    $SalesItemLineDetail->setItemName($OrderSkuArrayValue);
                    $SalesItemLineDetail->setQty($qty);
                    $Line->addSalesItemLineDetail($SalesItemLineDetail);
                    $Estimate->addLine($Line);
                    $d++;
                }
            }
            ///END FIRST ITEM LOOP FOR PRODUCTS
            ///DISCOUNT AND SHIPPING AND TAX WILL ADD AS ITEM IN ESTIMATE
            /// ITEM ID 1075 ITEM NAME SHIPPING & HANDLING
            if ($order['shipping_incl_tax'] > 0) {
                $Line = new QuickBooks_IPP_Object_Line();
                $Line->setDetailType('SalesItemLineDetail');
                $Line->setAmount($order['shipping_incl_tax']);
                $SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
                $SalesItemLineDetail->setUnitPrice($order['shipping_incl_tax']);
                $SalesItemLineDetail->setItemRef('1075');
                $SalesItemLineDetail->setQty('1');
                $Line->addSalesItemLineDetail($SalesItemLineDetail);
                $Estimate->addLine($Line);
            }
            /// ITEM ID 1075 ITEM NAME SHIPPING & HANDLING
            if ($order['discount_invoiced'] > 0) {
                $Line = new QuickBooks_IPP_Object_Line();
                $Line->setDetailType('SalesItemLineDetail');
                $Line->setAmount(-$order['discount_invoiced']);
                $SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
                $SalesItemLineDetail->setUnitPrice(-$order['discount_invoiced']);
                $SalesItemLineDetail->setItemRef('1076');
                $SalesItemLineDetail->setQty('1');
                $Line->addSalesItemLineDetail($SalesItemLineDetail);
                $Estimate->addLine($Line);
            }
            //ASSIGNING CUSTOMER TO ESTIMATE
            $Estimate->setCustomerRef($qb_customer_id);
            if ($resp = $EstimateService->add($Context, $realm, $Estimate)) {
                $resp = str_replace('-', '', $resp);
                $resp = str_replace('{', '', $resp);
                $estimate_id = str_replace('}', '', $resp);
                echo "<br>";
                print 'Our new Estimate ID is: [' . $resp . ']';
                echo "<br>";
                echo "Estimate Created in QuickBooks for Customer " . $customer_firstname . ' ' . $customer_lastname;
            } else {
                print $EstimateService->lastError();
            }
        }
        if ($estimate_id != 0) {
            ////CREATE INVOICE///
            $InvoiceService = new QuickBooks_IPP_Service_Invoice();
            $Invoice = new QuickBooks_IPP_Object_Invoice();
            $Invoice->setDocNumber($OrderNumber);
            $Invoice->setTxnDate(date('Y-m-d'));
            ///loop for each record for order items
            foreach ($items as $_item) {
                $newskus = $_item->getSku();
                $newdescs = $_item->getName();
                $sku = $_product["sku"];
                $qty = $_item->getQtyOrdered();
                $item_price = round($_item['price']);
                $sub_total = round($_item['row_total']);
                $OrderSkuArray = split('-', $newskus);
                if (count($OrderSkuArray) > 1) {
                    $first_sku = $OrderSkuArray[0];
                    $d = $OrderSkuArray[1];
                    //this is the logic where we check if item buy with accessories
                    foreach ($OrderSkuArray as $OrderSkuArrayKey => $OrderSkuArrayValue) {
                        $skuq = "SELECT *  FROM `catalog_product_entity` WHERE sku='{$OrderSkuArrayValue}'";
                        $squery = mysqli_query($kdbcon, $skuq);
                        $squerycount = mysqli_num_rows($squery);
                        if ($squerycount == 0) {
                            unset($OrderSkuArray[0]);
                        }
                    }
                }
                $d = 0;
                //ADD ITEMS IN ESTIMATE IF ONE ORDER CONTAIN MULTIPLE SKUS FIRST SKU WILL ADD WITH PRICE
                foreach ($OrderSkuArray as $OrderSkuArrayKey => $OrderSkuArrayValue) {
                    //CHECK THIS ITEM ALREADY IN QB OR NO
                    $db->where("Name like '%{$OrderSkuArrayValue}%'");
                    $item_array = $db->getOne("qb_example_item");
                    if ($db->count > 0) {
                        $qb_item_id = $item_array['ListID'];
                    }
                    $Line = new QuickBooks_IPP_Object_Line();
                    $Line->setDetailType('SalesItemLineDetail');
                    $Line->setAmount($item_price * $qty);
                    $Line->setDescription($qb_item_desc);
                    $SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
                    $SalesItemLineDetail->setUnitPrice($item_price);
                    $SalesItemLineDetail->setItemRef($qb_item_id);
                    $SalesItemLineDetail->setItemName($OrderSkuArrayValue);
                    $SalesItemLineDetail->setQty($qty);
                    $Line->addSalesItemLineDetail($SalesItemLineDetail);
                    $Invoice->addLine($Line);
                }
            }
            ///DISCOUNT AND SHIPPING AND TAX WILL ADD AS ITEM IN ESTIMATE
            /// ITEM ID 1075 ITEM NAME SHIPPING & HANDLING
            if ($order['shipping_incl_tax'] > 0) {
                $Line = new QuickBooks_IPP_Object_Line();
                $Line->setDetailType('SalesItemLineDetail');
                $Line->setAmount($order['shipping_incl_tax']);
                $SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
                $SalesItemLineDetail->setUnitPrice($order['shipping_incl_tax']);
                $SalesItemLineDetail->setItemRef('1075');
                $SalesItemLineDetail->setQty('1');
                $Line->addSalesItemLineDetail($SalesItemLineDetail);
                $Invoice->addLine($Line);
            }
            /// ITEM ID 1075 ITEM NAME SHIPPING & HANDLING
            if ($order['discount_invoiced'] > 0) {
                $Line = new QuickBooks_IPP_Object_Line();
                $Line->setDetailType('SalesItemLineDetail');
                $Line->setAmount(-$order['discount_invoiced']);
                $SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
                $SalesItemLineDetail->setUnitPrice(-$order['discount_invoiced']);
                $SalesItemLineDetail->setItemRef('1076');
                $SalesItemLineDetail->setQty('1');
                $Line->addSalesItemLineDetail($SalesItemLineDetail);
                $Invoice->addLine($Line);
            }
            $Invoice->setCustomerRef($qb_customer_id);
            if ($resp = $InvoiceService->add($Context, $realm, $Invoice)) {
                $resp = str_replace('-', '', $resp);
                $resp = str_replace('{', '', $resp);
                $invoice_id = str_replace('}', '', $resp);
                print 'Our new Invoice ID is: [' . $resp . ']';
            } else {
                print $InvoiceService->lastError();
            }
        }
    }
    if ($PaymentOnly || $invoice_id != 0) {
        ////CREATING PAMENTS
        $total = $order['shipping_incl_tax'];
        if (!$PaymentOnly) {
            ///loop for each record for order items
            foreach ($items as $_item) {
                $qty = $_item->getQtyOrdered();
                $item_price = round($_item['price']);
                $total += $item_price * $qty;
            }
            $total = $total - $order['discount_invoiced'];
        } else {
            $total = $PaymentTotal;
        }
        $PaymentService = new QuickBooks_IPP_Service_Payment();
        // Create payment object
        $Payment = new QuickBooks_IPP_Object_Payment();
        $Payment->setPaymentRefNum($OrderNumber);
        $Payment->setTxnDate(date('Y-m-d'));
        $Payment->setTotalAmt($total);
        if (!$PaymentOnly) {
            // Create line for payment (this details what it's applied to)
            $Line = new QuickBooks_IPP_Object_Line();
            $Line->setAmount($total);
            $Line->setDescription($qb_item_desc);
            // The line has a LinkedTxn node which links to the actual invoice
            $LinkedTxn = new QuickBooks_IPP_Object_LinkedTxn();
            $LinkedTxn->setTxnId($invoice_id);
            $LinkedTxn->setTxnType('Invoice');
            $Line->setLinkedTxn($LinkedTxn);
            $Payment->addLine($Line);
        }
        $Payment->setPaymentMethodRef('6');
        ///CHART OF ACCOUNT REF///
        if ($GatewayName == 'paytabs') {
            ///name PT-Paytabs.co id 105
            ///name PT-Paytabs-USD id 137
            if ($order_currency_code == 'USD') {
                $Payment->setDepositToAccountRef('137');
            }
            if ($order_currency_code == 'AED' || $order_currency_code == 'SAR') {
                $Payment->setDepositToAccountRef('105');
            }
        }
        if ($GatewayName == 'paypal') {
            ///name PP-Paypal id 110
            if ($order_currency_code == 'USD') {
                $Payment->setDepositToAccountRef('110');
            }
        }
        $Payment->setCustomerRef($qb_customer_id);
        // Send payment to QBO
        if ($resp = $PaymentService->add($Context, $realm, $Payment)) {
            print 'Our new Payment ID is: [' . $resp . ']';
        } else {
            print $PaymentService->lastError();
        }
    }
}