import { Client, Environment } from "squareup" const client = new Client({ accessToken: process.env.SQUARE_ACCESS_TOKEN, environment: process.env.SQUARE_ENVIRONMENT === "production" ? Environment.Production : Environment.Sandbox, }) export async function POST(request: Request) { try { const { items, customerInfo } = await request.json() const ordersApi = client.ordersApi const orderRequest = { order: { locationId: process.env.SQUARE_LOCATION_ID, lineItems: items.map((item: any) => ({ name: item.name, quantity: item.quantity.toString(), basePriceMoney: { amount: BigInt(item.price * 100), currency: "USD", }, note: item.description || "", })), metadata: { customerName: customerInfo.name, customerEmail: customerInfo.email, customerPhone: customerInfo.phone || "", shippingAddress: JSON.stringify(customerInfo.address || {}), }, }, idempotencyKey: crypto.randomUUID(), } const response = await ordersApi.createOrder(orderRequest) if (response.result.order) { return Response.json({ success: true, order: response.result.order, }) } else { throw new Error("Failed to create order") } } catch (error) { console.error("Square order creation error:", error) return Response.json({ success: false, error: "Failed to create order" }, { status: 500 }) } }