-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(square): add Square integration with 34 commerce operations #5053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
339ec6e
feat(square): add Square integration with 34 commerce operations
waleedlatif1 81d5107
fix(square): correct catalog image part name and address review feedback
waleedlatif1 fca2e6c
fix(square): correct canonical file param usage and revert query split
waleedlatif1 4c7ea07
chore(square): trigger fresh review
waleedlatif1 59179ba
fix(square): single-location invoice search and guard numeric coercion
waleedlatif1 580e981
fix(square): accept real booleans for autocomplete/includeRelatedObjects
waleedlatif1 6b161a7
fix(square): validate parsed JSON field shapes (array vs object)
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,6 +192,7 @@ | |
| "slack", | ||
| "smtp", | ||
| "sqs", | ||
| "square", | ||
| "ssh", | ||
| "stagehand", | ||
| "stripe", | ||
|
|
||
1,479 changes: 1,479 additions & 0 deletions
1,479
apps/docs/content/docs/en/integrations/square.mdx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { squareCatalogImageContract } from '@/lib/api/contracts/tools/square' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { checkInternalAuth } from '@/lib/auth/hybrid' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { processFilesToUserFiles, type RawFileInput } from '@/lib/uploads/utils/file-utils' | ||
| import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server' | ||
| import { assertToolFileAccess } from '@/app/api/files/authorization' | ||
| import { SQUARE_API_VERSION, SQUARE_BASE_URL } from '@/tools/square/types' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| const logger = createLogger('SquareCatalogImageAPI') | ||
|
|
||
| export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| const requestId = generateRequestId() | ||
|
|
||
| try { | ||
| const authResult = await checkInternalAuth(request, { requireWorkflowId: false }) | ||
|
|
||
| if (!authResult.success || !authResult.userId) { | ||
| logger.warn(`[${requestId}] Unauthorized Square catalog image upload: ${authResult.error}`) | ||
| return NextResponse.json( | ||
| { success: false, error: authResult.error || 'Authentication required' }, | ||
| { status: 401 } | ||
| ) | ||
| } | ||
|
|
||
| const parsed = await parseRequest(squareCatalogImageContract, request, {}) | ||
| if (!parsed.success) return parsed.response | ||
| const validatedData = parsed.data.body | ||
|
|
||
| if (!validatedData.file) { | ||
| return NextResponse.json({ success: false, error: 'File is required' }, { status: 400 }) | ||
| } | ||
|
|
||
| const userFiles = processFilesToUserFiles( | ||
| [validatedData.file as RawFileInput], | ||
| requestId, | ||
| logger | ||
| ) | ||
|
|
||
| if (userFiles.length === 0) { | ||
| return NextResponse.json({ success: false, error: 'Invalid file input' }, { status: 400 }) | ||
| } | ||
|
|
||
| const userFile = userFiles[0] | ||
| const denied = await assertToolFileAccess(userFile.key, authResult.userId, requestId, logger) | ||
| if (denied) return denied | ||
|
|
||
| const fileBuffer = await downloadFileFromStorage(userFile, requestId, logger) | ||
| const fileName = validatedData.fileName || userFile.name | ||
| const mimeType = userFile.type || 'application/octet-stream' | ||
|
|
||
| const imageRequest: Record<string, unknown> = { | ||
| idempotency_key: validatedData.idempotencyKey || generateId(), | ||
| image: { | ||
| type: 'IMAGE', | ||
| id: '#square_catalog_image', | ||
| image_data: validatedData.caption ? { caption: validatedData.caption } : {}, | ||
| }, | ||
| } | ||
| if (validatedData.objectId) imageRequest.object_id = validatedData.objectId | ||
|
|
||
| const formData = new FormData() | ||
| formData.append('request', JSON.stringify(imageRequest)) | ||
| formData.append('file', new Blob([new Uint8Array(fileBuffer)], { type: mimeType }), fileName) | ||
|
|
||
| const response = await fetch(`${SQUARE_BASE_URL}/v2/catalog/images`, { | ||
| method: 'POST', | ||
| headers: { | ||
| Authorization: `Bearer ${validatedData.accessToken}`, | ||
| 'Square-Version': SQUARE_API_VERSION, | ||
| }, | ||
| body: formData, | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text() | ||
| let detail: string | undefined | ||
| try { | ||
| detail = JSON.parse(errorText)?.errors?.[0]?.detail | ||
| } catch { | ||
| detail = undefined | ||
| } | ||
| logger.error(`[${requestId}] Square API error:`, { status: response.status, body: errorText }) | ||
| return NextResponse.json( | ||
| { | ||
| success: false, | ||
| error: detail || `Failed to upload catalog image (HTTP ${response.status})`, | ||
| }, | ||
| { status: response.status } | ||
| ) | ||
| } | ||
|
|
||
| const data = await response.json() | ||
| const object = data.image ?? {} | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { | ||
| object, | ||
| metadata: { | ||
| id: object.id ?? '', | ||
| type: object.type ?? null, | ||
| version: object.version ?? null, | ||
| }, | ||
| }, | ||
| }) | ||
| } catch (error) { | ||
| logger.error(`[${requestId}] Unexpected error:`, error) | ||
| return NextResponse.json( | ||
| { success: false, error: getErrorMessage(error, 'Unknown error') }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.