May 9, 2017 | Hao Lu

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://qbonline-e2e.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!

Comments

View all
Load more comments