05 July 2014

Sample Code to create Invoice into Quickbooks using Quickbook API


Few days ago, we were working on a project to sync Quickbooks invoice data with 3rd party application,
But after writing initial code we were getting error CheckNullResponseAndThrowException - Response Null or Empty everytime, but as such there was no solution available for this problem even on google, so somehow we were able to debug the problem, the problem was with customer id that needs to be passed as a string instead of integer value

So here is sample working code you can use to generate invoice

<?php
require_once('../config.php');
require_once(PATH_SDK_ROOT . 'Core/ServiceContext.php');
require_once(PATH_SDK_ROOT . 'DataService/DataService.php');
require_once(PATH_SDK_ROOT . 'PlatformService/PlatformService.php');
require_once(PATH_SDK_ROOT . 'Utility/Configuration/ConfigurationManager.php');

//Specify QBO or QBD
$serviceType = IntuitServicesType::QBO;

// Please set your own config values
$accessToken = 'NULL';
$accessTokenSecret = 'NULL';
$consumerKey = 'NULL';
$consumerSecret = 'NULL';
$realmId = 'NULL';



$requestValidator = new OAuthRequestValidator($accessToken, $accessTokenSecret, $consumerKey, $consumerSecret);

$serviceContext = new ServiceContext($realmId, $serviceType, $requestValidator);

if (!$serviceContext)
 exit("Problem while initializing ServiceContext.\n");

// Prep Data Services

$dataService = new DataService($serviceContext);

if (!$dataService)
 exit("Problem while initializing DataService.\n");
    
//create an invoice
$invoiceObj = new IPPInvoice();

$Line = new IPPline();
$Line->Amount = 15;
$Line->DetailType = 'SalesItemLineDetail';
 
$saleItemLineDetail = new IPPSalesItemLineDetail();
$saleItemLineDetail->ItemRef = 1;
$Line->SalesItemLineDetail = $saleItemLineDetail;

$invoiceObj->Line = $Line;
$invoiceObj->CustomerRef = 8;

try{
 $resultingInvoiceObj = $dataService->Add($invoiceObj);
 print_r($resultingInvoiceObj); 
} catch (Exception $e){
 echo $e->getMessage();
}



?>

1 comment:

  1. I've followed your code and I'm still having an issue. When I submit my invoice, I receive a blank page using your code and no invoice is created in my sandbox account - but no errors either. I then modified the last part of the code to reflect:

    print_r($invoiceObj);

    try{
    $resultingInvoiceObj = $dataService->Add($invoiceObj);
    print_r($resultingInvoiceObj);
    echo "Object Created";
    } catch (Exception $e){
    print_r($e);
    echo $e->getMessage();
    echo "Failed";
    }

    With this, I can see my object being sent and everything looks good. I can also see my message of "Object Created" so I know that it's not failing. But I never see anything displayed for: print_r($resultingInvoiceObj); nor is it being added to my sandbox account as an invoice. Thoughts?

    ReplyDelete