29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { FrappeError, METHODS, frappeCall, registrationEnabled } from '@/lib/lukenya-api'
|
|
|
|
// Asks the backend to open a Paystack checkout for a saved entry and returns its link.
|
|
export async function POST(req: Request) {
|
|
if (!registrationEnabled()) {
|
|
return NextResponse.json({ error: 'Online payment is not open yet.' }, { status: 403 })
|
|
}
|
|
const body = await req.json().catch(() => ({}))
|
|
const registration = typeof body.registration === 'string' ? body.registration : ''
|
|
if (!registration) return NextResponse.json({ error: 'No entry to pay for.' }, { status: 400 })
|
|
|
|
try {
|
|
const result = await frappeCall<{ authorization_url: string; reference: string }>(METHODS.startPayment, {
|
|
args: {
|
|
registration,
|
|
payment_token: typeof body.payment_token === 'string' ? body.payment_token : undefined,
|
|
channel: body.channel === 'card' ? 'card' : 'mpesa',
|
|
},
|
|
post: true,
|
|
})
|
|
if (!result?.authorization_url) throw new Error('The payment page could not be opened.')
|
|
return NextResponse.json(result)
|
|
} catch (err) {
|
|
const status = err instanceof FrappeError && err.status < 500 ? 400 : 502
|
|
return NextResponse.json({ error: (err as Error).message }, { status })
|
|
}
|
|
}
|