QuickBooks API (php) 集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20393709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QuickBooks API (php) Integration
提问by Yunus Aslam
I am a very new to QuickBooks. I have created a trial account in Quick Books and I want to add customers, create invoices or similar sort of things to my account. I have downloaded the php SDK from github. Now I have no Idea how to start, from where to start to add the customer to my account after the customer make the order from my website. Can anyone help me with some detail documentation or some examples so that I can move forward. I have no knowledge about the web app connector. I am totally new. Thanks..
我是 QuickBooks 的新手。我在 Quick Books 中创建了一个试用帐户,我想向我的帐户添加客户、创建发票或类似的事情。我已经从github下载了php SDK。现在我不知道如何开始,在客户从我的网站下订单后从哪里开始将客户添加到我的帐户。任何人都可以帮助我提供一些详细的文档或一些示例,以便我可以继续前进。我对 Web 应用程序连接器一无所知。我是全新的。谢谢..
回答by Keith Palmer Jr.
This is going to be a two-part answer, because you didn't specify if you're using QuickBooks ONLINE, or QuickBooks for WINDOWS.
这将是一个由两部分组成的答案,因为您没有指定是使用 QuickBooks ONLINE 还是 QuickBooks for WINDOWS。
The process will be DIFFERENT depending on which you're using, so pay attention to the bold headers below:
该过程将根据您使用的不同而有所不同,因此请注意下面的粗体标题:
For QuickBooks ONLINE:
对于 QuickBooks 在线:
If you're using the open source QuickBooks PHP DevKitfrom GitHub, then the best place to start is the QuickBooks Online with PHP quick-startguide.
如果您使用的是来自 GitHub 的开源 QuickBooks PHP DevKit,那么最好的起点是QuickBooks Online with PHP 快速入门指南。
The very first thing you'll have to do is register your app with Intuit. When you do this, Intuit will give youthese variables:
您必须做的第一件事是向 Intuit 注册您的应用程序。当你这样做时,Intuit会给你这些变量:
- app token
- consumer secret
- consumer key
- 应用令牌
- 消费者秘密
- 消费者密钥
You will substitute these variables into the config.phpfile that is included in the example. You'll also update these values to point to your app:
您将把这些变量替换到示例中包含的config.php文件中。您还将更新这些值以指向您的应用:
- oauth url (e.g. your-site.com/path/to/example/oauth.php)
- success url (e.g. your-site.com/path/to/example/success.php)
- menu url (e.g. your-site.com/path/to/example/menu.php)
- dsn (your database credentails for OAuth token storage)
- oauth 网址(例如 your-site.com/path/to/example/oauth.php)
- 成功网址(例如 your-site.com/path/to/example/success.php)
- 菜单网址(例如 your-site.com/path/to/example/menu.php)
- dsn(用于 OAuth 令牌存储的数据库凭据)
Beyond that, you can leave all other variables in config.php at their defaults.
除此之外,您可以将 config.php 中的所有其他变量保留为默认值。
If you then visit the index.php file, it will prompt you to connect to QuickBooks. You can connect, and then visit the example files. Here are some examples of adding customers/orders to QuickBooks Online:
如果您随后访问 index.php 文件,它会提示您连接到 QuickBooks。您可以连接,然后访问示例文件。以下是将客户/订单添加到 QuickBooks Online 的一些示例:
The code ends up looking something like:
代码最终看起来像:
$CustomerService = new QuickBooks_IPP_Service_Customer();
$Customer = new QuickBooks_IPP_Object_Customer();
$Customer->setTitle('Mr');
$Customer->setGivenName('Keith');
$Customer->setMiddleName('R');
$Customer->setFamilyName('Palmer');
$Customer->setDisplayName('Keith R Palmer Jr ' . mt_rand(0, 1000));
// Phone #
$PrimaryPhone = new QuickBooks_IPP_Object_PrimaryPhone();
$PrimaryPhone->setFreeFormNumber('860-532-0089');
$Customer->setPrimaryPhone($PrimaryPhone);
// Bill address
$BillAddr = new QuickBooks_IPP_Object_BillAddr();
$BillAddr->setLine1('72 E Blue Grass Road');
$BillAddr->setLine2('Suite D');
$BillAddr->setCity('Mt Pleasant');
$BillAddr->setCountrySubDivisionCode('MI');
$BillAddr->setPostalCode('48858');
$Customer->setBillAddr($BillAddr);
if ($resp = $CustomerService->add($Context, $realm, $Customer))
{
print('Our new customer ID is: [' . $resp . ']');
}
To implement additional functionality, you'll find other examples included in the code.
要实现附加功能,您将找到代码中包含的其他示例。
The objects/methods available also mirror Intuit's documentationso you'll want to look at that.
可用的对象/方法也反映了Intuit 的文档,因此您需要查看它。
For QuickBooks for WINDOWS:
对于 WINDOWS 版 QuickBooks:
For QuickBooks for Windows, you'll use the Web Connector. Again, start with the open source QuickBooks PHP DevKitfrom GitHub. Use the QuickBooks for Windows + PHP quick-startguide instead.
对于 Windows 版 QuickBooks,您将使用 Web 连接器。再次从 GitHub 上的开源 QuickBooks PHP DevKit开始。请改用QuickBooks for Windows + PHP 快速入门指南。
That will walk you through setting up a simple Web Connector service which adds test customers to QuickBooks.
这将引导您设置一个简单的 Web 连接器服务,该服务将测试客户添加到 QuickBooks。
Basically you'll create a .QWC filewhich you'll load into the QuickBooks Web Connector (Start > All Programs > QuickBooks > Web Connector). That .QWC file will point to a PHP script which negotiates the connection between QuickBooks and PHP. All you have to do in that example script is swap this variable:
基本上,您将创建一个 .QWC 文件,将其加载到 QuickBooks Web 连接器(开始 > 所有程序 > QuickBooks > Web 连接器)。该 .QWC 文件将指向一个PHP 脚本,该脚本协商 QuickBooks 和 PHP 之间的连接。您在该示例脚本中要做的就是交换这个变量:
- $dsn (point it to your own database)
- $dsn(指向你自己的数据库)
For each new piece of functionality you want to add, you'll end up writing a new request and response function, as detailed in the QuickBooks Web Connector + PHP docs.
对于您想要添加的每个新功能,您最终都会编写一个新的请求和响应函数,如QuickBooks Web 连接器 + PHP 文档 中所述。
Your code will end up looking something like:
您的代码最终将类似于:
function _quickbooks_customer_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
// You'd probably do some database access here to pull the record with
// ID = $ID from your database and build a request to add that particular
// customer to QuickBooks.
//
// So, when you implement this for your business, you'd probably do
// something like this...:
/*
// Fetch your customer record from your database
$record = mysql_fetch_array(mysql_query("SELECT * FROM your_customer_table WHERE your_customer_ID_field = " . (int) $ID));
// Create and return a qbXML request
$qbxml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="2.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<CustomerAddRq requestID="' . $requestID . '">
<CustomerAdd>
<Name>' . $record['your_customer_name_field'] . '</Name>
<CompanyName>' . $record['your_customer_company_field'] . '</CompanyName>
... lots of other customer related fields ...
</CustomerAdd>
</CustomerAddRq>
</QBXMLMsgsRq>
</QBXML>';
return $qbxml;
*/
// But we're just testing, so we'll just use a static test request:
$xml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="2.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<CustomerAddRq requestID="' . $requestID . '">
<CustomerAdd>
<Name>ConsoliBYTE, LLC (' . mt_rand() . ')</Name>
<CompanyName>ConsoliBYTE, LLC</CompanyName>
<FirstName>Keith</FirstName>
<LastName>Palmer</LastName>
<BillAddress>
<Addr1>ConsoliBYTE, LLC</Addr1>
<Addr2>134 Stonemill Road</Addr2>
<City>Mansfield</City>
<State>CT</State>
<PostalCode>06268</PostalCode>
<Country>United States</Country>
</BillAddress>
<Phone>860-634-1602</Phone>
<AltPhone>860-429-0021</AltPhone>
<Fax>860-429-5183</Fax>
<Email>[email protected]</Email>
<Contact>Keith Palmer</Contact>
</CustomerAdd>
</CustomerAddRq>
</QBXMLMsgsRq>
</QBXML>';
return $xml;
}
You can find additional qbXML reference using the QuickBooks OSR.
您可以使用QuickBooks OSR找到其他 qbXML 参考。
We also provide a wiki with lots of example qbXML requeststhat you can use.
我们还提供了一个 wiki,其中包含许多您可以使用的示例 qbXML 请求。
回答by user3040610
From the SDK,use example_ipp_ids_6.php for adding customers.
在 SDK 中,使用 example_ipp_ids_6.php 添加客户。
Here is the link to the full code on GitHub:
以下是 GitHub 上完整代码的链接:
And a quick-start guide:
以及快速入门指南:
example_ipp_ids_6.php
example_ipp_ids_6.php
<?php
// Turn on some error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: text/plain');
/**
* Require the QuickBooks library
*/
require_once dirname(__FILE__) . '/../QuickBooks.php';
/**
* Require some IPP/OAuth configuration data
*/
require_once dirname(__FILE__) . '/example_ipp_config.php';
// Set up the IPP instance
$IPP = new QuickBooks_IPP($dsn);
// Set up our IntuitAnywhere instance
$IntuitAnywhere = new QuickBooks_IPP_IntuitAnywhere($dsn, $encryption_key, $oauth_consumer_key, $oauth_consumer_secret);
// 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 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_r($Customer);
print('New customer is [' . $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...?');
}
Configure your keys and username in example_ipp_config.php
在 example_ipp_config.php 中配置您的密钥和用户名
<?php
/**
* Intuit Partner Platform configuration variables
*
* See the scripts that use these variables for more details.
*
* @package QuickBooks
* @subpackage Documentation
*/
// Your OAuth token (Intuit will give you this when you register an Intuit Anywhere app)
$token = 'c640731cb411db4132b8475b4198a7efae08';
// Your OAuth consumer key and secret (Intuit will give you both of these when you register an Intuit app)
//
// IMPORTANT:
// To pass your tech review with Intuit, you'll have to AES encrypt these and
// store them somewhere safe.
//
// The OAuth request/access tokens will be encrypted and stored for you by the
// PHP DevKit IntuitAnywhere classes automatically.
$oauth_consumer_key = 'qyprdzUiOLX60UK4cMwYhg1QVGfOGT';
$oauth_consumer_secret = '32mIB75pqqPreOADcxRvryC0fBduJhnRr52JfUdf';
// This is the URL of your OAuth auth handler page
$this_url = 'http://localhost/quick/docs/example_ipp_oauth.php';
// This is the URL to forward the user to after they have connected to IPP/IDS via OAuth
$that_url = 'http://localhost/quick/docs/example_ipp_ids_6.php';
// This is a database connection string that will be used to store the OAuth credentials
// $dsn = 'pgsql://username:password@hostname/database';
// $dsn = 'mysql://username:password@hostname/database';
$dsn = 'mysql://root:@localhost/quickbooks';
// You should set this to an encryption key specific to your app
$encryption_key = 'abcd1234';
// The user that's logged in
$the_username = '[email protected]';
// The tenant that user is accessing within your own app
$the_tenant = 12345;
// Initialize the database tables for storing OAuth information
if (!QuickBooks_Utilities::initialized($dsn))
{
// Initialize creates the neccessary database schema for queueing up requests and logging
QuickBooks_Utilities::initialize($dsn);
}

