Logo
Home

PHP: Facade support for Create operations

Before our PHP SDK for QuickBooks Online v3.0.1 release, creating and updating Invoices could be a painful job for developers. We have written numerous examples for our PHP developers regarding how to create an Invoice, create a Bill, or other API endpoints.

For the new 3.1.0 release of the PHP SDK, we provide façade support for these entities. Currently there are three API endpoints we support:

  • Bill
  • Sales Receipt
  • Invoice

For example, below is a complete example of how to create an Invoice using version 3.1.0:

use QuickBooksOnline\API\Core\ServiceContext;
use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\PlatformService\PlatformService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Data\IPPCustomer;
use QuickBooksOnline\API\Facades\Invoice;

$dataService = DataService::Configure(array(
  'auth_mode' => 'oauth1',
'consumerKey' => "lve2eZN6ZNBrjN0Wp26JVYJbsOOFbF",
'consumerSecret' => "fUhPIeu6jrq1UmNGXSMsIsl0JaHuHzSkFf3tsmrW",
'accessTokenKey' => "qye2etcpyquO3B1t8ydZJI8OTelqJCMiLZlY5LdX7qZunwoo",
'accessTokenSecret' => "2lEUtSEIvXf64CEkMLaGDK5rCwaxE9UvfW1dYrrH",
'QBORealmID' => "193514489870599",
'baseUrl' => "https://quickbooks.api.intuit.com/"
));

$array = [
  "Deposit" => 0,
  "domain" => "QBO",
  "sparse" => false,
  "Id" => $IdGenerated,
  "SyncToken" => 0,
  "MetaData" => [
      "CreateTime" => "2015-07-24T10:35:08-07:00",
      "LastUpdatedTime" => "2015-07-24T10:35:08-07:00"
  ],
  "CustomField"=>  [ [
      "DefinitionId" => "1",
      "Name" => "Crew #",
      "Type" => "StringType"
  ]],
  "DocNumber" => "1070",
  "TxnDate" => "2015-07-24",
  "LinkedTxn" => [],
  "Line" => [[
      "Id" => "1",
      "LineNum" => 1,
      "Amount" => 150.0,
      "DetailType" => "SalesItemLineDetail",
      "SalesItemLineDetail" => [
          "ItemRef" => [
              "value" => "1",
              "name" => "Services"
          ],
          "TaxCodeRef" => [
              "value" => "NON"
          ]
      ]
  ], [
      "Amount" => 150.0,
      "DetailType" => "SubTotalLineDetail",
      "SubTotalLineDetail" => []
  ]],
  "TxnTaxDetail" => [
      "TotalTax" => 0
  ],
  "CustomerRef" => [
      "value" => "1",
      "name" => "Amy's Bird Sanctuary"
  ],
  "CustomerMemo" => [
      "value" => "Added customer memo."
  ],
  "BillAddr" => [
      "Id" => "2",
      "Line1" => "4581 Finch St.",
      "City" => "Bayshore",
      "CountrySubDivisionCode" => "CA",
      "PostalCode" => "94326",
      "Lat" => "INVALID",
      "Long"=> "INVALID"
  ],

  "DueDate" => "2015-08-23",
  "TotalAmt" => 150.0,
  "ApplyTaxAfterDiscount" => false,
  "PrintStatus" => "NeedToPrint",
  "EmailStatus" => "NotSet",
  "Balance" => 150.0
];

$myInvoice = Invoice::create($array);
$resultingInvoiceObj = $dataService->Add($myInvoice);
$error = $dataService->getLastError();
if ($error != null) {
    echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
    echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
    echo "The Response message is: " . $error->getResponseBody() . "\n";
}
else {
    # code...
    // Echo some formatted output
    echo "Created Invoice Id={$resultingInvoiceObj->Id}. Reconstructed response body:\n\n";
    $xmlBody = XmlObjectSerializer::getPostXmlFromArbitraryEntity($resultingInvoiceObj, $urlResource);
    echo $xmlBody . "\n";
}

What do you think?  Is this easier to use?  Let us know in the comments below!


Posted

in

by

Tags:

Comments

4 responses to “PHP: Facade support for Create operations”

  1. Jose Avatar
    Jose

    Is there an example of how it was done previously?

    When I first started to use the PHP SDK I had to integrate it with CodeIgniter. I was amazed about how easy it is to use it. This sample code you just posted follows the pattern of previous examples: simplicity.

    P. S. about the SDK, I would encourage Intuit team not to use the class name of PHP files as the constructor name, as PHP yields errors in newer versions. I had to replace some constructor names by the proper “__construct()” in PHP way.

  2. Hao Avatar
    Hao

    Hi Jose,

    The old constructors for 5.3 has been fixed. Did you have a chance to take a look at our Open-sourced SDK here? https://github.com/intuit/QuickBooks-V3-PHP-SDK

  3. Jose Avatar
    Jose

    Yes, Hao. I downloaded the 2.6 version of the SDK around February. I think it’s time for an update now because the version I downloaded still used constructors in the old fashion in the classes: RequestParameters, SyncRestHandler, and OAuthRequestValidator. I see the new version has corrected those details.

    As a side note, I used Composer autoload feature in order to load the SDK classes in a reasonable shorter way. Proceeding this way I had an “Ambiguous class resolution” reported by composer regarding the class “IEntitySerializer” which was found was found in both “[SDK dir]//Utility/IEntitySerializer.php” and “[SDK dir]/Utility/Serialization/IEntitySerializer.php”. According to Composer the first will be used. So far this has not caused any problems.

    I’ll let you know if I run into any difficulties regarding the new version of the SDK. So far it has run smoothly. Best regars,

  4. Doug Avatar
    Doug

    Hao – this is slightly easier to use – IF you don’t have multiple line items that you have to dynamically populate. In other words, we sync our invoicing system (internally created) with QuickBooks. When we create an invoice in our system, we must then also create a corresponding invoice in QuickBooks. If our invoice has multiple line items, how do you create the invoice using this method without a tedious string concatenation – especially with SalesItemLineDetail sub elements? There may be a way, but when I attempted to use this method I could not figure one out that worked well. For now, we have to stick to the object instantiation method due to this single issue.

Leave a Reply

Your email address will not be published. Required fields are marked *