Aunty-Sparkles-Website/app/api/square/create-payment/route.ts

42 lines
1.2 KiB
TypeScript

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 { sourceId, orderId, amount, customerInfo } = await request.json()
const paymentsApi = client.paymentsApi
const paymentRequest = {
sourceId,
idempotencyKey: crypto.randomUUID(),
amountMoney: {
amount: BigInt(amount * 100),
currency: "USD",
},
orderId,
buyerEmailAddress: customerInfo.email,
note: `Payment for Aunty Sparkles order - ${customerInfo.name}`,
autocomplete: true,
}
const response = await paymentsApi.createPayment(paymentRequest)
if (response.result.payment) {
return Response.json({
success: true,
payment: response.result.payment,
})
} else {
throw new Error("Payment failed")
}
} catch (error) {
console.error("Square payment error:", error)
return Response.json({ success: false, error: "Payment failed" }, { status: 500 })
}
}