Getting an uncaught timeout error when upserting a product

Hello,
I am using the \Square\Models\upsertCatalogObject() endpoint to add a product to square it is returning the following

Fatal error : Uncaught Square\Exceptions\ApiException: Operation timed out after 60001 milliseconds with 0 bytes received in /var/www/html/libraries/vendor/square/square/src/Utils/CompatibilityConverter.php:25

I am able to use the retrieveCatalogObject() to get a catalog object without issue.

Any help is greatly appreciated.

Thanks
Don

Do you see the request in our API logs? Also what’s the snippet of code your getting this error with? :slightly_smiling_face:

This is the code I am running.

$price_money = new \Square\Models\Money();
			$price_money->setAmount($priceInCents);
			$price_money->setCurrency('USD');

			$item_variation_data = new \Square\Models\CatalogItemVariation();
			$item_variation_data->setItemId('#'.$product['sku']);
			$item_variation_data->setSku($product['sku']);
			$item_variation_data->setName($product['name']);
			$item_variation_data->setPricingType('FIXED_PRICING');
			$item_variation_data->setPriceMoney($price_money);

			$catalog_object = new \Square\Models\CatalogObject('ITEM_VARIATION', '#'.$product['sku']);
			$catalog_object->setType('ITEM_VARIATION');
			$catalog_object->setItemVariationData($item_variation_data);

			$variations = [$catalog_object];
			$item_data = new \Square\Models\CatalogItem();
			$item_data->setName($product['name']);
			$item_data->setDescription($product['sku']);
			$item_data->setAbbreviation($product['sku']);
			$item_data->setVariations($variations);
			
			$object = new \Square\Models\CatalogObject('ITEM','#'.$product['sku']);
			$object->setType('ITEM');
			$object->setItemData($item_data);


			$body = new \Square\Models\UpsertCatalogObjectRequest($uniqid, $catalog_object);

			$api_response = $this->square->getCatalogApi()->upsertCatalogObject($body);

Have you checked that the request is making it too our servers by looking in the API Logs? :slightly_smiling_face:

Hi,
YEs I just did. The requests that are timing out are not making it but prior to that I was sending a bad request and they were making it and I got the following error.

 {
  "errors": [
    {
      "category": "INVALID_REQUEST_ERROR",
      "code": "INVALID_VALUE",
      "detail": "Non-ITEM object has ITEM data.",
      "field": "catalog_object.item_data"
    }
  ]
}

After fixing this error I am now getting the timeout error and it does not look like it is making it to the server.

Okay, if you try this does it work?

// Initialize the Square client
$client = new \Square\SquareClient([
    'accessToken' => 'YOUR_ACCESS_TOKEN',
    'environment' => \Square\Environment::SANDBOX, // or \Square\Environment::PRODUCTION
]);

// Create the Money object for the price
$price_money = new \Square\Models\Money();
$price_money->setAmount($priceInCents); // Amount in cents, e.g., $10.00 should be 1000
$price_money->setCurrency('USD'); // Set the currency, e.g., USD

// Create the CatalogItemVariation object
$item_variation_data = new \Square\Models\CatalogItemVariation();
$item_variation_data->setItemId('#' . $product['sku']); // Temporary ID for new items
$item_variation_data->setSku($product['sku']);
$item_variation_data->setName($product['name']);
$item_variation_data->setPricingType('FIXED_PRICING');
$item_variation_data->setPriceMoney($price_money);

// Create the CatalogObject for the item variation
$catalog_object_variation = new \Square\Models\CatalogObject('ITEM_VARIATION', '#' . $product['sku']);
$catalog_object_variation->setItemVariationData($item_variation_data);

// Create the CatalogItem object
$item_data = new \Square\Models\CatalogItem();
$item_data->setName($product['name']);
$item_data->setDescription($product['description']); // Assuming you have a description field
$item_data->setAbbreviation($product['sku']);
$item_data->setVariations([$catalog_object_variation]);

// Create the CatalogObject for the item
$catalog_object_item = new \Square\Models\CatalogObject('ITEM', '#' . $product['sku']);
$catalog_object_item->setItemData($item_data);

// Create the UpsertCatalogObjectRequest
$body = new \Square\Models\UpsertCatalogObjectRequest(uniqid(), $catalog_object_item);

try {
    // Call the upsertCatalogObject method
    $api_response = $client->getCatalogApi()->upsertCatalogObject($body);

    if ($api_response->isSuccess()) {
        $result = $api_response->getResult();
        echo "Item successfully upserted: " . print_r($result, true);
    } else {
        $errors = $api_response->getErrors();
        echo "Error upserting item: " . print_r($errors, true);
    }
} catch (\Square\Exceptions\ApiException $e) {
    echo "Exception when calling Square API: " . $e->getMessage();
}

:slightly_smiling_face:

Yes that seems to get tot he server and I am getting a response back. Says

Error upserting item: Array ( [0] => Square\Models\Error Object ( [category:Square\Models\Error:private] => INVALID_REQUEST_ERROR [code:Square\Models\Error:private] => INVALID_VALUE [detail:Square\Models\Error:private] => Invalid object: Invalid Object with Id: #272729-NVY-M [merchant_token=E76F07X6K85TQ] Duplicate temporary object token #272729-NVY-M. [field:Square\Models\Error:private] => ) ) 

Should all of the temporary Id’s be the same.

sorry here is the whole error

 {
  "errors": [
    {
      "category": "INVALID_REQUEST_ERROR",
      "code": "INVALID_VALUE",
      "detail": "Invalid object: Invalid Object with Id: #272729-NVY-M\n[merchant_token=E76F07X6K85TQ] Duplicate temporary object token #272729-NVY-M."
    }
  ]
}

I sorted out that last bit. Thanks for your help. Sometimes it feels like the documentation is great but there is always like one thing missing.

THanks
Don

Glad to hear you sorted it out. :slightly_smiling_face: