Hello there, I am trying to return categories from the Square API, but for some reason, it is no longer returning categories from the API. It used to work a few months ago, but now, I am not getting anything back. What should I do to fix the issue? Thank you.
public function getCategoryData()
{
$accessToken = env('SQUARE_TOKEN');
$url = 'https://connect.squareup.com/v2/catalog/list';
//$url = 'https://connect.squareupsandbox.com/v2/catalog/list';
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $accessToken,
'Accept' => 'application/json',
])->get($url);
if ($response->successful()) {
$catalogItems = $response->json()['objects'];
$categories = [];
// Iterate over all objects in the API response
foreach ($catalogItems as $catalogItem) {
// Check if the object is of type "CATEGORY"
if ($catalogItem['type'] === 'CATEGORY') {
$categoryId = $catalogItem['id'];
$categoryName = $catalogItem['category_data']['name'] ?? '';
$categoryImageIds = $catalogItem['category_data']['image_ids'] ?? [];
if (!is_array($categoryImageIds)) {
$categoryImageIds = [];
}
$images = [];
foreach ($categoryImageIds as $imageId) {
//$imageUrl = "https://connect.squareupsandbox.com/v2/catalog/object/$imageId";
$imageUrl = "https://connect.squareup.com/v2/catalog/object/$imageId";
$imageResponse = Http::withHeaders([
'Authorization' => 'Bearer ' . $accessToken,
])->get($imageUrl);
if ($imageResponse->successful()) {
$imageData = $imageResponse->json()['object']['image_data'];
$imageUrl = $imageData['url'];
$images[] = $imageUrl;
} else {
$error = $imageResponse->json();
info('Error fetching image URL for image ID ' . $imageId . ': ' . json_encode($error));
}
}
// Create a category object with ID and name
$category = [
'id' => $categoryId,
'name' => $categoryName,
'image' => $images
];
// Add the category object to the categories array
$categories[] = $category;
}
}
Log::info('Category Response: ' . json_encode($categories));
// Return the categories array as JSON response
return response()->json($categories);
} else {
$error = $response->json();
return response()->json(['error' => $error], $response->status());
}
}
6 posts - 3 participants