【问题标题】:Fetch Invoices from Quickbooks (with the help of API) using PHP使用 PHP 从 Quickbooks(在 API 的帮助下)获取发票
【发布时间】:2013-07-30 06:31:03
【问题描述】:

这是我的场景。

  1. 会有一家公司在 QuickBooks 上拥有企业帐户。

  2. 公司会将客户/客户/用户添加到他们的帐户中。

  3. 现在公司将向这些客户/客户/用户开具发票。

  4. 我必须显示(列出)公司生成的发票,并且我会将 CustomerID (ClientID/UserID) 作为参数传递,以便仅为该客户/用户/客户获取发票。

请注意,这些公司将向我们提供他们的凭据,以便获取其客户/客户/用户的发票。 我只需要出示发票,不需要对 Quickbooks 或 Quickbooks API 做任何其他事情。 另请注意,我已从 Quickbooks Online 和桌面版(使用 Web 连接器)获取发票。

提前致谢,

【问题讨论】:

    标签: php api quickbooks


    【解决方案1】:

    如果您正在构建 SaaS 服务(例如,您每月向客户收取将其 QuickBooks 连接到您的应用的费用),那么您可以使用 IPP/IDS 来实现。 如果您不向他们收取月费,那么您可以使用 Web 连接器/qbXML 来执行此操作。

    无论您选择哪种连接类型,这都没有任何意义:

    1. 我必须显示(列出)公司生成的发票,我将通过 CustomerID (ClientID/UserID) 作为参数来获取发票 仅限该客户/用户/客户。

    OAuth 令牌/连接是 QuickBooks-company-file-specific,并且没有“CustomerID”(或“ClientID”或“UserID”)这样的字段。

    对于 SaaS/IPP/IDS 示例,请下载:

    看看这些脚本:

    • docs/example_ipp_config.php
    • docs/example_ipp_oauth.php
    • docs/example_ipp_ids_5.php

    您必须在此处注册才能获得自己的 OAuth 令牌:

    docs/example_ipp_ids_5.php 脚本显示了如何获取所有客户。您可以将其更改为使用 QuickBooks_IPP_Service_Invoice 来获取所有发票。

    您的代码最终将如下所示:

    $Service = new QuickBooks_IPP_Service_Invoice();
    
    $perpage = 3;
    for ($page = 1; $page <= 3; $page++)
    {
        print('PAGE ' . $page . "\n\n");
    
        $list = $InvoiceService->findAll($Context, $realm, null, $page, $perpage);
    
        foreach ($list as $Invoice)
        {
            print('Internal ID [' . $Invoice->getId() . ' => ' . $Invoice->getHeader()->getDocNumber() . ']' . "\n\n");
        }
    
        print("\n\n\n");
    }
    

    如需 Web 连接器示例(QuickBooks 桌面),请下载:

    看看这个例子:

    • docs/example_web_connector_import.php

    以及快速入门指南:

    你最终会得到类似的东西:

    /**
     * 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;
    }
    

    如需QuickBooks Online via qbXML 示例(QuickBooks Online),请下载:

    看看这个例子:

    • docs/example_online_edition.php

    你最终会得到类似的东西:

    // Register in DESKTOP mode to get these. Docs: 
    //  http://www.consolibyte.com/docs/index.php/QuickBooks_Online_via_qbXML#Connecting_with_the_.27Desktop.27_model_of_communication
    $application_id = '134476472';
    $application_login = 'qboe.test.test.com';
    $connection_ticket = 'TGT-47-1sRm2nXMVfm$n8hb2MZfVQ';
    
    // Create our new gateway instance 
    $Gateway = new QuickBooks_Gateway_OnlineEdition(
        $application_id,
        $application_login,
        $connection_ticket);
    
    $xml = '<QBXMLMsgsRq onError="stopOnError">
                <InvoiceQueryRq>
    
                </InvoiceQueryRq>
            </QBXMLMsgsRq>';
    
    // Send the request
    $resp = $Gateway->qbxml($xml);
    
    print($resp);
    

    【讨论】:

    • 感谢您提供如此详细的回复,我选择了“通过 qbXML 示例获取 QuickBooks Online”选项。我已经生成了 application_id、application_login 和 connection_ticket/Connection Key,并在“docs/example_online_edition.php”中替换了它们,但不幸的是,当我使用生成的值浏览“docs/example_online_edition.php”时没有得到任何响应,但如果我保持值不变(默认由您提供) 我得到一个 XML 响应。我不确定我错过了什么。
    • 另请注意,在我将 YOUR-APPLICATION-ID-HERE 替换为我的应用程序 ID 后,链接“login.ptc.quickbooks.com/j/qbn/sdkapp/…”不起作用
    • 非常感谢您的帮助,最后我能够使用以下 xml 请求以 XML 格式获取所有发票(在 Quickbooks 在线生成)。 $xml = '';基思,我们不能过滤响应吗?我的意思是目前响应上述 XML 请求,我得到了所有发票,但是如果我只想获取为特定用户(比如 TOM)生成的发票怎么办?或者,如果我只想获取 UNPAID 发票怎么办?有办法吗?
    • 您可以使用 OSR (member.developer.intuit.com/qbsdk-current/Common/newOSR/…) 作为 qbXML 参考。确保将版本设置为 6.0,选中“OE”复选框,取消选中“US”复选框,使用“Select Message”下拉菜单选择您的请求类型,并使用“XML Ops”选项卡查看原始 XML发送。
    • 非常感谢,终于找到解决办法了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    相关资源
    最近更新 更多