如果您不愿意查看 QuickBooks SDK,那么您就是在给自己带来极大的伤害。
实现您想要做的最简单的方法是通过 SDK 和 Web 连接器。我们的 wiki 上有 QuickBooks Web 连接器的基本概述。它专门用于将数据从在线网站移动到适用于 Windows 的 QuickBooks(就像您尝试做的那样)。基本思想是它会轮询您的网站,要求您做一些事情,并且您可以向它提供 XML 请求来做一些事情(添加客户、添加发票等)。
基本协议如下所示:
// Web 连接器要求您的 SOAP 服务进行身份验证 Web 连接器 => SOAP 服务器:使用用户名“xyz”和密码“bla”进行身份验证(身份验证)如果身份验证成功,则 SOAP 服务器返回一个票证值并继续该过程
// Web 连接器要求做一些事情 Web 连接器 => SOAP 服务器:嘿 SOAP 服务器,你有什么要我做的吗?(sendRequestXML) SOAP 服务器生成并返回一个 qbXML 请求
// Web 连接器将该请求中继到 QuickBooks,并从 QuickBooks Web 连接器接收响应 => QuickBooks:嘿 QuickBooks,SOAP 服务器给了我这个请求,你能处理它吗?QuickBooks 处理请求,并向 Web 连接器返回响应
// Web 连接器将来自 QuickBooks 的响应中继回 SOAP 服务器 Web 连接器 => SOAP 服务器:嘿 SOAP 服务器,这是来自 QuickBooks 的最后一个请求的响应 (receiveResponseXML) SOAP 服务器处理响应,处理任何错误并执行任何操作响应 SOAP 服务器返回完成百分比,如果小于 100%,则此过程继续...
// 该过程重新开始,Web 连接器向 SOAP 服务器请求下一个请求... Web 连接器 => SOAP 服务器:嘿 SOAP 服务器,您还有什么需要我做的吗?(sendRequestXML) SOAP 服务器生成并返回一个 qbXML 请求
... 等等等等 ...
如果您下载QuickBooks SDK(哎呀,对不起!),您会发现它包含一些您要求的内容。具体来说,它在此目录中放置了示例代码:
C:\Program Files (x86)\Intuit\IDN\QBSDK12.0\samples\qbdt\c-sharp\qbXML\WCWebService
这是该示例代码中内容的清理、精简版本:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Win32;
using System.Xml;
using System.Text.RegularExpressions;
namespace WCWebService
{
/// <summary>
/// Web Service Namespace="http://developer.intuit.com/"
/// Web Service Name="WCWebService"
/// Web Service Description="Sample WebService in ASP.NET to
/// demonstrate QuickBooks WebConnector"
/// </summary>
[WebService(
Namespace="http://developer.intuit.com/",
Name="WCWebService",
Description="Sample WebService in ASP.NET to demonstrate " +
"QuickBooks WebConnector")]
// Important Note:
// You should keep the namespace as http://developer.intuit.com/ for all web
// services that communicates with QuickBooks Web Connector.
public class WCWebService : System.Web.Services.WebService
{
...
#region WebMethods
[WebMethod]
/// <summary>
/// WebMethod - authenticate()
/// To verify username and password for the web connector that is trying to connect
/// Signature: public string[] authenticate(string strUserName, string strPassword)
///
/// IN:
/// string strUserName
/// string strPassword
///
/// OUT:
/// string[] authReturn
/// Possible values:
/// string[0] = ticket
/// string[1]
/// - empty string = use current company file
/// - "none" = no further request/no further action required
/// - "nvu" = not valid user
/// - any other string value = use this company file
/// </summary>
public string[] authenticate(string strUserName, string strPassword)
{
string[] authReturn = new string[2];
// Code below uses a random GUID to use as session ticket
// An example of a GUID is {85B41BEE-5CD9-427a-A61B-83964F1EB426}
authReturn[0]= System.Guid.NewGuid().ToString();
// For simplicity of sample, a hardcoded username/password is used.
// In real world, you should handle authentication in using a standard way.
// For example, you could validate the username/password against an LDAP
// or a directory server
string pwd="password";
if (strUserName.Trim().Equals("username") && strPassword.Trim().Equals(pwd))
{
// An empty string for authReturn[1] means asking QBWebConnector
// to connect to the company file that is currently openned in QB
authReturn[1]="";
}
else
{
authReturn[1]="nvu";
}
// You could also return "none" to indicate there is no work to do
// or a company filename in the format C:\full\path\to\company.qbw
// based on your program logic and requirements.
return authReturn;
}
[ WebMethod(Description="This web method facilitates web service to send request XML to QuickBooks via QBWebConnector",EnableSession=true) ]
/// <summary>
/// WebMethod - sendRequestXML()
/// Signature: public string sendRequestXML(string ticket, string strHCPResponse, string strCompanyFileName,
/// string Country, int qbXMLMajorVers, int qbXMLMinorVers)
///
/// IN:
/// int qbXMLMajorVers
/// int qbXMLMinorVers
/// string ticket
/// string strHCPResponse
/// string strCompanyFileName
/// string Country
/// int qbXMLMajorVers
/// int qbXMLMinorVers
///
/// OUT:
/// string request
/// Possible values:
/// - “any_string” = Request XML for QBWebConnector to process
/// - "" = No more request XML
/// </summary>
public string sendRequestXML(string ticket, string strHCPResponse, string strCompanyFileName,
string qbXMLCountry, int qbXMLMajorVers, int qbXMLMinorVers)
{
... build some qbXML request here and return it ...
return request;
}
[ WebMethod(Description="This web method facilitates web service to receive response XML from QuickBooks via QBWebConnector",EnableSession=true) ]
/// <summary>
/// WebMethod - receiveResponseXML()
/// Signature: public int receiveResponseXML(string ticket, string response, string hresult, string message)
///
/// IN:
/// string ticket
/// string response
/// string hresult
/// string message
///
/// OUT:
/// int retVal
/// Greater than zero = There are more request to send
/// 100 = Done. no more request to send
/// Less than zero = Custom Error codes
/// </summary>
public int receiveResponseXML(string ticket, string response, string hresult, string message)
{
... process the response from QuickBooks here, and return an integer ...
return retVal;
}
#endregion
} // class: WCWebService
} // namespace: WCWebService
QuickBooks SDK 还包含一个 98 页的 Web 连接器文档 PDF,其中详细描述了实现,以及 600 页的可能有用的通用 QuickBooks SDK 文档。