Skip to content
Advertisement

Quickbooks PHP JSON Array Variable?

I’m trying to use the Quickbooks PHP SDK to submit an API call to create an invoice. I can get the call to work using a hardcoded JSON array, but when I attempt to pass it with a variable, the call fails.

This is the version that works:

$theResourceObj = Invoice::create([
        "Line" => [
            [
                "Amount" => 100.00,
                "DetailType" => "SalesItemLineDetail",
                "SalesItemLineDetail" => [
                    "ItemRef" => [
                        "value" => 1,
                        "name" => "Hours"
                    ]
                ]
            ],
            [
                "Amount" => 200.00,
                "DetailType" => "SalesItemLineDetail",
                "SalesItemLineDetail" => [
                    "ItemRef" => [
                        "value" => 2,
                        "name" => "Hours"
                    ]
                ]
            ]


        ], 
  "CustomerRef"=> [
    "value"=> 2228
  ],
  "BillEmail" => [
        "Address" => "xxx@gmail.com"
  ],
  "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"
  ]
]);

When I try to pass a variable (which I need to for a dynamic call), the code fails.

Failed attempt 1 with an imploded array of strings

$lines = array();
$line1 = '["Amount" => 100.00,"DetailType" => "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 1,"name" => "Hours"]]]';
$line2 = '["Amount" => 200.00,"DetailType" => "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 2,"name" => "Hours"]]]';

$theResourceObj = Invoice::create([
        "Line" => "[" . implode("," ,$lines) . "]",          
  "CustomerRef"=> [
    "value"=> 2228
  ],
  "BillEmail" => [
        "Address" => "xxx@gmail.com"
  ],
  "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"
  ]
]);

This is error from attempt #1:

[11-Jun-2020 15:16:28 UTC] PHP Fatal error:  Uncaught QuickBooksOnlineAPIExceptionServiceException: Http Status Code [400]: Request is not made successful. Response Code:[400] with body: [<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2020-06-11T08:16:27.998-07:00"><Fault type="ValidationFault"><Error code="2020" element="Line"><Message>Required param missing, need to supply the required value for the API</Message><Detail>Required parameter Line is missing in the request</Detail></Error></Fault></IntuitResponse>].

 thrown in /home/healt640/vendor/quickbooks/v3-php-sdk/src/Core/HttpClients/SyncRestHandler.php on line 214

This is failed attempt #2 with an array encoded with json_encode

$lines = array();
$lines[] = array("Amount" => 200.00, "DetailType" => "SalesItemLineDetail", "SalesLineItemDetail" => array("ItemRef" => array("value" => 2, "name" => "Hours")));
$lines[] = array("Amount" => 200.00, "DetailType" => "SalesItemLineDetail", "SalesLineItemDetail" => array("ItemRef" => array("value" => 2, "name" => "Hours")));
$json_array = json_encode($lines, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$theResourceObj = Invoice::create([
        "Line" => "[" . $json_array . "]",           
  "CustomerRef"=> [
    "value"=> 2228
  ],
  "BillEmail" => [
        "Address" => "xxx@gmail.com"
  ],
  "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"      ]

]);

Here is the error message from the failed attempt #2:

[11-Jun-2020 15:25:44 UTC] PHP Fatal error:  Uncaught QuickBooksOnlineAPIExceptionServiceException: Http Status Code [400]: Request is not made successful. Response Code:[400] with body: [<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2020-06-11T08:25:44.568-07:00"><Fault type="ValidationFault"><Error code="2020" element="Line"><Message>Required param missing, need to supply the required value for the API</Message><Detail>Required parameter Line is missing in the request</Detail></Error></Fault></IntuitResponse>].

thrown in /home/healt640/vendor/quickbooks/v3-php-sdk/src/Core/HttpClients/SyncRestHandler.php on line 214

I’m sure this is a simple fix like the removal of quotes or something.

If someone could reply with a working example that allows the dynamic passing of variables for those “Line” values I would greatly appreciate it!

Advertisement

Answer

Seems you need to push $line1 and $line1 to your $lines array and then use it on your $theResourceObj object like below before invoice API call.

Quick Fixes,

  1. Keep in mind $line1 and $line2 should be array, not a string.
  2. No need to encode $lines just send it as it is.

Code:

 $lines = array();
 $line1 = ["Amount" => 100.00,"DetailType" => 
       "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 1,"name" => "Hours"]]];
 $line2 = ["Amount" => 200.00,"DetailType" => 
       "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 2,"name" => "Hours"]]];
 $lines[] = $line1;
 $lines[] = $line2;
 $theResourceObj = Invoice::create([
    "Line" => $lines,          
    "CustomerRef"=> [
        "value"=> 2228
    ],
    "BillEmail" => [
        "Address" => "xxx@gmail.com"
    ],
    "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"
    ]
]);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement