Quickbook Web连接器不会将新客户发送到我的网站

时间:2017-11-07 13:19:18

标签: quickbooks quickbooks-online

我想使用REST API在Quickbook Desktop和我的网站之间交换数据(客户,发票,订单等)。我正在使用quickbook web连接器和Consolibyte PHP SDK(https://github.com/consolibyte/quickbooks-php)来实现此目的。我已经安装了Web连接器,配置好的PHP SDK也在我的配置中加载了web连接器中的example.qwc。

现在,当使用PHP SDK的文件和文件夹(quickbooks-php / docs / web_connector)将新客户添加到我的网站时,我可以为Quickbook Desktop创建新客户。这工作正常现在我正在寻找在Quickbook桌面上创建应该在我的网站上创建的新记录(客户,发票等)。如何将这些数据发送到我的网站

问题:

  1. Web连接器是否自动维护双向通信,或者我需要编写一些PHP Soap Web服务代码以从quickbook桌面读取数据。

  2. 我如何知道在Quick book Desktop上创建新客户,发票,订单。然后我如何将这些新数据发送到我的网站。

1 个答案:

答案 0 :(得分:1)

  

1.网络连接器自动保持双向通信

Web连接器不会自动执行任何操作。

你想要发生任何事情,你需要为其编写代码。 SDK为您处理了许多硬协议,但您仍需要编写一些代码。

  

2.我如何知道新客户,发票,订单是在Quick book Desktop

上创建的

您需要查询QuickBooks以获取该数据。有一些示例向您展示如何执行此操作:

https://github.com/consolibyte/quickbooks-php/blob/master/docs/web_connector/example_web_connector_import.php

基本上,你要去:

<强> 1。在$map变量中添加新内容:

`QUICKBOOKS_IMPORT_INVOICE => array( '_quickbooks_invoice_import_request', '_quickbooks_invoice_import_response' ),`

<强> 2。写下$map变量引用的那两个新函数。

请求函数将返回qbXML请求以查询新发票,响应函数将处理QuickBooks返回给您的新发票的大清单(在下面的示例中,它存储他们在MySQL数据库中):

/**
 * Build a request to import invoices already in QuickBooks into our application
 */
function _quickbooks_invoice_import_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
    // Iterator support (break the result set into small chunks)
    $attr_iteratorID = '';
    $attr_iterator = ' iterator="Start" ';
    if (empty($extra['iteratorID']))
    {
        // This is the first request in a new batch
        $last = _quickbooks_get_last_run($user, $action);
        _quickbooks_set_last_run($user, $action);           // Update the last run time to NOW()

        // Set the current run to $last
        _quickbooks_set_current_run($user, $action, $last);
    }
    else
    {
        // This is a continuation of a batch
        $attr_iteratorID = ' iteratorID="' . $extra['iteratorID'] . '" ';
        $attr_iterator = ' iterator="Continue" ';

        $last = _quickbooks_get_current_run($user, $action);
    }

    // Build the request
    $xml = '<?xml version="1.0" encoding="utf-8"?>
        <?qbxml version="' . $version . '"?>
        <QBXML>
            <QBXMLMsgsRq onError="stopOnError">
                <InvoiceQueryRq ' . $attr_iterator . ' ' . $attr_iteratorID . ' requestID="' . $requestID . '">
                    <MaxReturned>' . QB_QUICKBOOKS_MAX_RETURNED . '</MaxReturned>
                    <ModifiedDateRangeFilter>
                        <FromModifiedDate>' . $last . '</FromModifiedDate>
                    </ModifiedDateRangeFilter>
                    <IncludeLineItems>true</IncludeLineItems>
                    <OwnerID>0</OwnerID>
                </InvoiceQueryRq>   
            </QBXMLMsgsRq>
        </QBXML>';

    return $xml;
}
/** 
 * Handle a response from QuickBooks 
 */
function _quickbooks_invoice_import_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{   
    if (!empty($idents['iteratorRemainingCount']))
    {
        // Queue up another request

        $Queue = QuickBooks_WebConnector_Queue_Singleton::getInstance();
        $Queue->enqueue(QUICKBOOKS_IMPORT_INVOICE, null, QB_PRIORITY_INVOICE, array( 'iteratorID' => $idents['iteratorID'] ));
    }

    // This piece of the response from QuickBooks is now stored in $xml. You 
    //  can process the qbXML response in $xml in any way you like. Save it to 
    //  a file, stuff it in a database, parse it and stuff the records in a 
    //  database, etc. etc. etc. 
    //  
    // The following example shows how to use the built-in XML parser to parse 
    //  the response and stuff it into a database. 

    // Import all of the records
    $errnum = 0;
    $errmsg = '';
    $Parser = new QuickBooks_XML_Parser($xml);
    if ($Doc = $Parser->parse($errnum, $errmsg))
    {
        $Root = $Doc->getRoot();
        $List = $Root->getChildAt('QBXML/QBXMLMsgsRs/InvoiceQueryRs');

        foreach ($List->children() as $Invoice)
        {
            $arr = array(
                'TxnID' => $Invoice->getChildDataAt('InvoiceRet TxnID'),
                'TimeCreated' => $Invoice->getChildDataAt('InvoiceRet TimeCreated'),
                'TimeModified' => $Invoice->getChildDataAt('InvoiceRet TimeModified'),
                'RefNumber' => $Invoice->getChildDataAt('InvoiceRet RefNumber'),
                'Customer_ListID' => $Invoice->getChildDataAt('InvoiceRet CustomerRef ListID'),
                'Customer_FullName' => $Invoice->getChildDataAt('InvoiceRet CustomerRef FullName'),
                'ShipAddress_Addr1' => $Invoice->getChildDataAt('InvoiceRet ShipAddress Addr1'),
                'ShipAddress_Addr2' => $Invoice->getChildDataAt('InvoiceRet ShipAddress Addr2'),
                'ShipAddress_City' => $Invoice->getChildDataAt('InvoiceRet ShipAddress City'),
                'ShipAddress_State' => $Invoice->getChildDataAt('InvoiceRet ShipAddress State'),
                'ShipAddress_PostalCode' => $Invoice->getChildDataAt('InvoiceRet ShipAddress PostalCode'),
                'BalanceRemaining' => $Invoice->getChildDataAt('InvoiceRet BalanceRemaining'),
                );

            QuickBooks_Utilities::log(QB_QUICKBOOKS_DSN, 'Importing invoice #' . $arr['RefNumber'] . ': ' . print_r($arr, true));

            foreach ($arr as $key => $value)
            {
                $arr[$key] = mysql_real_escape_string($value);
            }

            // Store the invoices in MySQL
            mysql_query("
                REPLACE INTO
                    qb_example_invoice
                (
                    " . implode(", ", array_keys($arr)) . "
                ) VALUES (
                    '" . implode("', '", array_values($arr)) . "'
                )") or die(trigger_error(mysql_error()));

            // Remove any old line items
            mysql_query("DELETE FROM qb_example_invoice_lineitem WHERE TxnID = '" . mysql_real_escape_string($arr['TxnID']) . "' ") or die(trigger_error(mysql_error()));

            // Process the line items
            foreach ($Invoice->children() as $Child)
            {
                if ($Child->name() == 'InvoiceLineRet')
                {
                    $InvoiceLine = $Child;

                    $lineitem = array( 
                        'TxnID' => $arr['TxnID'], 
                        'TxnLineID' => $InvoiceLine->getChildDataAt('InvoiceLineRet TxnLineID'), 
                        'Item_ListID' => $InvoiceLine->getChildDataAt('InvoiceLineRet ItemRef ListID'), 
                        'Item_FullName' => $InvoiceLine->getChildDataAt('InvoiceLineRet ItemRef FullName'), 
                        'Descrip' => $InvoiceLine->getChildDataAt('InvoiceLineRet Desc'), 
                        'Quantity' => $InvoiceLine->getChildDataAt('InvoiceLineRet Quantity'),
                        'Rate' => $InvoiceLine->getChildDataAt('InvoiceLineRet Rate'), 
                        );

                    foreach ($lineitem as $key => $value)
                    {
                        $lineitem[$key] = mysql_real_escape_string($value);
                    }

                    // Store the lineitems in MySQL
                    mysql_query("
                        INSERT INTO
                            qb_example_invoice_lineitem
                        (
                            " . implode(", ", array_keys($lineitem)) . "
                        ) VALUES (
                            '" . implode("', '", array_values($lineitem)) . "'
                        ) ") or die(trigger_error(mysql_error()));
                }
            }
        }
    }

    return true;
}

第3。确保每次Web连接器连接时,您都会自动发送该查询以获取这些新发票。

注册一个这样的钩子:

// An array of callback hooks
$hooks = array(
    QuickBooks_WebConnector_Handlers::HOOK_LOGINSUCCESS => '_quickbooks_hook_loginsuccess',     // call this whenever a successful login occurs
    );

总是排队获取发票的请求:

function _quickbooks_hook_loginsuccess($requestID, $user, $hook, &$err, $hook_data, $callback_config)
{  
    $Queue->enqueue(QUICKBOOKS_IMPORT_INVOICE, 1, QB_PRIORITY_INVOICE);
}

如果要重新使用上面粘贴的代码,您需要确保查看上面链接的示例以获取一些辅助函数。

如果您遇到问题,请发布您的代码,以便我们查看到目前为止您所尝试的内容。